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.
5 #include "components/proximity_auth/wire_message.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace proximity_auth
{
11 TEST(ProximityAuthWireMessage
, Deserialize_EmptyMessage
) {
13 scoped_ptr
<WireMessage
> message
=
14 WireMessage::Deserialize(std::string(), &is_incomplete
);
15 EXPECT_TRUE(is_incomplete
);
16 EXPECT_FALSE(message
);
19 TEST(ProximityAuthWireMessage
, Deserialize_IncompleteHeader
) {
21 scoped_ptr
<WireMessage
> message
=
22 WireMessage::Deserialize("\3", &is_incomplete
);
23 EXPECT_TRUE(is_incomplete
);
24 EXPECT_FALSE(message
);
27 TEST(ProximityAuthWireMessage
, Deserialize_UnexpectedMessageFormatVersion
) {
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
);
36 TEST(ProximityAuthWireMessage
, Deserialize_BodyOfSizeZero
) {
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
);
44 TEST(ProximityAuthWireMessage
, Deserialize_IncompleteBody
) {
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
);
52 TEST(ProximityAuthWireMessage
, Deserialize_BodyLongerThanSpecifiedInHeader
) {
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
);
60 TEST(ProximityAuthWireMessage
, Deserialize_BodyIsNotValidJSON
) {
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
);
68 TEST(ProximityAuthWireMessage
, Deserialize_BodyIsNotADictionary
) {
70 std::string
header("\3\0\x29", 3);
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
);
79 // The permit ID is optional.
80 TEST(ProximityAuthWireMessage
, Deserialize_BodyLacksPermitId
) {
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
);
88 EXPECT_EQ(std::string(), message
->permit_id());
89 EXPECT_EQ("a", message
->payload());
92 TEST(ProximityAuthWireMessage
, Deserialize_BodyLacksPayload
) {
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
);
102 // The permit ID is optional.
103 TEST(ProximityAuthWireMessage
, Deserialize_BodyHasEmptyPermitId
) {
105 std::string
header("\3\0\x24", 3);
107 header
+ "{\"permit_id\": \"\", \"payload\": \"YQ==\"}";
108 scoped_ptr
<WireMessage
> message
=
109 WireMessage::Deserialize(bytes
, &is_incomplete
);
110 EXPECT_FALSE(is_incomplete
);
111 EXPECT_TRUE(message
);
112 EXPECT_EQ(std::string(), message
->permit_id());
113 EXPECT_EQ("a", message
->payload());
116 TEST(ProximityAuthWireMessage
, Deserialize_BodyHasEmptyPayload
) {
118 std::string
header("\3\0\x23", 3);
120 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"\"}";
121 scoped_ptr
<WireMessage
> message
=
122 WireMessage::Deserialize(bytes
, &is_incomplete
);
123 EXPECT_FALSE(is_incomplete
);
124 EXPECT_FALSE(message
);
127 TEST(ProximityAuthWireMessage
, Deserialize_PayloadIsNotBase64Encoded
) {
129 std::string
header("\3\0\x2A", 3);
131 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"garbage\"}";
132 scoped_ptr
<WireMessage
> message
=
133 WireMessage::Deserialize(bytes
, &is_incomplete
);
134 EXPECT_FALSE(is_incomplete
);
135 EXPECT_FALSE(message
);
138 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessage
) {
140 std::string
header("\3\0\x27", 3);
142 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"YQ==\"}";
143 scoped_ptr
<WireMessage
> message
=
144 WireMessage::Deserialize(bytes
, &is_incomplete
);
145 EXPECT_FALSE(is_incomplete
);
146 EXPECT_TRUE(message
);
147 EXPECT_EQ("Hi!", message
->permit_id());
148 EXPECT_EQ("a", message
->payload());
151 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessageWithExtraUnknownFields
) {
153 std::string
header("\3\0\x46", 3);
157 " \"permit_id\": \"Hi!\","
158 " \"payload\": \"YQ==\","
159 " \"unexpected\": \"surprise!\""
161 scoped_ptr
<WireMessage
> message
=
162 WireMessage::Deserialize(bytes
, &is_incomplete
);
163 EXPECT_FALSE(is_incomplete
);
164 EXPECT_TRUE(message
);
165 EXPECT_EQ("Hi!", message
->permit_id());
166 EXPECT_EQ("a", message
->payload());
169 } // namespace proximity_auth