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
|
#!perl -T
use strict;
use warnings;
use Test::More 'no_plan';
use Carp;
use IO::File;
use MIME::Base64;
use UUID::Tiny qw(:std);
#
# Pre-defined UUIDs ...
ok( equal_uuids(UUID_NIL, '00000000-0000-0000-0000-000000000000'), 'NIL' );
ok( equal_uuids(UUID_NS_DNS, '6ba7b810-9dad-11d1-80b4-00c04fd430c8'), 'DNS' );
ok( equal_uuids(UUID_NS_URL, '6ba7b811-9dad-11d1-80b4-00c04fd430c8'), 'URL' );
ok( equal_uuids(UUID_NS_OID, '6ba7b812-9dad-11d1-80b4-00c04fd430c8'), 'OID' );
ok( equal_uuids(UUID_NS_X500, '6ba7b814-9dad-11d1-80b4-00c04fd430c8'), 'X500' );
#
# is_uuid_string() ...
#
ok( is_uuid_string(uuid_to_string(UUID_NIL)), 'is_uuid_string($UUID_NIL)' );
ok( is_uuid_string(uuid_to_string(UUID_NS_URL)),
'is_uuid_string() with URL UUID'
);
ok(
!is_uuid_string('6ba7b810-9dad-11d1-80b4-00c04fd430'),
'is_uuid_string() with truncated UUID-string'
);
#
# uuid_to_string() and string_to_uuid() ...
#
is(
uuid_to_string(UUID_NIL),
'00000000-0000-0000-0000-000000000000',
'uuid_to_string(UUID_NIL)',
);
is(
uuid_to_string(UUID_NS_DNS),
'6ba7b810-9dad-11d1-80b4-00c04fd430c8',
'uuid_to_string(UUID_NS_DNS)',
);
is(
uuid_to_string(uuid_to_string(UUID_NS_URL)),
uuid_to_string(UUID_NS_URL),
'uuid_to_string with UUID string returns UUID string'
);
is(
string_to_uuid(UUID_NS_OID),
UUID_NS_OID,
'string_to_uuid of UUID return UUID'
);
my $hex_uuid = uuid_to_string(UUID_NS_URL);
$hex_uuid =~ tr/-//;
is(
string_to_uuid($hex_uuid),
UUID_NS_URL,
'string_to_uuid of hex string'
);
my $base64_uuid = encode_base64(UUID_NS_URL);
is(
string_to_uuid($base64_uuid),
UUID_NS_URL,
'string_to_uuid of Base64 string'
);
is(
string_to_uuid( 'urn:uuid:' . uuid_to_string(UUID_NS_DNS) ),
UUID_NS_DNS,
'string_to_uuid with URN string representation'
);
is(
string_to_uuid( 'uuid:' . uuid_to_string(UUID_NS_DNS) ),
UUID_NS_DNS,
'string_to_uuid with shortened URN string representation'
);
is(
string_to_uuid( 'URN:UUID:' . uc(uuid_to_string(UUID_NS_DNS)) ),
UUID_NS_DNS,
'string_to_uuid with all-uppercase URN string representation'
);
eval{ string_to_uuid( 'This is nonsense!' ) };
like( $@, qr/is no UUID string/, 'string_to_uuid with invalid string' );
#
# Create v3 (MD5) UUIDs ...
#
is(
create_uuid_as_string( UUID_MD5, UUID_NS_DNS, 'python.org' ),
'6fa459ea-ee8a-3ca4-894e-db77e160355e',
'v3 UUID with DNS und python.org'
);
is(
create_uuid_as_string( UUID_V3, UUID_NS_DNS, 'www.doughellmann.com' ),
'bcd02e22-68f0-3046-a512-327cca9def8f',
'v3 UUID test with www.doughellmann.com and DNS Namespace UUID'
);
my $test_data = do {
local $/;
open my $fh, '<', 't/data/test.jpg' or croak "Open failed!";
<$fh>;
};
my $fh;
open $fh, '<', 't/data/test.jpg' or croak "Open failed!";
is(
create_uuid_as_string( UUID_MD5, $fh ),
create_uuid_as_string( UUID_V3, $test_data ),
'V3 UUID from GLOB'
);
undef $fh;
$fh = new IO::File 't/data/test.jpg' or croak 'IO::File failed.';
is(
create_uuid_as_string( UUID_MD5, $fh ),
create_uuid_as_string( UUID_V3, $test_data ),
'V3 UUID from IO::File'
);
undef $fh;
#
# Create v5 UUIDs ...
#
if (!UUID_SHA1_AVAIL) {
diag('No SHA1 available, skipping UUID_SHA1 tests ...');
}
else {
is(
create_uuid_as_string( UUID_SHA1, UUID_NS_DNS, 'python.org' ),
'886313e1-3b8a-5372-9b90-0c9aee199e5d',
'v5 UUID with DNS und python.org'
);
is(
create_uuid_as_string( UUID_V5, UUID_NS_DNS, 'www.doughellmann.com' ),
'e3329b12-30b7-57c4-8117-c2cd34a87ce9',
'v5 UUID test with www.doughellmann.com and DNS Namespace UUID'
);
open $fh, '<', 't/data/test.jpg' or croak "Open failed!";
is(
create_uuid_as_string( UUID_SHA1, $fh ),
create_uuid_as_string( UUID_V5, $test_data ),
'V5 UUID from GLOB'
);
undef $fh;
$fh = new IO::File 't/data/test.jpg' or croak 'IO::File failed.';
is(
create_uuid_as_string( UUID_SHA1, $fh ),
create_uuid_as_string( UUID_V5, $test_data ),
'V5 UUID from IO::File'
);
undef $fh;
is(
create_uuid(UUID_SHA1, 'Ein Test-String.'),
create_uuid(UUID_V5, UUID_NIL, 'Ein Test-String.'),
'create_uuid without NS UUID'
);
}
#
# is_v1_uuid() and is_v5_uuid() ...
#
ok( version_of_uuid(UUID_NS_URL) == 1, 'is_v1_uuid with UUID' );
ok(
version_of_uuid(string_to_uuid(UUID_NS_URL)) == 1,
'is_v1_uuid with UUID string'
);
ok(
version_of_uuid(
string_to_uuid('e3329b12-30b7-57c4-8117-c2cd34a87ce9')) == 5,
'is_v5_uuid with UUID'
);
ok(
version_of_uuid('e3329b12-30b7-57c4-8117-c2cd34a87ce9') == 5,
'is_v5_uuid with UUID string'
);
#
# Generate v1mc UUIDs ...
#
my $now = time();
my $v1_uuid = create_uuid();
#diag uuid_to_string($v1_uuid);
ok( version_of_uuid($v1_uuid) == 1, 'create_uuid creates v1 UUID' );
# Check time_of_uuid() ...
my $uuid_time = int(time_of_uuid($v1_uuid));
ok( ($uuid_time == $now) || ($uuid_time == $now + 1), 'check time of UUID' );
is( time_of_uuid(UUID_NIL), undef, 'time_of_uuid($UUID_NIL) is undef' );
is(
time_of_uuid($v1_uuid),
time_of_uuid(uuid_to_string($v1_uuid)),
'time_of_uuid with UUID and UUID string'
);
# Check clk_seq_of_uuid() ...
ok( defined clk_seq_of_uuid($v1_uuid), 'clk_seq_of_uuid works as expected' );
ok( !defined clk_seq_of_uuid(UUID_NIL), 'clk_seq_of_uuid of UUID NIL undef');
is(
clk_seq_of_uuid($v1_uuid),
clk_seq_of_uuid(uuid_to_string($v1_uuid)),
'clk_seq_of_uuid with UUID and UUID string'
);
# Check equal_uuids() ...
ok( equal_uuids($v1_uuid, uuid_to_string($v1_uuid)), 'equal_uuids()' );
ok( !equal_uuids(uuid_to_string($v1_uuid), UUID_NS_URL), '!equal_uuids()' );
# Check if time advances as expected ...
sleep 1;
ok( $now < time_of_uuid(create_uuid()), 'check if time advances ...');
# Check for uniqueness of consecutive UUIDs ...
my %uuid;
my $prev_uuid;
for (my $i = 0; $i < 10000; $i++) {
my $act_uuid = create_uuid();
if (!exists $uuid{$act_uuid}) {
$uuid{$act_uuid} = 1;
if (defined $prev_uuid) {
ok(
(
(time_of_uuid($prev_uuid) < time_of_uuid($act_uuid))
&& (clk_seq_of_uuid($prev_uuid)
== clk_seq_of_uuid($act_uuid))
) || (
(time_of_uuid($prev_uuid) >= time_of_uuid($act_uuid))
&& (clk_seq_of_uuid($prev_uuid)
!= clk_seq_of_uuid($act_uuid))
),
'time advances or clk_seq is different'
);
}
$prev_uuid = $act_uuid;
}
else {
fail('Consecutive v1 UUIDs are not unique!');
}
}
#
# Generate v4 UUIDs ...
#
my $v4_uuid = create_uuid(UUID_RANDOM);
#diag uuid_to_string($v4_uuid);
ok( version_of_uuid($v4_uuid) == 4, 'create_uuid creates v4 UUID' );
# Check for uniqueness of random UUIDs ...
my $not_unique = 0;
for (my $i = 0; $i < 100000; $i++) {
my $act_uuid = create_uuid(UUID_V4);
if (!exists $uuid{$act_uuid}) {
$uuid{$act_uuid} = 1;
}
else {
$not_unique = 1;
last;
}
}
ok( !$not_unique, '100.000 V4 UUIDs are unique!' );
|