aboutsummaryrefslogtreecommitdiff
path: root/tests/test-util-filemonitor.c
blob: 5d95cea5ee3571171d015f5558d1c3bf895fc6e8 (plain)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
 * Tests for util/filemonitor-*.c
 *
 * Copyright 2018 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "qemu/osdep.h"
#include "qemu/main-loop.h"
#include "qapi/error.h"
#include "qemu/filemonitor.h"

#include <utime.h>

enum {
    QFILE_MONITOR_TEST_OP_CREATE,
    QFILE_MONITOR_TEST_OP_APPEND,
    QFILE_MONITOR_TEST_OP_TRUNC,
    QFILE_MONITOR_TEST_OP_RENAME,
    QFILE_MONITOR_TEST_OP_TOUCH,
    QFILE_MONITOR_TEST_OP_UNLINK,
};

typedef struct {
    int type;
    const char *filesrc;
    const char *filedst;
} QFileMonitorTestOp;

typedef struct {
    const char *file;
} QFileMonitorTestWatch;

typedef struct {
    gsize nwatches;
    const QFileMonitorTestWatch *watches;

    gsize nops;
    const QFileMonitorTestOp *ops;
} QFileMonitorTestPlan;

typedef struct {
    int id;
    QFileMonitorEvent event;
    char *filename;
} QFileMonitorTestRecord;


typedef struct {
    QemuMutex lock;
    GList *records;
} QFileMonitorTestData;

static QemuMutex evlock;
static bool evstopping;
static bool evrunning;

/*
 * Main function for a background thread that is
 * running the event loop during the test
 */
static void *
qemu_file_monitor_test_event_loop(void *opaque G_GNUC_UNUSED)
{
    qemu_mutex_lock(&evlock);

    while (!evstopping) {
        qemu_mutex_unlock(&evlock);
        main_loop_wait(true);
        qemu_mutex_lock(&evlock);
    }

    evrunning = false;
    qemu_mutex_unlock(&evlock);
    return NULL;
}


/*
 * File monitor event handler which simply maintains
 * an ordered list of all events that it receives
 */
static void
qemu_file_monitor_test_handler(int id,
                               QFileMonitorEvent event,
                               const char *filename,
                               void *opaque)
{
    QFileMonitorTestData *data = opaque;
    QFileMonitorTestRecord *rec = g_new0(QFileMonitorTestRecord, 1);

    rec->id = id;
    rec->event = event;
    rec->filename = g_strdup(filename);

    qemu_mutex_lock(&data->lock);
    data->records = g_list_append(data->records, rec);
    qemu_mutex_unlock(&data->lock);
}


static void
qemu_file_monitor_test_record_free(QFileMonitorTestRecord *rec)
{
    g_free(rec->filename);
    g_free(rec);
}


/*
 * Get the next event record that has been received by
 * the file monitor event handler. Since events are
 * emitted in the background thread running the event
 * loop, we can't assume there is a record available
 * immediately. Thus we will sleep for upto 5 seconds
 * to wait for the event to be queued for us.
 */
static QFileMonitorTestRecord *
qemu_file_monitor_test_next_record(QFileMonitorTestData *data)
{
    GTimer *timer = g_timer_new();
    QFileMonitorTestRecord *record = NULL;
    GList *tmp;

    qemu_mutex_lock(&data->lock);
    while (!data->records && g_timer_elapsed(timer, NULL) < 5) {
        qemu_mutex_unlock(&data->lock);
        usleep(10 * 1000);
        qemu_mutex_lock(&data->lock);
    }
    if (data->records) {
        record = data->records->data;
        tmp = data->records;
        data->records = g_list_remove_link(data->records, tmp);
        g_list_free(tmp);
    }
    qemu_mutex_unlock(&data->lock);

    g_timer_destroy(timer);
    return record;
}


/*
 * Check whether the event record we retrieved matches
 * data we were expecting to see for the event
 */
