Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 5 | #include "base/ios/device_util.h" |
| 6 | |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 7 | #import <UIKit/UIKit.h> |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 8 | |
[email protected] | 9fe1a5b | 2013-02-07 19:18:03 | [diff] [blame] | 9 | #include "base/strings/sys_string_conversions.h" |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | #include "testing/gtest_mac.h" |
| 12 | #include "testing/platform_test.h" |
| 13 | |
| 14 | namespace { |
Avi Drissman | 56ed131e | 2023-05-25 19:05:37 | [diff] [blame] | 15 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 16 | // The behavior of most of these utility functions depends on what they are run |
| 17 | // on, so there is not much to unittest them. The APIs are run to make sure they |
| 18 | // don't choke. Additional checks are added for particular APIs when needed. |
| 19 | |
| 20 | typedef PlatformTest DeviceUtilTest; |
| 21 | |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 22 | void CleanNSUserDefaultsForDeviceId() { |
| 23 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 24 | [defaults removeObjectForKey:@"ChromeClientID"]; |
| 25 | [defaults removeObjectForKey:@"ChromiumClientID"]; |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 26 | [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"]; |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 27 | [defaults synchronize]; |
| 28 | } |
| 29 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 30 | TEST_F(DeviceUtilTest, IsSingleCoreDevice) { |
| 31 | ios::device_util::IsSingleCoreDevice(); |
| 32 | } |
| 33 | |
| 34 | TEST_F(DeviceUtilTest, GetMacAddress) { |
| 35 | GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U); |
| 36 | } |
| 37 | |
| 38 | TEST_F(DeviceUtilTest, GetRandomId) { |
| 39 | GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U); |
| 40 | } |
| 41 | |
| 42 | TEST_F(DeviceUtilTest, GetDeviceIdentifier) { |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 43 | CleanNSUserDefaultsForDeviceId(); |
| 44 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 45 | std::string default_id = ios::device_util::GetDeviceIdentifier(NULL); |
| 46 | std::string other_id = ios::device_util::GetDeviceIdentifier("ForTest"); |
| 47 | EXPECT_NE(default_id, other_id); |
| 48 | |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 49 | CleanNSUserDefaultsForDeviceId(); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 50 | |
| 51 | std::string new_default_id = ios::device_util::GetDeviceIdentifier(NULL); |
[email protected] | d01f311 | 2014-06-19 22:49:55 | [diff] [blame] | 52 | if (![[[[UIDevice currentDevice] identifierForVendor] UUIDString] |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 53 | isEqualToString:@"00000000-0000-0000-0000-000000000000"]) { |
[email protected] | aebcd0dd | 2012-10-05 17:48:58 | [diff] [blame] | 54 | EXPECT_EQ(default_id, new_default_id); |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 55 | } else { |
[email protected] | aebcd0dd | 2012-10-05 17:48:58 | [diff] [blame] | 56 | EXPECT_NE(default_id, new_default_id); |
[email protected] | d724b556 | 2012-12-13 17:39:47 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | CleanNSUserDefaultsForDeviceId(); |
| 60 | } |
| 61 | |
| 62 | TEST_F(DeviceUtilTest, CheckMigration) { |
| 63 | CleanNSUserDefaultsForDeviceId(); |
| 64 | |
| 65 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 66 | [defaults setObject:@"10000000-0000-0000-0000-000000000000" |
| 67 | forKey:@"ChromeClientID"]; |
| 68 | [defaults synchronize]; |
| 69 | std::string expected_id = ios::device_util::GetDeviceIdentifier(NULL); |
| 70 | [defaults removeObjectForKey:@"ChromeClientID"]; |
| 71 | [defaults setObject:@"10000000-0000-0000-0000-000000000000" |
| 72 | forKey:@"ChromiumClientID"]; |
| 73 | [defaults synchronize]; |
| 74 | std::string new_id = ios::device_util::GetDeviceIdentifier(NULL); |
| 75 | EXPECT_EQ(expected_id, new_id); |
| 76 | |
| 77 | CleanNSUserDefaultsForDeviceId(); |
| 78 | } |
| 79 | |
| 80 | TEST_F(DeviceUtilTest, CheckMigrationFromZero) { |
| 81 | CleanNSUserDefaultsForDeviceId(); |
| 82 | |
| 83 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 84 | [defaults setObject:@"00000000-0000-0000-0000-000000000000" |
| 85 | forKey:@"ChromeClientID"]; |
| 86 | [defaults synchronize]; |
| 87 | std::string zero_id = ios::device_util::GetDeviceIdentifier(NULL); |
| 88 | [defaults removeObjectForKey:@"ChromeClientID"]; |
| 89 | [defaults setObject:@"00000000-0000-0000-0000-000000000000" |
| 90 | forKey:@"ChromiumClientID"]; |
| 91 | [defaults synchronize]; |
| 92 | std::string new_id = ios::device_util::GetDeviceIdentifier(NULL); |
| 93 | EXPECT_NE(zero_id, new_id); |
| 94 | |
| 95 | CleanNSUserDefaultsForDeviceId(); |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 940b67d | 2014-06-03 14:27:18 | [diff] [blame] | 98 | TEST_F(DeviceUtilTest, GetSaltedStringEquals) { |
| 99 | std::string string1("The quick brown fox jumps over the lazy dog"); |
| 100 | std::string string2("The quick brown fox jumps over the lazy dog"); |
| 101 | std::string salt("salt"); |
| 102 | // Same string and same salt should result in the same salted string. |
| 103 | EXPECT_EQ(ios::device_util::GetSaltedString(string1, salt), |
| 104 | ios::device_util::GetSaltedString(string2, salt)); |
| 105 | } |
| 106 | |
| 107 | TEST_F(DeviceUtilTest, GetSaltedStringNotEquals) { |
| 108 | std::string string1("The quick brown fox jumps over the lazy dog"); |
| 109 | std::string string2("The lazy brown fox jumps over the quick dog"); |
| 110 | std::string salt("salt"); |
| 111 | // Different string and same salt should result in different salted strings. |
| 112 | EXPECT_NE(ios::device_util::GetSaltedString(string1, salt), |
| 113 | ios::device_util::GetSaltedString(string2, salt)); |
| 114 | } |
| 115 | |
| 116 | TEST_F(DeviceUtilTest, GetSaltedStringDifferentSalt) { |
| 117 | std::string string1("The quick brown fox jumps over the lazy dog"); |
| 118 | std::string salt1("salt"); |
| 119 | std::string salt2("pepper"); |
| 120 | // Same string with different salt should result in different salted strings. |
| 121 | EXPECT_NE(ios::device_util::GetSaltedString(string1, salt1), |
| 122 | ios::device_util::GetSaltedString(string1, salt2)); |
| 123 | } |
| 124 | |
[email protected] | af6a8c9 | 2012-12-20 16:41:33 | [diff] [blame] | 125 | TEST_F(DeviceUtilTest, CheckDeviceMigration) { |
| 126 | CleanNSUserDefaultsForDeviceId(); |
| 127 | |
| 128 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
| 129 | [defaults setObject:@"10000000-0000-0000-0000-000000000000" |
| 130 | forKey:@"ChromeClientID"]; |
| 131 | [defaults synchronize]; |
| 132 | std::string base_id = ios::device_util::GetDeviceIdentifier(NULL); |
| 133 | [defaults setObject:@"Foo" forKey:@"ClientIDGenerationHardwareType"]; |
| 134 | [defaults synchronize]; |
| 135 | std::string new_id = ios::device_util::GetDeviceIdentifier(NULL); |
| 136 | EXPECT_NE(new_id, base_id); |
| 137 | |
| 138 | CleanNSUserDefaultsForDeviceId(); |
| 139 | } |
| 140 | |
[email protected] | e16e873 | 2012-08-07 11:03:11 | [diff] [blame] | 141 | } // namespace |