1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import <UIKit/UIKit.h>
7 #include "base/ios/device_util.h"
8 #include "base/ios/ios_util.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/gtest_mac.h"
12 #include "testing/platform_test.h"
15 // The behavior of most of these utility functions depends on what they are run
16 // on, so there is not much to unittest them. The APIs are run to make sure they
17 // don't choke. Additional checks are added for particular APIs when needed.
19 typedef PlatformTest DeviceUtilTest;
21 void CleanNSUserDefaultsForDeviceId() {
22 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
23 [defaults removeObjectForKey:@"ChromeClientID"];
24 [defaults removeObjectForKey:@"ChromiumClientID"];
25 [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"];
26 [defaults synchronize];
29 TEST_F(DeviceUtilTest, GetPlatform) {
30 GTEST_ASSERT_GT(ios::device_util::GetPlatform().length(), 0U);
33 TEST_F(DeviceUtilTest, IsSingleCoreDevice) {
34 ios::device_util::IsSingleCoreDevice();
37 TEST_F(DeviceUtilTest, GetMacAddress) {
38 GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U);
41 TEST_F(DeviceUtilTest, GetRandomId) {
42 GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U);
45 TEST_F(DeviceUtilTest, GetDeviceIdentifier) {
46 CleanNSUserDefaultsForDeviceId();
48 std::string default_id = ios::device_util::GetDeviceIdentifier(NULL);
49 std::string other_id = ios::device_util::GetDeviceIdentifier("ForTest");
50 EXPECT_NE(default_id, other_id);
52 CleanNSUserDefaultsForDeviceId();
54 std::string new_default_id = ios::device_util::GetDeviceIdentifier(NULL);
55 if (![[[[UIDevice currentDevice] identifierForVendor] UUIDString]
56 isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
57 EXPECT_EQ(default_id, new_default_id);
59 EXPECT_NE(default_id, new_default_id);
62 CleanNSUserDefaultsForDeviceId();
65 TEST_F(DeviceUtilTest, CheckMigration) {
66 CleanNSUserDefaultsForDeviceId();
68 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
69 [defaults setObject:@"10000000-0000-0000-0000-000000000000"
70 forKey:@"ChromeClientID"];
71 [defaults synchronize];
72 std::string expected_id = ios::device_util::GetDeviceIdentifier(NULL);
73 [defaults removeObjectForKey:@"ChromeClientID"];
74 [defaults setObject:@"10000000-0000-0000-0000-000000000000"
75 forKey:@"ChromiumClientID"];
76 [defaults synchronize];
77 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
78 EXPECT_EQ(expected_id, new_id);
80 CleanNSUserDefaultsForDeviceId();
83 TEST_F(DeviceUtilTest, CheckMigrationFromZero) {
84 CleanNSUserDefaultsForDeviceId();
86 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
87 [defaults setObject:@"00000000-0000-0000-0000-000000000000"
88 forKey:@"ChromeClientID"];
89 [defaults synchronize];
90 std::string zero_id = ios::device_util::GetDeviceIdentifier(NULL);
91 [defaults removeObjectForKey:@"ChromeClientID"];
92 [defaults setObject:@"00000000-0000-0000-0000-000000000000"
93 forKey:@"ChromiumClientID"];
94 [defaults synchronize];
95 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
96 EXPECT_NE(zero_id, new_id);
98 CleanNSUserDefaultsForDeviceId();
101 TEST_F(DeviceUtilTest, GetSaltedStringEquals) {
102 std::string string1("The quick brown fox jumps over the lazy dog");
103 std::string string2("The quick brown fox jumps over the lazy dog");
104 std::string salt("salt");
105 // Same string and same salt should result in the same salted string.
106 EXPECT_EQ(ios::device_util::GetSaltedString(string1, salt),
107 ios::device_util::GetSaltedString(string2, salt));
110 TEST_F(DeviceUtilTest, GetSaltedStringNotEquals) {
111 std::string string1("The quick brown fox jumps over the lazy dog");
112 std::string string2("The lazy brown fox jumps over the quick dog");
113 std::string salt("salt");
114 // Different string and same salt should result in different salted strings.
115 EXPECT_NE(ios::device_util::GetSaltedString(string1, salt),
116 ios::device_util::GetSaltedString(string2, salt));
119 TEST_F(DeviceUtilTest, GetSaltedStringDifferentSalt) {
120 std::string string1("The quick brown fox jumps over the lazy dog");
121 std::string salt1("salt");
122 std::string salt2("pepper");
123 // Same string with different salt should result in different salted strings.
124 EXPECT_NE(ios::device_util::GetSaltedString(string1, salt1),
125 ios::device_util::GetSaltedString(string1, salt2));
128 TEST_F(DeviceUtilTest, CheckDeviceMigration) {
129 CleanNSUserDefaultsForDeviceId();
131 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
132 [defaults setObject:@"10000000-0000-0000-0000-000000000000"
133 forKey:@"ChromeClientID"];
134 [defaults synchronize];
135 std::string base_id = ios::device_util::GetDeviceIdentifier(NULL);
136 [defaults setObject:@"Foo" forKey:@"ClientIDGenerationHardwareType"];
137 [defaults synchronize];
138 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
139 EXPECT_NE(new_id, base_id);
141 CleanNSUserDefaultsForDeviceId();