2 // si-73-secpasswordgenerate.c
7 #include <Security/SecPasswordGenerate.h>
8 #include <utilities/SecCFRelease.h>
9 #include "Security_regressions.h"
11 static void tests(void)
13 CFErrorRef error
= NULL
;
14 CFStringRef password
= NULL
;
15 CFMutableArrayRef requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
16 CFMutableDictionaryRef passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
18 //generates random digit string
19 is(true, (password
= SecPasswordCreateWithRandomDigits(8, &error
)) != NULL
) ;
20 CFReleaseNull(password
);
22 is(true, (password
= SecPasswordCreateWithRandomDigits(7, &error
)) != NULL
) ;
23 CFReleaseNull(password
);
25 is(true, (password
= SecPasswordCreateWithRandomDigits(6, &error
)) != NULL
) ;
26 CFReleaseNull(password
);
28 is(true, (password
= SecPasswordCreateWithRandomDigits(5, &error
)) != NULL
) ;
29 CFReleaseNull(password
);
31 CFCharacterSetRef uppercaseLetterCharacterSet
= CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter
);
32 CFCharacterSetRef lowercaseLetterCharacterSet
= CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter
);
33 CFCharacterSetRef decimalDigitCharacterSet
= CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit
);
35 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
36 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
37 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
39 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDefaultForType
, CFSTR("true"));
42 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
45 CFReleaseNull(password
);
48 //test default icloud recovery code
49 password
= SecPasswordGenerate(kSecPasswordTypeiCloudRecovery
, &error
, passwordRequirements
);
52 CFReleaseNull(password
);
56 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
57 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDefaultForType
, CFSTR("true"));
58 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
61 CFReleaseNull(password
);
63 CFRelease(passwordRequirements
);
66 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
67 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDefaultForType
, CFSTR("true"));
68 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
73 CFReleaseNull(password
);
74 CFRelease(passwordRequirements
);
76 //test icloud recovery code generation
77 password
= SecPasswordGenerate(kSecPasswordTypeiCloudRecovery
, &error
, NULL
);
82 CFReleaseNull(password
);
88 CFNumberRef minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
89 CFNumberRef maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
91 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
92 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
93 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
94 CFStringRef allowedCharacters
= CFSTR("abcdsefw2345");
96 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
97 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
99 //test wifi code generation
100 //test with min/max in range of default
101 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
102 isnt(password
, NULL
);
106 CFReleaseNull(password
);
107 CFRelease(passwordRequirements
);
110 CFRelease(allowedCharacters
);
113 //test with max == min
117 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
118 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
120 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
121 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
122 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
123 allowedCharacters
= CFSTR("abcdsefw2345");
125 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
126 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
127 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
128 isnt(password
, NULL
);
132 CFReleaseNull(password
);
133 CFRelease(passwordRequirements
);
136 CFRelease(allowedCharacters
);
138 passwordRequirements
= NULL
;
140 //test disallowed characters
144 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
145 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
147 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
148 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
149 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
150 allowedCharacters
= CFSTR("abcdefghijklmnopqrstuvwxyz0123456789");
152 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
153 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
154 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDisallowedCharacters
, CFSTR("aidfl"));
156 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
157 isnt(password
, NULL
);
161 CFReleaseNull(password
);
162 CFRelease(passwordRequirements
);
165 CFRelease(allowedCharacters
);
166 passwordRequirements
= NULL
;
168 //test can't start with characters
172 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
173 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
175 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
176 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
177 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
178 allowedCharacters
= CFSTR("diujk");
180 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
181 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
182 CFDictionaryAddValue(passwordRequirements
, kSecPasswordCantStartWithChars
, CFSTR("d"));
184 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
185 isnt(password
, NULL
);
189 CFReleaseNull(password
);
190 CFRelease(passwordRequirements
);
193 CFRelease(allowedCharacters
);
194 passwordRequirements
= NULL
;
197 //test can't end with characters
201 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
202 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
204 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
205 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
206 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
207 allowedCharacters
= CFSTR("diujk89");
209 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
210 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
211 CFDictionaryAddValue(passwordRequirements
, kSecPasswordCantEndWithChars
, CFSTR("d"));
213 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
214 isnt(password
, NULL
);
218 CFReleaseNull(password
);
219 CFRelease(passwordRequirements
);
222 CFRelease(allowedCharacters
);
223 passwordRequirements
= NULL
;
226 //test 4 digit pin generation
227 for(int i
=0 ; i
< 100; i
++){
228 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
229 isnt(password
, NULL
);
233 CFReleaseNull(password
);
240 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
241 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
243 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
244 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
245 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
246 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
247 isnt(password
, NULL
);
251 CFReleaseNull(password
);
254 CFRelease(passwordRequirements
);
260 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
261 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
263 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
264 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
265 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
266 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
267 isnt(password
, NULL
);
271 CFReleaseNull(password
);
274 CFRelease(passwordRequirements
);
275 //test safari password
279 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
280 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
282 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
283 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
284 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
285 allowedCharacters
= CFSTR("abcdsefw2345");
286 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
287 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
288 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
289 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
291 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
292 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
294 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
295 isnt(password
, NULL
);
299 CFReleaseNull(password
);
300 CFRelease(passwordRequirements
);
303 CFRelease(allowedCharacters
);
304 CFRelease(requiredCharacterSets
);
306 //test flexible group size and number of groups in the password
307 //test safari password
311 int numberOfGroups
= 23;
313 CFTypeRef groupSizeRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &groupSize
);
314 CFTypeRef numberOfGroupsRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &numberOfGroups
);
316 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
317 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
319 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
320 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
321 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
322 allowedCharacters
= CFSTR("abcdsefw2345");
323 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
324 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
325 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
326 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
328 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
329 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
330 CFDictionaryAddValue(passwordRequirements
, kSecPasswordGroupSize
, groupSizeRef
);
331 CFDictionaryAddValue(passwordRequirements
, kSecPasswordNumberOfGroups
, numberOfGroupsRef
);
332 CFDictionaryAddValue(passwordRequirements
, kSecPasswordSeparator
, CFSTR("*"));
334 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
335 isnt(password
, NULL
);
339 CFReleaseNull(password
);
340 CFRelease(passwordRequirements
);
343 CFRelease(allowedCharacters
);
344 CFRelease(requiredCharacterSets
);
346 //test at least N characters
347 //test safari password
352 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
353 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
354 CFNumberRef threshold
= CFNumberCreate(NULL
, kCFNumberIntType
, &N
);
356 CFStringRef characters
= CFSTR("ab");
357 CFMutableDictionaryRef atLeast
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
358 CFDictionaryAddValue(atLeast
, kSecPasswordCharacters
, characters
);
359 CFDictionaryAddValue(atLeast
, kSecPasswordCharacterCount
, threshold
);
361 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
362 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
363 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
364 allowedCharacters
= CFSTR("abcdsefw2345");
365 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
366 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
367 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
368 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
370 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
371 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
372 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsAtLeastNSpecificCharacters
, atLeast
);
374 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
375 isnt(password
, NULL
);
379 CFReleaseNull(password
);
380 CFRelease(passwordRequirements
);
383 CFRelease(allowedCharacters
);
384 CFRelease(requiredCharacterSets
);
386 //test no More than N characters
387 //test safari password
393 threshold
= CFNumberCreate(NULL
, kCFNumberIntType
, &N
);
394 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
395 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
397 CFMutableDictionaryRef noMoreThan
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
398 CFStringRef noMore
= CFSTR("ab");
399 CFDictionaryAddValue(noMoreThan
, kSecPasswordCharacters
, noMore
);
400 CFDictionaryAddValue(noMoreThan
, kSecPasswordCharacterCount
, threshold
);
402 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
403 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
404 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
405 allowedCharacters
= CFSTR("abcdsefw2345");
406 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
407 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
408 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
409 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
411 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
412 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
413 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNSpecificCharacters
, noMoreThan
);
415 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
416 isnt(password
, NULL
);
420 CFReleaseNull(password
);
421 CFRelease(passwordRequirements
);
424 CFRelease(allowedCharacters
);
425 CFRelease(requiredCharacterSets
);
427 //test identical character threshold
428 //test safari password
433 threshold
= CFNumberCreate(NULL
, kCFNumberIntType
, &N
);
434 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
435 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
437 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
438 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
439 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
440 allowedCharacters
= CFSTR("abcdsefw2345");
441 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
442 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
443 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
444 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
446 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
447 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
448 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNConsecutiveIdenticalCharacters
, threshold
);
450 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
451 isnt(password
, NULL
);
455 CFReleaseNull(password
);
456 CFRelease(passwordRequirements
);
459 CFRelease(allowedCharacters
);
460 CFRelease(requiredCharacterSets
);
462 /////////////////now test all the error cases
463 //test with no required characters
468 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
469 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
470 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
472 allowedCharacters
= CFSTR("abcdsefw2345");
473 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
475 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
476 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
477 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
478 ok(password
== NULL
);
485 CFRelease(allowedCharacters
);
486 CFRelease(passwordRequirements
);
488 //test with no allowed characters
492 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
493 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
495 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
496 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
497 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
498 allowedCharacters
= CFSTR("");
500 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
501 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
502 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
503 ok(password
== NULL
);
510 CFRelease(passwordRequirements
);
512 //test with min > max
516 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
517 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
519 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
521 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
522 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
523 allowedCharacters
= CFSTR("abcdsefw2345");
525 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
526 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
527 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
528 ok(password
== NULL
);
534 CFRelease(allowedCharacters
);
535 CFRelease(passwordRequirements
);
537 //test by ommitting dictionary parameters
543 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
544 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
546 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
548 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
549 allowedCharacters
= CFSTR("abcdsefw2345");
551 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
552 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
553 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
554 ok(password
== NULL
);
560 CFRelease(allowedCharacters
);
561 CFRelease(passwordRequirements
);
567 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
568 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
570 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
572 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
573 allowedCharacters
= CFSTR("abcdsefw2345");
575 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
576 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
577 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
578 ok(password
== NULL
);
584 CFRelease(allowedCharacters
);
585 CFRelease(passwordRequirements
);
587 //omit allowed characters
591 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
592 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
594 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
596 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
597 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
598 allowedCharacters
= CFSTR("abcdsefw2345");
600 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
601 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
602 ok(password
== NULL
);
608 CFRelease(allowedCharacters
);
609 CFRelease(passwordRequirements
);
611 //omit required characters
615 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
616 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
618 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
620 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
621 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
622 allowedCharacters
= CFSTR("abcdsefw2345");
624 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
625 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
626 ok(password
== NULL
);
632 CFRelease(allowedCharacters
);
633 CFRelease(passwordRequirements
);
635 //pass in wrong type for min
639 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
640 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
642 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
644 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, allowedCharacters
);
645 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
646 allowedCharacters
= CFSTR("abcdsefw2345");
647 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
648 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
649 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
650 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
652 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
653 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
655 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
656 ok(password
== NULL
);
660 CFRelease(passwordRequirements
);
663 CFRelease(allowedCharacters
);
665 //pass in wrong type for max
669 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
670 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
672 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
674 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
675 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, allowedCharacters
);
676 allowedCharacters
= CFSTR("abcdsefw2345");
678 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
679 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
681 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
682 ok(password
== NULL
);
686 CFRelease(passwordRequirements
);
689 CFRelease(allowedCharacters
);
691 //pass in wrong type for allowed
695 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
696 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
698 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
700 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
701 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
702 allowedCharacters
= CFSTR("abcdsefw2345");
704 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, minRef
);
705 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
707 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
708 ok(password
== NULL
);
712 CFRelease(passwordRequirements
);
715 CFRelease(allowedCharacters
);
717 //pass in wrong type for required
721 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
722 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
724 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
726 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
727 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
728 allowedCharacters
= CFSTR("abcdsefw2345");
730 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
731 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
732 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
733 ok(password
== NULL
);
737 CFRelease(passwordRequirements
);
740 CFRelease(allowedCharacters
);
742 //pass in wrong type for no less than
746 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
747 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
749 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
750 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
751 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
752 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
753 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
755 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
756 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
757 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
758 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
759 allowedCharacters
= CFSTR("abcdsefw2345");
761 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
762 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
763 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsAtLeastNSpecificCharacters
, CFSTR("hehe"));
764 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
765 ok(password
== NULL
);
769 CFRelease(passwordRequirements
);
772 CFRelease(allowedCharacters
);
774 //pass in wrong type for no more than
778 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
779 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
781 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
782 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
783 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
784 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
785 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
787 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
788 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
789 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
790 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
791 allowedCharacters
= CFSTR("abcdsefw2345");
793 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
794 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
795 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNSpecificCharacters
, CFSTR("hehe"));
797 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
798 ok(password
== NULL
);
802 CFRelease(passwordRequirements
);
805 CFRelease(allowedCharacters
);
807 //pass in wrong disallowed characters
811 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
812 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
814 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
815 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
816 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
817 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
818 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
820 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
821 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
822 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
823 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
824 allowedCharacters
= CFSTR("abcdsefw2345");
826 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
827 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
828 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDisallowedCharacters
, requiredCharacterSets
);
830 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
831 ok(password
== NULL
);
835 CFRelease(passwordRequirements
);
838 CFRelease(allowedCharacters
);
840 //pass in wrong type for no more than's dictionary
844 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
845 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
847 CFMutableDictionaryRef wrongCount
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
848 CFDictionaryAddValue(wrongCount
, kSecPasswordCharacters
, CFSTR("lkj"));
849 CFDictionaryAddValue(wrongCount
, kSecPasswordCharacterCount
, CFSTR("sdf"));
851 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
852 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
853 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
854 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
855 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
857 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
858 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
859 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
860 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
861 allowedCharacters
= CFSTR("abcdsefw2345");
863 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
864 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
865 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNSpecificCharacters
, wrongCount
);
867 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
868 ok(password
== NULL
);
872 CFRelease(wrongCount
);
873 CFRelease(passwordRequirements
);
876 CFRelease(allowedCharacters
);
878 password
= CFSTR("Apple1?");
879 isnt(true, SecPasswordIsPasswordWeak(password
));
882 password
= CFSTR("Singhal190");
883 isnt(true, SecPasswordIsPasswordWeak(password
));
886 password
= CFSTR("1Hollow2");
887 isnt(true, SecPasswordIsPasswordWeak(password
));
890 password
= CFSTR("1Hollow/");
891 isnt(true, SecPasswordIsPasswordWeak(password
));
894 password
= CFSTR("baj/paj1");
895 isnt(true, SecPasswordIsPasswordWeak(password
));
898 password
= CFSTR("Zaxs1009?");
899 isnt(true, SecPasswordIsPasswordWeak(password
));
902 password
= CFSTR("6666");
903 isnt(false, SecPasswordIsPasswordWeak(password
));
906 password
= CFSTR("123456");
907 isnt(false, SecPasswordIsPasswordWeak(password
));
910 password
= CFSTR("654321");
911 isnt(false, SecPasswordIsPasswordWeak(password
));
914 password
= CFSTR("A");
915 isnt(false, SecPasswordIsPasswordWeak(password
));
918 password
= CFSTR("password");
919 isnt(true, SecPasswordIsPasswordWeak(password
));
922 password
= CFSTR("password1");
923 isnt(true, SecPasswordIsPasswordWeak(password
));
926 password
= CFSTR("P@ssw0rd");
927 isnt(true, SecPasswordIsPasswordWeak(password
));
930 password
= CFSTR("facebook!{}");
931 isnt(true, SecPasswordIsPasswordWeak(password
));
934 password
= CFSTR("12345678");
935 isnt(false, SecPasswordIsPasswordWeak(password
));
938 bool isSimple
= false;
940 password
= CFSTR("Apple1?");
941 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
944 password
= CFSTR("Singhal190");
945 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
948 password
= CFSTR("1Hollow2");
949 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
952 password
= CFSTR("1Hollow/");
953 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
956 password
= CFSTR("baj/paj1");
957 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
960 password
= CFSTR("Zaxs1009?");
961 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
964 password
= CFSTR("6666");
965 is(true, SecPasswordIsPasswordWeak2(isSimple
, password
));
968 password
= CFSTR("1235");
969 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
973 password
= CFSTR("6666");
974 is(true, SecPasswordIsPasswordWeak2(isSimple
, password
));
977 password
= CFSTR("1235");
978 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
982 password
= CFSTR("123456");
983 is(true, SecPasswordIsPasswordWeak2(isSimple
, password
));
986 password
= CFSTR("654321");
987 is(true, SecPasswordIsPasswordWeak2(isSimple
, password
));
990 password
= CFSTR("1577326");
991 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
994 password
= CFSTR("A");
995 is(true, SecPasswordIsPasswordWeak2(isSimple
, password
));
998 password
= CFSTR("password");
999 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
1000 CFRelease(password
);
1002 password
= CFSTR("password1");
1003 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
1004 CFRelease(password
);
1006 password
= CFSTR("P@ssw0rd");
1007 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
1008 CFRelease(password
);
1010 password
= CFSTR("facebook!{}");
1011 is(false, SecPasswordIsPasswordWeak2(isSimple
, password
));
1012 CFRelease(password
);
1014 password
= CFSTR("12345678");
1015 is(true, SecPasswordIsPasswordWeak2(isSimple
, password
));
1016 CFRelease(password
);
1018 is(true, SecPasswordIsPasswordWeak2(true, CFSTR("666666")));
1019 is(true, SecPasswordIsPasswordWeak2(true, CFSTR("123456")));
1020 is(false, SecPasswordIsPasswordWeak2(true, CFSTR("666166")));
1024 int si_73_secpasswordgenerate(int argc
, char *const *argv
)