blob: 9eb520ace29f44da2427b483197c25a50da1895c [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]e16e8732012-08-07 11:03:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Peter Kasting134ef9af2024-12-28 02:30:095#include "base/ios/device_util.h"
6
[email protected]d724b5562012-12-13 17:39:477#import <UIKit/UIKit.h>
[email protected]e16e8732012-08-07 11:03:118
[email protected]9fe1a5b2013-02-07 19:18:039#include "base/strings/sys_string_conversions.h"
[email protected]e16e8732012-08-07 11:03:1110#include "testing/gtest/include/gtest/gtest.h"
11#include "testing/gtest_mac.h"
12#include "testing/platform_test.h"
13
14namespace {
Avi Drissman56ed131e2023-05-25 19:05:3715
[email protected]e16e8732012-08-07 11:03:1116// 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
20typedef PlatformTest DeviceUtilTest;
21
[email protected]d724b5562012-12-13 17:39:4722void CleanNSUserDefaultsForDeviceId() {
23 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
24 [defaults removeObjectForKey:@"ChromeClientID"];
25 [defaults removeObjectForKey:@"ChromiumClientID"];
[email protected]af6a8c92012-12-20 16:41:3326 [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"];
[email protected]d724b5562012-12-13 17:39:4727 [defaults synchronize];
28}
29
[email protected]e16e8732012-08-07 11:03:1130TEST_F(DeviceUtilTest, IsSingleCoreDevice) {
31 ios::device_util::IsSingleCoreDevice();
32}
33
34TEST_F(DeviceUtilTest, GetMacAddress) {
35 GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U);
36}
37
38TEST_F(DeviceUtilTest, GetRandomId) {
39 GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U);
40}
41
42TEST_F(DeviceUtilTest, GetDeviceIdentifier) {
[email protected]d724b5562012-12-13 17:39:4743 CleanNSUserDefaultsForDeviceId();
44
[email protected]e16e8732012-08-07 11:03:1145 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]d724b5562012-12-13 17:39:4749 CleanNSUserDefaultsForDeviceId();
[email protected]e16e8732012-08-07 11:03:1150
51 std::string new_default_id = ios::device_util::GetDeviceIdentifier(NULL);
[email protected]d01f3112014-06-19 22:49:5552 if (![[[[UIDevice currentDevice] identifierForVendor] UUIDString]
[email protected]d724b5562012-12-13 17:39:4753 isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
[email protected]aebcd0dd2012-10-05 17:48:5854 EXPECT_EQ(default_id, new_default_id);
[email protected]d724b5562012-12-13 17:39:4755 } else {
[email protected]aebcd0dd2012-10-05 17:48:5856 EXPECT_NE(default_id, new_default_id);
[email protected]d724b5562012-12-13 17:39:4757 }
58
59 CleanNSUserDefaultsForDeviceId();
60}
61
62TEST_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
80TEST_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]e16e8732012-08-07 11:03:1196}
97
[email protected]940b67d2014-06-03 14:27:1898TEST_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
107TEST_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
116TEST_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]af6a8c92012-12-20 16:41:33125TEST_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]e16e8732012-08-07 11:03:11141} // namespace