Implement finding BLE connections in chrome://proximity-auth.
[chromium-blink-merge.git] / components / proximity_auth / wire_message_unittest.cc
blob512f4802ce3c32685fd62c2f5927186f5f51684c
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) {
13 bool is_incomplete;
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) {
21 bool is_incomplete;
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) {
29 bool is_incomplete;
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) {
38 bool is_incomplete;
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) {
46 bool is_incomplete;
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) {
54 bool is_incomplete;
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) {
62 bool is_incomplete;
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) {
70 bool is_incomplete;
71 std::string header("\3\0\x29", 3);
72 std::string bytes =
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) {
82 bool is_incomplete;
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);
88 EXPECT_TRUE(message);
89 EXPECT_EQ(std::string(), message->permit_id());
90 EXPECT_EQ("a", message->payload());
93 TEST(ProximityAuthWireMessage, Deserialize_BodyLacksPayload) {
94 bool is_incomplete;
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) {
105 bool is_incomplete;
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) {
117 bool is_incomplete;
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) {
127 bool is_incomplete;
128 std::string header("\3\0\x2A", 3);
129 std::string bytes =
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) {
138 bool is_incomplete;
139 std::string header("\3\0\x27", 3);
140 std::string bytes =
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) {
151 bool is_incomplete;
152 std::string header("\3\0\x27", 3);
153 std::string bytes =
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) {
164 bool is_incomplete;
165 std::string header("\3\0\x46", 3);
166 std::string bytes = header +
168 " \"permit_id\": \"Hi!\","
169 " \"payload\": \"YQ==\","
170 " \"unexpected\": \"surprise!\""
171 "}";
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;
194 bool is_incomplete;
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 payload", "example id");
204 std::string bytes = message1.Serialize();
205 ASSERT_FALSE(bytes.empty());
207 bool is_incomplete;
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("example payload");
218 std::string bytes = message1.Serialize();
219 ASSERT_FALSE(bytes.empty());
221 bool is_incomplete;
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(std::string(), "example id");
232 std::string bytes = message1.Serialize();
233 EXPECT_TRUE(bytes.empty());
236 } // namespace proximity_auth