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/remote_status_update.h"
7 #include "base/json/json_reader.h"
8 #include "base/values.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace proximity_auth
{
14 // Parses the |json| into a DictionaryValue.
15 scoped_ptr
<base::DictionaryValue
> ParseJson(const std::string
& json
) {
16 base::DictionaryValue
* as_dictionary
;
17 base::JSONReader::Read(json
)->GetAsDictionary(&as_dictionary
);
18 return make_scoped_ptr(as_dictionary
);
23 // Verify that all valid values can be parsed.
24 TEST(ProximityAuthRemoteStatusUpdateTest
, Deserialize_ValidStatuses
) {
26 const char kValidJson
[] =
28 " \"type\": \"status_update\","
29 " \"user_presence\": \"present\","
30 " \"secure_screen_lock\": \"enabled\","
31 " \"trust_agent\": \"enabled\""
33 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
34 RemoteStatusUpdate::Deserialize(*ParseJson(kValidJson
));
35 ASSERT_TRUE(parsed_update
);
36 EXPECT_EQ(USER_PRESENT
, parsed_update
->user_presence
);
37 EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED
,
38 parsed_update
->secure_screen_lock_state
);
39 EXPECT_EQ(TRUST_AGENT_ENABLED
, parsed_update
->trust_agent_state
);
43 const char kValidJson
[] =
45 " \"type\": \"status_update\","
46 " \"user_presence\": \"absent\","
47 " \"secure_screen_lock\": \"disabled\","
48 " \"trust_agent\": \"disabled\""
50 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
51 RemoteStatusUpdate::Deserialize(*ParseJson(kValidJson
));
52 ASSERT_TRUE(parsed_update
);
53 EXPECT_EQ(USER_ABSENT
, parsed_update
->user_presence
);
54 EXPECT_EQ(SECURE_SCREEN_LOCK_DISABLED
,
55 parsed_update
->secure_screen_lock_state
);
56 EXPECT_EQ(TRUST_AGENT_DISABLED
, parsed_update
->trust_agent_state
);
60 const char kValidJson
[] =
62 " \"type\": \"status_update\","
63 " \"user_presence\": \"unknown\","
64 " \"secure_screen_lock\": \"unknown\","
65 " \"trust_agent\": \"unsupported\""
67 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
68 RemoteStatusUpdate::Deserialize(*ParseJson(kValidJson
));
69 ASSERT_TRUE(parsed_update
);
70 EXPECT_EQ(USER_PRESENCE_UNKNOWN
, parsed_update
->user_presence
);
71 EXPECT_EQ(SECURE_SCREEN_LOCK_STATE_UNKNOWN
,
72 parsed_update
->secure_screen_lock_state
);
73 EXPECT_EQ(TRUST_AGENT_UNSUPPORTED
, parsed_update
->trust_agent_state
);
77 TEST(ProximityAuthRemoteStatusUpdateTest
, Deserialize_InvalidType
) {
80 " \"type\": \"garbage\","
81 " \"user_presence\": \"present\","
82 " \"secure_screen_lock\": \"enabled\","
83 " \"trust_agent\": \"enabled\""
85 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
86 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
87 EXPECT_FALSE(parsed_update
);
90 TEST(ProximityAuthRemoteStatusUpdateTest
, Deserialize_MissingValue
) {
94 " \"type\": \"status_update\","
95 " \"secure_screen_lock\": \"enabled\","
96 " \"trust_agent\": \"enabled\""
98 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
99 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
100 EXPECT_FALSE(parsed_update
);
106 " \"type\": \"status_update\","
107 " \"user_presence\": \"present\","
108 " \"trust_agent\": \"enabled\""
110 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
111 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
112 EXPECT_FALSE(parsed_update
);
118 " \"type\": \"status_update\","
119 " \"user_presence\": \"present\","
120 " \"secure_screen_lock\": \"enabled\""
122 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
123 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
124 EXPECT_FALSE(parsed_update
);
128 TEST(ProximityAuthRemoteStatusUpdateTest
, Deserialize_InvalidValues
) {
132 " \"type\": \"status_update\","
133 " \"user_presence\": \"garbage\","
134 " \"secure_screen_lock\": \"enabled\","
135 " \"trust_agent\": \"enabled\""
137 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
138 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
139 EXPECT_FALSE(parsed_update
);
145 " \"type\": \"status_update\","
146 " \"user_presence\": \"present\","
147 " \"secure_screen_lock\": \"garbage\","
148 " \"trust_agent\": \"enabled\""
150 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
151 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
152 EXPECT_FALSE(parsed_update
);
158 " \"type\": \"status_update\","
159 " \"user_presence\": \"present\","
160 " \"secure_screen_lock\": \"enabled\","
161 " \"trust_agent\": \"garbage\""
163 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
164 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
165 EXPECT_FALSE(parsed_update
);
169 // Verify that extra fields do not prevent parsing. This provides
170 // forward-compatibility.
171 TEST(ProximityAuthRemoteStatusUpdateTest
,
172 Deserialize_ValidStatusWithExtraFields
) {
175 " \"type\": \"status_update\","
176 " \"user_presence\": \"present\","
177 " \"secure_screen_lock\": \"enabled\","
178 " \"trust_agent\": \"enabled\","
179 " \"secret_sauce\": \"chipotle\""
181 scoped_ptr
<RemoteStatusUpdate
> parsed_update
=
182 RemoteStatusUpdate::Deserialize(*ParseJson(kJson
));
183 ASSERT_TRUE(parsed_update
);
184 EXPECT_EQ(USER_PRESENT
, parsed_update
->user_presence
);
185 EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED
,
186 parsed_update
->secure_screen_lock_state
);
187 EXPECT_EQ(TRUST_AGENT_ENABLED
, parsed_update
->trust_agent_state
);
190 } // namespace proximity_auth