Commit f4718d0b authored by Kyle Horimoto's avatar Kyle Horimoto Committed by Commit Bot

[CrOS MultiDevice] Add StructTraits conversions for DeviceSync service.

This CL adds conversions for the BeaconSeed and RemoteDevice message
types.

Bug: 824568, 752273
Change-Id: I03b76739ecc4a21aace82dfcd2b0a07972265455
Reviewed-on: https://chromium-review.googlesource.com/996642
Commit-Queue: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548627}
parent f856c63c
......@@ -79,6 +79,7 @@ source_set("unit_tests") {
"//base",
"//base/test:test_support",
"//chromeos/services/device_sync/public/mojom",
"//chromeos/services/device_sync/public/mojom:unit_tests",
"//components/cryptauth",
"//components/cryptauth:test_support",
"//mojo/common",
......
......@@ -14,3 +14,21 @@ mojom("mojom") {
"//mojo/public/mojom/base",
]
}
source_set("unit_tests") {
testonly = true
sources = [
"device_sync_mojom_traits_unittest.cc",
]
deps = [
":mojom",
"//base",
"//base/test:test_support",
"//components/cryptauth",
"//mojo/common",
"//mojo/public/cpp/test_support:test_utils",
"//testing/gtest",
]
}
per-file *.mojom=set noparent
per-file *.mojom=file://ipc/SECURITY_OWNERS
per-file *_mojom_traits*.*=set noparent
per-file *_mojom_traits*.*=file://ipc/SECURITY_OWNERS
per-file *.typemap=set noparent
per-file *.typemap=file://ipc/SECURITY_OWNERS
......@@ -11,8 +11,8 @@ import "mojo/public/mojom/base/time.mojom";
// create the BLE channel, both devices must possess the other's BeaconSeeds.
struct BeaconSeed {
string data;
mojo_base.mojom.TimeTicks start_time;
mojo_base.mojom.TimeTicks end_time;
mojo_base.mojom.Time start_time;
mojo_base.mojom.Time end_time;
};
// Metadata describing a remote device with which the current device can
......@@ -21,9 +21,6 @@ struct RemoteDevice {
// Public key used to authenticate a communication channel.
string public_key;
// Identifier which is unique to each device.
string device_id;
// Identifier for the user to whom this device is registered.
string user_id;
......@@ -31,6 +28,9 @@ struct RemoteDevice {
// model, but this value is editable.
string device_name;
// Encryption key used for communication with this device.
string persistent_symmetric_key;
// True if this device has the capability of unlocking another device via
// EasyUnlock.
bool unlock_key;
......@@ -38,7 +38,11 @@ struct RemoteDevice {
// True if this device can enable a Wi-Fi hotspot to support Instant
// Tethering (i.e., the device supports mobile data and its model supports the
// feature).
bool mobile_hotspot_supported;
bool supports_mobile_hotspot;
// The time at which this device's metadata was last updated on the CryptAuth
// back-end.
mojo_base.mojom.Time last_update_time;
// Seeds belonging to the device. Each seed has start and end timestamps which
// indicate how long the seed is valid, and each device has enough associated
......
# Copyright 2018 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.
mojom = "//chromeos/services/device_sync/public/mojom/device_sync.mojom"
public_headers = [
"//components/cryptauth/proto/cryptauth_api.pb.h",
"//components/cryptauth/remote_device.h",
]
traits_headers = [
"//chromeos/services/device_sync/public/mojom/device_sync_mojom_traits.h",
]
sources = [
"//chromeos/services/device_sync/public/mojom/device_sync_mojom_traits.cc",
"//chromeos/services/device_sync/public/mojom/device_sync_mojom_traits.h",
]
public_deps = [
"//components/cryptauth",
"//components/cryptauth/proto",
]
type_mappings = [
"chromeos.device_sync.mojom.BeaconSeed=cryptauth::BeaconSeed",
"chromeos.device_sync.mojom.RemoteDevice=cryptauth::RemoteDevice",
]
// Copyright 2018 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 "chromeos/services/device_sync/public/mojom/device_sync_mojom_traits.h"
#include "mojo/public/cpp/base/time_mojom_traits.h"
namespace mojo {
const std::string& StructTraits<
chromeos::device_sync::mojom::BeaconSeedDataView,
cryptauth::BeaconSeed>::data(const cryptauth::BeaconSeed& beacon_seed) {
return beacon_seed.data();
}
base::Time
StructTraits<chromeos::device_sync::mojom::BeaconSeedDataView,
cryptauth::BeaconSeed>::start_time(const cryptauth::BeaconSeed&
beacon_seed) {
return base::Time::FromJavaTime(beacon_seed.start_time_millis());
}
base::Time StructTraits<
chromeos::device_sync::mojom::BeaconSeedDataView,
cryptauth::BeaconSeed>::end_time(const cryptauth::BeaconSeed& beacon_seed) {
return base::Time::FromJavaTime(beacon_seed.end_time_millis());
}
bool StructTraits<chromeos::device_sync::mojom::BeaconSeedDataView,
cryptauth::BeaconSeed>::
Read(chromeos::device_sync::mojom::BeaconSeedDataView in,
cryptauth::BeaconSeed* out) {
std::string beacon_seed_data;
base::Time start_time;
base::Time end_time;
if (!in.ReadData(&beacon_seed_data) || !in.ReadStartTime(&start_time) ||
!in.ReadEndTime(&end_time)) {
return false;
}
out->set_data(beacon_seed_data);
out->set_start_time_millis(start_time.ToJavaTime());
out->set_end_time_millis(end_time.ToJavaTime());
return true;
};
const std::string&
StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::public_key(const cryptauth::RemoteDevice&
remote_device) {
return remote_device.public_key;
}
const std::string&
StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::user_id(const cryptauth::RemoteDevice&
remote_device) {
return remote_device.user_id;
}
const std::string& StructTraits<
chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::device_name(const cryptauth::RemoteDevice&
remote_device) {
return remote_device.name;
}
const std::string&
StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::
persistent_symmetric_key(const cryptauth::RemoteDevice& remote_device) {
return remote_device.persistent_symmetric_key;
}
bool StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::
unlock_key(const cryptauth::RemoteDevice& remote_device) {
return remote_device.unlock_key;
}
bool StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::
supports_mobile_hotspot(const cryptauth::RemoteDevice& remote_device) {
return remote_device.supports_mobile_hotspot;
}
base::Time StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::
last_update_time(const cryptauth::RemoteDevice& remote_device) {
return base::Time::FromJavaTime(remote_device.last_update_time_millis);
}
const std::vector<cryptauth::BeaconSeed>& StructTraits<
chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::beacon_seeds(const cryptauth::RemoteDevice&
remote_device) {
return remote_device.beacon_seeds;
}
bool StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice>::
Read(chromeos::device_sync::mojom::RemoteDeviceDataView in,
cryptauth::RemoteDevice* out) {
std::string user_id;
std::string device_name;
std::string public_key;
std::string persistent_symmetric_key;
base::Time last_update_time;
std::vector<cryptauth::BeaconSeed> beacon_seeds_out;
if (!in.ReadUserId(&user_id) || !in.ReadDeviceName(&device_name) ||
!in.ReadPublicKey(&public_key) ||
!in.ReadPersistentSymmetricKey(&persistent_symmetric_key) ||
!in.ReadLastUpdateTime(&last_update_time) ||
!in.ReadBeaconSeeds(&beacon_seeds_out)) {
return false;
}
out->user_id = user_id;
out->name = device_name;
out->public_key = public_key;
out->persistent_symmetric_key = persistent_symmetric_key;
out->unlock_key = in.unlock_key();
out->supports_mobile_hotspot = in.supports_mobile_hotspot();
out->last_update_time_millis = last_update_time.ToJavaTime();
out->LoadBeaconSeeds(beacon_seeds_out);
return true;
}
} // namespace mojo
// Copyright 2018 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 CHROMEOS_SERVICES_DEVICE_SYNC_PUBLIC_MOJOM_DEVICE_SYNC_MOJOM_TRAITS_H_
#define CHROMEOS_SERVICES_DEVICE_SYNC_PUBLIC_MOJOM_DEVICE_SYNC_MOJOM_TRAITS_H_
#include <string>
#include <vector>
#include "base/time/time.h"
#include "chromeos/services/device_sync/public/mojom/device_sync.mojom.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h"
#include "components/cryptauth/remote_device.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
namespace mojo {
template <>
class StructTraits<chromeos::device_sync::mojom::BeaconSeedDataView,
cryptauth::BeaconSeed> {
public:
static const std::string& data(const cryptauth::BeaconSeed& beacon_seed);
static base::Time start_time(const cryptauth::BeaconSeed& beacon_seed);
static base::Time end_time(const cryptauth::BeaconSeed& beacon_seed);
static bool Read(chromeos::device_sync::mojom::BeaconSeedDataView in,
cryptauth::BeaconSeed* out);
};
template <>
class StructTraits<chromeos::device_sync::mojom::RemoteDeviceDataView,
cryptauth::RemoteDevice> {
public:
static const std::string& public_key(
const cryptauth::RemoteDevice& remote_device);
static const std::string& user_id(
const cryptauth::RemoteDevice& remote_device);
static const std::string& device_name(
const cryptauth::RemoteDevice& remote_device);
static const std::string& persistent_symmetric_key(
const cryptauth::RemoteDevice& remote_device);
static bool unlock_key(const cryptauth::RemoteDevice& remote_device);
static bool supports_mobile_hotspot(
const cryptauth::RemoteDevice& remote_device);
static base::Time last_update_time(
const cryptauth::RemoteDevice& remote_device);
static const std::vector<cryptauth::BeaconSeed>& beacon_seeds(
const cryptauth::RemoteDevice& remote_device);
static bool Read(chromeos::device_sync::mojom::RemoteDeviceDataView in,
cryptauth::RemoteDevice* out);
};
} // namespace mojo
#endif // CHROMEOS_SERVICES_DEVICE_SYNC_PUBLIC_MOJOM_DEVICE_SYNC_MOJOM_TRAITS_H_
// Copyright 2018 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 "chromeos/services/device_sync/public/mojom/device_sync_mojom_traits.h"
#include "base/time/time.h"
#include "chromeos/services/device_sync/public/mojom/device_sync.mojom.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h"
#include "components/cryptauth/remote_device.h"
#include "mojo/public/cpp/base/time_mojom_traits.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kTestBeaconSeedData[] = "data";
const int64_t kTestBeaconSeedStartTimeMillis = 1L;
const int64_t kTestBeaconSeedEndTimeMillis = 2L;
cryptauth::BeaconSeed CreateTestBeaconSeed() {
cryptauth::BeaconSeed beacon_seed;
beacon_seed.set_data(kTestBeaconSeedData);
beacon_seed.set_start_time_millis(kTestBeaconSeedStartTimeMillis);
beacon_seed.set_end_time_millis(kTestBeaconSeedEndTimeMillis);
return beacon_seed;
}
} // namespace
TEST(DeviceSyncMojomStructTraitsTest, BeaconSeed) {
cryptauth::BeaconSeed input = CreateTestBeaconSeed();
cryptauth::BeaconSeed output;
EXPECT_TRUE(mojo::test::SerializeAndDeserialize<
chromeos::device_sync::mojom::BeaconSeed>(&input, &output));
EXPECT_EQ(kTestBeaconSeedData, output.data());
EXPECT_EQ(kTestBeaconSeedStartTimeMillis, output.start_time_millis());
EXPECT_EQ(kTestBeaconSeedEndTimeMillis, output.end_time_millis());
}
TEST(DeviceSyncMojomStructTraitsTest, RemoteDevice) {
cryptauth::RemoteDevice input;
input.user_id = "userId";
input.name = "name";
input.public_key = "publicKey";
input.persistent_symmetric_key = "persistentSymmetricKey";
input.unlock_key = true;
input.supports_mobile_hotspot = true;
input.last_update_time_millis = 3L;
input.LoadBeaconSeeds({CreateTestBeaconSeed()});
cryptauth::RemoteDevice output;
EXPECT_TRUE(mojo::test::SerializeAndDeserialize<
chromeos::device_sync::mojom::RemoteDevice>(&input, &output));
EXPECT_EQ("userId", output.user_id);
EXPECT_EQ("name", output.name);
EXPECT_EQ("publicKey", output.public_key);
EXPECT_EQ("persistentSymmetricKey", output.persistent_symmetric_key);
EXPECT_TRUE(output.unlock_key);
EXPECT_TRUE(output.supports_mobile_hotspot);
EXPECT_EQ(3L, output.last_update_time_millis);
ASSERT_EQ(1u, output.beacon_seeds.size());
EXPECT_EQ(kTestBeaconSeedData, output.beacon_seeds[0].data());
EXPECT_EQ(kTestBeaconSeedStartTimeMillis,
output.beacon_seeds[0].start_time_millis());
EXPECT_EQ(kTestBeaconSeedEndTimeMillis,
output.beacon_seeds[0].end_time_millis());
}
# Copyright 2018 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.
typemaps =
[ "//chromeos/services/device_sync/public/mojom/device_sync.typemap" ]
......@@ -8,6 +8,7 @@ _typemap_imports = [
"//chrome/common/importer/typemaps.gni",
"//chrome/common/media_router/mojo/typemaps.gni",
"//chrome/typemaps.gni",
"//chromeos/services/device_sync/public/mojom/typemaps.gni",
"//components/arc/common/typemaps.gni",
"//components/metrics/public/cpp/typemaps.gni",
"//components/sync/mojo/typemaps.gni",
......
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