Home | History | Annotate | Download | only in proximity_auth
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "components/proximity_auth/wire_message.h"
      6 
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace proximity_auth {
     10 
     11 TEST(ProximityAuthWireMessage, Deserialize_EmptyMessage) {
     12   bool is_incomplete;
     13   scoped_ptr<WireMessage> message =
     14       WireMessage::Deserialize(std::string(), &is_incomplete);
     15   EXPECT_TRUE(is_incomplete);
     16   EXPECT_FALSE(message);
     17 }
     18 
     19 TEST(ProximityAuthWireMessage, Deserialize_IncompleteHeader) {
     20   bool is_incomplete;
     21   scoped_ptr<WireMessage> message =
     22       WireMessage::Deserialize("\3", &is_incomplete);
     23   EXPECT_TRUE(is_incomplete);
     24   EXPECT_FALSE(message);
     25 }
     26 
     27 TEST(ProximityAuthWireMessage, Deserialize_UnexpectedMessageFormatVersion) {
     28   bool is_incomplete;
     29   // Version 2 is below the minimum supported version.
     30   scoped_ptr<WireMessage> message =
     31       WireMessage::Deserialize("\2\1\1", &is_incomplete);
     32   EXPECT_FALSE(is_incomplete);
     33   EXPECT_FALSE(message);
     34 }
     35 
     36 TEST(ProximityAuthWireMessage, Deserialize_BodyOfSizeZero) {
     37   bool is_incomplete;
     38   scoped_ptr<WireMessage> message =
     39       WireMessage::Deserialize(std::string("\3\0\0", 3), &is_incomplete);
     40   EXPECT_FALSE(is_incomplete);
     41   EXPECT_FALSE(message);
     42 }
     43 
     44 TEST(ProximityAuthWireMessage, Deserialize_IncompleteBody) {
     45   bool is_incomplete;
     46   scoped_ptr<WireMessage> message =
     47       WireMessage::Deserialize(std::string("\3\0\5", 3), &is_incomplete);
     48   EXPECT_TRUE(is_incomplete);
     49   EXPECT_FALSE(message);
     50 }
     51 
     52 TEST(ProximityAuthWireMessage, Deserialize_BodyLongerThanSpecifiedInHeader) {
     53   bool is_incomplete;
     54   scoped_ptr<WireMessage> message = WireMessage::Deserialize(
     55       std::string("\3\0\5", 3) + "123456", &is_incomplete);
     56   EXPECT_FALSE(is_incomplete);
     57   EXPECT_FALSE(message);
     58 }
     59 
     60 TEST(ProximityAuthWireMessage, Deserialize_BodyIsNotValidJSON) {
     61   bool is_incomplete;
     62   scoped_ptr<WireMessage> message = WireMessage::Deserialize(
     63       std::string("\3\0\5", 3) + "12345", &is_incomplete);
     64   EXPECT_FALSE(is_incomplete);
     65   EXPECT_FALSE(message);
     66 }
     67 
     68 TEST(ProximityAuthWireMessage, Deserialize_BodyIsNotADictionary) {
     69   bool is_incomplete;
     70   std::string header("\3\0\x29", 3);
     71   std::string bytes =
     72       header + "[{\"permit_id\": \"Hi!\", \"payload\": \"YQ==\"}]";
     73   scoped_ptr<WireMessage> message =
     74       WireMessage::Deserialize(bytes, &is_incomplete);
     75   EXPECT_FALSE(is_incomplete);
     76   EXPECT_FALSE(message);
     77 }
     78 
     79 // The permit ID is optional.
     80 TEST(ProximityAuthWireMessage, Deserialize_BodyLacksPermitId) {
     81   bool is_incomplete;
     82   std::string header("\3\0\x13", 3);
     83   std::string bytes = header + "{\"payload\": \"YQ==\"}";
     84   scoped_ptr<WireMessage> message =
     85       WireMessage::Deserialize(bytes, &is_incomplete);
     86   EXPECT_FALSE(is_incomplete);
     87   EXPECT_TRUE(message);
     88   EXPECT_EQ(std::string(), message->permit_id());
     89   EXPECT_EQ("a", message->payload());
     90 }
     91 
     92 TEST(ProximityAuthWireMessage, Deserialize_BodyLacksPayload) {
     93   bool is_incomplete;
     94   std::string header("\3\0\x14", 3);
     95   std::string bytes = header + "{\"permit_id\": \"Hi!\"}";
     96   scoped_ptr<WireMessage> message =
     97       WireMessage::Deserialize(bytes, &is_incomplete);
     98   EXPECT_FALSE(is_incomplete);
     99   EXPECT_FALSE(message);
    100 }
    101 
    102 // The permit ID is optional.
    103 TEST(ProximityAuthWireMessage, Deserialize_BodyHasEmptyPermitId) {
    104   bool is_incomplete;
    105   std::string header("\3\0\x24", 3);
    106   std::string bytes = header + "{\"permit_id\": \"\", \"payload\": \"YQ==\"}";
    107   scoped_ptr<WireMessage> message =
    108       WireMessage::Deserialize(bytes, &is_incomplete);
    109   EXPECT_FALSE(is_incomplete);
    110   EXPECT_TRUE(message);
    111   EXPECT_EQ(std::string(), message->permit_id());
    112   EXPECT_EQ("a", message->payload());
    113 }
    114 
    115 TEST(ProximityAuthWireMessage, Deserialize_BodyHasEmptyPayload) {
    116   bool is_incomplete;
    117   std::string header("\3\0\x23", 3);
    118   std::string bytes = header + "{\"permit_id\": \"Hi!\", \"payload\": \"\"}";
    119   scoped_ptr<WireMessage> message =
    120       WireMessage::Deserialize(bytes, &is_incomplete);
    121   EXPECT_FALSE(is_incomplete);
    122   EXPECT_FALSE(message);
    123 }
    124 
    125 TEST(ProximityAuthWireMessage, Deserialize_PayloadIsNotBase64Encoded) {
    126   bool is_incomplete;
    127   std::string header("\3\0\x2A", 3);
    128   std::string bytes =
    129       header + "{\"permit_id\": \"Hi!\", \"payload\": \"garbage\"}";
    130   scoped_ptr<WireMessage> message =
    131       WireMessage::Deserialize(bytes, &is_incomplete);
    132   EXPECT_FALSE(is_incomplete);
    133   EXPECT_FALSE(message);
    134 }
    135 
    136 TEST(ProximityAuthWireMessage, Deserialize_ValidMessage) {
    137   bool is_incomplete;
    138   std::string header("\3\0\x27", 3);
    139   std::string bytes =
    140       header + "{\"permit_id\": \"Hi!\", \"payload\": \"YQ==\"}";
    141   scoped_ptr<WireMessage> message =
    142       WireMessage::Deserialize(bytes, &is_incomplete);
    143   EXPECT_FALSE(is_incomplete);
    144   EXPECT_TRUE(message);
    145   EXPECT_EQ("Hi!", message->permit_id());
    146   EXPECT_EQ("a", message->payload());
    147 }
    148 
    149 TEST(ProximityAuthWireMessage, Deserialize_ValidMessageWithExtraUnknownFields) {
    150   bool is_incomplete;
    151   std::string header("\3\0\x46", 3);
    152   std::string bytes = header +
    153                       "{"
    154                       "  \"permit_id\": \"Hi!\","
    155                       "  \"payload\": \"YQ==\","
    156                       "  \"unexpected\": \"surprise!\""
    157                       "}";
    158   scoped_ptr<WireMessage> message =
    159       WireMessage::Deserialize(bytes, &is_incomplete);
    160   EXPECT_FALSE(is_incomplete);
    161   EXPECT_TRUE(message);
    162   EXPECT_EQ("Hi!", message->permit_id());
    163   EXPECT_EQ("a", message->payload());
    164 }
    165 
    166 }  // namespace proximity_auth
    167