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 CFCharacterSetRef uppercaseLetterCharacterSet
= CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter
);
19 CFCharacterSetRef lowercaseLetterCharacterSet
= CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter
);
20 CFCharacterSetRef decimalDigitCharacterSet
= CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit
);
22 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
23 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
24 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
26 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDefaultForType
, CFSTR("true"));
29 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
32 CFReleaseNull(password
);
35 //test default icloud recovery code
36 password
= SecPasswordGenerate(kSecPasswordTypeiCloudRecovery
, &error
, passwordRequirements
);
39 CFReleaseNull(password
);
43 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
44 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDefaultForType
, CFSTR("true"));
45 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
48 CFReleaseNull(password
);
50 CFRelease(passwordRequirements
);
53 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
54 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDefaultForType
, CFSTR("true"));
55 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
60 CFReleaseNull(password
);
61 CFRelease(passwordRequirements
);
63 //test icloud recovery code generation
64 password
= SecPasswordGenerate(kSecPasswordTypeiCloudRecovery
, &error
, NULL
);
69 CFReleaseNull(password
);
75 CFNumberRef minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
76 CFNumberRef maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
78 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
79 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
80 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
81 CFStringRef allowedCharacters
= CFSTR("abcdsefw2345");
83 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
84 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
86 //test wifi code generation
87 //test with min/max in range of default
88 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
93 CFReleaseNull(password
);
94 CFRelease(passwordRequirements
);
97 CFRelease(allowedCharacters
);
100 //test with max == min
104 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
105 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
107 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
108 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
109 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
110 allowedCharacters
= CFSTR("abcdsefw2345");
112 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
113 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
114 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
115 isnt(password
, NULL
);
119 CFReleaseNull(password
);
120 CFRelease(passwordRequirements
);
123 CFRelease(allowedCharacters
);
125 passwordRequirements
= NULL
;
127 //test disallowed characters
131 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
132 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
134 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
135 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
136 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
137 allowedCharacters
= CFSTR("abcdefghijklmnopqrstuvwxyz0123456789");
139 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
140 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
141 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDisallowedCharacters
, CFSTR("aidfl"));
143 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
144 isnt(password
, NULL
);
148 CFReleaseNull(password
);
149 CFRelease(passwordRequirements
);
152 CFRelease(allowedCharacters
);
153 passwordRequirements
= NULL
;
155 //test can't start with characters
159 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
160 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
162 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
163 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
164 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
165 allowedCharacters
= CFSTR("diujk");
167 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
168 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
169 CFDictionaryAddValue(passwordRequirements
, kSecPasswordCantStartWithChars
, CFSTR("d"));
171 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
172 isnt(password
, NULL
);
176 CFReleaseNull(password
);
177 CFRelease(passwordRequirements
);
180 CFRelease(allowedCharacters
);
181 passwordRequirements
= NULL
;
184 //test can't end with characters
188 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
189 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
191 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
192 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
193 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
194 allowedCharacters
= CFSTR("diujk89");
196 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
197 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
198 CFDictionaryAddValue(passwordRequirements
, kSecPasswordCantEndWithChars
, CFSTR("d"));
200 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
201 isnt(password
, NULL
);
205 CFReleaseNull(password
);
206 CFRelease(passwordRequirements
);
209 CFRelease(allowedCharacters
);
210 passwordRequirements
= NULL
;
213 //test 4 digit pin generation
214 for(int i
=0 ; i
< 100; i
++){
215 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
216 isnt(password
, NULL
);
220 CFReleaseNull(password
);
227 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
228 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
230 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
231 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
232 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
233 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
234 isnt(password
, NULL
);
238 CFReleaseNull(password
);
241 CFRelease(passwordRequirements
);
247 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
248 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
250 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
251 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
252 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
253 password
= SecPasswordGenerate(kSecPasswordTypePIN
, &error
, passwordRequirements
);
254 isnt(password
, NULL
);
258 CFReleaseNull(password
);
261 CFRelease(passwordRequirements
);
262 //test safari password
266 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
267 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
269 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
270 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
271 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
272 allowedCharacters
= CFSTR("abcdsefw2345");
273 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
274 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
275 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
276 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
278 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
279 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
281 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
282 isnt(password
, NULL
);
286 CFReleaseNull(password
);
287 CFRelease(passwordRequirements
);
290 CFRelease(allowedCharacters
);
291 CFRelease(requiredCharacterSets
);
293 //test flexible group size and number of groups in the password
294 //test safari password
298 int numberOfGroups
= 23;
300 CFTypeRef groupSizeRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &groupSize
);
301 CFTypeRef numberOfGroupsRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &numberOfGroups
);
303 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
304 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
306 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
307 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
308 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
309 allowedCharacters
= CFSTR("abcdsefw2345");
310 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
311 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
312 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
313 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
315 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
316 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
317 CFDictionaryAddValue(passwordRequirements
, kSecPasswordGroupSize
, groupSizeRef
);
318 CFDictionaryAddValue(passwordRequirements
, kSecPasswordNumberOfGroups
, numberOfGroupsRef
);
319 CFDictionaryAddValue(passwordRequirements
, kSecPasswordSeparator
, CFSTR("*"));
321 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
322 isnt(password
, NULL
);
326 CFReleaseNull(password
);
327 CFRelease(passwordRequirements
);
330 CFRelease(allowedCharacters
);
331 CFRelease(requiredCharacterSets
);
333 //test at least N characters
334 //test safari password
339 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
340 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
341 CFNumberRef threshold
= CFNumberCreate(NULL
, kCFNumberIntType
, &N
);
343 CFStringRef characters
= CFSTR("ab");
344 CFMutableDictionaryRef atLeast
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
345 CFDictionaryAddValue(atLeast
, kSecPasswordCharacters
, characters
);
346 CFDictionaryAddValue(atLeast
, kSecPasswordCharacterCount
, threshold
);
348 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
349 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
350 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
351 allowedCharacters
= CFSTR("abcdsefw2345");
352 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
353 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
354 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
355 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
357 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
358 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
359 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsAtLeastNSpecificCharacters
, atLeast
);
361 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
362 isnt(password
, NULL
);
366 CFReleaseNull(password
);
367 CFRelease(passwordRequirements
);
370 CFRelease(allowedCharacters
);
371 CFRelease(requiredCharacterSets
);
373 //test no More than N characters
374 //test safari password
380 threshold
= CFNumberCreate(NULL
, kCFNumberIntType
, &N
);
381 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
382 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
384 CFMutableDictionaryRef noMoreThan
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
385 CFStringRef noMore
= CFSTR("ab");
386 CFDictionaryAddValue(noMoreThan
, kSecPasswordCharacters
, noMore
);
387 CFDictionaryAddValue(noMoreThan
, kSecPasswordCharacterCount
, threshold
);
389 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
390 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
391 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
392 allowedCharacters
= CFSTR("abcdsefw2345");
393 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
394 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
395 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
396 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
398 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
399 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
400 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNSpecificCharacters
, noMoreThan
);
402 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
403 isnt(password
, NULL
);
407 CFReleaseNull(password
);
408 CFRelease(passwordRequirements
);
411 CFRelease(allowedCharacters
);
412 CFRelease(requiredCharacterSets
);
414 //test identical character threshold
415 //test safari password
420 threshold
= CFNumberCreate(NULL
, kCFNumberIntType
, &N
);
421 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
422 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
424 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
425 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
426 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
427 allowedCharacters
= CFSTR("abcdsefw2345");
428 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
429 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
430 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
431 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
433 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
434 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
435 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNConsecutiveIdenticalCharacters
, threshold
);
437 password
= SecPasswordGenerate(kSecPasswordTypeSafari
, &error
, passwordRequirements
);
438 isnt(password
, NULL
);
442 CFReleaseNull(password
);
443 CFRelease(passwordRequirements
);
446 CFRelease(allowedCharacters
);
447 CFRelease(requiredCharacterSets
);
449 /////////////////now test all the error cases
450 //test with no required characters
455 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
456 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
457 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
459 allowedCharacters
= CFSTR("abcdsefw2345");
460 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
462 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
463 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
464 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
465 ok(password
== NULL
);
472 CFRelease(allowedCharacters
);
473 CFRelease(passwordRequirements
);
475 //test with no allowed characters
479 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
480 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
482 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
483 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
484 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
485 allowedCharacters
= CFSTR("");
487 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
488 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
489 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
490 ok(password
== NULL
);
497 CFRelease(passwordRequirements
);
499 //test with min > max
503 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
504 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
506 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
508 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
509 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
510 allowedCharacters
= CFSTR("abcdsefw2345");
512 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
513 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
514 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
515 ok(password
== NULL
);
521 CFRelease(allowedCharacters
);
522 CFRelease(passwordRequirements
);
524 //test by ommitting dictionary parameters
530 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
531 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
533 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
535 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
536 allowedCharacters
= CFSTR("abcdsefw2345");
538 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
539 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
540 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
541 ok(password
== NULL
);
547 CFRelease(allowedCharacters
);
548 CFRelease(passwordRequirements
);
554 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
555 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
557 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
559 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
560 allowedCharacters
= CFSTR("abcdsefw2345");
562 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
563 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
564 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
565 ok(password
== NULL
);
571 CFRelease(allowedCharacters
);
572 CFRelease(passwordRequirements
);
574 //omit allowed characters
578 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
579 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
581 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
583 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
584 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
585 allowedCharacters
= CFSTR("abcdsefw2345");
587 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
588 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
589 ok(password
== NULL
);
595 CFRelease(allowedCharacters
);
596 CFRelease(passwordRequirements
);
598 //omit required characters
602 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
603 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
605 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
607 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
608 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
609 allowedCharacters
= CFSTR("abcdsefw2345");
611 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
612 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
613 ok(password
== NULL
);
619 CFRelease(allowedCharacters
);
620 CFRelease(passwordRequirements
);
622 //pass in wrong type for min
626 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
627 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
629 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
631 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, allowedCharacters
);
632 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
633 allowedCharacters
= CFSTR("abcdsefw2345");
634 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
635 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
636 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
637 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
639 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
640 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
642 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
643 ok(password
== NULL
);
647 CFRelease(passwordRequirements
);
650 CFRelease(allowedCharacters
);
652 //pass in wrong type for max
656 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
657 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
659 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
661 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
662 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, allowedCharacters
);
663 allowedCharacters
= CFSTR("abcdsefw2345");
665 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
666 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
668 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
669 ok(password
== NULL
);
673 CFRelease(passwordRequirements
);
676 CFRelease(allowedCharacters
);
678 //pass in wrong type for allowed
682 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
683 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
685 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
687 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
688 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
689 allowedCharacters
= CFSTR("abcdsefw2345");
691 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, minRef
);
692 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
694 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
695 ok(password
== NULL
);
699 CFRelease(passwordRequirements
);
702 CFRelease(allowedCharacters
);
704 //pass in wrong type for required
708 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
709 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
711 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
713 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
714 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
715 allowedCharacters
= CFSTR("abcdsefw2345");
717 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
718 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
719 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
720 ok(password
== NULL
);
724 CFRelease(passwordRequirements
);
727 CFRelease(allowedCharacters
);
729 //pass in wrong type for no less than
733 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
734 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
736 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
737 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
738 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
739 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
740 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
742 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
743 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
744 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
745 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
746 allowedCharacters
= CFSTR("abcdsefw2345");
748 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
749 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
750 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsAtLeastNSpecificCharacters
, CFSTR("hehe"));
751 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
752 ok(password
== NULL
);
756 CFRelease(passwordRequirements
);
759 CFRelease(allowedCharacters
);
761 //pass in wrong type for no more than
765 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
766 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
768 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
769 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
770 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
771 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
772 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
774 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
775 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
776 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
777 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
778 allowedCharacters
= CFSTR("abcdsefw2345");
780 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
781 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
782 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNSpecificCharacters
, CFSTR("hehe"));
784 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
785 ok(password
== NULL
);
789 CFRelease(passwordRequirements
);
792 CFRelease(allowedCharacters
);
794 //pass in wrong disallowed characters
798 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
799 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
801 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
802 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
803 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
804 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
805 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
807 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
808 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
809 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
810 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
811 allowedCharacters
= CFSTR("abcdsefw2345");
813 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
814 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
815 CFDictionaryAddValue(passwordRequirements
, kSecPasswordDisallowedCharacters
, requiredCharacterSets
);
817 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
818 ok(password
== NULL
);
822 CFRelease(passwordRequirements
);
825 CFRelease(allowedCharacters
);
827 //pass in wrong type for no more than's dictionary
831 minRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &min
);
832 maxRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &max
);
834 CFMutableDictionaryRef wrongCount
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
835 CFDictionaryAddValue(wrongCount
, kSecPasswordCharacters
, CFSTR("lkj"));
836 CFDictionaryAddValue(wrongCount
, kSecPasswordCharacterCount
, CFSTR("sdf"));
838 passwordRequirements
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
839 requiredCharacterSets
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
840 CFArrayAppendValue(requiredCharacterSets
, uppercaseLetterCharacterSet
);
841 CFArrayAppendValue(requiredCharacterSets
, lowercaseLetterCharacterSet
);
842 CFArrayAppendValue(requiredCharacterSets
, decimalDigitCharacterSet
);
844 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
845 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, requiredCharacterSets
);
846 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMinLengthKey
, minRef
);
847 CFDictionaryAddValue(passwordRequirements
, kSecPasswordMaxLengthKey
, maxRef
);
848 allowedCharacters
= CFSTR("abcdsefw2345");
850 CFDictionaryAddValue(passwordRequirements
, kSecPasswordAllowedCharactersKey
, allowedCharacters
);
851 CFDictionaryAddValue(passwordRequirements
, kSecPasswordRequiredCharactersKey
, minRef
);
852 CFDictionaryAddValue(passwordRequirements
, kSecPasswordContainsNoMoreThanNSpecificCharacters
, wrongCount
);
854 password
= SecPasswordGenerate(kSecPasswordTypeWifi
, &error
, passwordRequirements
);
855 ok(password
== NULL
);
859 CFRelease(wrongCount
);
860 CFRelease(passwordRequirements
);
863 CFRelease(allowedCharacters
);
865 password
= CFSTR("Apple1?");
866 isnt(true, SecPasswordIsPasswordWeak(password
));
869 password
= CFSTR("Singhal190");
870 isnt(true, SecPasswordIsPasswordWeak(password
));
873 password
= CFSTR("1Hollow2");
874 isnt(true, SecPasswordIsPasswordWeak(password
));
877 password
= CFSTR("1Hollow/");
878 isnt(true, SecPasswordIsPasswordWeak(password
));
881 password
= CFSTR("baj/paj1");
882 isnt(true, SecPasswordIsPasswordWeak(password
));
885 password
= CFSTR("Zaxs1009?");
886 isnt(true, SecPasswordIsPasswordWeak(password
));
889 password
= CFSTR("6666");
890 isnt(false, SecPasswordIsPasswordWeak(password
));
893 password
= CFSTR("123456");
894 isnt(false, SecPasswordIsPasswordWeak(password
));
897 password
= CFSTR("654321");
898 isnt(false, SecPasswordIsPasswordWeak(password
));
901 password
= CFSTR("A");
902 isnt(false, SecPasswordIsPasswordWeak(password
));
905 password
= CFSTR("password");
906 isnt(true, SecPasswordIsPasswordWeak(password
));
909 password
= CFSTR("password1");
910 isnt(true, SecPasswordIsPasswordWeak(password
));
913 password
= CFSTR("P@ssw0rd");
914 isnt(true, SecPasswordIsPasswordWeak(password
));
917 password
= CFSTR("facebook!{}");
918 isnt(true, SecPasswordIsPasswordWeak(password
));
921 password
= CFSTR("12345678");
922 isnt(false, SecPasswordIsPasswordWeak(password
));
926 int si_73_secpasswordgenerate(int argc
, char *const *argv
)