1 #include "testsecevent.h"
4 #include <CoreFoundation/CFRunLoop.h>
8 #include "testeventqueue.h"
10 static OSStatus
test_sec_event_callback(SecKeychainEvent keychainEvent
,
11 SecKeychainCallbackInfo
*info
, void *inContext
)
13 if (keychainEvent
== kSecLockEvent
|| keychainEvent
== kSecUnlockEvent
)
19 cd
.event
= keychainEvent
;
20 cd
.version
= info
->version
;
21 cd
.itemRef
= info
->item
;
22 cd
.keychain
= info
->keychain
;
27 CFRetain (cd
.itemRef
);
32 CFRetain (cd
.keychain
);
41 OSStatus
test_sec_event_register(SecKeychainEventMask mask
)
43 return SecKeychainAddCallback(test_sec_event_callback
, mask
, NULL
);
46 OSStatus
test_sec_event_deregister()
48 return SecKeychainRemoveCallback(test_sec_event_callback
);
51 double GetCurrentTime()
54 gettimeofday(&tv
, NULL
);
56 double d
= tv
.tv_sec
+ tv
.tv_usec
/ 1000000.0;
62 /* Get the next keychain event, and optionally return the events
63 keychain, item and pid. */
64 int test_is_sec_event(SecKeychainEvent event
, SecKeychainRef
*keychain
,
65 SecKeychainItemRef
*item
, pid_t
*pid
, const char *description
,
66 const char *directive
, const char *reason
, const char *file
, unsigned line
)
68 int expected
= event
== 0 ? 0 : 1;
70 if (event
== 0) // looking for no event?
72 if (TEQ_ItemsInQueue() == 0)
75 description
, directive
, reason
, file
, line
,
81 double startTime
= GetCurrentTime();
82 double nextTime
= startTime
+ 2.0;
85 while ((currentTime
= GetCurrentTime()) < nextTime
)
87 /* Run the runloop until we get an event. Don't hang for
88 more than 0.15 seconds though. */
89 SInt32 result
= kCFRunLoopRunHandledSource
;
91 if (TEQ_ItemsInQueue () == 0) // are there events left over?
92 result
= CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 0.0, TRUE
);
96 case kCFRunLoopRunFinished
:
97 return test_ok(0, description
, directive
, reason
, file
, line
,
98 "# no sources registered in runloop\n");
99 case kCFRunLoopRunStopped
:
100 return test_ok(0, description
, directive
, reason
, file
, line
,
101 "# runloop was stopped\n");
102 case kCFRunLoopRunTimedOut
:
105 case kCFRunLoopRunHandledSource
:
112 dataInQueue
= TEQ_Dequeue (&cd
);
115 if (keychain
!= NULL
)
116 *keychain
= cd
.keychain
;
117 else if (cd
.keychain
)
118 CFRelease(cd
.keychain
);
123 CFRelease(cd
.itemRef
);
125 return test_ok(cd
.event
== event
,
126 description
, directive
, reason
, file
, line
,
128 "# expected: '%d'\n",
134 // oops, we didn't get an event, even though we were looking for one.
135 return test_ok(0, description
, directive
, reason
, file
, line
,
136 "# event expected but not received\n");
140 /* We didn't expect anything and we got one event or more. Report them */
141 dataInQueue
= TEQ_Dequeue (&cd
);
142 int unexpected_events
= 0;
145 test_diag(directive
, reason
, file
, line
,
146 " got unexpected event: '%d'", cd
.event
);
149 dataInQueue
= TEQ_Dequeue (&cd
);
151 return test_ok(unexpected_events
== 0, description
, directive
, reason
, file
, line
,
152 "# got %d unexpected events\n", unexpected_events
);
156 return test_ok(0, description
, directive
, reason
, file
, line
,
157 "# runloop returned: '%d'\n"
158 "# expected: 'kCFRunLoopRunHandledSource'\n",
164 return test_ok(0, description
, directive
, reason
,
165 file
, line
, "# runloop timed out waiting for event : %d\n",
168 return test_ok(TRUE
, description
, directive
, reason
, file
, line
,
169 "# got %d unexpected events\n", 0);