OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/proximity_auth/remote_status_update.h" | 5 #include "components/proximity_auth/remote_status_update.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace proximity_auth { | 11 namespace proximity_auth { |
12 namespace { | 12 namespace { |
13 | 13 |
14 // Parses the |json| into a RemoteStatusUpdate instance. | 14 // Parses the |json| into a RemoteStatusUpdate instance. |
15 scoped_ptr<RemoteStatusUpdate> ParseJson(const std::string& json) { | 15 std::unique_ptr<RemoteStatusUpdate> ParseJson(const std::string& json) { |
16 scoped_ptr<base::DictionaryValue> as_dictionary = | 16 std::unique_ptr<base::DictionaryValue> as_dictionary = |
17 base::DictionaryValue::From(base::JSONReader::Read(json)); | 17 base::DictionaryValue::From(base::JSONReader::Read(json)); |
18 return RemoteStatusUpdate::Deserialize(*as_dictionary); | 18 return RemoteStatusUpdate::Deserialize(*as_dictionary); |
19 } | 19 } |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 // Verify that all valid values can be parsed. | 23 // Verify that all valid values can be parsed. |
24 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_Valid_UserPresent) { | 24 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_Valid_UserPresent) { |
25 const char kValidJson[] = | 25 const char kValidJson[] = |
26 "{" | 26 "{" |
27 " \"type\": \"status_update\"," | 27 " \"type\": \"status_update\"," |
28 " \"user_presence\": \"present\"," | 28 " \"user_presence\": \"present\"," |
29 " \"secure_screen_lock\": \"enabled\"," | 29 " \"secure_screen_lock\": \"enabled\"," |
30 " \"trust_agent\": \"enabled\"" | 30 " \"trust_agent\": \"enabled\"" |
31 "}"; | 31 "}"; |
32 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kValidJson); | 32 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kValidJson); |
33 ASSERT_TRUE(parsed_update); | 33 ASSERT_TRUE(parsed_update); |
34 EXPECT_EQ(USER_PRESENT, parsed_update->user_presence); | 34 EXPECT_EQ(USER_PRESENT, parsed_update->user_presence); |
35 EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED, | 35 EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED, |
36 parsed_update->secure_screen_lock_state); | 36 parsed_update->secure_screen_lock_state); |
37 EXPECT_EQ(TRUST_AGENT_ENABLED, parsed_update->trust_agent_state); | 37 EXPECT_EQ(TRUST_AGENT_ENABLED, parsed_update->trust_agent_state); |
38 } | 38 } |
39 | 39 |
40 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_Valid_UserAbsent) { | 40 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_Valid_UserAbsent) { |
41 const char kValidJson[] = | 41 const char kValidJson[] = |
42 "{" | 42 "{" |
43 " \"type\": \"status_update\"," | 43 " \"type\": \"status_update\"," |
44 " \"user_presence\": \"absent\"," | 44 " \"user_presence\": \"absent\"," |
45 " \"secure_screen_lock\": \"disabled\"," | 45 " \"secure_screen_lock\": \"disabled\"," |
46 " \"trust_agent\": \"disabled\"" | 46 " \"trust_agent\": \"disabled\"" |
47 "}"; | 47 "}"; |
48 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kValidJson); | 48 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kValidJson); |
49 ASSERT_TRUE(parsed_update); | 49 ASSERT_TRUE(parsed_update); |
50 EXPECT_EQ(USER_ABSENT, parsed_update->user_presence); | 50 EXPECT_EQ(USER_ABSENT, parsed_update->user_presence); |
51 EXPECT_EQ(SECURE_SCREEN_LOCK_DISABLED, | 51 EXPECT_EQ(SECURE_SCREEN_LOCK_DISABLED, |
52 parsed_update->secure_screen_lock_state); | 52 parsed_update->secure_screen_lock_state); |
53 EXPECT_EQ(TRUST_AGENT_DISABLED, parsed_update->trust_agent_state); | 53 EXPECT_EQ(TRUST_AGENT_DISABLED, parsed_update->trust_agent_state); |
54 } | 54 } |
55 | 55 |
56 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_Valid_Unknown) { | 56 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_Valid_Unknown) { |
57 const char kValidJson[] = | 57 const char kValidJson[] = |
58 "{" | 58 "{" |
59 " \"type\": \"status_update\"," | 59 " \"type\": \"status_update\"," |
60 " \"user_presence\": \"unknown\"," | 60 " \"user_presence\": \"unknown\"," |
61 " \"secure_screen_lock\": \"unknown\"," | 61 " \"secure_screen_lock\": \"unknown\"," |
62 " \"trust_agent\": \"unsupported\"" | 62 " \"trust_agent\": \"unsupported\"" |
63 "}"; | 63 "}"; |
64 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kValidJson); | 64 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kValidJson); |
65 ASSERT_TRUE(parsed_update); | 65 ASSERT_TRUE(parsed_update); |
66 EXPECT_EQ(USER_PRESENCE_UNKNOWN, parsed_update->user_presence); | 66 EXPECT_EQ(USER_PRESENCE_UNKNOWN, parsed_update->user_presence); |
67 EXPECT_EQ(SECURE_SCREEN_LOCK_STATE_UNKNOWN, | 67 EXPECT_EQ(SECURE_SCREEN_LOCK_STATE_UNKNOWN, |
68 parsed_update->secure_screen_lock_state); | 68 parsed_update->secure_screen_lock_state); |
69 EXPECT_EQ(TRUST_AGENT_UNSUPPORTED, parsed_update->trust_agent_state); | 69 EXPECT_EQ(TRUST_AGENT_UNSUPPORTED, parsed_update->trust_agent_state); |
70 } | 70 } |
71 | 71 |
72 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_MissingUserPresence) { | 72 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_MissingUserPresence) { |
73 const char kJson[] = | 73 const char kJson[] = |
74 "{" | 74 "{" |
75 " \"type\": \"status_update\"," | 75 " \"type\": \"status_update\"," |
76 " \"secure_screen_lock\": \"enabled\"," | 76 " \"secure_screen_lock\": \"enabled\"," |
77 " \"trust_agent\": \"enabled\"" | 77 " \"trust_agent\": \"enabled\"" |
78 "}"; | 78 "}"; |
79 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 79 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
80 EXPECT_FALSE(parsed_update); | 80 EXPECT_FALSE(parsed_update); |
81 } | 81 } |
82 | 82 |
83 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_MissingSecureScreenLock) { | 83 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_MissingSecureScreenLock) { |
84 const char kJson[] = | 84 const char kJson[] = |
85 "{" | 85 "{" |
86 " \"type\": \"status_update\"," | 86 " \"type\": \"status_update\"," |
87 " \"user_presence\": \"present\"," | 87 " \"user_presence\": \"present\"," |
88 " \"trust_agent\": \"enabled\"" | 88 " \"trust_agent\": \"enabled\"" |
89 "}"; | 89 "}"; |
90 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 90 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
91 EXPECT_FALSE(parsed_update); | 91 EXPECT_FALSE(parsed_update); |
92 } | 92 } |
93 | 93 |
94 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_MissingTrustAgent) { | 94 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_MissingTrustAgent) { |
95 const char kJson[] = | 95 const char kJson[] = |
96 "{" | 96 "{" |
97 " \"type\": \"status_update\"," | 97 " \"type\": \"status_update\"," |
98 " \"user_presence\": \"present\"," | 98 " \"user_presence\": \"present\"," |
99 " \"secure_screen_lock\": \"enabled\"" | 99 " \"secure_screen_lock\": \"enabled\"" |
100 "}"; | 100 "}"; |
101 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 101 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
102 EXPECT_FALSE(parsed_update); | 102 EXPECT_FALSE(parsed_update); |
103 } | 103 } |
104 | 104 |
105 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidType) { | 105 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidType) { |
106 const char kJson[] = | 106 const char kJson[] = |
107 "{" | 107 "{" |
108 " \"type\": \"garbage\"," | 108 " \"type\": \"garbage\"," |
109 " \"user_presence\": \"present\"," | 109 " \"user_presence\": \"present\"," |
110 " \"secure_screen_lock\": \"enabled\"," | 110 " \"secure_screen_lock\": \"enabled\"," |
111 " \"trust_agent\": \"enabled\"" | 111 " \"trust_agent\": \"enabled\"" |
112 "}"; | 112 "}"; |
113 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 113 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
114 EXPECT_FALSE(parsed_update); | 114 EXPECT_FALSE(parsed_update); |
115 } | 115 } |
116 | 116 |
117 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidPresence) { | 117 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidPresence) { |
118 const char kJson[] = | 118 const char kJson[] = |
119 "{" | 119 "{" |
120 " \"type\": \"status_update\"," | 120 " \"type\": \"status_update\"," |
121 " \"user_presence\": \"garbage\"," | 121 " \"user_presence\": \"garbage\"," |
122 " \"secure_screen_lock\": \"enabled\"," | 122 " \"secure_screen_lock\": \"enabled\"," |
123 " \"trust_agent\": \"enabled\"" | 123 " \"trust_agent\": \"enabled\"" |
124 "}"; | 124 "}"; |
125 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 125 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
126 EXPECT_FALSE(parsed_update); | 126 EXPECT_FALSE(parsed_update); |
127 } | 127 } |
128 | 128 |
129 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidLock) { | 129 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidLock) { |
130 const char kJson[] = | 130 const char kJson[] = |
131 "{" | 131 "{" |
132 " \"type\": \"status_update\"," | 132 " \"type\": \"status_update\"," |
133 " \"user_presence\": \"present\"," | 133 " \"user_presence\": \"present\"," |
134 " \"secure_screen_lock\": \"garbage\"," | 134 " \"secure_screen_lock\": \"garbage\"," |
135 " \"trust_agent\": \"enabled\"" | 135 " \"trust_agent\": \"enabled\"" |
136 "}"; | 136 "}"; |
137 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 137 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
138 EXPECT_FALSE(parsed_update); | 138 EXPECT_FALSE(parsed_update); |
139 } | 139 } |
140 | 140 |
141 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidAgent) { | 141 TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidAgent) { |
142 const char kJson[] = | 142 const char kJson[] = |
143 "{" | 143 "{" |
144 " \"type\": \"status_update\"," | 144 " \"type\": \"status_update\"," |
145 " \"user_presence\": \"present\"," | 145 " \"user_presence\": \"present\"," |
146 " \"secure_screen_lock\": \"enabled\"," | 146 " \"secure_screen_lock\": \"enabled\"," |
147 " \"trust_agent\": \"garbage\"" | 147 " \"trust_agent\": \"garbage\"" |
148 "}"; | 148 "}"; |
149 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 149 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
150 EXPECT_FALSE(parsed_update); | 150 EXPECT_FALSE(parsed_update); |
151 } | 151 } |
152 | 152 |
153 // Verify that extra fields do not prevent parsing. This provides | 153 // Verify that extra fields do not prevent parsing. This provides |
154 // forward-compatibility. | 154 // forward-compatibility. |
155 TEST(ProximityAuthRemoteStatusUpdateTest, | 155 TEST(ProximityAuthRemoteStatusUpdateTest, |
156 Deserialize_ValidStatusWithExtraFields) { | 156 Deserialize_ValidStatusWithExtraFields) { |
157 const char kJson[] = | 157 const char kJson[] = |
158 "{" | 158 "{" |
159 " \"type\": \"status_update\"," | 159 " \"type\": \"status_update\"," |
160 " \"user_presence\": \"present\"," | 160 " \"user_presence\": \"present\"," |
161 " \"secure_screen_lock\": \"enabled\"," | 161 " \"secure_screen_lock\": \"enabled\"," |
162 " \"trust_agent\": \"enabled\"," | 162 " \"trust_agent\": \"enabled\"," |
163 " \"secret_sauce\": \"chipotle\"" | 163 " \"secret_sauce\": \"chipotle\"" |
164 "}"; | 164 "}"; |
165 scoped_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); | 165 std::unique_ptr<RemoteStatusUpdate> parsed_update = ParseJson(kJson); |
166 ASSERT_TRUE(parsed_update); | 166 ASSERT_TRUE(parsed_update); |
167 EXPECT_EQ(USER_PRESENT, parsed_update->user_presence); | 167 EXPECT_EQ(USER_PRESENT, parsed_update->user_presence); |
168 EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED, | 168 EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED, |
169 parsed_update->secure_screen_lock_state); | 169 parsed_update->secure_screen_lock_state); |
170 EXPECT_EQ(TRUST_AGENT_ENABLED, parsed_update->trust_agent_state); | 170 EXPECT_EQ(TRUST_AGENT_ENABLED, parsed_update->trust_agent_state); |
171 } | 171 } |
172 | 172 |
173 } // namespace proximity_auth | 173 } // namespace proximity_auth |
OLD | NEW |