1 | // |
2 | // UUIDTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "UUIDTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/UUID.h" |
15 | #include "Poco/Exception.h" |
16 | |
17 | |
18 | using Poco::UUID; |
19 | |
20 | |
21 | UUIDTest::UUIDTest(const std::string& rName): CppUnit::TestCase(rName) |
22 | { |
23 | } |
24 | |
25 | |
26 | UUIDTest::~UUIDTest() |
27 | { |
28 | } |
29 | |
30 | |
31 | void UUIDTest::testParse() |
32 | { |
33 | UUID uuid("6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
34 | assertTrue (uuid.toString() == "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
35 | |
36 | uuid.parse("6BA7B810-9DAD-11D1-80B4-00C04FD430C8" ); |
37 | assertTrue (uuid.toString() == "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
38 | |
39 | uuid.parse("6BA7B8109DAD11D180B400C04FD430C8" ); |
40 | assertTrue (uuid.toString() == "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
41 | |
42 | try |
43 | { |
44 | uuid.parse("6xA7B8109DAD11D180B400C04FD430C8" ); |
45 | fail("invalid UUID - must throw" ); |
46 | } |
47 | catch (Poco::SyntaxException&) |
48 | { |
49 | } |
50 | |
51 | try |
52 | { |
53 | uuid.parse("6xa7b810-9dad-11d1-80b4-00c04fd430c8" ); |
54 | fail("invalid UUID - must throw" ); |
55 | } |
56 | catch (Poco::SyntaxException&) |
57 | { |
58 | } |
59 | |
60 | try |
61 | { |
62 | uuid.parse("6ba7b810-xdad-11d1-80b4-00c04fd430c8" ); |
63 | fail("invalid UUID - must throw" ); |
64 | } |
65 | catch (Poco::SyntaxException&) |
66 | { |
67 | } |
68 | |
69 | try |
70 | { |
71 | uuid.parse("6ba7b810-9dad-x1d1-80b4-00c04fd430c8" ); |
72 | fail("invalid UUID - must throw" ); |
73 | } |
74 | catch (Poco::SyntaxException&) |
75 | { |
76 | } |
77 | |
78 | try |
79 | { |
80 | uuid.parse("6ba7b810-9dad-11d1-x0b4-00c04fd430c8" ); |
81 | fail("invalid UUID - must throw" ); |
82 | } |
83 | catch (Poco::SyntaxException&) |
84 | { |
85 | } |
86 | |
87 | try |
88 | { |
89 | uuid.parse("6ba7b810-9dad-11d1-80b4-00x04fd430c8" ); |
90 | fail("invalid UUID - must throw" ); |
91 | } |
92 | catch (Poco::SyntaxException&) |
93 | { |
94 | } |
95 | |
96 | try |
97 | { |
98 | uuid.parse("6ba7b8109dad11d180b400c04fd430c81234" ); |
99 | fail("invalid UUID - must throw" ); |
100 | } |
101 | catch (Poco::SyntaxException&) |
102 | { |
103 | } |
104 | |
105 | try |
106 | { |
107 | uuid.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c81234" ); |
108 | fail("invalid UUID - must throw" ); |
109 | } |
110 | catch (Poco::SyntaxException&) |
111 | { |
112 | } |
113 | } |
114 | |
115 | |
116 | void UUIDTest::testBuffer() |
117 | { |
118 | UUID uuid("6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
119 | char buffer[16]; |
120 | uuid.copyTo(buffer); |
121 | UUID uuid2; |
122 | uuid2.copyFrom(buffer); |
123 | assertTrue (uuid2.toString() == "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
124 | } |
125 | |
126 | |
127 | void UUIDTest::testCompare() |
128 | { |
129 | UUID null; |
130 | assertTrue (null.isNull()); |
131 | assertTrue (UUID::null().isNull()); |
132 | |
133 | UUID uuid1 = null; |
134 | UUID uuid2; |
135 | assertTrue (uuid1.isNull()); |
136 | assertTrue (uuid1 == null); |
137 | assertTrue (!(uuid1 != null)); |
138 | assertTrue (uuid1 >= null); |
139 | assertTrue (uuid1 <= null); |
140 | assertTrue (!(uuid1 > null)); |
141 | assertTrue (!(uuid1 < null)); |
142 | assertTrue (uuid1.toString() == "00000000-0000-0000-0000-000000000000" ); |
143 | |
144 | uuid1 = UUID::dns(); |
145 | assertTrue (!uuid1.isNull()); |
146 | assertTrue (uuid1 != null); |
147 | assertTrue (!(uuid1 == null)); |
148 | assertTrue (uuid1 >= null); |
149 | assertTrue (!(uuid1 <= null)); |
150 | assertTrue (uuid1 > null); |
151 | assertTrue (!(uuid1 < null)); |
152 | assertTrue (uuid1.toString() == "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
153 | |
154 | assertTrue (null != uuid1); |
155 | assertTrue (!(null == uuid1)); |
156 | assertTrue (!(null >= uuid1)); |
157 | assertTrue (null <= uuid1); |
158 | assertTrue (!(null > uuid1)); |
159 | assertTrue (null < uuid1); |
160 | |
161 | uuid2 = uuid1; |
162 | assertTrue (uuid2 == uuid1); |
163 | assertTrue (!(uuid2 != uuid1)); |
164 | assertTrue (uuid2 >= uuid1); |
165 | assertTrue (uuid2 <= uuid1); |
166 | assertTrue (!(uuid2 > uuid1)); |
167 | assertTrue (!(uuid2 < uuid1)); |
168 | } |
169 | |
170 | |
171 | void UUIDTest::testVersion() |
172 | { |
173 | UUID uuid("db4fa7e9-9e62-4597-99e0-b1ec0b59800e" ); |
174 | UUID::Version v = uuid.version(); |
175 | assertTrue (v == UUID::UUID_RANDOM); |
176 | |
177 | uuid.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
178 | v = uuid.version(); |
179 | assertTrue (v == UUID::UUID_TIME_BASED); |
180 | |
181 | uuid.parse("d2ee4220-3625-11d9-9669-0800200c9a66" ); |
182 | v = uuid.version(); |
183 | assertTrue (v == UUID::UUID_TIME_BASED); |
184 | |
185 | uuid.parse("360d3652-4411-4786-bbe6-b9675b548559" ); |
186 | v = uuid.version(); |
187 | assertTrue (v == UUID::UUID_RANDOM); |
188 | } |
189 | |
190 | |
191 | void UUIDTest::testVariant() |
192 | { |
193 | UUID uuid("db4fa7e9-9e62-4597-99e0-b1ec0b59800e" ); |
194 | int v = uuid.variant(); |
195 | assertTrue (v == 2); |
196 | |
197 | uuid.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
198 | v = uuid.variant(); |
199 | assertTrue (v == 2); |
200 | |
201 | uuid.parse("d2ee4220-3625-11d9-9669-0800200c9a66" ); |
202 | v = uuid.variant(); |
203 | assertTrue (v == 2); |
204 | |
205 | uuid.parse("360d3652-4411-4786-bbe6-b9675b548559" ); |
206 | v = uuid.variant(); |
207 | assertTrue (v == 2); |
208 | } |
209 | |
210 | |
211 | void UUIDTest::testSwap() |
212 | { |
213 | UUID uuid1("db4fa7e9-9e62-4597-99e0-b1ec0b59800e" ); |
214 | UUID uuid2("d2ee4220-3625-11d9-9669-0800200c9a66" ); |
215 | uuid1.swap(uuid2); |
216 | assertTrue (uuid1.toString() == "d2ee4220-3625-11d9-9669-0800200c9a66" ); |
217 | assertTrue (uuid2.toString() == "db4fa7e9-9e62-4597-99e0-b1ec0b59800e" ); |
218 | } |
219 | |
220 | void UUIDTest::testTryParse() |
221 | { |
222 | UUID uuid; |
223 | assertTrue (uuid.tryParse("6BA7B810-9DAD-11D1-80B4-00C04FD430C8" )); |
224 | assertTrue (uuid.toString() == "6ba7b810-9dad-11d1-80b4-00c04fd430c8" ); |
225 | |
226 | UUID notUuid; |
227 | assertTrue (!notUuid.tryParse("not a uuid" )); |
228 | assertTrue (notUuid.isNull()); |
229 | } |
230 | |
231 | void UUIDTest::setUp() |
232 | { |
233 | } |
234 | |
235 | |
236 | void UUIDTest::tearDown() |
237 | { |
238 | } |
239 | |
240 | |
241 | CppUnit::Test* UUIDTest::suite() |
242 | { |
243 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("UUIDTest" ); |
244 | |
245 | CppUnit_addTest(pSuite, UUIDTest, testParse); |
246 | CppUnit_addTest(pSuite, UUIDTest, testBuffer); |
247 | CppUnit_addTest(pSuite, UUIDTest, testCompare); |
248 | CppUnit_addTest(pSuite, UUIDTest, testVersion); |
249 | CppUnit_addTest(pSuite, UUIDTest, testVariant); |
250 | CppUnit_addTest(pSuite, UUIDTest, testSwap); |
251 | CppUnit_addTest(pSuite, UUIDTest, testTryParse); |
252 | |
253 | return pSuite; |
254 | } |
255 | |