static bool
qemu_file_monitor_test_expect(QFileMonitorTestData *data,
                              int id,
                              QFileMonitorEvent event,
                              const char *filename)
{
    QFileMonitorTestRecord *rec;
    bool ret = false;

    rec = qemu_file_monitor_test_next_record(data);

    if (!rec) {
        g_printerr("Missing event watch id %d event %d file %s\n",
                   id, event, filename);
        return false;
    }

    if (id != rec->id) {
        g_printerr("Expected watch id %d but got %d\n", id, rec->id);
        goto cleanup;
    }

    if (event != rec->event) {
        g_printerr("Expected event %d but got %d\n", event, rec->event);
        goto cleanup;
    }

    if (!g_str_equal(filename, rec->filename)) {
        g_printerr("Expected filename %s but got %s\n",
                   filename, rec->filename);
        goto cleanup;
    }

    ret = true;

 cleanup:
    qemu_file_monitor_test_record_free(rec);
    return ret;
}


static void
test_file_monitor_events(const void *opaque)
{
    const QFileMonitorTestPlan *plan = opaque;
    Error *local_err = NULL;
    GError *gerr = NULL;
    QFileMonitor *mon = qemu_file_monitor_new(&local_err);
    QemuThread th;
    GTimer *timer;
    gchar *dir = NULL;
    int err = -1;
    gsize i, j;
    char *pathsrc = NULL;
    char *pathdst = NULL;
    QFileMonitorTestData data;

    qemu_mutex_init(&data.lock);
    data.records = NULL;

    /*
     * The file monitor needs the main loop running in
     * order to receive events from inotify. We must
     * thus spawn a background thread to run an event
     * loop impl, while this thread triggers the
     * actual file operations we're testing
     */
    evrunning = 1;
    evstopping = 0;
    qemu_thread_create(&th, "event-loop",
                       qemu_file_monitor_test_event_loop, NULL,
                       QEMU_THREAD_JOINABLE);

    if (local_err) {
        g_printerr("File monitoring not available: %s",
                   error_get_pretty(local_err));
        error_free(local_err);
        return;
    }

    dir = g_dir_make_tmp("test-util-filemonitor-XXXXXX",
                         &gerr);
    if (!dir) {
        g_printerr("Unable to create tmp dir %s",
                   gerr->message);
        g_error_free(gerr);
        abort();
    }

    /*
     * First register all the directory / file watches
     * we're interested in seeing events against
     */
    for (i = 0; i < plan->nwatches; i++) {
        int watchid;
        watchid = qemu_file_monitor_add_watch(mon,
                                              dir,
                                              plan->watches[i].file,
                                              qemu_file_monitor_test_handler,
                                              &data,
                                              &local_err);
        if (watchid < 0) {
            g_printerr("Unable to add watch %s",
                       error_get_pretty(local_err));
            goto cleanup;
        }
    }


    /*
     * Now invoke all the file operations (create,
     * delete, rename, chmod, etc). These operations
     * will trigger the various file monitor events
     */
    for (i = 0; i < plan->nops; i++) {
        const QFileMonitorTestOp *op = &(plan->ops[i]);
        int fd;
        struct utimbuf ubuf;

        pathsrc = g_strdup_printf("%s/%s", dir, op->filesrc);
        if (op->filedst) {
            pathdst = g_strdup_printf("%s/%s", dir, op->filedst);
        }

        switch (op->type) {
        case QFILE_MONITOR_TEST_OP_CREATE:
            fd = open(pathsrc, O_WRONLY | O_CREAT, 0700);
            if (fd < 0) {
                g_printerr("Unable to create %s: %s",
                           pathsrc, strerror(errno));
                goto cleanup;
            }
            close(fd);
            break;

        case QFILE_MONITOR_TEST_OP_APPEND:
            fd = open(pathsrc, O_WRONLY | O_APPEND, 0700);
            if (fd < 0) {
                g_printerr("Unable to open %s: %s",
                           pathsrc, strerror(errno));
                goto cleanup;
            }

            if (write(fd, "Hello World", 10) != 10) {
                g_printerr("Unable to write %s: %s",
                           pathsrc, strerror(errno));
                close(fd);
                goto cleanup;
            }
            close(fd);
            break;

        case QFILE_MONITOR_TEST_OP_TRUNC:
            if (truncate(pathsrc, 4) < 0) {
                g_printerr("Unable to truncate %s: %s",
                           pathsrc, strerror(errno));
                goto cleanup;
            }
            break;

        case QFILE_MONITOR_TEST_OP_RENAME:
            if (rename(pathsrc, pathdst) < 0) {
                g_printerr("Unable to rename %s to %s: %s",
                           pathsrc, pathdst, strerror(errno));
                goto cleanup;
            }
            break;

        case QFILE_MONITOR_TEST_OP_UNLINK:
            if (unlink(pathsrc) < 0) {
                g_printerr("Unable to unlink %s: %s",
                           pathsrc, strerror(errno));
                goto cleanup;
            }
            break;

        case QFILE_MONITOR_TEST_OP_TOUCH:
            ubuf.actime = 1024;
            ubuf.modtime = 1025;
            if (utime(pathsrc, &ubuf) < 0) {
                g_printerr("Unable to touch %s: %s",
                           pathsrc, strerror(errno));
                goto cleanup;
            }
            break;

        default:
            g_assert_not_reached();
        }

        g_free(pathsrc);
        g_free(pathdst);
        pathsrc = pathdst = NULL;
    }


    /*
     * Finally validate that we have received all the events
     * we expect to see for the combination of watches and
     * file operations
     */
    for (i = 0; i < plan->nops; i++) {
        const QFileMonitorTestOp *op = &(plan->ops[i]);

        switch (op->type) {
        case QFILE_MONITOR_TEST_OP_CREATE:
            for (j = 0; j < plan->nwatches; j++) {
                if (plan->watches[j].file &&
                    !g_str_equal(plan->watches[j].file, op->filesrc))
                    continue;

                if (!qemu_file_monitor_test_expect(
                        &data, j, QFILE_MONITOR_EVENT_CREATED, op->filesrc))
                    goto cleanup;
            }
            break;

        case QFILE_MONITOR_TEST_OP_APPEND:
        case QFILE_MONITOR_TEST_OP_TRUNC:
            for (j = 0; j < plan->nwatches; j++) {
                if (plan->watches[j].file &&
                    !g_str_equal(plan->watches[j].file, op->filesrc))
                    continue;

                if (!qemu_file_monitor_test_expect(
                        &data, j, QFILE_MONITOR_EVENT_MODIFIED, op->filesrc))
                    goto cleanup;
            }
            break;

        case QFILE_MONITOR_TEST_OP_RENAME:
            for (j = 0; j < plan->nwatches; j++) {
                if (plan->watches[j].file &&
                    !g_str_equal(plan->watches[j].file, op->filesrc))
                    continue;

                if (!qemu_file_monitor_test_expect(
                        &data, j, QFILE_MONITOR_EVENT_DELETED, op->filesrc))
                    goto cleanup;
            }

            for (j = 0; j < plan->nwatches; j++) {
                if (plan->watches[j].file &&
                    !g_str_equal(plan->watches[j].file, op->filedst))
                    continue;

                if (!qemu_file_monitor_test_expect(
                        &data, j, QFILE_MONITOR_EVENT_CREATED, op->filedst))
                    goto cleanup;
            }
            break;

        case QFILE_MONITOR_TEST_OP_TOUCH:
            for (j = 0; j < plan->nwatches; j++) {
                if (plan->watches[j].file &&
                    !g_str_equal(plan->watches[j].file, op->filesrc))
                    continue;

                if (!qemu_file_monitor_test_expect(
                        &data, j, QFILE_MONITOR_EVENT_ATTRIBUTES, op->filesrc))
                    goto cleanup;
            }
            break;

        case QFILE_MONITOR_TEST_OP_UNLINK:
            for (j = 0; j < plan->nwatches; j++) {
                if (plan->watches[j].file &&
                    !g_str_equal(plan->watches[j].file, op->filesrc))
                    continue;

                if (!qemu_file_monitor_test_expect(
                        &data, j, QFILE_MONITOR_EVENT_DELETED, op->filesrc))
                    goto cleanup;
            }
            break;

        default:
            g_assert_not_reached();
        }
    }

    err = 0;

 cleanup:
    g_free(pathsrc);
    g_free(pathdst);

    qemu_mutex_lock(&evlock);
    evstopping = 1;
    timer = g_timer_new();
    while (evrunning && g_timer_elapsed(timer, NULL) < 5) {
        qemu_mutex_unlock(&evlock);
        usleep(10 * 1000);
        qemu_mutex_lock(&evlock);
    }
    qemu_mutex_unlock(&evlock);

    if (g_timer_elapsed(timer, NULL) >= 5) {
        g_printerr("Event loop failed to quit after 5 seconds\n");
    }
    g_timer_destroy(timer);

    for (i = 0; i < plan->nops; i++) {
        const QFileMonitorTestOp *op = &(plan->ops[i]);
        pathsrc = g_strdup_printf("%s/%s", dir, op->filesrc);
        unlink(pathsrc);
        g_free(pathsrc);
        if (op->filedst) {
            pathdst = g_strdup_printf("%s/%s", dir, op->filedst);
            unlink(pathdst);
            g_free(pathdst);
        }
    }

    qemu_file_monitor_free(mon);
    g_list_foreach(data.records,
                   (GFunc)qemu_file_monitor_test_record_free, NULL);
    g_list_free(data.records);
    qemu_mutex_destroy(&data.lock);
    if (dir) {
        rmdir(dir);
    }
    g_free(dir);
    g_assert(err == 0);
}


