]> git.saurik.com Git - apple/security.git/blob - sec/Security/Regressions/secitem/si-73-secpasswordgenerate.c
Security-55471.tar.gz
[apple/security.git] / sec / Security / Regressions / secitem / si-73-secpasswordgenerate.c
1 //
2 // si-73-secpasswordgenerate.c
3 // sec
4 //
5
6 #include <stdio.h>
7 #include <Security/SecPasswordGenerate.h>
8 #include <utilities/SecCFRelease.h>
9 #include "Security_regressions.h"
10
11 static void tests(void)
12 {
13 CFErrorRef error = NULL;
14 CFStringRef password = NULL;
15 CFMutableArrayRef requiredCharacterSets = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
16 CFMutableDictionaryRef passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
17
18 CFCharacterSetRef uppercaseLetterCharacterSet = CFCharacterSetGetPredefined(kCFCharacterSetUppercaseLetter);
19 CFCharacterSetRef lowercaseLetterCharacterSet = CFCharacterSetGetPredefined(kCFCharacterSetLowercaseLetter);
20 CFCharacterSetRef decimalDigitCharacterSet = CFCharacterSetGetPredefined(kCFCharacterSetDecimalDigit);
21
22 CFArrayAppendValue(requiredCharacterSets, uppercaseLetterCharacterSet);
23 CFArrayAppendValue(requiredCharacterSets, lowercaseLetterCharacterSet);
24 CFArrayAppendValue(requiredCharacterSets, decimalDigitCharacterSet);
25
26 CFDictionaryAddValue(passwordRequirements, kSecPasswordDefaultForType, CFSTR("true"));
27
28 //test default PIN
29 password = SecPasswordGenerate(kSecPasswordTypePIN, &error, passwordRequirements);
30 isnt(password, NULL);
31 ok(error == NULL);
32 CFReleaseNull(password);
33 error = NULL;
34
35 //test default icloud recovery code
36 password = SecPasswordGenerate(kSecPasswordTypeiCloudRecovery, &error, passwordRequirements);
37 isnt(password, NULL);
38 ok(error == NULL);
39 CFReleaseNull(password);
40 error = NULL;
41
42 //test default wifi
43 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
44 CFDictionaryAddValue(passwordRequirements, kSecPasswordDefaultForType, CFSTR("true"));
45 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
46 isnt(password, NULL);
47 ok(error == NULL);
48 CFReleaseNull(password);
49 error = NULL;
50 CFRelease(passwordRequirements);
51
52 //test default safari
53 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
54 CFDictionaryAddValue(passwordRequirements, kSecPasswordDefaultForType, CFSTR("true"));
55 password = SecPasswordGenerate(kSecPasswordTypeSafari, &error, passwordRequirements);
56 isnt(password, NULL);
57 ok(error == NULL);
58
59 error = NULL;
60 CFReleaseNull(password);
61 CFRelease(passwordRequirements);
62
63 //test icloud recovery code generation
64 password = SecPasswordGenerate(kSecPasswordTypeiCloudRecovery, &error, NULL);
65 isnt(password, NULL);
66 ok(error == NULL);
67
68 error = NULL;
69 CFReleaseNull(password);
70
71 //dictionary setup
72 int min = 20;
73 int max = 32;
74
75 CFNumberRef minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
76 CFNumberRef maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
77
78 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
79 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
80 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
81 CFStringRef allowedCharacters = CFSTR("abcdsefw2345");
82
83 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
84 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
85
86 //test wifi code generation
87 //test with min/max in range of default
88 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
89 isnt(password, NULL);
90 ok(error == NULL);
91
92 error = NULL;
93 CFReleaseNull(password);
94 CFRelease(passwordRequirements);
95 CFRelease(minRef);
96 CFRelease(maxRef);
97 CFRelease(allowedCharacters);
98
99
100 //test with max == min
101 min = 24;
102 max = 24;
103
104 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
105 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
106
107 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
108 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
109 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
110 allowedCharacters = CFSTR("abcdsefw2345");
111
112 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
113 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
114 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
115 isnt(password, NULL);
116 ok(error == NULL);
117
118 error = NULL;
119 CFReleaseNull(password);
120 CFRelease(passwordRequirements);
121 CFRelease(minRef);
122 CFRelease(maxRef);
123 CFRelease(allowedCharacters);
124
125 passwordRequirements = NULL;
126
127 //test disallowed characters
128 min = 24;
129 max = 56;
130
131 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
132 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
133
134 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
135 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
136 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
137 allowedCharacters = CFSTR("abcdefghijklmnopqrstuvwxyz0123456789");
138
139 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
140 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
141 CFDictionaryAddValue(passwordRequirements, kSecPasswordDisallowedCharacters, CFSTR("aidfl"));
142
143 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
144 isnt(password, NULL);
145 ok(error == NULL);
146
147 error = NULL;
148 CFReleaseNull(password);
149 CFRelease(passwordRequirements);
150 CFRelease(minRef);
151 CFRelease(maxRef);
152 CFRelease(allowedCharacters);
153 passwordRequirements = NULL;
154
155 //test can't start with characters
156 min = 24;
157 max = 56;
158
159 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
160 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
161
162 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
163 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
164 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
165 allowedCharacters = CFSTR("diujk");
166
167 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
168 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
169 CFDictionaryAddValue(passwordRequirements, kSecPasswordCantStartWithChars, CFSTR("d"));
170
171 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
172 isnt(password, NULL);
173 ok(error == NULL);
174
175 error = NULL;
176 CFReleaseNull(password);
177 CFRelease(passwordRequirements);
178 CFRelease(minRef);
179 CFRelease(maxRef);
180 CFRelease(allowedCharacters);
181 passwordRequirements = NULL;
182
183
184 //test can't end with characters
185 min = 24;
186 max = 56;
187
188 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
189 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
190
191 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
192 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
193 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
194 allowedCharacters = CFSTR("diujk89");
195
196 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
197 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
198 CFDictionaryAddValue(passwordRequirements, kSecPasswordCantEndWithChars, CFSTR("d"));
199
200 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
201 isnt(password, NULL);
202 ok(error == NULL);
203
204 error = NULL;
205 CFReleaseNull(password);
206 CFRelease(passwordRequirements);
207 CFRelease(minRef);
208 CFRelease(maxRef);
209 CFRelease(allowedCharacters);
210 passwordRequirements = NULL;
211
212
213 //test 4 digit pin generation
214 for(int i =0 ; i< 100; i++){
215 password = SecPasswordGenerate(kSecPasswordTypePIN, &error, passwordRequirements);
216 isnt(password, NULL);
217 ok(error == NULL);
218
219 error = NULL;
220 CFReleaseNull(password);
221 }
222
223 //test 6 digit pin
224 min = 4;
225 max = 6;
226
227 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
228 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
229
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);
235 ok(error == NULL);
236
237 error = NULL;
238 CFReleaseNull(password);
239 CFRelease(minRef);
240 CFRelease(maxRef);
241 CFRelease(passwordRequirements);
242
243 //test 5 digit pin
244 min = 4;
245 max = 5;
246
247 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
248 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
249
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);
255 ok(error == NULL);
256
257 error = NULL;
258 CFReleaseNull(password);
259 CFRelease(minRef);
260 CFRelease(maxRef);
261 CFRelease(passwordRequirements);
262 //test safari password
263 min = 20;
264 max = 32;
265
266 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
267 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
268
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);
277
278 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
279 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
280
281 password = SecPasswordGenerate(kSecPasswordTypeSafari, &error, passwordRequirements);
282 isnt(password, NULL);
283 ok(error == NULL);
284
285 error = NULL;
286 CFReleaseNull(password);
287 CFRelease(passwordRequirements);
288 CFRelease(minRef);
289 CFRelease(maxRef);
290 CFRelease(allowedCharacters);
291 CFRelease(requiredCharacterSets);
292
293 //test flexible group size and number of groups in the password
294 //test safari password
295 min = 12;
296 max = 19;
297 int groupSize = 5;
298 int numberOfGroups = 23;
299
300 CFTypeRef groupSizeRef = CFNumberCreate(NULL, kCFNumberIntType, &groupSize);
301 CFTypeRef numberOfGroupsRef = CFNumberCreate(NULL, kCFNumberIntType, &numberOfGroups);
302
303 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
304 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
305
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);
314
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("*"));
320
321 password = SecPasswordGenerate(kSecPasswordTypeSafari, &error, passwordRequirements);
322 isnt(password, NULL);
323 ok(error == NULL);
324
325 error = NULL;
326 CFReleaseNull(password);
327 CFRelease(passwordRequirements);
328 CFRelease(minRef);
329 CFRelease(maxRef);
330 CFRelease(allowedCharacters);
331 CFRelease(requiredCharacterSets);
332
333 //test at least N characters
334 //test safari password
335 min = 24;
336 max = 32;
337 int N = 5;
338
339 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
340 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
341 CFNumberRef threshold = CFNumberCreate(NULL, kCFNumberIntType, &N);
342
343 CFStringRef characters = CFSTR("ab");
344 CFMutableDictionaryRef atLeast = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
345 CFDictionaryAddValue(atLeast, kSecPasswordCharacters, characters);
346 CFDictionaryAddValue(atLeast, kSecPasswordCharacterCount, threshold);
347
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);
356
357 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
358 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
359 CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsAtLeastNSpecificCharacters, atLeast);
360
361 password = SecPasswordGenerate(kSecPasswordTypeSafari, &error, passwordRequirements);
362 isnt(password, NULL);
363 ok(error == NULL);
364
365 error = NULL;
366 CFReleaseNull(password);
367 CFRelease(passwordRequirements);
368 CFRelease(minRef);
369 CFRelease(maxRef);
370 CFRelease(allowedCharacters);
371 CFRelease(requiredCharacterSets);
372
373 //test no More than N characters
374 //test safari password
375 min = 24;
376 max = 32;
377 N = 5;
378
379
380 threshold = CFNumberCreate(NULL, kCFNumberIntType, &N);
381 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
382 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
383
384 CFMutableDictionaryRef noMoreThan = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
385 CFStringRef noMore = CFSTR("ab");
386 CFDictionaryAddValue(noMoreThan, kSecPasswordCharacters, noMore);
387 CFDictionaryAddValue(noMoreThan, kSecPasswordCharacterCount, threshold);
388
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);
397
398 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
399 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
400 CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsNoMoreThanNSpecificCharacters, noMoreThan);
401
402 password = SecPasswordGenerate(kSecPasswordTypeSafari, &error, passwordRequirements);
403 isnt(password, NULL);
404 ok(error == NULL);
405
406 error = NULL;
407 CFReleaseNull(password);
408 CFRelease(passwordRequirements);
409 CFRelease(minRef);
410 CFRelease(maxRef);
411 CFRelease(allowedCharacters);
412 CFRelease(requiredCharacterSets);
413
414 //test identical character threshold
415 //test safari password
416 min = 12;
417 max = 19;
418 N = 2;
419
420 threshold = CFNumberCreate(NULL, kCFNumberIntType, &N);
421 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
422 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
423
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);
432
433 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
434 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
435 CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsNoMoreThanNConsecutiveIdenticalCharacters, threshold);
436
437 password = SecPasswordGenerate(kSecPasswordTypeSafari, &error, passwordRequirements);
438 isnt(password, NULL);
439 ok(error == NULL);
440
441 error = NULL;
442 CFReleaseNull(password);
443 CFRelease(passwordRequirements);
444 CFRelease(minRef);
445 CFRelease(maxRef);
446 CFRelease(allowedCharacters);
447 CFRelease(requiredCharacterSets);
448
449 /////////////////now test all the error cases
450 //test with no required characters
451
452 min = 24;
453 max = 32;
454
455 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
456 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
457 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
458
459 allowedCharacters = CFSTR("abcdsefw2345");
460 requiredCharacterSets = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
461
462 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
463 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
464 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
465 ok(password == NULL);
466 ok(error != NULL);
467
468 CFRelease(error);
469 error = NULL;
470 CFRelease(minRef);
471 CFRelease(maxRef);
472 CFRelease(allowedCharacters);
473 CFRelease(passwordRequirements);
474
475 //test with no allowed characters
476 min = 24;
477 max = 32;
478
479 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
480 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
481
482 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
483 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
484 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
485 allowedCharacters = CFSTR("");
486
487 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
488 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
489 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
490 ok(password == NULL);
491 ok(error != NULL);
492
493 CFRelease(error);
494 error = NULL;
495 CFRelease(minRef);
496 CFRelease(maxRef);
497 CFRelease(passwordRequirements);
498
499 //test with min > max
500 min = 32;
501 max = 20;
502
503 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
504 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
505
506 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
507
508 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
509 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
510 allowedCharacters = CFSTR("abcdsefw2345");
511
512 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
513 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
514 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
515 ok(password == NULL);
516 ok(error != NULL);
517
518 error = NULL;
519 CFRelease(minRef);
520 CFRelease(maxRef);
521 CFRelease(allowedCharacters);
522 CFRelease(passwordRequirements);
523
524 //test by ommitting dictionary parameters
525
526 //omit max length
527 min = 20;
528 max = 32;
529
530 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
531 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
532
533 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
534
535 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
536 allowedCharacters = CFSTR("abcdsefw2345");
537
538 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
539 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
540 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
541 ok(password == NULL);
542 ok(error != NULL);
543
544 error = NULL;
545 CFRelease(minRef);
546 CFRelease(maxRef);
547 CFRelease(allowedCharacters);
548 CFRelease(passwordRequirements);
549
550 //omit min length
551 min = 20;
552 max = 32;
553
554 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
555 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
556
557 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
558
559 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
560 allowedCharacters = CFSTR("abcdsefw2345");
561
562 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
563 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
564 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
565 ok(password == NULL);
566 ok(error != NULL);
567
568 error = NULL;
569 CFRelease(minRef);
570 CFRelease(maxRef);
571 CFRelease(allowedCharacters);
572 CFRelease(passwordRequirements);
573
574 //omit allowed characters
575 min = 20;
576 max = 32;
577
578 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
579 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
580
581 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
582
583 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
584 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
585 allowedCharacters = CFSTR("abcdsefw2345");
586
587 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
588 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
589 ok(password == NULL);
590 ok(error != NULL);
591
592 error = NULL;
593 CFRelease(minRef);
594 CFRelease(maxRef);
595 CFRelease(allowedCharacters);
596 CFRelease(passwordRequirements);
597
598 //omit required characters
599 min = 20;
600 max = 32;
601
602 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
603 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
604
605 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
606
607 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
608 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
609 allowedCharacters = CFSTR("abcdsefw2345");
610
611 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
612 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
613 ok(password == NULL);
614 ok(error != NULL);
615
616 error = NULL;
617 CFRelease(minRef);
618 CFRelease(maxRef);
619 CFRelease(allowedCharacters);
620 CFRelease(passwordRequirements);
621
622 //pass in wrong type for min
623 min = 20;
624 max = 32;
625
626 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
627 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
628
629 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
630
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);
638
639 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
640 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
641
642 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
643 ok(password == NULL);
644 ok(error != NULL);
645
646 error = NULL;
647 CFRelease(passwordRequirements);
648 CFRelease(minRef);
649 CFRelease(maxRef);
650 CFRelease(allowedCharacters);
651
652 //pass in wrong type for max
653 min = 20;
654 max = 32;
655
656 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
657 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
658
659 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
660
661 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
662 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, allowedCharacters);
663 allowedCharacters = CFSTR("abcdsefw2345");
664
665 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
666 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
667
668 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
669 ok(password == NULL);
670 ok(error != NULL);
671
672 error = NULL;
673 CFRelease(passwordRequirements);
674 CFRelease(minRef);
675 CFRelease(maxRef);
676 CFRelease(allowedCharacters);
677
678 //pass in wrong type for allowed
679 min = 20;
680 max = 32;
681
682 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
683 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
684
685 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
686
687 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
688 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
689 allowedCharacters = CFSTR("abcdsefw2345");
690
691 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, minRef);
692 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
693
694 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
695 ok(password == NULL);
696 ok(error != NULL);
697
698 error = NULL;
699 CFRelease(passwordRequirements);
700 CFRelease(minRef);
701 CFRelease(maxRef);
702 CFRelease(allowedCharacters);
703
704 //pass in wrong type for required
705 min = 20;
706 max = 32;
707
708 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
709 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
710
711 passwordRequirements = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
712
713 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
714 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
715 allowedCharacters = CFSTR("abcdsefw2345");
716
717 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
718 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef);
719 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
720 ok(password == NULL);
721 ok(error != NULL);
722
723 error = NULL;
724 CFRelease(passwordRequirements);
725 CFRelease(minRef);
726 CFRelease(maxRef);
727 CFRelease(allowedCharacters);
728
729 //pass in wrong type for no less than
730 min = 20;
731 max = 32;
732
733 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
734 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
735
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);
741
742 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
743 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
744 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
745 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
746 allowedCharacters = CFSTR("abcdsefw2345");
747
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);
753 ok(error != NULL);
754
755 error = NULL;
756 CFRelease(passwordRequirements);
757 CFRelease(minRef);
758 CFRelease(maxRef);
759 CFRelease(allowedCharacters);
760
761 //pass in wrong type for no more than
762 min = 20;
763 max = 32;
764
765 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
766 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
767
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);
773
774 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
775 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
776 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
777 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
778 allowedCharacters = CFSTR("abcdsefw2345");
779
780 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
781 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef);
782 CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsNoMoreThanNSpecificCharacters, CFSTR("hehe"));
783
784 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
785 ok(password == NULL);
786 ok(error != NULL);
787
788 error = NULL;
789 CFRelease(passwordRequirements);
790 CFRelease(minRef);
791 CFRelease(maxRef);
792 CFRelease(allowedCharacters);
793
794 //pass in wrong disallowed characters
795 min = 20;
796 max = 32;
797
798 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
799 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
800
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);
806
807 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
808 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
809 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
810 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
811 allowedCharacters = CFSTR("abcdsefw2345");
812
813 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
814 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef);
815 CFDictionaryAddValue(passwordRequirements, kSecPasswordDisallowedCharacters, requiredCharacterSets);
816
817 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
818 ok(password == NULL);
819 ok(error != NULL);
820
821 error = NULL;
822 CFRelease(passwordRequirements);
823 CFRelease(minRef);
824 CFRelease(maxRef);
825 CFRelease(allowedCharacters);
826
827 //pass in wrong type for no more than's dictionary
828 min = 20;
829 max = 32;
830
831 minRef = CFNumberCreate(NULL, kCFNumberIntType, &min);
832 maxRef = CFNumberCreate(NULL, kCFNumberIntType, &max);
833
834 CFMutableDictionaryRef wrongCount = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
835 CFDictionaryAddValue(wrongCount, kSecPasswordCharacters, CFSTR("lkj"));
836 CFDictionaryAddValue(wrongCount, kSecPasswordCharacterCount, CFSTR("sdf"));
837
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);
843
844 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
845 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, requiredCharacterSets);
846 CFDictionaryAddValue(passwordRequirements, kSecPasswordMinLengthKey, minRef);
847 CFDictionaryAddValue(passwordRequirements, kSecPasswordMaxLengthKey, maxRef);
848 allowedCharacters = CFSTR("abcdsefw2345");
849
850 CFDictionaryAddValue(passwordRequirements, kSecPasswordAllowedCharactersKey, allowedCharacters);
851 CFDictionaryAddValue(passwordRequirements, kSecPasswordRequiredCharactersKey, minRef);
852 CFDictionaryAddValue(passwordRequirements, kSecPasswordContainsNoMoreThanNSpecificCharacters, wrongCount);
853
854 password = SecPasswordGenerate(kSecPasswordTypeWifi, &error, passwordRequirements);
855 ok(password == NULL);
856 ok(error != NULL);
857
858 error = NULL;
859 CFRelease(wrongCount);
860 CFRelease(passwordRequirements);
861 CFRelease(minRef);
862 CFRelease(maxRef);
863 CFRelease(allowedCharacters);
864
865 password = CFSTR("Apple1?");
866 isnt(true, SecPasswordIsPasswordWeak(password));
867 CFRelease(password);
868
869 password = CFSTR("Singhal190");
870 isnt(true, SecPasswordIsPasswordWeak(password));
871 CFRelease(password);
872
873 password = CFSTR("1Hollow2");
874 isnt(true, SecPasswordIsPasswordWeak(password));
875 CFRelease(password);
876
877 password = CFSTR("1Hollow/");
878 isnt(true, SecPasswordIsPasswordWeak(password));
879 CFRelease(password);
880
881 password = CFSTR("baj/paj1");
882 isnt(true, SecPasswordIsPasswordWeak(password));
883 CFRelease(password);
884
885 password = CFSTR("Zaxs1009?");
886 isnt(true, SecPasswordIsPasswordWeak(password));
887 CFRelease(password);
888
889 password = CFSTR("6666");
890 isnt(false, SecPasswordIsPasswordWeak(password));
891 CFRelease(password);
892
893 password = CFSTR("123456");
894 isnt(false, SecPasswordIsPasswordWeak(password));
895 CFRelease(password);
896
897 password = CFSTR("654321");
898 isnt(false, SecPasswordIsPasswordWeak(password));
899 CFRelease(password);
900
901 password = CFSTR("A");
902 isnt(false, SecPasswordIsPasswordWeak(password));
903 CFRelease(password);
904
905 password = CFSTR("password");
906 isnt(true, SecPasswordIsPasswordWeak(password));
907 CFRelease(password);
908
909 password = CFSTR("password1");
910 isnt(true, SecPasswordIsPasswordWeak(password));
911 CFRelease(password);
912
913 password = CFSTR("P@ssw0rd");
914 isnt(true, SecPasswordIsPasswordWeak(password));
915 CFRelease(password);
916
917 password = CFSTR("facebook!{}");
918 isnt(true, SecPasswordIsPasswordWeak(password));
919 CFRelease(password);
920
921 password = CFSTR("12345678");
922 isnt(false, SecPasswordIsPasswordWeak(password));
923 CFRelease(password);
924 }
925
926 int si_73_secpasswordgenerate(int argc, char *const *argv)
927 {
928 plan_tests(279);
929 tests();
930
931 return 0;
932 }