forked from breadwallet/breadwallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestSup.c
More file actions
414 lines (342 loc) · 11.4 KB
/
testSup.c
File metadata and controls
414 lines (342 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//
// testSup.c
// CoreTests
//
// Created by Ed Gamble on 1/25/19.
// Copyright © 2019 breadwallet LLC
//
#include <stdio.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <ftw.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include "support/BRFileService.h"
#include "support/BRAssert.h"
#define PTHREAD_NULL ((pthread_t) NULL)
/// MARK: - Helpers
static int _pthread_cond_timedwait_relative (pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *reltime) {
#if defined(__ANDROID__)
struct timeval t;
gettimeofday(&t, NULL);
struct timespec timeout;
timeout.tv_sec = reltime->tv_sec + t.tv_sec;
timeout.tv_nsec = reltime->tv_nsec +t.tv_usec*1000;
return pthread_cond_timedwait (cond, mutex, &timeout);
#else
return pthread_cond_timedwait_relative_np (cond, mutex, reltime);
#endif
}
/// MARK: - File Service Tests
static int
_remove(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
int rv = remove(fpath);
if (rv)
perror(fpath);
return rv;
}
static int _rmdir (char *path)
{
return nftw (path, _remove, 64, FTW_DEPTH | FTW_PHYS);
}
static int
fileServiceTestDone (char *path, int success) {
struct stat dirStat;
if (0 == stat (path, &dirStat)) _rmdir (path);
return success;
}
/// MARK: - File Service Tests
static int runSupFileServiceTests (void) {
printf ("==== SUP:FileService\n");
struct stat dirStat;
BRFileService fs;
char *path;
char *currency = "btc", *network = "mainnet";
char *type1 = "foo";
//
// Try to create a directory that is not writable; expect fileServiceCreate() to fail.
//
path = "private";
if (0 == stat (path, &dirStat)) _rmdir (path);
if (0 != mkdir (path, 0000)) return 0;
fs = fileServiceCreate(path, currency, network, NULL, NULL);
if (NULL != fs) return fileServiceTestDone(path, 0);
//
// Create a directory that is writeable; expect fileServiceCreate to succeed.
//
if (0 == stat (path, &dirStat)) _rmdir (path);
if (0 != mkdir (path, 0700)) return 0;
fs = fileServiceCreate(path, currency, network, NULL, NULL);
if (NULL == fs) return fileServiceTestDone(path, 0);
// Confirm the full path exists.
char dbpath[1024];
sprintf (dbpath, "%s/%s-%s-entities.db", path, currency, network);
if (0 != stat (dbpath, &dirStat)) return fileServiceTestDone (path, 0);
if (1 != fileServiceDefineType(fs, type1, 0, NULL, NULL, NULL, NULL))
return fileServiceTestDone (path, 0);
if (1 != fileServiceDefineCurrentVersion(fs, type1, 0))
return fileServiceTestDone (path, 0);
// Good, finally.
return fileServiceTestDone(path, 1);
}
/// MARK: - Assert Tests
#include <pthread.h>
#define DEFAULT_WORKERS (5)
#define SUP_MAIN_COUNT (3)
typedef void* (*ThreadRoutine) (void*); // pthread_create
///
/// Worker
///
/// This represents an arbitary computation. It runs in its own thread and will randomly fail.
/// It must be disconnected if an error occurs.
///
/// Multiple workers are owned by 'main'; on an error 'main' is responsible for disconnecting all
/// of main's worker.
///
typedef struct SupWorkerRecord {
pthread_t thread;
pthread_cond_t cond;
pthread_mutex_t lock;
} *SupWorker;
static SupWorker
supWorkerCreate () {
return calloc (1, sizeof (struct SupWorkerRecord));
}
/// Do the disconnect.
static void
supWorkerDisconnect (SupWorker worker, int report) {
if (report) printf ("Work (%p): Disconnect\n", worker);
pthread_cond_signal (&worker->cond);
pthread_mutex_lock(&worker->lock);
pthread_t thread = worker->thread;
worker->thread = PTHREAD_NULL;
pthread_mutex_unlock(&worker->lock);
pthread_join(thread, NULL);
}
/// Confirm a disconnect
static int
supWorkerIsConnected (SupWorker worker) {
int connected = 0;
pthread_mutex_lock(&worker->lock);
connected = PTHREAD_NULL != worker->thread;
pthread_mutex_unlock(&worker->lock);
return connected;
}
/// Release by disconnecting, then freeing
static void
supWorkerRelease (SupWorker worker) {
printf ("Work (%p): Release\n", worker);
supWorkerDisconnect(worker, 0);
free (worker);
}
static void *
supWorkerThread (SupWorker worker) {
struct timespec timeout = { 1, 0 }; // 1 second
printf ("Work (%p): Run\n", worker);
pthread_mutex_lock(&worker->lock);
while (1) {
switch (_pthread_cond_timedwait_relative (&worker->cond, &worker->lock, &timeout)) {
case ETIMEDOUT:
if (0 == arc4random_uniform (10 * DEFAULT_WORKERS)) {
printf ("Work (%p): Fail\n", worker);
pthread_mutex_unlock(&worker->lock);
BRFail();
}
break;
default:
pthread_mutex_unlock(&worker->lock);
pthread_exit(NULL);
}
}
}
/// Connet by spawning the thread.
static void
supWorkerConnect (SupWorker worker) {
worker->cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
worker->lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
{
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setstacksize (&attr, 1024 * 1024);
pthread_create(&worker->thread, &attr, (ThreadRoutine) supWorkerThread, worker);
pthread_attr_destroy(&attr);
}
}
///
/// Main
///
/// Manages a number of workers and does its own computation. This is will have an 'Assert
/// Recovery' which will a) disconnect the workers and b) disconnect itself.
///
typedef struct SupMainRecord {
pthread_t thread;
pthread_mutex_t lock;
pthread_cond_t cond;
SupWorker workers[DEFAULT_WORKERS];
} *SupMain;
/// Create itself and workers
static SupMain
supMainCreate (void) {
SupMain main = calloc (1, sizeof (struct SupMainRecord));
main->cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
main->lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
// Create workers
for (size_t index = 0; index < DEFAULT_WORKERS; index++)
main->workers[index] = supWorkerCreate();
return main;
}
/// Release each worker, then itself
static void
supMainRelease (SupMain main) {
printf ("Main (%p): Release Workers\n", main);
for (size_t index = 0; index < DEFAULT_WORKERS; index++) {
supWorkerRelease(main->workers[index]);
main->workers[index] = NULL;
}
printf ("Main (%p): Release Self\n", main);
free (main);
}
static void
supMainConnect (SupMain main);
/// Disconnect the workers (waiting for each) and then disconect and wait on itself.
static void
supMainDisconnect (SupMain main) {
pthread_mutex_lock (&main->lock);
printf ("Main (%p): Disconnect Workers\n", main);
for (size_t index = 0; index < DEFAULT_WORKERS; index++)
supWorkerDisconnect (main->workers[index], 1);
printf ("Main (%p): Disconnect Self\n", main);
if (PTHREAD_NULL != main->thread) {
pthread_cond_signal(&main->cond);
pthread_mutex_unlock (&main->lock);
pthread_join(main->thread, NULL);
main->thread = PTHREAD_NULL;
}
else pthread_mutex_unlock (&main->lock);
}
static int
supMainIsConnected (SupMain main) {
int connected;
pthread_mutex_lock (&main->lock);
connected = PTHREAD_NULL != main->thread;
pthread_mutex_unlock (&main->lock);
return connected;
}
/// Start the workers; do our own work
static void
supMainDoWork (SupMain main) {
printf ("Main (%p): Workers\n", main);
for (size_t index = 0; index < DEFAULT_WORKERS; index++)
supWorkerConnect (main->workers[index]);
// Do our own work.
pthread_mutex_lock(&main->lock);
pthread_cond_wait(&main->cond, &main->lock);
pthread_mutex_unlock(&main->lock);
}
/// Recover by disconnecting ourself and our workers
static void
supMainRecovery (SupMain main) {
printf ("Main (%p): Recover\n", main);
supMainDisconnect(main);
}
/// Install the recovery; then do work.
static void *
supMainThread (SupMain main) {
BRAssertDefineRecovery (main, (BRAssertRecoveryHandler) supMainRecovery);
printf ("Main (%p): Job\n", main);
supMainDoWork(main);
// BRAssertRemoveRecovery(main);
pthread_exit(NULL);
}
/// Spawn the thread to do work.
static void
supMainConnect (SupMain main) {
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setstacksize (&attr, 1024 * 1024);
pthread_create(&main->thread, &attr, (ThreadRoutine) supMainThread, main);
pthread_attr_destroy(&attr);
}
pthread_cond_t done_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t done_lock = PTHREAD_MUTEX_INITIALIZER;
/// Test is complete if all workers and each main is no longer connected
static int
supConfirmComplete (SupMain *mains) {
// Recovery is complete.
for (size_t m = 0; m < SUP_MAIN_COUNT; m++) {
for (size_t w = 0; w < DEFAULT_WORKERS; w++)
if (supWorkerIsConnected (mains[m]->workers[w])) return 0;
if (supMainIsConnected(mains[m])) return 0;
}
return 1;
}
/// Handle an assert by confirming that all mains are completed and then signal done
static void
supAssertHandler (SupMain *mains) {
supConfirmComplete(mains);
pthread_cond_signal (&done_cond);
}
/// connect all main threads and then wait until they finish. Return success if completed.
static int
supRunOnce (SupMain *mains) {
int success = 0;
struct timespec timeout = { 30, 0 };
for (size_t index = 0; index < SUP_MAIN_COUNT; index++)
supMainConnect (mains[index]);
// We wait for supAssertHandler to signal that we've handled a worker BRFail.
pthread_mutex_lock (&done_lock);
switch (_pthread_cond_timedwait_relative (&done_cond, &done_lock, &timeout)) {
case ETIMEDOUT: break;
default: success = 1; break;
}
pthread_mutex_unlock (&done_lock);
return success && supConfirmComplete(mains);
}
static int
runSupAssertTests (void) {
printf ("==== SUP: Assert\n");
SupMain mains[SUP_MAIN_COUNT];
int success = 1;
if (0 != BRAssertIsInstalled())
return 0;
BRAssertInstall(mains, (BRAssertHandler) supAssertHandler);
if (1 != BRAssertIsInstalled())
return 0;
BRAssertUninstall();
if (0 != BRAssertIsInstalled())
return 0;
if (0 != BRAssertRemoveRecovery((BRAssertRecoveryInfo) 1)) return 0;
BRAssertDefineRecovery((BRAssertRecoveryInfo) 1, NULL);
if (1 != BRAssertRemoveRecovery((BRAssertRecoveryInfo) 1)) return 0;
if (0 != BRAssertRemoveRecovery((BRAssertRecoveryInfo) 1)) return 0;
// Try once...
BRAssertInstall(mains, (BRAssertHandler) supAssertHandler);
for (size_t index = 0; index < SUP_MAIN_COUNT; index++)
mains[index] = supMainCreate();
printf ("==== SUP: Assert Run Once\n");
success = supRunOnce(mains);
// We have fully recovered
// Try again...
printf ("==== SUP:Assert Run Twice\n");
success &= supRunOnce(mains);
for (size_t index = 0; index < SUP_MAIN_COUNT; index++)
supMainRelease (mains[index]);
BRAssertUninstall();
printf ("==== SUP:Assert Done");
return success;
}
///
/// Support Tests
///
int BRRunSupTests (void) {
printf ("==== SUP\n");
int success = 1;
success &= runSupFileServiceTests();
success &= runSupAssertTests();
return success;
}