/*
 * Set of structs which define which file name patterns
 * we're trying to watch against. NULL, means all files
 * in the directory
 */
static const QFileMonitorTestWatch watches_any[] = {
    { NULL },
};

static const QFileMonitorTestWatch watches_one[] = {
    { "one.txt" },
};

static const QFileMonitorTestWatch watches_two[] = {
    { "two.txt" },
};

static const QFileMonitorTestWatch watches_many[] = {
    { NULL },
    { "one.txt" },
    { "two.txt" },
};


/*
 * Various sets of file operations we're going to
 * trigger and validate events for
 */
static const QFileMonitorTestOp ops_create_one[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", }
};

static const QFileMonitorTestOp ops_delete_one[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", },
    { .type = QFILE_MONITOR_TEST_OP_UNLINK,
      .filesrc = "one.txt", }
};

static const QFileMonitorTestOp ops_create_many[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", },
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "two.txt", },
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "three.txt", }
};

static const QFileMonitorTestOp ops_rename_one[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", },
    { .type = QFILE_MONITOR_TEST_OP_RENAME,
      .filesrc = "one.txt", .filedst = "two.txt" }
};

static const QFileMonitorTestOp ops_rename_many[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", },
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "two.txt", },
    { .type = QFILE_MONITOR_TEST_OP_RENAME,
      .filesrc = "one.txt", .filedst = "two.txt" }
};

