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 "base/strings/string_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace proximity_auth
{
12 TEST(ProximityAuthWireMessage
, Deserialize_EmptyMessage
) {
14 scoped_ptr
<WireMessage
> message
=
15 WireMessage::Deserialize(std::string(), &is_incomplete
);
16 EXPECT_TRUE(is_incomplete
);
17 EXPECT_FALSE(message
);
20 TEST(ProximityAuthWireMessage
, Deserialize_IncompleteHeader
) {
22 scoped_ptr
<WireMessage
> message
=
23 WireMessage::Deserialize("\3", &is_incomplete
);
24 EXPECT_TRUE(is_incomplete
);
25 EXPECT_FALSE(message
);
28 TEST(ProximityAuthWireMessage
, Deserialize_UnexpectedMessageFormatVersion
) {
30 // Version 2 is below the minimum supported version.
31 scoped_ptr
<WireMessage
> message
=
32 WireMessage::Deserialize("\2\1\1", &is_incomplete
);
33 EXPECT_FALSE(is_incomplete
);
34 EXPECT_FALSE(message
);
37 TEST(ProximityAuthWireMessage
, Deserialize_BodyOfSizeZero
) {
39 scoped_ptr
<WireMessage
> message
=
40 WireMessage::Deserialize(std::string("\3\0\0", 3), &is_incomplete
);
41 EXPECT_FALSE(is_incomplete
);
42 EXPECT_FALSE(message
);
45 TEST(ProximityAuthWireMessage
, Deserialize_IncompleteBody
) {
47 scoped_ptr
<WireMessage
> message
=
48 WireMessage::Deserialize(std::string("\3\0\5", 3), &is_incomplete
);
49 EXPECT_TRUE(is_incomplete
);
50 EXPECT_FALSE(message
);
53 TEST(ProximityAuthWireMessage
, Deserialize_BodyLongerThanSpecifiedInHeader
) {
55 scoped_ptr
<WireMessage
> message
= WireMessage::Deserialize(
56 std::string("\3\0\5", 3) + "123456", &is_incomplete
);
57 EXPECT_FALSE(is_incomplete
);
58 EXPECT_FALSE(message
);
61 TEST(ProximityAuthWireMessage
, Deserialize_BodyIsNotValidJSON
) {
63 scoped_ptr
<WireMessage
> message
= WireMessage::Deserialize(
64 std::string("\3\0\5", 3) + "12345", &is_incomplete
);
65 EXPECT_FALSE(is_incomplete
);
66 EXPECT_FALSE(message
);
69 TEST(ProximityAuthWireMessage
, Deserialize_BodyIsNotADictionary
) {
71 std::string
header("\3\0\x29", 3);
73 header
+ "[{\"permit_id\": \"Hi!\", \"payload\": \"YQ==\"}]";
74 scoped_ptr
<WireMessage
> message
=
75 WireMessage::Deserialize(bytes
, &is_incomplete
);
76 EXPECT_FALSE(is_incomplete
);
77 EXPECT_FALSE(message
);
80 // The permit ID is optional.
81 TEST(ProximityAuthWireMessage
, Deserialize_BodyLacksPermitId
) {
83 std::string
header("\3\0\x13", 3);
84 std::string bytes
= header
+ "{\"payload\": \"YQ==\"}";
85 scoped_ptr
<WireMessage
> message
=
86 WireMessage::Deserialize(bytes
, &is_incomplete
);
87 EXPECT_FALSE(is_incomplete
);
89 EXPECT_EQ(std::string(), message
->permit_id());
90 EXPECT_EQ("a", message
->payload());
93 TEST(ProximityAuthWireMessage
, Deserialize_BodyLacksPayload
) {
95 std::string
header("\3\0\x14", 3);
96 std::string bytes
= header
+ "{\"permit_id\": \"Hi!\"}";
97 scoped_ptr
<WireMessage
> message
=
98 WireMessage::Deserialize(bytes
, &is_incomplete
);
99 EXPECT_FALSE(is_incomplete
);
100 EXPECT_FALSE(message
);
103 // The permit ID is optional.
104 TEST(ProximityAuthWireMessage
, Deserialize_BodyHasEmptyPermitId
) {
106 std::string
header("\3\0\x24", 3);
107 std::string bytes
= 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);
119 std::string bytes
= header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"\"}";
120 scoped_ptr
<WireMessage
> message
=
121 WireMessage::Deserialize(bytes
, &is_incomplete
);
122 EXPECT_FALSE(is_incomplete
);
123 EXPECT_FALSE(message
);
126 TEST(ProximityAuthWireMessage
, Deserialize_PayloadIsNotBase64Encoded
) {
128 std::string
header("\3\0\x2A", 3);
130 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"garbage\"}";
131 scoped_ptr
<WireMessage
> message
=
132 WireMessage::Deserialize(bytes
, &is_incomplete
);
133 EXPECT_FALSE(is_incomplete
);
134 EXPECT_FALSE(message
);
137 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessage
) {
139 std::string
header("\3\0\x27", 3);
141 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"YQ==\"}";
142 scoped_ptr
<WireMessage
> message
=
143 WireMessage::Deserialize(bytes
, &is_incomplete
);
144 EXPECT_FALSE(is_incomplete
);
145 ASSERT_TRUE(message
);
146 EXPECT_EQ("Hi!", message
->permit_id());
147 EXPECT_EQ("a", message
->payload());
150 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessageWithBase64UrlEncoding
) {
152 std::string
header("\3\0\x27", 3);
154 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"_-Y=\"}";
155 scoped_ptr
<WireMessage
> message
=
156 WireMessage::Deserialize(bytes
, &is_incomplete
);
157 EXPECT_FALSE(is_incomplete
);
158 ASSERT_TRUE(message
);
159 EXPECT_EQ("Hi!", message
->permit_id());
160 EXPECT_EQ("\xFF\xE6", message
->payload());
163 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessageWithExtraUnknownFields
) {
165 std::string
header("\3\0\x46", 3);
166 std::string bytes
= header
+
168 " \"permit_id\": \"Hi!\","
169 " \"payload\": \"YQ==\","
170 " \"unexpected\": \"surprise!\""
172 scoped_ptr
<WireMessage
> message
=
173 WireMessage::Deserialize(bytes
, &is_incomplete
);
174 EXPECT_FALSE(is_incomplete
);
175 ASSERT_TRUE(message
);
176 EXPECT_EQ("Hi!", message
->permit_id());
177 EXPECT_EQ("a", message
->payload());
180 TEST(ProximityAuthWireMessage
, Deserialize_SizeEquals0x01FF
) {
181 // Create a message with a body of 0x01FF bytes to test the size contained in
182 // the header is parsed correctly.
183 std::string
header("\3\x01\xff", 3);
184 char json_template
[] = "{\"payload\":\"YQ==\", \"filler\":\"$1\"}";
185 // Add 3 to the size to take into account the "$1" and NUL terminator ("\0")
186 // characters in |json_template|.
187 uint16_t filler_size
= 0x01ff - sizeof(json_template
) + 3;
188 std::string
filler(filler_size
, 'F');
190 std::string body
= base::ReplaceStringPlaceholders(
191 json_template
, std::vector
<std::string
>(1u, filler
), nullptr);
192 std::string serialized_message
= header
+ body
;
195 scoped_ptr
<WireMessage
> message
=
196 WireMessage::Deserialize(serialized_message
, &is_incomplete
);
197 EXPECT_FALSE(is_incomplete
);
198 ASSERT_TRUE(message
);
199 EXPECT_EQ("a", message
->payload());
202 TEST(ProximityAuthWireMessage
, Serialize_WithPermitId
) {
203 WireMessage
message1("example id", "example payload");
204 std::string bytes
= message1
.Serialize();
205 ASSERT_FALSE(bytes
.empty());
208 scoped_ptr
<WireMessage
> message2
=
209 WireMessage::Deserialize(bytes
, &is_incomplete
);
210 EXPECT_FALSE(is_incomplete
);
211 ASSERT_TRUE(message2
);
212 EXPECT_EQ("example id", message2
->permit_id());
213 EXPECT_EQ("example payload", message2
->payload());
216 TEST(ProximityAuthWireMessage
, Serialize_WithoutPermitId
) {
217 WireMessage
message1(std::string(), "example payload");
218 std::string bytes
= message1
.Serialize();
219 ASSERT_FALSE(bytes
.empty());
222 scoped_ptr
<WireMessage
> message2
=
223 WireMessage::Deserialize(bytes
, &is_incomplete
);
224 EXPECT_FALSE(is_incomplete
);
225 ASSERT_TRUE(message2
);
226 EXPECT_EQ(std::string(), message2
->permit_id());
227 EXPECT_EQ("example payload", message2
->payload());
230 TEST(ProximityAuthWireMessage
, Serialize_FailsWithoutPayload
) {
231 WireMessage
message1("example id", std::string());
232 std::string bytes
= message1
.Serialize();
233 EXPECT_TRUE(bytes
.empty());
236 } // namespace proximity_auth