File: GlobalIdGeneratorTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (200 lines) | stat: -rw-r--r-- 5,954 bytes parent folder | download | duplicates (2)
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
<?php

use Wikimedia\Timestamp\ConvertibleTimestamp;
use Wikimedia\UUID\GlobalIdGenerator;

/**
 * @covers \Wikimedia\UUID\GlobalIdGenerator
 */
class GlobalIdGeneratorTest extends PHPUnit\Framework\TestCase {

	use MediaWikiCoversValidator;
	use MediaWikiTestCaseTrait;

	/** @var GlobalIdGenerator */
	private $globalIdGenerator;

	/**
	 * @dataProvider provider_testTimestampedUID
	 */
	public function testTimestampedUID( $method, $digitlen, $bits, $tbits, $hostbits ) {
		$id = $this->globalIdGenerator->$method();
		$this->assertTrue( ctype_digit( $id ), "UID made of digit characters" );
		$this->assertLessThanOrEqual( $digitlen, strlen( $id ),
			"UID has the right number of digits" );
		$this->assertLessThanOrEqual( $bits, strlen( Wikimedia\base_convert( $id, 10, 2 ) ),
			"UID has the right number of bits" );

		$ids = [];
		for ( $i = 0; $i < 300; $i++ ) {
			$ids[] = $this->globalIdGenerator->$method();
		}

		$lastId = array_shift( $ids );

		$this->assertSame( array_unique( $ids ), $ids, "All generated IDs are unique." );

		foreach ( $ids as $id ) {
			// Convert string to binary and pad to full length so we can
			// extract segments
			$id_bin = Wikimedia\base_convert( $id, 10, 2, $bits );
			$lastId_bin = Wikimedia\base_convert( $lastId, 10, 2, $bits );

			$timestamp_bin = substr( $id_bin, 0, $tbits );
			$last_timestamp_bin = substr( $lastId_bin, 0, $tbits );

			$this->assertGreaterThanOrEqual(
				$last_timestamp_bin,
				$timestamp_bin,
				"timestamp ($timestamp_bin) of current ID ($id_bin) >= timestamp ($last_timestamp_bin) " .
					"of prior one ($lastId_bin)" );

			$hostbits_bin = substr( $id_bin, -$hostbits );
			$last_hostbits_bin = substr( $lastId_bin, -$hostbits );

			if ( $hostbits ) {
				$this->assertEquals(
					$hostbits_bin,
					$last_hostbits_bin,
					"Host ID ($hostbits_bin) of current ID ($id_bin) is same as host ID ($last_hostbits_bin) " .
						"of prior one ($lastId_bin)." );
			}

			$lastId = $id;
		}
	}

	public static function provider_testTimestampedUID() {
		// [ method name, expected length, bits, hostbits ]
		return [
			[ 'newTimestampedUID128', 39, 128, 46, 48 ],
			[ 'newTimestampedUID128', 39, 128, 46, 48 ],
			[ 'newTimestampedUID88', 27, 88, 46, 32 ],
		];
	}

	public function testUUIDv1() {
		$ids = [];
		for ( $i = 0; $i < 100; $i++ ) {
			$id = $this->globalIdGenerator->newUUIDv1();
			$this->assertMatchesRegularExpression(
				'!^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!',
				$id,
				"UID $id has the right format"
			);
			$ids[] = $id;

			$id = $this->globalIdGenerator->newRawUUIDv1();
			$this->assertMatchesRegularExpression(
				'!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!',
				$id,
				"UID $id has the right format"
			);

			$id = $this->globalIdGenerator->newRawUUIDv1();
			$this->assertMatchesRegularExpression(
				'!^[0-9a-f]{12}1[0-9a-f]{3}[89ab][0-9a-f]{15}$!',
				$id,
				"UID $id has the right format"
			);
		}

		$this->assertEquals( array_unique( $ids ), $ids, "All generated IDs are unique." );
	}

	public function testUUIDv4() {
		$ids = [];
		for ( $i = 0; $i < 100; $i++ ) {
			$id = $this->globalIdGenerator->newUUIDv4();
			$ids[] = $id;
			$this->assertMatchesRegularExpression(
				'!^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$!',
				$id,
				"UID $id has the right format"
			);
		}

		$this->assertEquals( array_unique( $ids ), $ids, 'All generated IDs are unique.' );
	}

	public function testRawUUIDv4() {
		for ( $i = 0; $i < 100; $i++ ) {
			$id = $this->globalIdGenerator->newRawUUIDv4();
			$this->assertMatchesRegularExpression(
				'!^[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15}$!',
				$id,
				"UID $id has the right format"
			);
		}
	}

	public function testNewSequentialID() {
		$id1 = $this->globalIdGenerator->newSequentialPerNodeID( 'test', 32 );
		$id2 = $this->globalIdGenerator->newSequentialPerNodeID( 'test', 32 );

		$this->assertIsFloat( $id1, "ID returned as float" );
		$this->assertIsFloat( $id2, "ID returned as float" );
		$this->assertGreaterThan( 0, $id1, "ID greater than 1" );
		$this->assertGreaterThan( $id1, $id2, "IDs increasing in value" );
	}

	public function testNewSequentialIDs() {
		$ids = $this->globalIdGenerator->newSequentialPerNodeIDs( 'test', 32, 5 );
		$lastId = null;
		foreach ( $ids as $id ) {
			$this->assertIsFloat( $id, "ID returned as float" );
			$this->assertGreaterThan( 0, $id, "ID greater than 1" );
			if ( $lastId ) {
				$this->assertGreaterThan( $lastId, $id, "IDs increasing in value" );
			}
			$lastId = $id;
		}
	}

	public static function provideGetTimestampFromUUIDv1() {
		yield [ '65d143b0-3c7a-11ea-b77f-2e728ce88125', '20200121181818' ];
	}

	/**
	 * @param string $uuid
	 * @param string $ts
	 * @dataProvider provideGetTimestampFromUUIDv1
	 */
	public function testGetTimestampFromUUIDv1( string $uuid, string $ts ) {
		$this->assertEquals( $ts, $this->globalIdGenerator->getTimestampFromUUIDv1( $uuid ) );
		$this->assertEquals(
			ConvertibleTimestamp::convert( TS_ISO_8601, $ts ),
			$this->globalIdGenerator->getTimestampFromUUIDv1( $uuid, TS_ISO_8601 )
		);
	}

	public static function provideGetTimestampFromUUIDv1InvalidUUIDv1() {
		yield [ 'this_is_an_invalid_uuid_v1' ];
		yield [ 'e5bb7f6b-0f28-4867-a93c-1b33b5c63adf' ]; // This is a UUIDv4
	}

	/**
	 * @param string $uuid
	 * @dataProvider provideGetTimestampFromUUIDv1InvalidUUIDv1
	 */
	public function testGetTimestampFromUUIDv1InvalidUUIDv1( string $uuid ) {
		$this->expectException( InvalidArgumentException::class );
		$this->globalIdGenerator->getTimestampFromUUIDv1( $uuid );
	}

	protected function setUp(): void {
		$this->globalIdGenerator = new GlobalIdGenerator(
			wfTempDir(),
			static function ( $command ) {
				return wfShellExec( $command );
			}
		);
	}

	protected function tearDown(): void {
		// Clean up - T46850
		$this->globalIdGenerator->unitTestTearDown();
		parent::tearDown();
	}

}