Commit e6f0e2b1 authored by isherman's avatar isherman Committed by Commit bot

[Easy Unlock] Port RemoteStatusUpdate class to native code.

BUG=421277
TEST=components_unittests
R=tengs@chromium.org

Review URL: https://codereview.chromium.org/629183003

Cr-Commit-Position: refs/heads/master@{#299075}
parent 3d1e7a5b
......@@ -670,6 +670,7 @@
'proximity_auth/connection_unittest.cc',
'proximity_auth/cryptauth/cryptauth_api_call_flow_unittest.cc',
'proximity_auth/proximity_auth_system_unittest.cc',
'proximity_auth/remote_status_update_unittest.cc',
'proximity_auth/wire_message_unittest.cc',
],
'dependencies': [
......
......@@ -27,6 +27,8 @@
"proximity_auth/proximity_auth_system.cc",
"proximity_auth/proximity_auth_system.h",
"proximity_auth/remote_device.h",
"proximity_auth/remote_status_update.cc",
"proximity_auth/remote_status_update.h",
"proximity_auth/wire_message.cc",
"proximity_auth/wire_message.h",
],
......
......@@ -15,6 +15,8 @@ source_set("proximity_auth") {
"proximity_auth_system.cc",
"proximity_auth_system.h",
"remote_device.h",
"remote_status_update.cc",
"remote_status_update.h",
"wire_message.cc",
"wire_message.h",
]
......@@ -32,6 +34,7 @@ source_set("unit_tests") {
"bluetooth_connection_unittest.cc",
"connection_unittest.cc",
"proximity_auth_system_unittest.cc",
"remote_status_update_unittest.cc",
"wire_message_unittest.cc",
]
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/proximity_auth/remote_status_update.h"
#include "base/logging.h"
#include "base/values.h"
namespace {
// The value of the 'type' status update field.
const char kStatusUpdateType[] = "status_update";
// Keys in the serialized RemoteStatusUpdate JSON object.
const char kType[] = "type";
const char kUserPresence[] = "user_presence";
const char kSecureScreenLock[] = "secure_screen_lock";
const char kTrustAgent[] = "trust_agent";
// Values in the serialized RemoteStatusUpdate JSON object.
const char kUserPresent[] = "present";
const char kUserAbsent[] = "absent";
const char kUserPresenceUnknown[] = "unknown";
const char kSecureScreenLockEnabled[] = "enabled";
const char kSecureScreenLockDisabled[] = "disabled";
const char kSecureScreenLockStateUnknown[] = "unknown";
const char kTrustAgentEnabled[] = "enabled";
const char kTrustAgentDisabled[] = "disabled";
const char kTrustAgentUnsupported[] = "unsupported";
} // namespace
namespace proximity_auth {
// static
scoped_ptr<RemoteStatusUpdate> RemoteStatusUpdate::Deserialize(
const base::DictionaryValue& serialized_value) {
std::string type;
if (!serialized_value.GetString(kType, &type) || type != kStatusUpdateType) {
VLOG(1) << "Unable to parse remote status update: unexpected type. "
<< "Expected: '" << kStatusUpdateType << "', "
<< "Saw: '" << type << "'.";
return scoped_ptr<RemoteStatusUpdate>();
}
std::string user_presence, secure_screen_lock_state, trust_agent_state;
if (!serialized_value.GetString(kUserPresence, &user_presence) ||
!serialized_value.GetString(kSecureScreenLock,
&secure_screen_lock_state) ||
!serialized_value.GetString(kTrustAgent, &trust_agent_state)) {
VLOG(1) << "Unable to parse remote status update: missing data value. "
<< "Status update:\n" << serialized_value;
return scoped_ptr<RemoteStatusUpdate>();
}
scoped_ptr<RemoteStatusUpdate> parsed_update(new RemoteStatusUpdate);
if (user_presence == kUserPresent) {
parsed_update->user_presence = USER_PRESENT;
} else if (user_presence == kUserAbsent) {
parsed_update->user_presence = USER_ABSENT;
} else if (user_presence == kUserPresenceUnknown) {
parsed_update->user_presence = USER_PRESENCE_UNKNOWN;
} else {
VLOG(1) << "Unable to parse remote status update: invalid user presence: '"
<< user_presence << "'.";
return scoped_ptr<RemoteStatusUpdate>();
}
if (secure_screen_lock_state == kSecureScreenLockEnabled) {
parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_ENABLED;
} else if (secure_screen_lock_state == kSecureScreenLockDisabled) {
parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_DISABLED;
} else if (secure_screen_lock_state == kSecureScreenLockStateUnknown) {
parsed_update->secure_screen_lock_state = SECURE_SCREEN_LOCK_STATE_UNKNOWN;
} else {
VLOG(1) << "Unable to parse remote status update: invalid secure screen "
<< "lock state: '" << secure_screen_lock_state << "'.";
return scoped_ptr<RemoteStatusUpdate>();
}
if (trust_agent_state == kTrustAgentEnabled) {
parsed_update->trust_agent_state = TRUST_AGENT_ENABLED;
} else if (trust_agent_state == kTrustAgentDisabled) {
parsed_update->trust_agent_state = TRUST_AGENT_DISABLED;
} else if (trust_agent_state == kTrustAgentUnsupported) {
parsed_update->trust_agent_state = TRUST_AGENT_UNSUPPORTED;
} else {
VLOG(1) << "Unable to parse remote status update: invalid trust agent "
<< "state: '" << trust_agent_state << "'.";
return scoped_ptr<RemoteStatusUpdate>();
}
return parsed_update.Pass();
}
} // namespace proximity_auth
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PROXIMITY_AUTH_REMOTE_STATUS_UPDATE_H
#define COMPONENTS_PROXIMITY_AUTH_REMOTE_STATUS_UPDATE_H
#include "base/memory/scoped_ptr.h"
namespace base {
class DictionaryValue;
}
namespace proximity_auth {
// Corresponds to the possible values for the 'user_presence' status update
// field.
enum UserPresence {
USER_PRESENT,
USER_ABSENT,
USER_PRESENCE_UNKNOWN,
};
// Corresponds to the possible values for the 'secure_screen_lock' status update
// field.
enum SecureScreenLockState {
SECURE_SCREEN_LOCK_ENABLED,
SECURE_SCREEN_LOCK_DISABLED,
SECURE_SCREEN_LOCK_STATE_UNKNOWN,
};
// Corresponds to the possible values for the 'trust_agent' status update field.
enum TrustAgentState {
TRUST_AGENT_ENABLED,
TRUST_AGENT_DISABLED,
TRUST_AGENT_UNSUPPORTED,
};
// Represents a 'status_update' message received from the remote device.
struct RemoteStatusUpdate {
// Parses a dictionary value into a RemoteStatusUpdate. Returns a null pointer
// if the serialized dictionary value is not valid.
static scoped_ptr<RemoteStatusUpdate> Deserialize(
const base::DictionaryValue& serialized_value);
UserPresence user_presence;
SecureScreenLockState secure_screen_lock_state;
TrustAgentState trust_agent_state;
};
} // namespace proximity_auth
#endif // COMPONENTS_PROXIMITY_AUTH_REMOTE_STATUS_UPDATE_H
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/proximity_auth/remote_status_update.h"
#include "base/json/json_reader.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace proximity_auth {
namespace {
// Parses the |json| into a DictionaryValue.
scoped_ptr<base::DictionaryValue> ParseJson(const std::string& json) {
base::DictionaryValue* as_dictionary;
base::JSONReader::Read(json)->GetAsDictionary(&as_dictionary);
return make_scoped_ptr(as_dictionary);
}
} // namespace
// Verify that all valid values can be parsed.
TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_ValidStatuses) {
{
const char kValidJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"present\","
" \"secure_screen_lock\": \"enabled\","
" \"trust_agent\": \"enabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kValidJson));
ASSERT_TRUE(parsed_update);
EXPECT_EQ(USER_PRESENT, parsed_update->user_presence);
EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED,
parsed_update->secure_screen_lock_state);
EXPECT_EQ(TRUST_AGENT_ENABLED, parsed_update->trust_agent_state);
}
{
const char kValidJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"absent\","
" \"secure_screen_lock\": \"disabled\","
" \"trust_agent\": \"disabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kValidJson));
ASSERT_TRUE(parsed_update);
EXPECT_EQ(USER_ABSENT, parsed_update->user_presence);
EXPECT_EQ(SECURE_SCREEN_LOCK_DISABLED,
parsed_update->secure_screen_lock_state);
EXPECT_EQ(TRUST_AGENT_DISABLED, parsed_update->trust_agent_state);
}
{
const char kValidJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"unknown\","
" \"secure_screen_lock\": \"unknown\","
" \"trust_agent\": \"unsupported\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kValidJson));
ASSERT_TRUE(parsed_update);
EXPECT_EQ(USER_PRESENCE_UNKNOWN, parsed_update->user_presence);
EXPECT_EQ(SECURE_SCREEN_LOCK_STATE_UNKNOWN,
parsed_update->secure_screen_lock_state);
EXPECT_EQ(TRUST_AGENT_UNSUPPORTED, parsed_update->trust_agent_state);
}
}
TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidType) {
const char kJson[] =
"{"
" \"type\": \"garbage\","
" \"user_presence\": \"present\","
" \"secure_screen_lock\": \"enabled\","
" \"trust_agent\": \"enabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
EXPECT_FALSE(parsed_update);
}
TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_MissingValue) {
{
const char kJson[] =
"{"
" \"type\": \"status_update\","
" \"secure_screen_lock\": \"enabled\","
" \"trust_agent\": \"enabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
EXPECT_FALSE(parsed_update);
}
{
const char kJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"present\","
" \"trust_agent\": \"enabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
EXPECT_FALSE(parsed_update);
}
{
const char kJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"present\","
" \"secure_screen_lock\": \"enabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
EXPECT_FALSE(parsed_update);
}
}
TEST(ProximityAuthRemoteStatusUpdateTest, Deserialize_InvalidValues) {
{
const char kJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"garbage\","
" \"secure_screen_lock\": \"enabled\","
" \"trust_agent\": \"enabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
EXPECT_FALSE(parsed_update);
}
{
const char kJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"present\","
" \"secure_screen_lock\": \"garbage\","
" \"trust_agent\": \"enabled\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
EXPECT_FALSE(parsed_update);
}
{
const char kJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"present\","
" \"secure_screen_lock\": \"enabled\","
" \"trust_agent\": \"garbage\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
EXPECT_FALSE(parsed_update);
}
}
// Verify that extra fields do not prevent parsing. This provides
// forward-compatibility.
TEST(ProximityAuthRemoteStatusUpdateTest,
Deserialize_ValidStatusWithExtraFields) {
const char kJson[] =
"{"
" \"type\": \"status_update\","
" \"user_presence\": \"present\","
" \"secure_screen_lock\": \"enabled\","
" \"trust_agent\": \"enabled\","
" \"secret_sauce\": \"chipotle\""
"}";
scoped_ptr<RemoteStatusUpdate> parsed_update =
RemoteStatusUpdate::Deserialize(*ParseJson(kJson));
ASSERT_TRUE(parsed_update);
EXPECT_EQ(USER_PRESENT, parsed_update->user_presence);
EXPECT_EQ(SECURE_SCREEN_LOCK_ENABLED,
parsed_update->secure_screen_lock_state);
EXPECT_EQ(TRUST_AGENT_ENABLED, parsed_update->trust_agent_state);
}
} // namespace proximity_auth
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment