1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4173528 5068772 8148936
  26  * @summary Unit tests for java.util.UUID
  27  * @key randomness
  28  * @run main/othervm -XX:+CompactStrings UUIDTest
  29  * @run main/othervm -XX:-CompactStrings UUIDTest
  30  */
  31 
  32 import java.util.*;
  33 
  34 public class UUIDTest {
  35 
  36     static Random generator = new Random();
  37 
  38     public static void main(String[] args) throws Exception {
  39         containsTest();
  40         randomUUIDTest();
  41         nameUUIDFromBytesTest();
  42         stringTest();
  43         versionTest();
  44         variantTest();
  45         timestampTest();
  46         clockSequenceTest();
  47         nodeTest();
  48         hashCodeEqualsTest();
  49         compareTo();
  50     }
  51 
  52     // Verify that list.contains detects UUID collisons
  53     private static void containsTest() throws Exception {
  54         List list = new LinkedList();
  55         list.add(new UUID(4,4));
  56         if (!list.contains(new UUID(4,4)))
  57             throw new Exception("contains test did not work as expected");
  58     }
  59 
  60     private static void randomUUIDTest() throws Exception {
  61         List list = new LinkedList();
  62         for (int i=0; i<100; i++) {
  63             UUID u1 = UUID.randomUUID();
  64             if (4 != u1.version()) {
  65                 throw new Exception("bad version");
  66             }
  67             if (2 != u1.variant()) {
  68                 throw new Exception("bad variant");
  69             }
  70             if (list.contains(u1))
  71                 throw new Exception("random UUID collision very unlikely");
  72             list.add(u1);
  73         }
  74     }
  75 
  76     private static void nameUUIDFromBytesTest() throws Exception {
  77         Random byteSource = new Random();
  78         byte[] someBytes = new byte[12];
  79         List list = new LinkedList();
  80         for (int i=0; i<100; i++) {
  81             byteSource.nextBytes(someBytes);
  82             UUID u1 = UUID.nameUUIDFromBytes(someBytes);
  83             if (3 != u1.version()) {
  84                 throw new Exception("bad version");
  85             }
  86             if (2 != u1.variant()) {
  87                 throw new Exception("bad variant");
  88             }
  89             if (list.contains(u1))
  90                 throw new Exception("byte UUID collision very unlikely");
  91             list.add(u1);
  92         }
  93     }
  94 
  95     private static void stringTest() throws Exception {
  96         for (int i=0; i<100; i++) {
  97             UUID u1 = UUID.randomUUID();
  98             UUID u2 = UUID.fromString(u1.toString());
  99             if (!u1.equals(u2))
 100                 throw new Exception("UUID -> string -> UUID failed");
 101         }
 102 
 103         testFromStringError("-0");
 104         testFromStringError("x");
 105         testFromStringError("----");
 106         testFromStringError("-0-0-0-0");
 107         testFromStringError("0-0-0-0-");
 108         testFromStringError("0-0-0-0-0-");
 109         testFromStringError("0-0-0-0-x");
 110         testFromStringError("123456789-1234-1234-1234-123456789012");
 111         testFromStringError("123456789-2-3-4-5");
 112         testFromStringError("1-12345-3-4-5");
 113         testFromStringError("1-2-12345-4-5");
 114         testFromStringError("1-2-3-12345-5");
 115         testFromStringError("1-2-3-4-1234567890123");
 116     }
 117 
 118     private static void testFromStringError(String str) {
 119         try {
 120             UUID test = UUID.fromString(str);
 121             throw new RuntimeException("Should have thrown IAE");
 122         } catch (IllegalArgumentException iae) {
 123             // pass
 124         }
 125     }
 126 
 127     private static void versionTest() throws Exception {
 128         UUID test = UUID.randomUUID();
 129         if (test.version() != 4)
 130             throw new Exception("randomUUID not type 4");
 131         Random byteSource = new Random();
 132         byte[] someBytes = new byte[12];
 133         byteSource.nextBytes(someBytes);
 134         test = UUID.nameUUIDFromBytes(someBytes);
 135         if (test.version() != 3)
 136             throw new Exception("nameUUIDFromBytes not type 3");
 137         test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda");
 138         if (test.version() != 1)
 139             throw new Exception("wrong version fromString 1");
 140         test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda");
 141         if (test.version() != 2)
 142             throw new Exception("wrong version fromString 2");
 143         test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda");
 144         if (test.version() != 3)
 145             throw new Exception("wrong version fromString 3");
 146         test = UUID.fromString("9835451d-e2e0-4e41-8a5a-be785f17dcda");
 147         if (test.version() != 4)
 148             throw new Exception("wrong version fromString 4");
 149         test = new UUID(0x0000000000001000L, 55L);
 150         if (test.version() != 1)
 151             throw new Exception("wrong version from bit set to 1");
 152         test = new UUID(0x0000000000002000L, 55L);
 153         if (test.version() != 2)
 154             throw new Exception("wrong version from bit set to 2");
 155         test = new UUID(0x0000000000003000L, 55L);
 156         if (test.version() != 3)
 157             throw new Exception("wrong version from bit set to 3");
 158         test = new UUID(0x0000000000004000L, 55L);
 159         if (test.version() != 4)
 160             throw new Exception("wrong version from bit set to 4");
 161     }
 162 
 163     private static void variantTest() throws Exception {
 164         UUID test = UUID.randomUUID();
 165         if (test.variant() != 2)
 166             throw new Exception("randomUUID not variant 2");
 167         Random byteSource = new Random();
 168         byte[] someBytes = new byte[12];
 169         byteSource.nextBytes(someBytes);
 170         test = UUID.nameUUIDFromBytes(someBytes);
 171         if (test.variant() != 2)
 172             throw new Exception("nameUUIDFromBytes not variant 2");
 173         test = new UUID(55L, 0x0000000000001000L);
 174         if (test.variant() != 0)
 175             throw new Exception("wrong variant from bit set to 0");
 176         test = new UUID(55L, 0x8000000000001000L);
 177         if (test.variant() != 2)
 178             throw new Exception("wrong variant from bit set to 2");
 179        test = new UUID(55L, 0xc000000000001000L);
 180         if (test.variant() != 6)
 181             throw new Exception("wrong variant from bit set to 6");
 182        test = new UUID(55L, 0xe000000000001000L);
 183         if (test.variant() != 7)
 184             throw new Exception("wrong variant from bit set to 7");
 185     }
 186 
 187     private static void timestampTest() throws Exception {
 188         UUID test = UUID.randomUUID();
 189         try {
 190             test.timestamp();
 191             throw new Exception("Expected exception not thrown");
 192         } catch (UnsupportedOperationException uoe) {
 193             // Correct result
 194         }
 195         test = UUID.fromString("00000001-0000-1000-8a5a-be785f17dcda");
 196         if (test.timestamp() != 1)
 197             throw new Exception("Incorrect timestamp");
 198         test = UUID.fromString("00000400-0000-1000-8a5a-be785f17dcda");
 199         if (test.timestamp() != 1024)
 200             throw new Exception("Incorrect timestamp");
 201         test = UUID.fromString("FFFFFFFF-FFFF-1FFF-8a5a-be785f17dcda");
 202         if (test.timestamp() != Long.MAX_VALUE>>3)
 203             throw new Exception("Incorrect timestamp");
 204     }
 205 
 206     private static void clockSequenceTest() throws Exception {
 207         UUID test = UUID.randomUUID();
 208         try {
 209             test.clockSequence();
 210             throw new Exception("Expected exception not thrown");
 211         } catch (UnsupportedOperationException uoe) {
 212             // Correct result
 213         }
 214         test = UUID.fromString("00000001-0000-1000-8001-be785f17dcda");
 215         if (test.clockSequence() != 1)
 216             throw new Exception("Incorrect sequence");
 217         test = UUID.fromString("00000001-0000-1000-8002-be785f17dcda");
 218         if (test.clockSequence() != 2)
 219             throw new Exception("Incorrect sequence");
 220         test = UUID.fromString("00000001-0000-1000-8010-be785f17dcda");
 221         if (test.clockSequence() != 16)
 222             throw new Exception("Incorrect sequence");
 223         test = UUID.fromString("00000001-0000-1000-bFFF-be785f17dcda");
 224         if (test.clockSequence() != ((2L<<13)-1)) // 2^14 - 1
 225             throw new Exception("Incorrect sequence");
 226     }
 227 
 228     private static void nodeTest() throws Exception {
 229         UUID test = UUID.randomUUID();
 230         try {
 231             test.node();
 232             throw new Exception("Expected exception not thrown");
 233         } catch (UnsupportedOperationException uoe) {
 234             // Correct result
 235         }
 236         test = UUID.fromString("00000001-0000-1000-8001-000000000001");
 237         if (test.node() != 1)
 238             throw new Exception("Incorrect node");
 239         test = UUID.fromString("00000001-0000-1000-8002-FFFFFFFFFFFF");
 240         if (test.node() != ((2L<<47)-1)) // 2^48 - 1
 241             throw new Exception("Incorrect node");
 242     }
 243 
 244     private static void hashCodeEqualsTest() throws Exception {
 245         // If two UUIDs are equal they must have the same hashCode
 246         for (int i=0; i<100; i++) {
 247             UUID u1 = UUID.randomUUID();
 248             UUID u2 = UUID.fromString(u1.toString());
 249             if (u1.hashCode() != u2.hashCode())
 250                 throw new Exception("Equal UUIDs with different hashcodes");
 251         }
 252         // Test equality of UUIDs with tampered bits
 253         for (int i=0; i<1000; i++) {
 254             long l = generator.nextLong();
 255             long l2 = generator.nextLong();
 256             int position = generator.nextInt(64);
 257             UUID u1 = new UUID(l, l2);
 258             l = l ^ (1L << position);
 259             UUID u2 = new UUID(l, l2);
 260             if (u1.equals(u2))
 261                 throw new Exception("UUIDs with different bits equal");
 262         }
 263     }
 264 
 265     private static void compareTo() throws Exception {
 266         UUID id = new UUID(33L, 63L);
 267         UUID id2 = new UUID(34L, 62L);
 268         UUID id3 = new UUID(34L, 63L);
 269         UUID id4 = new UUID(34L, 64L);
 270         UUID id5 = new UUID(35L, 63L);
 271 
 272         if ((id.compareTo(id2) >= 0) ||
 273             (id2.compareTo(id3) >= 0) ||
 274             (id3.compareTo(id4) >= 0) ||
 275             (id4.compareTo(id5) >= 0))
 276             throw new RuntimeException("compareTo failure");
 277 
 278         if ((id5.compareTo(id4) <= 0) ||
 279             (id4.compareTo(id3) <= 0) ||
 280             (id3.compareTo(id2) <= 0) ||
 281             (id2.compareTo(id) <= 0))
 282             throw new RuntimeException("compareTo failure");
 283 
 284         if (id.compareTo(id) != 0)
 285             throw new RuntimeException("compareTo failure");
 286 
 287     }
 288 
 289 }