static const QFileMonitorTestOp ops_append_one[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", },
    { .type = QFILE_MONITOR_TEST_OP_APPEND,
      .filesrc = "one.txt", },
};

static const QFileMonitorTestOp ops_trunc_one[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", },
    { .type = QFILE_MONITOR_TEST_OP_TRUNC,
      .filesrc = "one.txt", },
};

static const QFileMonitorTestOp ops_touch_one[] = {
    { .type = QFILE_MONITOR_TEST_OP_CREATE,
      .filesrc = "one.txt", },
    { .type = QFILE_MONITOR_TEST_OP_TOUCH,
      .filesrc = "one.txt", },
};


/*
 * No we define data sets for the combinatorial
 * expansion of file watches and operation sets
 */
#define PLAN_DATA(o, w) \
    static const QFileMonitorTestPlan plan_ ## o ## _ ## w = { \
        .nops = G_N_ELEMENTS(ops_ ##o), \
        .ops = ops_ ##o, \
        .nwatches = G_N_ELEMENTS(watches_ ##w), \
        .watches = watches_ ## w, \
    }

PLAN_DATA(create_one, any);
PLAN_DATA(create_one, one);
PLAN_DATA(create_one, two);
PLAN_DATA(create_one, many);

PLAN_DATA(delete_one, any);
PLAN_DATA(delete_one, one);
PLAN_DATA(delete_one, two);
PLAN_DATA(delete_one, many);

PLAN_DATA(create_many, any);
PLAN_DATA(create_many, one);
PLAN_DATA(create_many, two);
PLAN_DATA(create_many, many);

PLAN_DATA(rename_one, any);
PLAN_DATA(rename_one, one);
PLAN_DATA(rename_one, two);
PLAN_DATA(rename_one, many);

PLAN_DATA(rename_many, any);
PLAN_DATA(rename_many, one);
PLAN_DATA(rename_many, two);
PLAN_DATA(rename_many, many);

PLAN_DATA(append_one, any);
PLAN_DATA(append_one, one);
PLAN_DATA(append_one, two);
PLAN_DATA(append_one, many);

PLAN_DATA(trunc_one, any);
PLAN_DATA(trunc_one, one);
PLAN_DATA(trunc_one, two);
PLAN_DATA(trunc_one, many);

PLAN_DATA(touch_one, any);
PLAN_DATA(touch_one, one);
PLAN_DATA(touch_one, two);
PLAN_DATA(touch_one, many);


int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);

    qemu_init_main_loop(&error_abort);

    qemu_mutex_init(&evlock);

    /*
     * Register test cases for the combinatorial
     * expansion of file watches and operation sets
     */
    #define PLAN_REGISTER(o, w)                                         \
        g_test_add_data_func("/util/filemonitor/" # o "/" # w,          \
                             &plan_ ## o ## _ ## w, test_file_monitor_events)

    PLAN_REGISTER(create_one, any);
    PLAN_REGISTER(create_one, one);
    PLAN_REGISTER(create_one, two);
    PLAN_REGISTER(create_one, many);

    PLAN_REGISTER(delete_one, any);
    PLAN_REGISTER(delete_one, one);
    PLAN_REGISTER(delete_one, two);
    PLAN_REGISTER(delete_one, many);

    PLAN_REGISTER(create_many, any);
    PLAN_REGISTER(create_many, one);
    PLAN_REGISTER(create_many, two);
    PLAN_REGISTER(create_many, many);

    PLAN_REGISTER(rename_one, any);
    PLAN_REGISTER(rename_one, one);
    PLAN_REGISTER(rename_one, two);
    PLAN_REGISTER(rename_one, many);

    PLAN_REGISTER(rename_many, any);
    PLAN_REGISTER(rename_many, one);
    PLAN_REGISTER(rename_many, two);
    PLAN_REGISTER(rename_many, many);

    PLAN_REGISTER(append_one, any);
    PLAN_REGISTER(append_one, one);
    PLAN_REGISTER(append_one, two);
    PLAN_REGISTER(append_one, many);

    PLAN_REGISTER(trunc_one, any);
    PLAN_REGISTER(trunc_one, one);
    PLAN_REGISTER(trunc_one, two);
    PLAN_REGISTER(trunc_one, many);

    PLAN_REGISTER(touch_one, any);
    PLAN_REGISTER(touch_one, one);
    PLAN_REGISTER(touch_one, two);
    PLAN_REGISTER(touch_one, many);

    return g_test_run();
}