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