| 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 "chrome/browser/prefs/tracked/pref_hash_calculator_helper.h" | 5 #include "chrome/browser/prefs/tracked/pref_hash_calculator_helper.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "chrome/browser/extensions/api/music_manager_private/device_id.h" | 16 #include "chrome/browser/extensions/api/music_manager_private/device_id.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
| 19 #include "crypto/hmac.h" | 19 #include "crypto/hmac.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 #if defined(ENABLE_RLZ) |
| 20 #include "rlz/lib/machine_id.h" | 23 #include "rlz/lib/machine_id.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 24 #endif |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 // extensions::api::DeviceId::GetDeviceId() signs the extension_id in | 28 // extensions::api::DeviceId::GetDeviceId() signs the extension_id in |
| 26 // GetRawDeviceIdCallback to get the final device_id, our code replicating this | 29 // GetRawDeviceIdCallback to get the final device_id, our code replicating this |
| 27 // id needs to do the same thing. | 30 // id needs to do the same thing. |
| 28 const char kFakeExtensionId[] = "foo"; | 31 const char kFakeExtensionId[] = "foo"; |
| 29 | 32 |
| 30 // Sets |media_device_id_out| to |media_device_id_in| and unblocks the original | 33 // Sets |media_device_id_out| to |media_device_id_in| and unblocks the original |
| 31 // binder by calling |unblock_callback|. | 34 // binder by calling |unblock_callback|. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 std::vector<uint8> digest(digest_length); | 66 std::vector<uint8> digest(digest_length); |
| 64 bool result = hmac.Init(raw_device_id) && | 67 bool result = hmac.Init(raw_device_id) && |
| 65 hmac.Sign(kFakeExtensionId, &digest[0], digest.size()); | 68 hmac.Sign(kFakeExtensionId, &digest[0], digest.size()); |
| 66 if (!result) { | 69 if (!result) { |
| 67 ADD_FAILURE(); | 70 ADD_FAILURE(); |
| 68 return std::string(); | 71 return std::string(); |
| 69 } | 72 } |
| 70 return StringToLowerASCII(base::HexEncode(digest.data(), digest.size())); | 73 return StringToLowerASCII(base::HexEncode(digest.data(), digest.size())); |
| 71 } | 74 } |
| 72 | 75 |
| 76 #if defined(ENABLE_RLZ) |
| 73 std::string GetLegacyIdBasedOnRlzId() { | 77 std::string GetLegacyIdBasedOnRlzId() { |
| 74 std::string rlz_machine_id; | 78 std::string rlz_machine_id; |
| 75 rlz_lib::GetMachineId(&rlz_machine_id); | 79 rlz_lib::GetMachineId(&rlz_machine_id); |
| 76 EXPECT_FALSE(rlz_machine_id.empty()); | 80 EXPECT_FALSE(rlz_machine_id.empty()); |
| 77 | 81 |
| 78 const std::string raw_legacy_device_id(GetLegacyDeviceId(rlz_machine_id)); | 82 const std::string raw_legacy_device_id(GetLegacyDeviceId(rlz_machine_id)); |
| 79 | 83 |
| 80 if (raw_legacy_device_id.empty()) { | 84 if (raw_legacy_device_id.empty()) { |
| 81 LOG(WARNING) << "Raw legacy device ID based on RLZ ID is empty."; | 85 LOG(WARNING) << "Raw legacy device ID based on RLZ ID is empty."; |
| 82 return std::string(); | 86 return std::string(); |
| 83 } | 87 } |
| 84 | 88 |
| 85 const std::string legacy_device_id( | 89 const std::string legacy_device_id( |
| 86 GetDeviceIdFromRawDeviceId(raw_legacy_device_id)); | 90 GetDeviceIdFromRawDeviceId(raw_legacy_device_id)); |
| 87 EXPECT_FALSE(legacy_device_id.empty()); | 91 EXPECT_FALSE(legacy_device_id.empty()); |
| 88 | 92 |
| 89 return legacy_device_id; | 93 return legacy_device_id; |
| 90 } | 94 } |
| 95 #endif // ENABLE_RLZ |
| 91 | 96 |
| 92 // Simulate browser threads (required by extensions::api::DeviceId) off of the | 97 // Simulate browser threads (required by extensions::api::DeviceId) off of the |
| 93 // main message loop. | 98 // main message loop. |
| 94 class PrefHashCalculatorHelperTest : public testing::Test { | 99 class PrefHashCalculatorHelperTest : public testing::Test { |
| 95 private: | 100 private: |
| 96 content::TestBrowserThreadBundle test_browser_thread_bundle; | 101 content::TestBrowserThreadBundle test_browser_thread_bundle; |
| 97 }; | 102 }; |
| 98 | 103 |
| 99 } // namespace | 104 } // namespace |
| 100 | 105 |
| 101 // The implementation for the legacy ID on Windows is copied from | 106 // The implementation for the legacy ID on Windows is copied from |
| 102 // the M33 version of extensions::api::DeviceId::GetDeviceId(). We no longer | 107 // the M33 version of extensions::api::DeviceId::GetDeviceId(). We no longer |
| 103 // depend on it as of M34, but should make sure that we are generating the same | 108 // depend on it as of M34, but should make sure that we are generating the same |
| 104 // results in the mean time (it will be okay for the extension API's | 109 // results in the mean time (it will be okay for the extension API's |
| 105 // implementation to diverge on M34+ and this test can be removed once M34 ships | 110 // implementation to diverge on M34+ and this test can be removed once M34 ships |
| 106 // to stable). | 111 // to stable). |
| 112 #if defined(ENABLE_RLZ) |
| 107 TEST_F(PrefHashCalculatorHelperTest, ResultMatchesMediaId) { | 113 TEST_F(PrefHashCalculatorHelperTest, ResultMatchesMediaId) { |
| 108 EXPECT_EQ(GetMediaDeviceIdSynchronously(), GetLegacyIdBasedOnRlzId()); | 114 EXPECT_EQ(GetMediaDeviceIdSynchronously(), GetLegacyIdBasedOnRlzId()); |
| 109 } | 115 } |
| 116 #endif // ENABLE_RLZ |
| 110 | 117 |
| 111 TEST_F(PrefHashCalculatorHelperTest, MediaIdIsDeterministic) { | 118 TEST_F(PrefHashCalculatorHelperTest, MediaIdIsDeterministic) { |
| 112 EXPECT_EQ(GetMediaDeviceIdSynchronously(), GetMediaDeviceIdSynchronously()); | 119 EXPECT_EQ(GetMediaDeviceIdSynchronously(), GetMediaDeviceIdSynchronously()); |
| 113 } | 120 } |
| 114 | 121 |
| 122 #if defined(ENABLE_RLZ) |
| 115 TEST_F(PrefHashCalculatorHelperTest, RlzBasedIdIsDeterministic) { | 123 TEST_F(PrefHashCalculatorHelperTest, RlzBasedIdIsDeterministic) { |
| 116 EXPECT_EQ(GetLegacyIdBasedOnRlzId(), GetLegacyIdBasedOnRlzId()); | 124 EXPECT_EQ(GetLegacyIdBasedOnRlzId(), GetLegacyIdBasedOnRlzId()); |
| 117 } | 125 } |
| 126 #endif // ENABLE_RLZ |
| OLD | NEW |