1// Copyright 2012 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#import <UIKit/UIKit.h> 6 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" 12 13namespace { 14 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. 18 19typedef PlatformTest DeviceUtilTest; 20 21void CleanNSUserDefaultsForDeviceId() { 22 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 23 [defaults removeObjectForKey:@"ChromeClientID"]; 24 [defaults removeObjectForKey:@"ChromiumClientID"]; 25 [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"]; 26 [defaults synchronize]; 27} 28 29TEST_F(DeviceUtilTest, GetPlatform) { 30 GTEST_ASSERT_GT(ios::device_util::GetPlatform().length(), 0U); 31} 32 33TEST_F(DeviceUtilTest, IsSingleCoreDevice) { 34 ios::device_util::IsSingleCoreDevice(); 35} 36 37TEST_F(DeviceUtilTest, GetMacAddress) { 38 GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U); 39} 40 41TEST_F(DeviceUtilTest, GetRandomId) { 42 GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U); 43} 44 45TEST_F(DeviceUtilTest, GetDeviceIdentifier) { 46 CleanNSUserDefaultsForDeviceId(); 47 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); 51 52 CleanNSUserDefaultsForDeviceId(); 53 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); 58 } else { 59 EXPECT_NE(default_id, new_default_id); 60 } 61 62 CleanNSUserDefaultsForDeviceId(); 63} 64 65TEST_F(DeviceUtilTest, CheckMigration) { 66 CleanNSUserDefaultsForDeviceId(); 67 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); 79 80 CleanNSUserDefaultsForDeviceId(); 81} 82 83TEST_F(DeviceUtilTest, CheckMigrationFromZero) { 84 CleanNSUserDefaultsForDeviceId(); 85 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); 97 98 CleanNSUserDefaultsForDeviceId(); 99} 100 101TEST_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)); 108} 109 110TEST_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)); 117} 118 119TEST_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)); 126} 127 128TEST_F(DeviceUtilTest, CheckDeviceMigration) { 129 CleanNSUserDefaultsForDeviceId(); 130 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); 140 141 CleanNSUserDefaultsForDeviceId(); 142} 143 144} // namespace 145