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, IsSingleCoreDevice) { 30 ios::device_util::IsSingleCoreDevice(); 31} 32 33TEST_F(DeviceUtilTest, GetMacAddress) { 34 GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U); 35} 36 37TEST_F(DeviceUtilTest, GetRandomId) { 38 GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U); 39} 40 41TEST_F(DeviceUtilTest, GetDeviceIdentifier) { 42 CleanNSUserDefaultsForDeviceId(); 43 44 std::string default_id = ios::device_util::GetDeviceIdentifier(NULL); 45 std::string other_id = ios::device_util::GetDeviceIdentifier("ForTest"); 46 EXPECT_NE(default_id, other_id); 47 48 CleanNSUserDefaultsForDeviceId(); 49 50 std::string new_default_id = ios::device_util::GetDeviceIdentifier(NULL); 51 if (![[[[UIDevice currentDevice] identifierForVendor] UUIDString] 52 isEqualToString:@"00000000-0000-0000-0000-000000000000"]) { 53 EXPECT_EQ(default_id, new_default_id); 54 } else { 55 EXPECT_NE(default_id, new_default_id); 56 } 57 58 CleanNSUserDefaultsForDeviceId(); 59} 60 61TEST_F(DeviceUtilTest, CheckMigration) { 62 CleanNSUserDefaultsForDeviceId(); 63 64 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 65 [defaults setObject:@"10000000-0000-0000-0000-000000000000" 66 forKey:@"ChromeClientID"]; 67 [defaults synchronize]; 68 std::string expected_id = ios::device_util::GetDeviceIdentifier(NULL); 69 [defaults removeObjectForKey:@"ChromeClientID"]; 70 [defaults setObject:@"10000000-0000-0000-0000-000000000000" 71 forKey:@"ChromiumClientID"]; 72 [defaults synchronize]; 73 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL); 74 EXPECT_EQ(expected_id, new_id); 75 76 CleanNSUserDefaultsForDeviceId(); 77} 78 79TEST_F(DeviceUtilTest, CheckMigrationFromZero) { 80 CleanNSUserDefaultsForDeviceId(); 81 82 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 83 [defaults setObject:@"00000000-0000-0000-0000-000000000000" 84 forKey:@"ChromeClientID"]; 85 [defaults synchronize]; 86 std::string zero_id = ios::device_util::GetDeviceIdentifier(NULL); 87 [defaults removeObjectForKey:@"ChromeClientID"]; 88 [defaults setObject:@"00000000-0000-0000-0000-000000000000" 89 forKey:@"ChromiumClientID"]; 90 [defaults synchronize]; 91 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL); 92 EXPECT_NE(zero_id, new_id); 93 94 CleanNSUserDefaultsForDeviceId(); 95} 96 97TEST_F(DeviceUtilTest, GetSaltedStringEquals) { 98 std::string string1("The quick brown fox jumps over the lazy dog"); 99 std::string string2("The quick brown fox jumps over the lazy dog"); 100 std::string salt("salt"); 101 // Same string and same salt should result in the same salted string. 102 EXPECT_EQ(ios::device_util::GetSaltedString(string1, salt), 103 ios::device_util::GetSaltedString(string2, salt)); 104} 105 106TEST_F(DeviceUtilTest, GetSaltedStringNotEquals) { 107 std::string string1("The quick brown fox jumps over the lazy dog"); 108 std::string string2("The lazy brown fox jumps over the quick dog"); 109 std::string salt("salt"); 110 // Different string and same salt should result in different salted strings. 111 EXPECT_NE(ios::device_util::GetSaltedString(string1, salt), 112 ios::device_util::GetSaltedString(string2, salt)); 113} 114 115TEST_F(DeviceUtilTest, GetSaltedStringDifferentSalt) { 116 std::string string1("The quick brown fox jumps over the lazy dog"); 117 std::string salt1("salt"); 118 std::string salt2("pepper"); 119 // Same string with different salt should result in different salted strings. 120 EXPECT_NE(ios::device_util::GetSaltedString(string1, salt1), 121 ios::device_util::GetSaltedString(string1, salt2)); 122} 123 124TEST_F(DeviceUtilTest, CheckDeviceMigration) { 125 CleanNSUserDefaultsForDeviceId(); 126 127 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 128 [defaults setObject:@"10000000-0000-0000-0000-000000000000" 129 forKey:@"ChromeClientID"]; 130 [defaults synchronize]; 131 std::string base_id = ios::device_util::GetDeviceIdentifier(NULL); 132 [defaults setObject:@"Foo" forKey:@"ClientIDGenerationHardwareType"]; 133 [defaults synchronize]; 134 std::string new_id = ios::device_util::GetDeviceIdentifier(NULL); 135 EXPECT_NE(new_id, base_id); 136 137 CleanNSUserDefaultsForDeviceId(); 138} 139 140} // namespace 141