1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4173528 5068772 8148936 8196334 8308803
* @summary Unit tests for java.util.UUID
* @key randomness
* @library /test/lib
* @build jdk.test.lib.RandomFactory
* @run main/othervm -Xmx1g -XX:+CompactStrings UUIDTest
* @run main/othervm -Xmx1g -XX:-CompactStrings UUIDTest
*/
import java.util.*;
import java.util.stream.IntStream;
import jdk.test.lib.RandomFactory;
public class UUIDTest {
// Single UUID instance is ~32 bytes, 1M instances take ~256M in the set
private static final int COUNT = 1_000_000;
static final Random generator = RandomFactory.getRandom();
public static void main(String[] args) throws Exception {
negativeTest();
randomUUIDTest();
randomUUIDTest_Multi();
nameUUIDFromBytesTest();
stringTest();
versionTest();
variantTest();
timestampTest();
clockSequenceTest();
nodeTest();
hashCodeEqualsTest();
compareTo();
}
private static void negativeTest() throws Exception {
Set<UUID> set = new HashSet<>();
set.add(new UUID(4, 4));
if (set.add(new UUID(4, 4))) {
throw new Exception("Contains test does not work as expected");
}
}
private static void randomUUIDTest() throws Exception {
List<UUID> collisions = new ArrayList<>();
Set<UUID> set = new HashSet<>();
for (int i = 0; i < COUNT; i++) {
UUID u = UUID.randomUUID();
if (u.version() != 4) {
throw new Exception("Bad version: " + u);
}
if (u.variant() != 2) {
throw new Exception("Bad variant: " + u);
}
if (!set.add(u)) {
collisions.add(u);
}
}
if (!collisions.isEmpty()) {
// This is extremely unlikely to happen. If you see this failure,
// this highly likely points to the implementation bug, rather than
// the odd chance.
throw new Exception("UUID collisions detected: " + collisions);
}
}
private static void randomUUIDTest_Multi() throws Exception {
List<UUID> uuids = IntStream.range(0, COUNT).parallel()
.mapToObj(i -> UUID.randomUUID())
.toList();
List<UUID> collisions = new ArrayList<>();
Set<UUID> set = new HashSet<>();
for (UUID u : uuids) {
if (u.version() != 4) {
throw new Exception("Bad version: " + u);
}
if (u.variant() != 2) {
throw new Exception("Bad variant: " + u);
}
if (!set.add(u)) {
collisions.add(u);
}
}
if (!collisions.isEmpty()) {
// This is extremely unlikely to happen. If you see this failure,
// this highly likely points to the implementation bug, rather than
// the odd chance.
throw new Exception("UUID collisions detected: " + collisions);
}
}
private static void nameUUIDFromBytesTest() throws Exception {
List<UUID> collisions = new ArrayList<>();
byte[] someBytes = new byte[12];
Set<UUID> set = new HashSet<>();
for (int i = 0; i < COUNT; i++) {
generator.nextBytes(someBytes);
UUID u = UUID.nameUUIDFromBytes(someBytes);
if (u.version() != 3) {
throw new Exception("Bad version: " + u);
}
if (u.variant() != 2) {
throw new Exception("Bad variant: " + u);
}
if (!set.add(u)) {
collisions.add(u);
}
}
if (!collisions.isEmpty()) {
// This is extremely unlikely to happen. If you see this failure,
// this highly likely points to the implementation bug, rather than
// the odd chance.
throw new Exception("UUID collisions detected: " + collisions);
}
}
private static void stringTest() throws Exception {
for (int i = 0; i < COUNT; i++) {
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.fromString(u1.toString().toLowerCase());
UUID u3 = UUID.fromString(u1.toString().toUpperCase());
if (!u1.equals(u2) || !u1.equals(u3)) {
throw new Exception("UUID -> string -> UUID failed: " + u1 + " -> " + u2 + " -> " + u3);
}
}
testFromStringError("-0");
testFromStringError("x");
testFromStringError("----");
testFromStringError("-0-0-0-0");
testFromStringError("0-0-0-0-");
testFromStringError("0-0-0-0-0-");
testFromStringError("0-0-0-0-x");
}
private static void testFromStringError(String str) {
try {
UUID test = UUID.fromString(str);
throw new RuntimeException("Should have thrown IAE");
} catch (IllegalArgumentException iae) {
// pass
}
}
private static void versionTest() throws Exception {
UUID test = UUID.randomUUID();
if (test.version() != 4) {
throw new Exception("randomUUID not type 4: " + test);
}
byte[] someBytes = new byte[12];
generator.nextBytes(someBytes);
test = UUID.nameUUIDFromBytes(someBytes);
if (test.version() != 3) {
throw new Exception("nameUUIDFromBytes not type 3: " + test);
}
test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda");
if (test.version() != 1) {
throw new Exception("wrong version fromString 1");
}
test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda");
if (test.version() != 2) {
throw new Exception("wrong version fromString 2");
}
test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda");
if (test.version() != 3) {
throw new Exception("wrong version fromString 3");
}
test = UUID.fromString("9835451d-e2e0-4e41-8a5a-be785f17dcda");
if (test.version() != 4) {
throw new Exception("wrong version fromString 4");
}
test = new UUID(0x0000000000001000L, 55L);
if (test.version() != 1) {
throw new Exception("wrong version from bit set to 1");
}
test = new UUID(0x0000000000002000L, 55L);
if (test.version() != 2) {
throw new Exception("wrong version from bit set to 2");
}
test = new UUID(0x0000000000003000L, 55L);
if (test.version() != 3) {
throw new Exception("wrong version from bit set to 3");
}
test = new UUID(0x0000000000004000L, 55L);
if (test.version() != 4) {
throw new Exception("wrong version from bit set to 4");
}
}
private static void variantTest() throws Exception {
UUID test = UUID.randomUUID();
if (test.variant() != 2) {
throw new Exception("randomUUID not variant 2");
}
byte[] someBytes = new byte[12];
generator.nextBytes(someBytes);
test = UUID.nameUUIDFromBytes(someBytes);
if (test.variant() != 2) {
throw new Exception("nameUUIDFromBytes not variant 2");
}
test = new UUID(55L, 0x0000000000001000L);
if (test.variant() != 0) {
throw new Exception("wrong variant from bit set to 0");
}
test = new UUID(55L, 0x8000000000001000L);
if (test.variant() != 2) {
throw new Exception("wrong variant from bit set to 2");
}
test = new UUID(55L, 0xc000000000001000L);
if (test.variant() != 6) {
throw new Exception("wrong variant from bit set to 6");
}
test = new UUID(55L, 0xe000000000001000L);
if (test.variant() != 7) {
throw new Exception("wrong variant from bit set to 7");
}
}
private static void timestampTest() throws Exception {
UUID test = UUID.randomUUID();
try {
test.timestamp();
throw new Exception("Expected exception not thrown");
} catch (UnsupportedOperationException uoe) {
// Correct result
}
test = UUID.fromString("00000001-0000-1000-8a5a-be785f17dcda");
if (test.timestamp() != 1) {
throw new Exception("Incorrect timestamp");
}
test = UUID.fromString("00000400-0000-1000-8a5a-be785f17dcda");
if (test.timestamp() != 1024) {
throw new Exception("Incorrect timestamp");
}
test = UUID.fromString("FFFFFFFF-FFFF-1FFF-8a5a-be785f17dcda");
if (test.timestamp() != (Long.MAX_VALUE >> 3)) {
throw new Exception("Incorrect timestamp");
}
}
private static void clockSequenceTest() throws Exception {
UUID test = UUID.randomUUID();
try {
test.clockSequence();
throw new Exception("Expected exception not thrown");
} catch (UnsupportedOperationException uoe) {
// Correct result
}
test = UUID.fromString("00000001-0000-1000-8001-be785f17dcda");
if (test.clockSequence() != 1) {
throw new Exception("Incorrect sequence");
}
test = UUID.fromString("00000001-0000-1000-8002-be785f17dcda");
if (test.clockSequence() != 2) {
throw new Exception("Incorrect sequence");
}
test = UUID.fromString("00000001-0000-1000-8010-be785f17dcda");
if (test.clockSequence() != 16) {
throw new Exception("Incorrect sequence");
}
test = UUID.fromString("00000001-0000-1000-bFFF-be785f17dcda");
if (test.clockSequence() != ((1L << 14) - 1)) {
throw new Exception("Incorrect sequence");
}
}
private static void nodeTest() throws Exception {
UUID test = UUID.randomUUID();
try {
test.node();
throw new Exception("Expected exception not thrown");
} catch (UnsupportedOperationException uoe) {
// Correct result
}
test = UUID.fromString("00000001-0000-1000-8001-000000000001");
if (test.node() != 1) {
throw new Exception("Incorrect node");
}
test = UUID.fromString("00000001-0000-1000-8002-FFFFFFFFFFFF");
if (test.node() != ((1L << 48) - 1)) {
throw new Exception("Incorrect node");
}
}
private static void hashCodeEqualsTest() throws Exception {
// If two UUIDs are equal they must have the same hashCode
for (int i = 0; i < COUNT; i++) {
UUID u1 = UUID.randomUUID();
UUID u2 = UUID.fromString(u1.toString());
if (u1.hashCode() != u2.hashCode()) {
throw new Exception("Equal UUIDs with different hash codes: " + u1 + "(" + u1.hashCode() + ") " +
"and " + u2 + "(" + u2.hashCode() + ")");
}
}
// Test equality of UUIDs with tampered bits
for (int i = 0; i < COUNT; i++) {
long l = generator.nextLong();
long l2 = generator.nextLong();
int position = generator.nextInt(64);
UUID u1 = new UUID(l, l2);
l = l ^ (1L << position);
UUID u2 = new UUID(l, l2);
if (u1.equals(u2)) {
throw new Exception("UUIDs with different bits equal: " + u1 + " and " + u2);
}
}
}
private static void compareTo() throws Exception {
UUID id = new UUID(33L, 63L);
UUID id2 = new UUID(34L, 62L);
UUID id3 = new UUID(34L, 63L);
UUID id4 = new UUID(34L, 64L);
UUID id5 = new UUID(35L, 63L);
if ((id.compareTo(id2) >= 0) ||
(id2.compareTo(id3) >= 0) ||
(id3.compareTo(id4) >= 0) ||
(id4.compareTo(id5) >= 0)) {
throw new RuntimeException("compareTo failure");
}
if ((id5.compareTo(id4) <= 0) ||
(id4.compareTo(id3) <= 0) ||
(id3.compareTo(id2) <= 0) ||
(id2.compareTo(id) <= 0)) {
throw new RuntimeException("compareTo failure");
}
if (id.compareTo(id) != 0) {
throw new RuntimeException("compareTo failure");
}
}
}
|