Cleanup: Only build extensions renderer code when extensions are enabled.
[chromium-blink-merge.git] / components / proximity_auth / wire_message_unittest.cc
blobafa05ef4f446dad6c367a5f59d63c3e365f517f5
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 =
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) {
117 bool is_incomplete;
118 std::string header("\3\0\x23", 3);
119 std::string bytes =
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) {
128 bool is_incomplete;
129 std::string header("\3\0\x2A", 3);
130 std::string bytes =
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) {
139 bool is_incomplete;
140 std::string header("\3\0\x27", 3);
141 std::string bytes =
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) {
152 bool is_incomplete;
153 std::string header("\3\0\x46", 3);
154 std::string bytes =
155 header +
157 " \"permit_id\": \"Hi!\","
158 " \"payload\": \"YQ==\","
159 " \"unexpected\": \"surprise!\""
160 "}";
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