This source file includes following definitions.
- SetMediaDeviceIdAndUnblock
- GetMediaDeviceIdSynchronously
- GetDeviceIdFromRawDeviceId
- GetLegacyIdBasedOnRlzId
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/prefs/tracked/pref_hash_calculator_helper.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "chrome/browser/extensions/api/music_manager_private/device_id.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_utils.h"
#include "crypto/hmac.h"
#include "rlz/lib/machine_id.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kFakeExtensionId[] = "foo";
void SetMediaDeviceIdAndUnblock(const base::Closure& unblock_callback,
std::string* media_device_id_out,
const std::string& media_device_id_in) {
if (media_device_id_in.empty())
LOG(WARNING) << "Media device ID is empty.";
*media_device_id_out = media_device_id_in;
unblock_callback.Run();
}
std::string GetMediaDeviceIdSynchronously() {
std::string media_device_id;
const scoped_refptr<content::MessageLoopRunner> runner(
new content::MessageLoopRunner);
extensions::api::DeviceId::GetDeviceId(kFakeExtensionId,
base::Bind(&SetMediaDeviceIdAndUnblock,
runner->QuitClosure(),
base::Unretained(&media_device_id)));
runner->Run();
return media_device_id;
}
std::string GetDeviceIdFromRawDeviceId(const std::string& raw_device_id) {
crypto::HMAC hmac(crypto::HMAC::SHA256);
const size_t digest_length = hmac.DigestLength();
std::vector<uint8> digest(digest_length);
bool result = hmac.Init(raw_device_id) &&
hmac.Sign(kFakeExtensionId, &digest[0], digest.size());
if (!result) {
ADD_FAILURE();
return std::string();
}
return StringToLowerASCII(base::HexEncode(digest.data(), digest.size()));
}
std::string GetLegacyIdBasedOnRlzId() {
std::string rlz_machine_id;
rlz_lib::GetMachineId(&rlz_machine_id);
EXPECT_FALSE(rlz_machine_id.empty());
const std::string raw_legacy_device_id(GetLegacyDeviceId(rlz_machine_id));
if (raw_legacy_device_id.empty()) {
LOG(WARNING) << "Raw legacy device ID based on RLZ ID is empty.";
return std::string();
}
const std::string legacy_device_id(
GetDeviceIdFromRawDeviceId(raw_legacy_device_id));
EXPECT_FALSE(legacy_device_id.empty());
return legacy_device_id;
}
class PrefHashCalculatorHelperTest : public testing::Test {
private:
content::TestBrowserThreadBundle test_browser_thread_bundle;
};
}
TEST_F(PrefHashCalculatorHelperTest, ResultMatchesMediaId) {
EXPECT_EQ(GetMediaDeviceIdSynchronously(), GetLegacyIdBasedOnRlzId());
}
TEST_F(PrefHashCalculatorHelperTest, MediaIdIsDeterministic) {
EXPECT_EQ(GetMediaDeviceIdSynchronously(), GetMediaDeviceIdSynchronously());
}
TEST_F(PrefHashCalculatorHelperTest, RlzBasedIdIsDeterministic) {
EXPECT_EQ(GetLegacyIdBasedOnRlzId(), GetLegacyIdBasedOnRlzId());
}