Fix Up SingletonHwnd Observer Lifetime Issues
[chromium-blink-merge.git] / components / proximity_auth / wire_message_unittest.cc
blob68ce0865ebfa7e4279a080dda1495a801302241d
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) {
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);
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);
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);
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);
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);
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);
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);
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);
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());
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);
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());
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);
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);
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 ASSERT_TRUE(message);
145 EXPECT_EQ("Hi!", message->permit_id());
146 EXPECT_EQ("a", message->payload());
149 TEST(ProximityAuthWireMessage, Deserialize_ValidMessageWithBase64UrlEncoding) {
150 bool is_incomplete;
151 std::string header("\3\0\x27", 3);
152 std::string bytes =
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) {
163 bool is_incomplete;
164 std::string header("\3\0\x46", 3);
165 std::string bytes = header +
167 " \"permit_id\": \"Hi!\","
168 " \"payload\": \"YQ==\","
169 " \"unexpected\": \"surprise!\""
170 "}";
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