1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
use DeGraciaMathieu\Riddler\Formats;
|
4
|
|
|
use DeGraciaMathieu\Riddler\Criteria;
|
5
|
|
|
use DeGraciaMathieu\Riddler\Password;
|
6
|
|
|
use DeGraciaMathieu\Riddler\Occurrences;
|
7
|
|
|
use DeGraciaMathieu\Riddler\Dictionaries;
|
8
|
|
|
|
9
|
|
|
class Test extends \PHPUnit\Framework\TestCase
|
|
|
|
|
10
|
|
|
{
|
11
|
|
|
/** @test */
|
12
|
|
|
public function emptyPassword()
|
13
|
|
|
{
|
14
|
|
|
$pw = new Password();
|
15
|
|
|
|
16
|
|
|
$str = $pw->generate();
|
17
|
|
|
|
18
|
|
|
$this->assertEmpty($str);
|
19
|
|
|
}
|
20
|
|
|
|
21
|
|
|
/** @test */
|
22
|
|
View Code Duplication |
public function letterPasswordOnly()
|
|
|
|
|
23
|
|
|
{
|
24
|
|
|
$pw = new Password();
|
25
|
|
|
|
26
|
|
|
$pw->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(10));
|
27
|
|
|
|
28
|
|
|
$str = $pw->generate();
|
29
|
|
|
|
30
|
|
|
$this->assertNotEmpty($str);
|
31
|
|
|
|
32
|
|
|
$this->assertRegExp('/[a-z]/', $str);
|
33
|
|
|
}
|
34
|
|
|
|
35
|
|
|
/** @test */
|
36
|
|
|
public function alphanumericPasswordOnly()
|
37
|
|
|
{
|
38
|
|
|
$pw = new Password();
|
39
|
|
|
|
40
|
|
|
$pw->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(5));
|
41
|
|
|
$pw->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(5));
|
42
|
|
|
$pw->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(5));
|
43
|
|
|
|
44
|
|
|
$str = $pw->generate();
|
45
|
|
|
|
46
|
|
|
$this->assertNotEmpty($str);
|
47
|
|
|
|
48
|
|
|
$this->assertEquals(15, mb_strlen($str));
|
49
|
|
|
|
50
|
|
|
$this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
/** @test */
|
54
|
|
View Code Duplication |
public function accentedLetterOnly()
|
|
|
|
|
55
|
|
|
{
|
56
|
|
|
$pw = new Password();
|
57
|
|
|
|
58
|
|
|
$accentedLetter = new Dictionaries\AccentedLetter();
|
59
|
|
|
|
60
|
|
|
$pw->addCriteria($accentedLetter, new Occurrences\Strict(5));
|
61
|
|
|
|
62
|
|
|
$str = $pw->generate();
|
63
|
|
|
|
64
|
|
|
$this->assertNotEmpty($str);
|
65
|
|
|
|
66
|
|
|
$this->assertEquals(5, mb_strlen($str));
|
67
|
|
|
|
68
|
|
|
$this->assertRegExp('/[' . implode($accentedLetter->handle()) . ']{5}/', $str);
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
/** @test */
|
72
|
|
View Code Duplication |
public function accentedUppercaseLetterOnly()
|
|
|
|
|
73
|
|
|
{
|
74
|
|
|
$pw = new Password();
|
75
|
|
|
|
76
|
|
|
$accentedUppercaseLetter = new Dictionaries\AccentedUppercaseLetter();
|
77
|
|
|
|
78
|
|
|
$pw->addCriteria($accentedUppercaseLetter, new Occurrences\Strict(5));
|
79
|
|
|
|
80
|
|
|
$str = $pw->generate();
|
81
|
|
|
|
82
|
|
|
$this->assertNotEmpty($str);
|
83
|
|
|
|
84
|
|
|
$this->assertEquals(5, mb_strlen($str));
|
85
|
|
|
|
86
|
|
|
$this->assertRegExp('/[' . implode($accentedUppercaseLetter->handle()) . ']{5}/', $str);
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
/** @test */
|
90
|
|
|
public function specialCharacterOnly()
|
91
|
|
|
{
|
92
|
|
|
$pw = new Password();
|
93
|
|
|
|
94
|
|
|
$specialCharacter = new Dictionaries\SpecialCharacter();
|
95
|
|
|
|
96
|
|
|
$pw->addCriteria($specialCharacter, new Occurrences\Strict(5));
|
97
|
|
|
|
98
|
|
|
$str = $pw->generate();
|
99
|
|
|
|
100
|
|
|
$this->assertNotEmpty($str);
|
101
|
|
|
|
102
|
|
|
$this->assertEquals(5, mb_strlen($str));
|
103
|
|
|
|
104
|
|
|
$chars = array_map(function ($c) {
|
105
|
|
|
return '\\' . $c;
|
106
|
|
|
}, $specialCharacter->handle());
|
107
|
|
|
|
108
|
|
|
$this->assertRegExp('/[' . implode($chars) . ']{5}/', $str);
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
/** @test */
|
112
|
|
|
public function longDigitFormatOnly()
|
113
|
|
|
{
|
114
|
|
|
$pw = new Password();
|
115
|
|
|
|
116
|
|
|
$pw->addFormat(new Formats\LongDigit());
|
117
|
|
|
|
118
|
|
|
$str = $pw->generate();
|
119
|
|
|
|
120
|
|
|
$this->assertNotEmpty($str);
|
121
|
|
|
|
122
|
|
|
$this->assertRegExp('/\d{30}/', $str);
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
/** @test */
|
126
|
|
View Code Duplication |
public function smallAlphanumericFormatOnly()
|
|
|
|
|
127
|
|
|
{
|
128
|
|
|
$pw = new Password();
|
129
|
|
|
|
130
|
|
|
$pw->addFormat(new Formats\SmallAlphanumeric());
|
131
|
|
|
|
132
|
|
|
$str = $pw->generate();
|
133
|
|
|
|
134
|
|
|
$this->assertNotEmpty($str);
|
135
|
|
|
|
136
|
|
|
$this->assertEquals(15, mb_strlen($str));
|
137
|
|
|
|
138
|
|
|
$this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
/** @test */
|
142
|
|
View Code Duplication |
public function strongAlphanumericFormatOnly()
|
|
|
|
|
143
|
|
|
{
|
144
|
|
|
$pw = new Password();
|
145
|
|
|
|
146
|
|
|
$pw->addFormat(new Formats\StrongAlphanumeric());
|
147
|
|
|
|
148
|
|
|
$str = $pw->generate();
|
149
|
|
|
|
150
|
|
|
$this->assertNotEmpty($str);
|
151
|
|
|
|
152
|
|
|
$this->assertEquals(30, mb_strlen($str));
|
153
|
|
|
|
154
|
|
|
$this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
155
|
|
|
}
|
156
|
|
|
|
157
|
|
|
/** @test */
|
158
|
|
|
public function mixedStrongFormatOnly()
|
159
|
|
|
{
|
160
|
|
|
$pw = new Password();
|
161
|
|
|
|
162
|
|
|
$pw->addFormat(new Formats\MixedStrong());
|
163
|
|
|
|
164
|
|
|
$str = $pw->generate();
|
165
|
|
|
|
166
|
|
|
$this->assertNotEmpty($str);
|
167
|
|
|
|
168
|
|
|
// $this->assertEquals(30, mb_strlen($str));
|
|
|
|
|
169
|
|
|
|
170
|
|
|
// $this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
|
|
|
|
171
|
|
|
}
|
172
|
|
|
|
173
|
|
|
/** @test */
|
174
|
|
|
public function mixedComplexFormatOnly()
|
175
|
|
|
{
|
176
|
|
|
$pw = new Password();
|
177
|
|
|
|
178
|
|
|
$pw->addFormat(new Formats\MixedComplex());
|
179
|
|
|
|
180
|
|
|
$str = $pw->generate();
|
181
|
|
|
|
182
|
|
|
$this->assertNotEmpty($str);
|
183
|
|
|
|
184
|
|
|
// $this->assertEquals(30, mb_strlen($str));
|
|
|
|
|
185
|
|
|
|
186
|
|
|
// $this->assertRegExp('/[a-zA-Z0-9]/', $str);
|
|
|
|
|
187
|
|
|
}
|
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** @test */
|
191
|
|
View Code Duplication |
public function digitPasswordOnly()
|
|
|
|
|
192
|
|
|
{
|
193
|
|
|
$pw = new Password();
|
194
|
|
|
|
195
|
|
|
$pw->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(10));
|
196
|
|
|
|
197
|
|
|
$str = $pw->generate();
|
198
|
|
|
|
199
|
|
|
$this->assertNotEmpty($str);
|
200
|
|
|
|
201
|
|
|
$this->assertRegExp('/\d{10}/', $str);
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
/** @test */
|
205
|
|
|
public function strictOccurrences()
|
206
|
|
|
{
|
207
|
|
|
$dictionary = new Dictionaries\Letter();
|
208
|
|
|
|
209
|
|
|
$occurrence = new Occurrences\Strict(5);
|
210
|
|
|
|
211
|
|
|
$criteria = new Criteria($dictionary, $occurrence);
|
212
|
|
|
|
213
|
|
|
$result = $criteria->build();
|
214
|
|
|
|
215
|
|
|
$this->assertTrue(is_array($result));
|
216
|
|
|
$this->assertEquals(5, count($result));
|
217
|
|
|
$this->assertContains($result[0], $dictionary->handle());
|
218
|
|
|
$this->assertContains($result[1], $dictionary->handle());
|
219
|
|
|
$this->assertContains($result[2], $dictionary->handle());
|
220
|
|
|
$this->assertContains($result[3], $dictionary->handle());
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
/** @test */
|
224
|
|
|
public function betweenOccurrences()
|
225
|
|
|
{
|
226
|
|
|
$dictionary = new Dictionaries\Letter();
|
227
|
|
|
|
228
|
|
|
$occurrence = new Occurrences\Between(3, 5);
|
229
|
|
|
|
230
|
|
|
$criteria = new Criteria($dictionary, $occurrence);
|
231
|
|
|
|
232
|
|
|
$result = $criteria->build();
|
233
|
|
|
|
234
|
|
|
$this->assertTrue(is_array($result));
|
235
|
|
|
$this->assertTrue(count($result) >= 3 && count($result) <= 5);
|
236
|
|
|
$this->assertContains($result[0], $dictionary->handle());
|
237
|
|
|
$this->assertContains($result[1], $dictionary->handle());
|
238
|
|
|
$this->assertContains($result[2], $dictionary->handle());
|
239
|
|
|
}
|
240
|
|
|
|
241
|
|
|
/** @test */
|
242
|
|
View Code Duplication |
public function simpleStrictPerfectScore()
|
|
|
|
|
243
|
|
|
{
|
244
|
|
|
$string = '1a2b3c';
|
245
|
|
|
|
246
|
|
|
$password = new Password();
|
247
|
|
|
|
248
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(3));
|
249
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(3));
|
250
|
|
|
|
251
|
|
|
$this->assertEquals($password->score($string), 100);
|
252
|
|
|
}
|
253
|
|
|
|
254
|
|
|
/** @test */
|
255
|
|
View Code Duplication |
public function simpleBetweenPerfectScore()
|
|
|
|
|
256
|
|
|
{
|
257
|
|
|
$string = '1a2b3c4';
|
258
|
|
|
|
259
|
|
|
$password = new Password();
|
260
|
|
|
|
261
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Between(3, 5));
|
262
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Between(3, 5));
|
263
|
|
|
|
264
|
|
|
$this->assertEquals($password->score($string), 100);
|
265
|
|
|
}
|
266
|
|
|
|
267
|
|
|
/** @test */
|
268
|
|
View Code Duplication |
public function simpleMixedPerfectScore()
|
|
|
|
|
269
|
|
|
{
|
270
|
|
|
$string = '1a2b3cde';
|
271
|
|
|
|
272
|
|
|
$password = new Password();
|
273
|
|
|
|
274
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(3));
|
275
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Between(3, 5));
|
276
|
|
|
|
277
|
|
|
$this->assertEquals($password->score($string), 100);
|
278
|
|
|
}
|
279
|
|
|
|
280
|
|
|
/** @test */
|
281
|
|
View Code Duplication |
public function simpleMixedIncompleteScore()
|
|
|
|
|
282
|
|
|
{
|
283
|
|
|
$string = '1a2bc';
|
284
|
|
|
|
285
|
|
|
$password = new Password();
|
286
|
|
|
|
287
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(3));
|
288
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Between(3, 5));
|
289
|
|
|
|
290
|
|
|
$this->assertEquals($password->score($string), 50);
|
291
|
|
|
}
|
292
|
|
|
|
293
|
|
|
/** @test */
|
294
|
|
View Code Duplication |
public function complexStrictPerfectScore()
|
|
|
|
|
295
|
|
|
{
|
296
|
|
|
$string = '123abcABC[&+àáâÀÁÂ';
|
297
|
|
|
|
298
|
|
|
$password = new Password();
|
299
|
|
|
|
300
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(3));
|
301
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(3));
|
302
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(3));
|
303
|
|
|
$password->addCriteria(new Dictionaries\SpecialCharacter(), new Occurrences\Strict(3));
|
304
|
|
|
$password->addCriteria(new Dictionaries\AccentedLetter(), new Occurrences\Strict(3));
|
305
|
|
|
$password->addCriteria(new Dictionaries\AccentedUppercaseLetter(), new Occurrences\Strict(3));
|
306
|
|
|
|
307
|
|
|
$this->assertEquals($password->score($string), 100);
|
308
|
|
|
}
|
309
|
|
|
|
310
|
|
|
/** @test */
|
311
|
|
View Code Duplication |
public function complexStrictIncompleteScore()
|
|
|
|
|
312
|
|
|
{
|
313
|
|
|
$string = '12abcAB[&+àáâÀÁÂ';
|
314
|
|
|
|
315
|
|
|
$password = new Password();
|
316
|
|
|
|
317
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(3));
|
318
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(3));
|
319
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(3));
|
320
|
|
|
$password->addCriteria(new Dictionaries\SpecialCharacter(), new Occurrences\Strict(3));
|
321
|
|
|
$password->addCriteria(new Dictionaries\AccentedLetter(), new Occurrences\Strict(3));
|
322
|
|
|
$password->addCriteria(new Dictionaries\AccentedUppercaseLetter(), new Occurrences\Strict(3));
|
323
|
|
|
|
324
|
|
|
$this->assertEquals($password->score($string), 67);
|
325
|
|
|
}
|
326
|
|
|
|
327
|
|
|
/** @test */
|
328
|
|
View Code Duplication |
public function complexMixedPerfectScore()
|
|
|
|
|
329
|
|
|
{
|
330
|
|
|
$string = '123abcdEFG[&+éèàÒÓÔ';
|
331
|
|
|
|
332
|
|
|
$password = new Password();
|
333
|
|
|
|
334
|
|
|
$password->addCriteria(new Dictionaries\Digit(), new Occurrences\Strict(3));
|
335
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Between(3, 5));
|
336
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(3));
|
337
|
|
|
$password->addCriteria(new Dictionaries\SpecialCharacter(), new Occurrences\Between(3, 5));
|
338
|
|
|
$password->addCriteria(new Dictionaries\AccentedLetter(), new Occurrences\Strict(3));
|
339
|
|
|
$password->addCriteria(new Dictionaries\AccentedUppercaseLetter(), new Occurrences\Between(3, 5));
|
340
|
|
|
|
341
|
|
|
$this->assertEquals($password->score($string), 100);
|
342
|
|
|
}
|
343
|
|
|
|
344
|
|
|
/** @test */
|
345
|
|
|
public function checkEfficientParseLetter()
|
346
|
|
|
{
|
347
|
|
|
$string = 'eaEAéèàÉÈÀ';
|
348
|
|
|
|
349
|
|
|
$password = new Password();
|
350
|
|
|
|
351
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(2));
|
352
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(2));
|
353
|
|
|
$password->addCriteria(new Dictionaries\AccentedLetter(), new Occurrences\Strict(3));
|
354
|
|
|
$password->addCriteria(new Dictionaries\AccentedUppercaseLetter(), new Occurrences\Strict(3));
|
355
|
|
|
|
356
|
|
|
$this->assertEquals($password->score($string), 100);
|
357
|
|
|
}
|
358
|
|
|
|
359
|
|
|
/** @test */
|
360
|
|
View Code Duplication |
public function checkEfficientClassicParseLetter()
|
|
|
|
|
361
|
|
|
{
|
362
|
|
|
$string = 'ea';
|
363
|
|
|
|
364
|
|
|
$password = new Password();
|
365
|
|
|
|
366
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(2));
|
367
|
|
|
$password->addCriteria(new Dictionaries\AccentedLetter(), new Occurrences\Strict(3));
|
368
|
|
|
$password->addCriteria(new Dictionaries\AccentedUppercaseLetter(), new Occurrences\Strict(3));
|
369
|
|
|
|
370
|
|
|
$this->assertEquals($password->score($string), 0);
|
371
|
|
|
}
|
372
|
|
|
|
373
|
|
|
|
374
|
|
|
/** @test */
|
375
|
|
View Code Duplication |
public function checkEfficientUppercaseParseLetter()
|
|
|
|
|
376
|
|
|
{
|
377
|
|
|
$string = 'EA';
|
378
|
|
|
|
379
|
|
|
$password = new Password();
|
380
|
|
|
|
381
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(2));
|
382
|
|
|
$password->addCriteria(new Dictionaries\AccentedLetter(), new Occurrences\Strict(3));
|
383
|
|
|
$password->addCriteria(new Dictionaries\AccentedUppercaseLetter(), new Occurrences\Strict(3));
|
384
|
|
|
|
385
|
|
|
$this->assertEquals($password->score($string), 0);
|
386
|
|
|
}
|
387
|
|
|
|
388
|
|
|
/** @test */
|
389
|
|
View Code Duplication |
public function checkEfficientAccentedParseLetter()
|
|
|
|
|
390
|
|
|
{
|
391
|
|
|
$string = 'éèà';
|
392
|
|
|
|
393
|
|
|
$password = new Password();
|
394
|
|
|
|
395
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(2));
|
396
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(2));
|
397
|
|
|
$password->addCriteria(new Dictionaries\AccentedUppercaseLetter(), new Occurrences\Strict(3));
|
398
|
|
|
|
399
|
|
|
$this->assertEquals($password->score($string), 0);
|
400
|
|
|
}
|
401
|
|
|
|
402
|
|
|
/** @test */
|
403
|
|
View Code Duplication |
public function checkEfficientAccentedUppercaseParseLetter()
|
|
|
|
|
404
|
|
|
{
|
405
|
|
|
$string = 'ÉÈÀ';
|
406
|
|
|
|
407
|
|
|
$password = new Password();
|
408
|
|
|
|
409
|
|
|
$password->addCriteria(new Dictionaries\Letter(), new Occurrences\Strict(2));
|
410
|
|
|
$password->addCriteria(new Dictionaries\UppercaseLetter(), new Occurrences\Strict(2));
|
411
|
|
|
$password->addCriteria(new Dictionaries\AccentedLetter(), new Occurrences\Strict(3));
|
412
|
|
|
|
413
|
|
|
$this->assertEquals($password->score($string), 0);
|
414
|
|
|
}
|
415
|
|
|
}
|
416
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.