]> git.saurik.com Git - apple/security.git/blob - RegressionTests/manifeststresstest/mark.m
Security-59306.41.2.tar.gz
[apple/security.git] / RegressionTests / manifeststresstest / mark.m
1 //
2 // mark.m
3 // Security
4 //
5 // Created by Ben Williamson on 6/2/17.
6 //
7 //
8
9 #import "mark.h"
10 #import "Keychain.h"
11
12 NSDictionary<NSString *, NSString *> *markForIdent(NSString *ident)
13 {
14 return @{
15 [NSString stringWithFormat:@"mark-%@-a", ident]: @"I",
16 [NSString stringWithFormat:@"mark-%@-b", ident]: @"am",
17 [NSString stringWithFormat:@"mark-%@-c", ident]: @"done.",
18 };
19 }
20
21 NSDictionary<NSString *, NSString *> *updateForIdent(NSString *ident)
22 {
23 return @{
24 [NSString stringWithFormat:@"mark-%@-a", ident]: @"Updated,",
25 [NSString stringWithFormat:@"mark-%@-b", ident]: @"I",
26 [NSString stringWithFormat:@"mark-%@-c", ident]: @"am.",
27 };
28 }
29
30
31 void writeMark(NSString *ident, NSString *view)
32 {
33 Keychain *keychain = [[Keychain alloc] init];
34 NSDictionary<NSString *, NSString *> *dict = markForIdent(ident);
35 [dict enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {
36 NSLog(@"Writing mark in %@: %@ = %@", view, name, value);
37 OSStatus status = [keychain addItem:name value:value view:view];
38 switch (status) {
39 case errSecSuccess:
40 break;
41 case errSecDuplicateItem:
42 NSLog(@"(mark was already there, fine)");
43 break;
44 default:
45 NSLog(@"Error writing mark %@: %d", name, (int)status);
46 exit(1);
47 }
48 }];
49 }
50
51 void deleteMark(NSString *ident)
52 {
53 Keychain *keychain = [[Keychain alloc] init];
54 NSDictionary<NSString *, NSString *> *dict = markForIdent(ident);
55 [dict enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {
56 NSLog(@"Deleting mark: %@", name);
57 [keychain deleteItemWithName:name];
58 }];
59 }
60
61 void updateMark(NSString *ident)
62 {
63 Keychain *keychain = [[Keychain alloc] init];
64 NSDictionary<NSString *, NSString *> *dict = updateForIdent(ident);
65 [dict enumerateKeysAndObjectsUsingBlock:^(NSString *name, NSString *value, BOOL *stop) {
66 NSLog(@"Updating mark: %@ = %@", name, value);
67 OSStatus status = [keychain updateItemWithName:name newValue:value];
68 switch (status) {
69 case errSecSuccess:
70 break;
71 case errSecDuplicateItem:
72 NSLog(@"(updated mark was already there, fine)");
73 break;
74 default:
75 NSLog(@"Error updating mark %@: %d", name, (int)status);
76 exit(1);
77 }
78 }];
79 }
80
81 static BOOL verifyMarksGeneric(NSArray<NSString *> *idents, BOOL updated)
82 {
83 Keychain *keychain = [[Keychain alloc] init];
84
85 NSMutableDictionary<NSString *, NSString *> *expected = [NSMutableDictionary dictionary];
86 for (NSString *ident in idents) {
87 NSDictionary<NSString *, NSString *> *mark = nil;
88 if (!updated) {
89 mark = markForIdent(ident);
90 } else {
91 mark = updateForIdent(ident);
92 }
93 [expected addEntriesFromDictionary:mark];
94 }
95
96 NSDictionary<NSString *, NSArray *> *actual = [keychain getAllItems];
97 NSMutableDictionary<NSString *, NSString *> *actualNoPRefs = [NSMutableDictionary dictionary];
98 for (NSString *name in actual) {
99 actualNoPRefs[name] = actual[name][1];
100 }
101 NSLog(@"verifyMarks - getAllItems got %lu items", (unsigned long)actual.count);
102
103 if ([actualNoPRefs isEqualToDictionary:expected]) {
104 NSLog(@"Verify passed");
105 return YES;
106 }
107 NSLog(@"Verify failed for idents %@", idents);
108
109 for (NSString *name in actual) {
110 if (!expected[name]) {
111 NSLog(@"- unexpected item %@ %@ = %@", actual[name][0], name, actual[name][1]);
112 }
113 }
114 for (NSString *name in expected) {
115 if (!actual[name]) {
116 NSLog(@"- missing item %@", name);
117 continue;
118 }
119 if (![actual[name][1] isEqualToString:expected[name]]) {
120 NSLog(@"- incorrect item %@ %@ = %@ should be %@", actual[name][0], name, actual[name][1], expected[name]);
121 }
122 }
123 return NO;
124 }
125
126 BOOL verifyMarks(NSArray<NSString *> *idents)
127 {
128 return verifyMarksGeneric(idents, NO);
129 }
130
131 BOOL verifyUpdateMarks(NSArray<NSString *> *idents)
132 {
133 return verifyMarksGeneric(idents, YES);
134 }