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/strings/sys_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/gtest_mac.h"
11 #include "testing/platform_test.h"
14 // The behavior of most of these utility functions depends on what they are run
15 // on, so there is not much to unittest them. The APIs are run to make sure they
16 // don't choke. Additional checks are added for particular APIs when needed.
18 typedef PlatformTest DeviceUtilTest;
20 void CleanNSUserDefaultsForDeviceId() {
21 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
22 [defaults removeObjectForKey:@"ChromeClientID"];
23 [defaults removeObjectForKey:@"ChromiumClientID"];
24 [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"];
25 [defaults synchronize];
28 TEST_F(DeviceUtilTest, GetPlatform) {
29 GTEST_ASSERT_GT(ios::device_util::GetPlatform().length(), 0U);
32 TEST_F(DeviceUtilTest, IsSingleCoreDevice) {
33 ios::device_util::IsSingleCoreDevice();
36 TEST_F(DeviceUtilTest, GetMacAddress) {
37 GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U);
40 TEST_F(DeviceUtilTest, GetRandomId) {
41 GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U);
44 TEST_F(DeviceUtilTest, GetDeviceIdentifier) {
45 CleanNSUserDefaultsForDeviceId();
47 std::string default_id = ios::device_util::GetDeviceIdentifier(NULL);
48 std::string other_id = ios::device_util::GetDeviceIdentifier("ForTest");
49 EXPECT_NE(default_id, other_id);
51 CleanNSUserDefaultsForDeviceId();
53 std::string new_default_id = ios::device_util::GetDeviceIdentifier(NULL);
54 if (![[[[UIDevice currentDevice] identifierForVendor] UUIDString]
55 isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
56 EXPECT_EQ(default_id, new_default_id);
58 EXPECT_NE(default_id, new_default_id);
61 CleanNSUserDefaultsForDeviceId();
64 TEST_F(DeviceUtilTest, CheckMigration) {
65 CleanNSUserDefaultsForDeviceId();
67 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
68 [defaults setObject:@"10000000-0000-0000-0000-000000000000"
69 forKey:@"ChromeClientID"];
70 [defaults synchronize];
71 std::string expected_id = ios::device_util::GetDeviceIdentifier(NULL);
72 [defaults removeObjectForKey:@"ChromeClientID"];
73 [defaults setObject:@"10000000-0000-0000-0000-000000000000"
74 forKey:@"ChromiumClientID"];
75 [defaults synchronize];
76 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
77 EXPECT_EQ(expected_id, new_id);
79 CleanNSUserDefaultsForDeviceId();
82 TEST_F(DeviceUtilTest, CheckMigrationFromZero) {
83 CleanNSUserDefaultsForDeviceId();
85 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
86 [defaults setObject:@"00000000-0000-0000-0000-000000000000"
87 forKey:@"ChromeClientID"];
88 [defaults synchronize];
89 std::string zero_id = ios::device_util::GetDeviceIdentifier(NULL);
90 [defaults removeObjectForKey:@"ChromeClientID"];
91 [defaults setObject:@"00000000-0000-0000-0000-000000000000"
92 forKey:@"ChromiumClientID"];
93 [defaults synchronize];
94 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
95 EXPECT_NE(zero_id, new_id);
97 CleanNSUserDefaultsForDeviceId();
100 TEST_F(DeviceUtilTest, GetSaltedStringEquals) {
101 std::string string1("The quick brown fox jumps over the lazy dog");
102 std::string string2("The quick brown fox jumps over the lazy dog");
103 std::string salt("salt");
104 // Same string and same salt should result in the same salted string.
105 EXPECT_EQ(ios::device_util::GetSaltedString(string1, salt),
106 ios::device_util::GetSaltedString(string2, salt));
109 TEST_F(DeviceUtilTest, GetSaltedStringNotEquals) {
110 std::string string1("The quick brown fox jumps over the lazy dog");
111 std::string string2("The lazy brown fox jumps over the quick dog");
112 std::string salt("salt");
113 // Different string and same salt should result in different salted strings.
114 EXPECT_NE(ios::device_util::GetSaltedString(string1, salt),
115 ios::device_util::GetSaltedString(string2, salt));
118 TEST_F(DeviceUtilTest, GetSaltedStringDifferentSalt) {
119 std::string string1("The quick brown fox jumps over the lazy dog");
120 std::string salt1("salt");
121 std::string salt2("pepper");
122 // Same string with different salt should result in different salted strings.
123 EXPECT_NE(ios::device_util::GetSaltedString(string1, salt1),
124 ios::device_util::GetSaltedString(string1, salt2));
127 TEST_F(DeviceUtilTest, CheckDeviceMigration) {
128 CleanNSUserDefaultsForDeviceId();
130 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
131 [defaults setObject:@"10000000-0000-0000-0000-000000000000"
132 forKey:@"ChromeClientID"];
133 [defaults synchronize];
134 std::string base_id = ios::device_util::GetDeviceIdentifier(NULL);
135 [defaults setObject:@"Foo" forKey:@"ClientIDGenerationHardwareType"];
136 [defaults synchronize];
137 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
138 EXPECT_NE(new_id, base_id);
140 CleanNSUserDefaultsForDeviceId();