5 // Created by Ben Williamson on 6/2/17.
12 NSDictionary<NSString *, NSString *> *markForIdent(NSString *ident)
15 [NSString stringWithFormat:@"mark-%@-a", ident]: @"I",
16 [NSString stringWithFormat:@"mark-%@-b", ident]: @"am",
17 [NSString stringWithFormat:@"mark-%@-c", ident]: @"done.",
21 NSDictionary<NSString *, NSString *> *updateForIdent(NSString *ident)
24 [NSString stringWithFormat:@"mark-%@-a", ident]: @"Updated,",
25 [NSString stringWithFormat:@"mark-%@-b", ident]: @"I",
26 [NSString stringWithFormat:@"mark-%@-c", ident]: @"am.",
31 void writeMark(NSString *ident, NSString *view)
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];
41 case errSecDuplicateItem:
42 NSLog(@"(mark was already there, fine)");
45 NSLog(@"Error writing mark %@: %d", name, (int)status);
51 void deleteMark(NSString *ident)
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];
61 void updateMark(NSString *ident)
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];
71 case errSecDuplicateItem:
72 NSLog(@"(updated mark was already there, fine)");
75 NSLog(@"Error updating mark %@: %d", name, (int)status);
81 static BOOL verifyMarksGeneric(NSArray<NSString *> *idents, BOOL updated)
83 Keychain *keychain = [[Keychain alloc] init];
85 NSMutableDictionary<NSString *, NSString *> *expected = [NSMutableDictionary dictionary];
86 for (NSString *ident in idents) {
87 NSDictionary<NSString *, NSString *> *mark = nil;
89 mark = markForIdent(ident);
91 mark = updateForIdent(ident);
93 [expected addEntriesFromDictionary:mark];
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];
101 NSLog(@"verifyMarks - getAllItems got %lu items", (unsigned long)actual.count);
103 if ([actualNoPRefs isEqualToDictionary:expected]) {
104 NSLog(@"Verify passed");
107 NSLog(@"Verify failed for idents %@", idents);
109 for (NSString *name in actual) {
110 if (!expected[name]) {
111 NSLog(@"- unexpected item %@ %@ = %@", actual[name][0], name, actual[name][1]);
114 for (NSString *name in expected) {
116 NSLog(@"- missing item %@", name);
119 if (![actual[name][1] isEqualToString:expected[name]]) {
120 NSLog(@"- incorrect item %@ %@ = %@ should be %@", actual[name][0], name, actual[name][1], expected[name]);
126 BOOL verifyMarks(NSArray<NSString *> *idents)
128 return verifyMarksGeneric(idents, NO);
131 BOOL verifyUpdateMarks(NSArray<NSString *> *idents)
133 return verifyMarksGeneric(idents, YES);