1 /* 2 * Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24 /* @test 25 * @bug 4173528 5068772 26 * @compile -source 1.4 -target 1.4 UUIDTest.java 27 * @run main UUIDTest 28 * @summary Unit tests for java.util.UUID 29 */ 30 31 import java.util.*; 32 33 public class UUIDTest { 34 35 static Random generator = new Random(); 36 37 public static void main(String[] args) throws Exception { 38 containsTest(); 39 randomUUIDTest(); 40 nameUUIDFromBytesTest(); 41 stringTest(); 42 versionTest(); 43 variantTest(); 44 timestampTest(); 45 clockSequenceTest(); 46 nodeTest(); 47 hashCodeEqualsTest(); 48 compareTo(); 49 } 50 51 // Verify that list.contains detects UUID collisons 52 private static void containsTest() throws Exception { 53 List list = new LinkedList(); 54 list.add(new UUID(4,4)); 55 if (!list.contains(new UUID(4,4))) 56 throw new Exception("contains test did not work as expected"); 57 } 58 59 private static void randomUUIDTest() throws Exception { 60 List list = new LinkedList(); 61 for (int i=0; i<100; i++) { 62 UUID u1 = UUID.randomUUID(); 63 if (list.contains(u1)) 64 throw new Exception("random UUID collision very unlikely"); 65 list.add(u1); 66 } 67 } 68 69 private static void nameUUIDFromBytesTest() throws Exception { 70 Random byteSource = new Random(); 71 byte[] someBytes = new byte[12]; 72 List list = new LinkedList(); 73 for (int i=0; i<100; i++) { 74 byteSource.nextBytes(someBytes); 75 UUID test = UUID.nameUUIDFromBytes(someBytes); 76 if (list.contains(test)) 77 throw new Exception("byte UUID collision very unlikely"); 78 list.add(test); 79 } 80 } 81 82 private static void stringTest() throws Exception { 83 for (int i=0; i<100; i++) { 84 UUID u1 = UUID.randomUUID(); 85 UUID u2 = UUID.fromString(u1.toString()); 86 if (!u1.equals(u2)) 87 throw new Exception("UUID -> string -> UUID failed"); 88 } 89 } 90 91 private static void versionTest() throws Exception { 92 UUID test = UUID.randomUUID(); 93 if (test.version() != 4) 94 throw new Exception("randomUUID not type 4"); 95 Random byteSource = new Random(); 96 byte[] someBytes = new byte[12]; 97 byteSource.nextBytes(someBytes); 98 test = UUID.nameUUIDFromBytes(someBytes); 99 if (test.version() != 3) 100 throw new Exception("nameUUIDFromBytes not type 3"); 101 test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda"); 102 if (test.version() != 1) 103 throw new Exception("wrong version fromString 1"); 104 test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda"); 105 if (test.version() != 2) 106 throw new Exception("wrong version fromString 2"); 107 test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda"); 108 if (test.version() != 3) 109 throw new Exception("wrong version fromString 3"); 110 test = UUID.fromString("9835451d-e2e0-4e41-8a5a-be785f17dcda"); 111 if (test.version() != 4) 112 throw new Exception("wrong version fromString 4"); 113 test = new UUID(0x0000000000001000L, 55L); 114 if (test.version() != 1) 115 throw new Exception("wrong version from bit set to 1"); 116 test = new UUID(0x0000000000002000L, 55L); 117 if (test.version() != 2) 118 throw new Exception("wrong version from bit set to 2"); 119 test = new UUID(0x0000000000003000L, 55L); 120 if (test.version() != 3) 121 throw new Exception("wrong version from bit set to 3"); 122 test = new UUID(0x0000000000004000L, 55L); 123 if (test.version() != 4) 124 throw new Exception("wrong version from bit set to 4"); 125 } 126 127 private static void variantTest() throws Exception { 128 UUID test = UUID.randomUUID(); 129 if (test.variant() != 2) 130 throw new Exception("randomUUID not variant 2"); 131 Random byteSource = new Random(); 132 byte[] someBytes = new byte[12]; 133 byteSource.nextBytes(someBytes); 134 test = UUID.nameUUIDFromBytes(someBytes); 135 if (test.variant() != 2) 136 throw new Exception("nameUUIDFromBytes not variant 2"); 137 test = new UUID(55L, 0x0000000000001000L); 138 if (test.variant() != 0) 139 throw new Exception("wrong variant from bit set to 0"); 140 test = new UUID(55L, 0x8000000000001000L); 141 if (test.variant() != 2) 142 throw new Exception("wrong variant from bit set to 2"); 143 test = new UUID(55L, 0xc000000000001000L); 144 if (test.variant() != 6) 145 throw new Exception("wrong variant from bit set to 6"); 146 test = new UUID(55L, 0xe000000000001000L); 147 if (test.variant() != 7) 148 throw new Exception("wrong variant from bit set to 7"); 149 } 150 151 private static void timestampTest() throws Exception { 152 UUID test = UUID.randomUUID(); 153 try { 154 test.timestamp(); 155 throw new Exception("Expected exception not thrown"); 156 } catch (UnsupportedOperationException uoe) { 157 // Correct result 158 } 159 test = UUID.fromString("00000001-0000-1000-8a5a-be785f17dcda"); 160 if (test.timestamp() != 1) 161 throw new Exception("Incorrect timestamp"); 162 test = UUID.fromString("00000400-0000-1000-8a5a-be785f17dcda"); 163 if (test.timestamp() != 1024) 164 throw new Exception("Incorrect timestamp"); 165 test = UUID.fromString("FFFFFFFF-FFFF-1FFF-8a5a-be785f17dcda"); 166 if (test.timestamp() != Long.MAX_VALUE>>3) 167 throw new Exception("Incorrect timestamp"); 168 } 169 170 private static void clockSequenceTest() throws Exception { 171 UUID test = UUID.randomUUID(); 172 try { 173 test.clockSequence(); 174 throw new Exception("Expected exception not thrown"); 175 } catch (UnsupportedOperationException uoe) { 176 // Correct result 177 } 178 test = UUID.fromString("00000001-0000-1000-8001-be785f17dcda"); 179 if (test.clockSequence() != 1) 180 throw new Exception("Incorrect sequence"); 181 test = UUID.fromString("00000001-0000-1000-8002-be785f17dcda"); 182 if (test.clockSequence() != 2) 183 throw new Exception("Incorrect sequence"); 184 test = UUID.fromString("00000001-0000-1000-8010-be785f17dcda"); 185 if (test.clockSequence() != 16) 186 throw new Exception("Incorrect sequence"); 187 test = UUID.fromString("00000001-0000-1000-bFFF-be785f17dcda"); 188 if (test.clockSequence() != ((2L<<13)-1)) // 2^14 - 1 189 throw new Exception("Incorrect sequence"); 190 } 191 192 private static void nodeTest() throws Exception { 193 UUID test = UUID.randomUUID(); 194 try { 195 test.node(); 196 throw new Exception("Expected exception not thrown"); 197 } catch (UnsupportedOperationException uoe) { 198 // Correct result 199 } 200 test = UUID.fromString("00000001-0000-1000-8001-000000000001"); 201 if (test.node() != 1) 202 throw new Exception("Incorrect node"); 203 test = UUID.fromString("00000001-0000-1000-8002-FFFFFFFFFFFF"); 204 if (test.node() != ((2L<<47)-1)) // 2^48 - 1 205 throw new Exception("Incorrect node"); 206 } 207 208 private static void hashCodeEqualsTest() throws Exception { 209 // If two UUIDs are equal they must have the same hashCode 210 for (int i=0; i<100; i++) { 211 UUID u1 = UUID.randomUUID(); 212 UUID u2 = UUID.fromString(u1.toString()); 213 if (u1.hashCode() != u2.hashCode()) 214 throw new Exception("Equal UUIDs with different hashcodes"); 215 } 216 // Test equality of UUIDs with tampered bits 217 for (int i=0; i<1000; i++) { 218 long l = generator.nextLong(); 219 long l2 = generator.nextLong(); 220 int position = generator.nextInt(64); 221 UUID u1 = new UUID(l, l2); 222 l = l ^ (1L << position); 223 UUID u2 = new UUID(l, l2); 224 if (u1.equals(u2)) 225 throw new Exception("UUIDs with different bits equal"); 226 } 227 } 228 229 private static void compareTo() throws Exception { 230 UUID id = new UUID(33L, 63L); 231 UUID id2 = new UUID(34L, 62L); 232 UUID id3 = new UUID(34L, 63L); 233 UUID id4 = new UUID(34L, 64L); 234 UUID id5 = new UUID(35L, 63L); 235 236 if ((id.compareTo(id2) >= 0) || 237 (id2.compareTo(id3) >= 0) || 238 (id3.compareTo(id4) >= 0) || 239 (id4.compareTo(id5) >= 0)) 240 throw new RuntimeException("compareTo failure"); 241 242 if ((id5.compareTo(id4) <= 0) || 243 (id4.compareTo(id3) <= 0) || 244 (id3.compareTo(id2) <= 0) || 245 (id2.compareTo(id) <= 0)) 246 throw new RuntimeException("compareTo failure"); 247 248 if (id.compareTo(id) != 0) 249 throw new RuntimeException("compareTo failure"); 250 251 } 252 253 }