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