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);
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());
115 TEST(ProximityAuthWireMessage
, Deserialize_BodyHasEmptyPayload
) {
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
);
125 TEST(ProximityAuthWireMessage
, Deserialize_PayloadIsNotBase64Encoded
) {
127 std::string
header("\3\0\x2A", 3);
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
);
136 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessage
) {
138 std::string
header("\3\0\x27", 3);
140 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"YQ==\"}";
141 scoped_ptr
<WireMessage
> message
=
142 WireMessage::Deserialize(bytes
, &is_incomplete
);
143 EXPECT_FALSE(is_incomplete
);
144 ASSERT_TRUE(message
);
145 EXPECT_EQ("Hi!", message
->permit_id());
146 EXPECT_EQ("a", message
->payload());
149 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessageWithBase64UrlEncoding
) {
151 std::string
header("\3\0\x27", 3);
153 header
+ "{\"permit_id\": \"Hi!\", \"payload\": \"_-Y=\"}";
154 scoped_ptr
<WireMessage
> message
=
155 WireMessage::Deserialize(bytes
, &is_incomplete
);
156 EXPECT_FALSE(is_incomplete
);
157 ASSERT_TRUE(message
);
158 EXPECT_EQ("Hi!", message
->permit_id());
159 EXPECT_EQ("\xFF\xE6", message
->payload());
162 TEST(ProximityAuthWireMessage
, Deserialize_ValidMessageWithExtraUnknownFields
) {
164 std::string
header("\3\0\x46", 3);
165 std::string bytes
= header
+
167 " \"permit_id\": \"Hi!\","
168 " \"payload\": \"YQ==\","
169 " \"unexpected\": \"surprise!\""
171 scoped_ptr
<WireMessage
> message
=
172 WireMessage::Deserialize(bytes
, &is_incomplete
);
173 EXPECT_FALSE(is_incomplete
);
174 ASSERT_TRUE(message
);
175 EXPECT_EQ("Hi!", message
->permit_id());
176 EXPECT_EQ("a", message
->payload());
179 } // namespace proximity_auth