OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
26 #include "gtest/gtest.h" | 26 #include "gtest/gtest.h" |
27 | 27 |
28 namespace crashpad { | 28 namespace crashpad { |
29 namespace test { | 29 namespace test { |
30 namespace { | 30 namespace { |
31 | 31 |
32 TEST(UUID, UUID) { | 32 TEST(UUID, UUID) { |
33 UUID uuid_zero; | 33 UUID uuid_zero; |
34 uuid_zero.InitializeToZero(); | 34 uuid_zero.InitializeToZero(); |
35 EXPECT_EQ(0u, uuid_zero.data_1); | 35 EXPECT_EQ(uuid_zero.data_1, 0u); |
36 EXPECT_EQ(0u, uuid_zero.data_2); | 36 EXPECT_EQ(uuid_zero.data_2, 0u); |
37 EXPECT_EQ(0u, uuid_zero.data_3); | 37 EXPECT_EQ(uuid_zero.data_3, 0u); |
38 EXPECT_EQ(0u, uuid_zero.data_4[0]); | 38 EXPECT_EQ(uuid_zero.data_4[0], 0u); |
39 EXPECT_EQ(0u, uuid_zero.data_4[1]); | 39 EXPECT_EQ(uuid_zero.data_4[1], 0u); |
40 EXPECT_EQ(0u, uuid_zero.data_5[0]); | 40 EXPECT_EQ(uuid_zero.data_5[0], 0u); |
41 EXPECT_EQ(0u, uuid_zero.data_5[1]); | 41 EXPECT_EQ(uuid_zero.data_5[1], 0u); |
42 EXPECT_EQ(0u, uuid_zero.data_5[2]); | 42 EXPECT_EQ(uuid_zero.data_5[2], 0u); |
43 EXPECT_EQ(0u, uuid_zero.data_5[3]); | 43 EXPECT_EQ(uuid_zero.data_5[3], 0u); |
44 EXPECT_EQ(0u, uuid_zero.data_5[4]); | 44 EXPECT_EQ(uuid_zero.data_5[4], 0u); |
45 EXPECT_EQ(0u, uuid_zero.data_5[5]); | 45 EXPECT_EQ(uuid_zero.data_5[5], 0u); |
46 EXPECT_EQ("00000000-0000-0000-0000-000000000000", uuid_zero.ToString()); | 46 EXPECT_EQ(uuid_zero.ToString(), "00000000-0000-0000-0000-000000000000"); |
47 | 47 |
48 const uint8_t kBytes[16] = {0x00, | 48 const uint8_t kBytes[16] = {0x00, |
49 0x01, | 49 0x01, |
50 0x02, | 50 0x02, |
51 0x03, | 51 0x03, |
52 0x04, | 52 0x04, |
53 0x05, | 53 0x05, |
54 0x06, | 54 0x06, |
55 0x07, | 55 0x07, |
56 0x08, | 56 0x08, |
57 0x09, | 57 0x09, |
58 0x0a, | 58 0x0a, |
59 0x0b, | 59 0x0b, |
60 0x0c, | 60 0x0c, |
61 0x0d, | 61 0x0d, |
62 0x0e, | 62 0x0e, |
63 0x0f}; | 63 0x0f}; |
64 UUID uuid; | 64 UUID uuid; |
65 uuid.InitializeFromBytes(kBytes); | 65 uuid.InitializeFromBytes(kBytes); |
66 EXPECT_EQ(0x00010203u, uuid.data_1); | 66 EXPECT_EQ(uuid.data_1, 0x00010203u); |
67 EXPECT_EQ(0x0405u, uuid.data_2); | 67 EXPECT_EQ(uuid.data_2, 0x0405u); |
68 EXPECT_EQ(0x0607u, uuid.data_3); | 68 EXPECT_EQ(uuid.data_3, 0x0607u); |
69 EXPECT_EQ(0x08u, uuid.data_4[0]); | 69 EXPECT_EQ(uuid.data_4[0], 0x08u); |
70 EXPECT_EQ(0x09u, uuid.data_4[1]); | 70 EXPECT_EQ(uuid.data_4[1], 0x09u); |
71 EXPECT_EQ(0x0au, uuid.data_5[0]); | 71 EXPECT_EQ(uuid.data_5[0], 0x0au); |
72 EXPECT_EQ(0x0bu, uuid.data_5[1]); | 72 EXPECT_EQ(uuid.data_5[1], 0x0bu); |
73 EXPECT_EQ(0x0cu, uuid.data_5[2]); | 73 EXPECT_EQ(uuid.data_5[2], 0x0cu); |
74 EXPECT_EQ(0x0du, uuid.data_5[3]); | 74 EXPECT_EQ(uuid.data_5[3], 0x0du); |
75 EXPECT_EQ(0x0eu, uuid.data_5[4]); | 75 EXPECT_EQ(uuid.data_5[4], 0x0eu); |
76 EXPECT_EQ(0x0fu, uuid.data_5[5]); | 76 EXPECT_EQ(uuid.data_5[5], 0x0fu); |
77 EXPECT_EQ("00010203-0405-0607-0809-0a0b0c0d0e0f", uuid.ToString()); | 77 EXPECT_EQ(uuid.ToString(), "00010203-0405-0607-0809-0a0b0c0d0e0f"); |
78 | 78 |
79 // Test both operator== and operator!=. | 79 // Test both operator== and operator!=. |
80 EXPECT_FALSE(uuid == uuid_zero); | 80 EXPECT_FALSE(uuid == uuid_zero); |
81 EXPECT_NE(uuid, uuid_zero); | 81 EXPECT_NE(uuid, uuid_zero); |
82 | 82 |
83 UUID uuid_2; | 83 UUID uuid_2; |
84 uuid_2.InitializeFromBytes(kBytes); | 84 uuid_2.InitializeFromBytes(kBytes); |
85 EXPECT_EQ(uuid, uuid_2); | 85 EXPECT_EQ(uuid_2, uuid); |
86 EXPECT_FALSE(uuid != uuid_2); | 86 EXPECT_FALSE(uuid != uuid_2); |
87 | 87 |
88 // Make sure that operator== and operator!= check the entire UUID. | 88 // Make sure that operator== and operator!= check the entire UUID. |
89 ++uuid.data_1; | 89 ++uuid.data_1; |
90 EXPECT_NE(uuid, uuid_2); | 90 EXPECT_NE(uuid, uuid_2); |
91 --uuid.data_1; | 91 --uuid.data_1; |
92 ++uuid.data_2; | 92 ++uuid.data_2; |
93 EXPECT_NE(uuid, uuid_2); | 93 EXPECT_NE(uuid, uuid_2); |
94 --uuid.data_2; | 94 --uuid.data_2; |
95 ++uuid.data_3; | 95 ++uuid.data_3; |
96 EXPECT_NE(uuid, uuid_2); | 96 EXPECT_NE(uuid, uuid_2); |
97 --uuid.data_3; | 97 --uuid.data_3; |
98 for (size_t index = 0; index < arraysize(uuid.data_4); ++index) { | 98 for (size_t index = 0; index < arraysize(uuid.data_4); ++index) { |
99 ++uuid.data_4[index]; | 99 ++uuid.data_4[index]; |
100 EXPECT_NE(uuid, uuid_2); | 100 EXPECT_NE(uuid, uuid_2); |
101 --uuid.data_4[index]; | 101 --uuid.data_4[index]; |
102 } | 102 } |
103 for (size_t index = 0; index < arraysize(uuid.data_5); ++index) { | 103 for (size_t index = 0; index < arraysize(uuid.data_5); ++index) { |
104 ++uuid.data_5[index]; | 104 ++uuid.data_5[index]; |
105 EXPECT_NE(uuid, uuid_2); | 105 EXPECT_NE(uuid, uuid_2); |
106 --uuid.data_5[index]; | 106 --uuid.data_5[index]; |
107 } | 107 } |
108 | 108 |
109 // Make sure that the UUIDs are equal again, otherwise the test above may not | 109 // Make sure that the UUIDs are equal again, otherwise the test above may not |
110 // have been valid. | 110 // have been valid. |
111 EXPECT_EQ(uuid, uuid_2); | 111 EXPECT_EQ(uuid_2, uuid); |
112 | 112 |
113 const uint8_t kMoreBytes[16] = {0xff, | 113 const uint8_t kMoreBytes[16] = {0xff, |
114 0xee, | 114 0xee, |
115 0xdd, | 115 0xdd, |
116 0xcc, | 116 0xcc, |
117 0xbb, | 117 0xbb, |
118 0xaa, | 118 0xaa, |
119 0x99, | 119 0x99, |
120 0x88, | 120 0x88, |
121 0x77, | 121 0x77, |
122 0x66, | 122 0x66, |
123 0x55, | 123 0x55, |
124 0x44, | 124 0x44, |
125 0x33, | 125 0x33, |
126 0x22, | 126 0x22, |
127 0x11, | 127 0x11, |
128 0x00}; | 128 0x00}; |
129 uuid.InitializeFromBytes(kMoreBytes); | 129 uuid.InitializeFromBytes(kMoreBytes); |
130 EXPECT_EQ(0xffeeddccu, uuid.data_1); | 130 EXPECT_EQ(uuid.data_1, 0xffeeddccu); |
131 EXPECT_EQ(0xbbaau, uuid.data_2); | 131 EXPECT_EQ(uuid.data_2, 0xbbaau); |
132 EXPECT_EQ(0x9988u, uuid.data_3); | 132 EXPECT_EQ(uuid.data_3, 0x9988u); |
133 EXPECT_EQ(0x77u, uuid.data_4[0]); | 133 EXPECT_EQ(uuid.data_4[0], 0x77u); |
134 EXPECT_EQ(0x66u, uuid.data_4[1]); | 134 EXPECT_EQ(uuid.data_4[1], 0x66u); |
135 EXPECT_EQ(0x55u, uuid.data_5[0]); | 135 EXPECT_EQ(uuid.data_5[0], 0x55u); |
136 EXPECT_EQ(0x44u, uuid.data_5[1]); | 136 EXPECT_EQ(uuid.data_5[1], 0x44u); |
137 EXPECT_EQ(0x33u, uuid.data_5[2]); | 137 EXPECT_EQ(uuid.data_5[2], 0x33u); |
138 EXPECT_EQ(0x22u, uuid.data_5[3]); | 138 EXPECT_EQ(uuid.data_5[3], 0x22u); |
139 EXPECT_EQ(0x11u, uuid.data_5[4]); | 139 EXPECT_EQ(uuid.data_5[4], 0x11u); |
140 EXPECT_EQ(0x00u, uuid.data_5[5]); | 140 EXPECT_EQ(uuid.data_5[5], 0x00u); |
141 EXPECT_EQ("ffeeddcc-bbaa-9988-7766-554433221100", uuid.ToString()); | 141 EXPECT_EQ(uuid.ToString(), "ffeeddcc-bbaa-9988-7766-554433221100"); |
142 | 142 |
143 EXPECT_NE(uuid, uuid_2); | 143 EXPECT_NE(uuid, uuid_2); |
144 EXPECT_NE(uuid, uuid_zero); | 144 EXPECT_NE(uuid, uuid_zero); |
145 | 145 |
146 // Test that UUID is standard layout. | 146 // Test that UUID is standard layout. |
147 memset(&uuid, 0x45, 16); | 147 memset(&uuid, 0x45, 16); |
148 EXPECT_EQ(0x45454545u, uuid.data_1); | 148 EXPECT_EQ(uuid.data_1, 0x45454545u); |
149 EXPECT_EQ(0x4545u, uuid.data_2); | 149 EXPECT_EQ(uuid.data_2, 0x4545u); |
150 EXPECT_EQ(0x4545u, uuid.data_3); | 150 EXPECT_EQ(uuid.data_3, 0x4545u); |
151 EXPECT_EQ(0x45u, uuid.data_4[0]); | 151 EXPECT_EQ(uuid.data_4[0], 0x45u); |
152 EXPECT_EQ(0x45u, uuid.data_4[1]); | 152 EXPECT_EQ(uuid.data_4[1], 0x45u); |
153 EXPECT_EQ(0x45u, uuid.data_5[0]); | 153 EXPECT_EQ(uuid.data_5[0], 0x45u); |
154 EXPECT_EQ(0x45u, uuid.data_5[1]); | 154 EXPECT_EQ(uuid.data_5[1], 0x45u); |
155 EXPECT_EQ(0x45u, uuid.data_5[2]); | 155 EXPECT_EQ(uuid.data_5[2], 0x45u); |
156 EXPECT_EQ(0x45u, uuid.data_5[3]); | 156 EXPECT_EQ(uuid.data_5[3], 0x45u); |
157 EXPECT_EQ(0x45u, uuid.data_5[4]); | 157 EXPECT_EQ(uuid.data_5[4], 0x45u); |
158 EXPECT_EQ(0x45u, uuid.data_5[5]); | 158 EXPECT_EQ(uuid.data_5[5], 0x45u); |
159 EXPECT_EQ("45454545-4545-4545-4545-454545454545", uuid.ToString()); | 159 EXPECT_EQ(uuid.ToString(), "45454545-4545-4545-4545-454545454545"); |
160 | 160 |
161 UUID initialized_generated; | 161 UUID initialized_generated; |
162 initialized_generated.InitializeWithNew(); | 162 initialized_generated.InitializeWithNew(); |
163 EXPECT_NE(initialized_generated, uuid_zero); | 163 EXPECT_NE(initialized_generated, uuid_zero); |
164 } | 164 } |
165 | 165 |
166 TEST(UUID, FromString) { | 166 TEST(UUID, FromString) { |
167 const struct TestCase { | 167 const struct TestCase { |
168 const char* uuid_string; | 168 const char* uuid_string; |
169 bool success; | 169 bool success; |
(...skipping 20 matching lines...) Expand all Loading... |
190 uuid_zero.InitializeToZero(); | 190 uuid_zero.InitializeToZero(); |
191 const std::string empty_uuid = uuid_zero.ToString(); | 191 const std::string empty_uuid = uuid_zero.ToString(); |
192 | 192 |
193 for (size_t index = 0; index < arraysize(kCases); ++index) { | 193 for (size_t index = 0; index < arraysize(kCases); ++index) { |
194 const TestCase& test_case = kCases[index]; | 194 const TestCase& test_case = kCases[index]; |
195 SCOPED_TRACE(base::StringPrintf( | 195 SCOPED_TRACE(base::StringPrintf( |
196 "index %" PRIuS ": %s", index, test_case.uuid_string)); | 196 "index %" PRIuS ": %s", index, test_case.uuid_string)); |
197 | 197 |
198 UUID uuid; | 198 UUID uuid; |
199 uuid.InitializeToZero(); | 199 uuid.InitializeToZero(); |
200 EXPECT_EQ(test_case.success, | 200 EXPECT_EQ(uuid.InitializeFromString(test_case.uuid_string), |
201 uuid.InitializeFromString(test_case.uuid_string)); | 201 test_case.success); |
202 if (test_case.success) { | 202 if (test_case.success) { |
203 EXPECT_EQ(test_case.uuid_string, uuid.ToString()); | 203 EXPECT_EQ(uuid.ToString(), test_case.uuid_string); |
204 } else { | 204 } else { |
205 EXPECT_EQ(empty_uuid, uuid.ToString()); | 205 EXPECT_EQ(uuid.ToString(), empty_uuid); |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 // Test for case insensitivty. | 209 // Test for case insensitivty. |
210 UUID uuid; | 210 UUID uuid; |
211 uuid.InitializeFromString("F32E5BDC-2681-4C73-A4E6-911FFD89B846"); | 211 uuid.InitializeFromString("F32E5BDC-2681-4C73-A4E6-911FFD89B846"); |
212 EXPECT_EQ("f32e5bdc-2681-4c73-a4e6-911ffd89b846", uuid.ToString()); | 212 EXPECT_EQ(uuid.ToString(), "f32e5bdc-2681-4c73-a4e6-911ffd89b846"); |
213 | 213 |
214 // Mixed case. | 214 // Mixed case. |
215 uuid.InitializeFromString("5762C15D-50b5-4171-a2e9-7429C9EC6CAB"); | 215 uuid.InitializeFromString("5762C15D-50b5-4171-a2e9-7429C9EC6CAB"); |
216 EXPECT_EQ("5762c15d-50b5-4171-a2e9-7429c9ec6cab", uuid.ToString()); | 216 EXPECT_EQ(uuid.ToString(), "5762c15d-50b5-4171-a2e9-7429c9ec6cab"); |
217 } | 217 } |
218 | 218 |
219 #if defined(OS_WIN) | 219 #if defined(OS_WIN) |
220 | 220 |
221 TEST(UUID, FromSystem) { | 221 TEST(UUID, FromSystem) { |
222 ::GUID system_uuid; | 222 ::GUID system_uuid; |
223 ASSERT_EQ(RPC_S_OK, UuidCreate(&system_uuid)); | 223 ASSERT_EQ(UuidCreate(&system_uuid), RPC_S_OK); |
224 | 224 |
225 UUID uuid; | 225 UUID uuid; |
226 uuid.InitializeFromSystemUUID(&system_uuid); | 226 uuid.InitializeFromSystemUUID(&system_uuid); |
227 | 227 |
228 RPC_WSTR system_string; | 228 RPC_WSTR system_string; |
229 ASSERT_EQ(RPC_S_OK, UuidToString(&system_uuid, &system_string)); | 229 ASSERT_EQ(UuidToString(&system_uuid, &system_string), RPC_S_OK); |
230 | 230 |
231 struct ScopedRpcStringFreeTraits { | 231 struct ScopedRpcStringFreeTraits { |
232 static RPC_WSTR* InvalidValue() { return nullptr; } | 232 static RPC_WSTR* InvalidValue() { return nullptr; } |
233 static void Free(RPC_WSTR* rpc_string) { | 233 static void Free(RPC_WSTR* rpc_string) { |
234 EXPECT_EQ(RPC_S_OK, RpcStringFree(rpc_string)); | 234 EXPECT_EQ(RpcStringFree(rpc_string), RPC_S_OK); |
235 } | 235 } |
236 }; | 236 }; |
237 using ScopedRpcString = | 237 using ScopedRpcString = |
238 base::ScopedGeneric<RPC_WSTR*, ScopedRpcStringFreeTraits>; | 238 base::ScopedGeneric<RPC_WSTR*, ScopedRpcStringFreeTraits>; |
239 ScopedRpcString scoped_system_string(&system_string); | 239 ScopedRpcString scoped_system_string(&system_string); |
240 | 240 |
241 EXPECT_EQ(reinterpret_cast<wchar_t*>(system_string), uuid.ToString16()); | 241 EXPECT_EQ(uuid.ToString16(), reinterpret_cast<wchar_t*>(system_string)); |
242 } | 242 } |
243 | 243 |
244 #endif // OS_WIN | 244 #endif // OS_WIN |
245 | 245 |
246 } // namespace | 246 } // namespace |
247 } // namespace test | 247 } // namespace test |
248 } // namespace crashpad | 248 } // namespace crashpad |
OLD | NEW |