blob: bd5fa8a25557a39282f11260128fae265b3eab22 [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
[email protected]d724b5562012-12-13 17:39:475#import <UIKit/UIKit.h>
[email protected]e16e8732012-08-07 11:03:116
7#include "base/ios/device_util.h"
[email protected]9fe1a5b2013-02-07 19:18:038#include "base/strings/sys_string_conversions.h"
[email protected]e16e8732012-08-07 11:03:119#include "testing/gtest/include/gtest/gtest.h"
10#include "testing/gtest_mac.h"
11#include "testing/platform_test.h"
12
Avi Drissman56ed131e2023-05-25 19:05:3713#if !defined(__has_feature) || !__has_feature(objc_arc)
14#error "This file requires ARC support."
15#endif
16
[email protected]e16e8732012-08-07 11:03:1117namespace {
Avi Drissman56ed131e2023-05-25 19:05:3718
[email protected]e16e8732012-08-07 11:03:1119// 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
23typedef PlatformTest DeviceUtilTest;
24
[email protected]d724b5562012-12-13 17:39:4725void CleanNSUserDefaultsForDeviceId() {
26 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
27 [defaults removeObjectForKey:@"ChromeClientID"];
28 [defaults removeObjectForKey:@"ChromiumClientID"];
[email protected]af6a8c92012-12-20 16:41:3329 [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"];
[email protected]d724b5562012-12-13 17:39:4730 [defaults synchronize];
31}
32
[email protected]e16e8732012-08-07 11:03:1133TEST_F(DeviceUtilTest, GetPlatform) {
34 GTEST_ASSERT_GT(ios::device_util::GetPlatform().length(), 0U);
35}
36
[email protected]e16e8732012-08-07 11:03:1137TEST_F(DeviceUtilTest, IsSingleCoreDevice) {
38 ios::device_util::IsSingleCoreDevice();
39}
40
41TEST_F(DeviceUtilTest, GetMacAddress) {
42 GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U);
43}
44
45TEST_F(DeviceUtilTest, GetRandomId) {
46 GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U);
47}
48
49TEST_F(DeviceUtilTest, GetDeviceIdentifier) {
[email protected]d724b5562012-12-13 17:39:4750 CleanNSUserDefaultsForDeviceId();
51
[email protected]e16e8732012-08-07 11:03:1152 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]d724b5562012-12-13 17:39:4756 CleanNSUserDefaultsForDeviceId();
[email protected]e16e8732012-08-07 11:03:1157
58 std::string new_default_id = ios::device_util::GetDeviceIdentifier(NULL);
[email protected]d01f3112014-06-19 22:49:5559 if (![[[[UIDevice currentDevice] identifierForVendor] UUIDString]
[email protected]d724b5562012-12-13 17:39:4760 isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
[email protected]aebcd0dd2012-10-05 17:48:5861 EXPECT_EQ(default_id, new_default_id);
[email protected]d724b5562012-12-13 17:39:4762 } else {
[email protected]aebcd0dd2012-10-05 17:48:5863 EXPECT_NE(default_id, new_default_id);
[email protected]d724b5562012-12-13 17:39:4764 }
65
66 CleanNSUserDefaultsForDeviceId();
67}
68
69TEST_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
87TEST_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]e16e8732012-08-07 11:03:11103}
104
[email protected]940b67d2014-06-03 14:27:18105TEST_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
114TEST_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
123TEST_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]af6a8c92012-12-20 16:41:33132TEST_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]e16e8732012-08-07 11:03:11148} // namespace