2018-06-08 18:24:57 +02:00
|
|
|
/*
|
|
|
|
* 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 {
|
2019-03-14 15:45:08 +01:00
|
|
|
QFILE_MONITOR_TEST_OP_ADD_WATCH,
|
|
|
|
QFILE_MONITOR_TEST_OP_DEL_WATCH,
|
|
|
|
QFILE_MONITOR_TEST_OP_EVENT,
|
2018-06-08 18:24:57 +02:00
|
|
|
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,
|
2019-03-13 18:36:18 +01:00
|
|
|
QFILE_MONITOR_TEST_OP_MKDIR,
|
|
|
|
QFILE_MONITOR_TEST_OP_RMDIR,
|
2018-06-08 18:24:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type;
|
|
|
|
const char *filesrc;
|
|
|
|
const char *filedst;
|
2019-03-19 16:47:47 +01:00
|
|
|
int64_t *watchid;
|
2019-03-14 15:45:08 +01:00
|
|
|
int eventid;
|
2019-08-21 17:14:27 +02:00
|
|
|
/*
|
|
|
|
* Only valid with OP_EVENT - this event might be
|
|
|
|
* swapped with the next OP_EVENT
|
|
|
|
*/
|
|
|
|
bool swapnext;
|
2018-06-08 18:24:57 +02:00
|
|
|
} QFileMonitorTestOp;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-03-19 16:47:47 +01:00
|
|
|
int64_t id;
|
2018-06-08 18:24:57 +02:00
|
|
|
QFileMonitorEvent event;
|
|
|
|
char *filename;
|
|
|
|
} QFileMonitorTestRecord;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
QemuMutex lock;
|
|
|
|
GList *records;
|
|
|
|
} QFileMonitorTestData;
|
|
|
|
|
|
|
|
static QemuMutex evlock;
|
|
|
|
static bool evstopping;
|
|
|
|
static bool evrunning;
|
2019-03-14 15:45:08 +01:00
|
|
|
static bool debug;
|
2018-06-08 18:24:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2019-03-19 16:47:47 +01:00
|
|
|
qemu_file_monitor_test_handler(int64_t id,
|
2018-06-08 18:24:57 +02:00
|
|
|
QFileMonitorEvent event,
|
|
|
|
const char *filename,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
QFileMonitorTestData *data = opaque;
|
|
|
|
QFileMonitorTestRecord *rec = g_new0(QFileMonitorTestRecord, 1);
|
|
|
|
|
2019-08-21 17:14:27 +02:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Queue event id %" PRIx64 " event %d file %s\n",
|
|
|
|
id, event, filename);
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
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 *
|
2019-08-21 17:14:27 +02:00
|
|
|
qemu_file_monitor_test_next_record(QFileMonitorTestData *data,
|
|
|
|
QFileMonitorTestRecord *pushback)
|
2018-06-08 18:24:57 +02:00
|
|
|
{
|
|
|
|
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;
|
2019-08-21 17:14:27 +02:00
|
|
|
if (pushback) {
|
|
|
|
data->records->data = pushback;
|
|
|
|
} else {
|
|
|
|
tmp = data->records;
|
|
|
|
data->records = g_list_remove_link(data->records, tmp);
|
|
|
|
g_list_free(tmp);
|
|
|
|
}
|
|
|
|
} else if (pushback) {
|
|
|
|
qemu_file_monitor_test_record_free(pushback);
|
2018-06-08 18:24:57 +02:00
|
|
|
}
|
|
|
|
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,
|
2019-03-19 16:47:47 +01:00
|
|
|
int64_t id,
|
2018-06-08 18:24:57 +02:00
|
|
|
QFileMonitorEvent event,
|
2019-08-21 17:14:27 +02:00
|
|
|
const char *filename,
|
|
|
|
bool swapnext)
|
2018-06-08 18:24:57 +02:00
|
|
|
{
|
|
|
|
QFileMonitorTestRecord *rec;
|
|
|
|
bool ret = false;
|
|
|
|
|
2019-08-21 17:14:27 +02:00
|
|
|
rec = qemu_file_monitor_test_next_record(data, NULL);
|
2018-06-08 18:24:57 +02:00
|
|
|
|
2019-08-21 17:14:27 +02:00
|
|
|
retry:
|
2018-06-08 18:24:57 +02:00
|
|
|
if (!rec) {
|
2019-03-19 16:47:47 +01:00
|
|
|
g_printerr("Missing event watch id %" PRIx64 " event %d file %s\n",
|
2018-06-08 18:24:57 +02:00
|
|
|
id, event, filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id != rec->id) {
|
2019-08-21 17:14:27 +02:00
|
|
|
if (swapnext) {
|
|
|
|
rec = qemu_file_monitor_test_next_record(data, rec);
|
|
|
|
swapnext = false;
|
|
|
|
goto retry;
|
|
|
|
}
|
2019-03-19 16:47:47 +01:00
|
|
|
g_printerr("Expected watch id %" PRIx64 " but got %" PRIx64 "\n",
|
|
|
|
id, rec->id);
|
2018-06-08 18:24:57 +02:00
|
|
|
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
|
2019-03-14 15:45:08 +01:00
|
|
|
test_file_monitor_events(void)
|
2018-06-08 18:24:57 +02:00
|
|
|
{
|
2019-03-19 16:47:47 +01:00
|
|
|
int64_t watch0 = 0;
|
|
|
|
int64_t watch1 = 0;
|
|
|
|
int64_t watch2 = 0;
|
|
|
|
int64_t watch3 = 0;
|
|
|
|
int64_t watch4 = 0;
|
|
|
|
int64_t watch5 = 0;
|
2019-03-14 15:45:08 +01:00
|
|
|
QFileMonitorTestOp ops[] = {
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_ADD_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = NULL, .watchid = &watch0 },
|
2019-03-14 15:45:08 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_ADD_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch1 },
|
2019-03-14 15:45:08 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_ADD_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2 },
|
2019-03-14 15:45:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_CREATE,
|
|
|
|
.filesrc = "one.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch1,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_CREATE,
|
|
|
|
.filesrc = "two.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_CREATE,
|
|
|
|
.filesrc = "three.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "three.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_UNLINK,
|
|
|
|
.filesrc = "three.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "three.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_RENAME,
|
|
|
|
.filesrc = "one.txt", .filedst = "two.txt" },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch1,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_APPEND,
|
|
|
|
.filesrc = "two.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_MODIFIED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_MODIFIED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_TOUCH,
|
|
|
|
.filesrc = "two.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_ATTRIBUTES },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_ATTRIBUTES },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_DEL_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch1 },
|
2019-03-14 15:45:08 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_ADD_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch3 },
|
2019-03-14 15:45:08 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_CREATE,
|
|
|
|
.filesrc = "one.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch3,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_DEL_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch3 },
|
2019-03-14 15:45:08 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_UNLINK,
|
|
|
|
.filesrc = "one.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
|
|
|
|
|
2019-03-13 18:36:18 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_MKDIR,
|
|
|
|
.filesrc = "fish", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "fish", .watchid = &watch0,
|
2019-03-13 18:36:18 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_ADD_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "fish/", .watchid = &watch4 },
|
2019-03-13 18:36:18 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_ADD_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "fish/one.txt", .watchid = &watch5 },
|
2019-03-13 18:36:18 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_CREATE,
|
|
|
|
.filesrc = "fish/one.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch4,
|
2019-03-13 18:36:18 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch5,
|
2019-03-13 18:36:18 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_DEL_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "fish/one.txt", .watchid = &watch5 },
|
2019-03-13 18:36:18 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_RENAME,
|
|
|
|
.filesrc = "fish/one.txt", .filedst = "two.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "one.txt", .watchid = &watch4,
|
2019-03-13 18:36:18 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch0,
|
2019-03-13 18:36:18 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2,
|
2019-03-13 18:36:18 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_CREATED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_RMDIR,
|
|
|
|
.filesrc = "fish", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "", .watchid = &watch4,
|
2019-08-21 17:14:27 +02:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_IGNORED,
|
|
|
|
.swapnext = true },
|
2019-03-13 18:36:18 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "fish", .watchid = &watch0,
|
2019-03-13 18:36:18 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_DEL_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "fish", .watchid = &watch4 },
|
2019-03-13 18:36:18 +01:00
|
|
|
|
|
|
|
|
2019-03-14 15:45:08 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_UNLINK,
|
|
|
|
.filesrc = "two.txt", },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch0,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_EVENT,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2,
|
2019-03-14 15:45:08 +01:00
|
|
|
.eventid = QFILE_MONITOR_EVENT_DELETED },
|
|
|
|
|
|
|
|
|
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_DEL_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = "two.txt", .watchid = &watch2 },
|
2019-03-14 15:45:08 +01:00
|
|
|
{ .type = QFILE_MONITOR_TEST_OP_DEL_WATCH,
|
2019-03-19 16:47:47 +01:00
|
|
|
.filesrc = NULL, .watchid = &watch0 },
|
2019-03-14 15:45:08 +01:00
|
|
|
};
|
2018-06-08 18:24:57 +02:00
|
|
|
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;
|
2019-03-14 15:45:08 +01:00
|
|
|
gsize i;
|
2018-06-08 18:24:57 +02:00
|
|
|
char *pathsrc = NULL;
|
|
|
|
char *pathdst = NULL;
|
|
|
|
QFileMonitorTestData data;
|
2019-03-19 16:47:47 +01:00
|
|
|
GHashTable *ids = g_hash_table_new(g_int64_hash, g_int64_equal);
|
2019-12-04 16:46:16 +01:00
|
|
|
char *travis_arch;
|
2018-06-08 18:24:57 +02:00
|
|
|
|
|
|
|
qemu_mutex_init(&data.lock);
|
|
|
|
data.records = NULL;
|
|
|
|
|
2019-12-04 16:46:16 +01:00
|
|
|
/*
|
|
|
|
* This test does not work on Travis LXD containers since some
|
|
|
|
* syscalls are blocked in that environment.
|
|
|
|
*/
|
|
|
|
travis_arch = getenv("TRAVIS_ARCH");
|
|
|
|
if (travis_arch && !g_str_equal(travis_arch, "x86_64")) {
|
|
|
|
g_test_skip("Test does not work on non-x86 Travis containers.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-08 18:24:57 +02:00
|
|
|
/*
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-03-14 15:45:08 +01:00
|
|
|
* Run through the operation sequence validating events
|
|
|
|
* as we go
|
2018-06-08 18:24:57 +02:00
|
|
|
*/
|
2019-03-14 15:45:08 +01:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(ops); i++) {
|
|
|
|
const QFileMonitorTestOp *op = &(ops[i]);
|
2018-06-08 18:24:57 +02:00
|
|
|
int fd;
|
|
|
|
struct utimbuf ubuf;
|
2019-03-13 18:36:18 +01:00
|
|
|
char *watchdir;
|
|
|
|
const char *watchfile;
|
2018-06-08 18:24:57 +02:00
|
|
|
|
|
|
|
pathsrc = g_strdup_printf("%s/%s", dir, op->filesrc);
|
|
|
|
if (op->filedst) {
|
|
|
|
pathdst = g_strdup_printf("%s/%s", dir, op->filedst);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (op->type) {
|
2019-03-14 15:45:08 +01:00
|
|
|
case QFILE_MONITOR_TEST_OP_ADD_WATCH:
|
|
|
|
if (debug) {
|
2019-03-19 16:47:47 +01:00
|
|
|
g_printerr("Add watch %s %s\n",
|
|
|
|
dir, op->filesrc);
|
2019-03-14 15:45:08 +01:00
|
|
|
}
|
2019-03-13 18:36:18 +01:00
|
|
|
if (op->filesrc && strchr(op->filesrc, '/')) {
|
|
|
|
watchdir = g_strdup_printf("%s/%s", dir, op->filesrc);
|
|
|
|
watchfile = strrchr(watchdir, '/');
|
|
|
|
*(char *)watchfile = '\0';
|
|
|
|
watchfile++;
|
|
|
|
if (*watchfile == '\0') {
|
|
|
|
watchfile = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
watchdir = g_strdup(dir);
|
|
|
|
watchfile = op->filesrc;
|
|
|
|
}
|
2019-03-19 16:47:47 +01:00
|
|
|
*op->watchid =
|
2019-03-14 15:45:08 +01:00
|
|
|
qemu_file_monitor_add_watch(mon,
|
2019-03-13 18:36:18 +01:00
|
|
|
watchdir,
|
|
|
|
watchfile,
|
2019-03-14 15:45:08 +01:00
|
|
|
qemu_file_monitor_test_handler,
|
|
|
|
&data,
|
|
|
|
&local_err);
|
2019-03-13 18:36:18 +01:00
|
|
|
g_free(watchdir);
|
2019-03-19 16:47:47 +01:00
|
|
|
if (*op->watchid < 0) {
|
2019-03-14 15:45:08 +01:00
|
|
|
g_printerr("Unable to add watch %s",
|
|
|
|
error_get_pretty(local_err));
|
2020-06-30 11:03:35 +02:00
|
|
|
error_free(local_err);
|
2019-03-14 15:45:08 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-03-19 16:47:47 +01:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Watch ID %" PRIx64 "\n", *op->watchid);
|
|
|
|
}
|
|
|
|
if (g_hash_table_contains(ids, op->watchid)) {
|
|
|
|
g_printerr("Watch ID %" PRIx64 "already exists", *op->watchid);
|
2019-03-14 15:45:08 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-03-19 16:47:47 +01:00
|
|
|
g_hash_table_add(ids, op->watchid);
|
2019-03-14 15:45:08 +01:00
|
|
|
break;
|
|
|
|
case QFILE_MONITOR_TEST_OP_DEL_WATCH:
|
|
|
|
if (debug) {
|
2019-03-19 16:47:47 +01:00
|
|
|
g_printerr("Del watch %s %" PRIx64 "\n", dir, *op->watchid);
|
2019-03-14 15:45:08 +01:00
|
|
|
}
|
2019-03-13 18:36:18 +01:00
|
|
|
if (op->filesrc && strchr(op->filesrc, '/')) {
|
|
|
|
watchdir = g_strdup_printf("%s/%s", dir, op->filesrc);
|
|
|
|
watchfile = strrchr(watchdir, '/');
|
|
|
|
*(char *)watchfile = '\0';
|
|
|
|
} else {
|
|
|
|
watchdir = g_strdup(dir);
|
|
|
|
}
|
2019-03-19 16:47:47 +01:00
|
|
|
g_hash_table_remove(ids, op->watchid);
|
2019-03-14 15:45:08 +01:00
|
|
|
qemu_file_monitor_remove_watch(mon,
|
2019-03-13 18:36:18 +01:00
|
|
|
watchdir,
|
2019-03-19 16:47:47 +01:00
|
|
|
*op->watchid);
|
2019-03-13 18:36:18 +01:00
|
|
|
g_free(watchdir);
|
2019-03-14 15:45:08 +01:00
|
|
|
break;
|
|
|
|
case QFILE_MONITOR_TEST_OP_EVENT:
|
|
|
|
if (debug) {
|
2019-03-19 16:47:47 +01:00
|
|
|
g_printerr("Event id=%" PRIx64 " event=%d file=%s\n",
|
|
|
|
*op->watchid, op->eventid, op->filesrc);
|
2019-03-14 15:45:08 +01:00
|
|
|
}
|
2019-08-21 17:14:27 +02:00
|
|
|
if (!qemu_file_monitor_test_expect(&data, *op->watchid,
|
|
|
|
op->eventid, op->filesrc,
|
|
|
|
op->swapnext))
|
2019-03-14 15:45:08 +01:00
|
|
|
goto cleanup;
|
|
|
|
break;
|
2018-06-08 18:24:57 +02:00
|
|
|
case QFILE_MONITOR_TEST_OP_CREATE:
|
2019-03-14 15:45:08 +01:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Create %s\n", pathsrc);
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
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:
|
2019-03-14 15:45:08 +01:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Append %s\n", pathsrc);
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
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:
|
2019-03-14 15:45:08 +01:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Truncate %s\n", pathsrc);
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
if (truncate(pathsrc, 4) < 0) {
|
|
|
|
g_printerr("Unable to truncate %s: %s",
|
|
|
|
pathsrc, strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QFILE_MONITOR_TEST_OP_RENAME:
|
2019-03-14 15:45:08 +01:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Rename %s -> %s\n", pathsrc, pathdst);
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
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:
|
2019-03-14 15:45:08 +01:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Unlink %s\n", pathsrc);
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
if (unlink(pathsrc) < 0) {
|
|
|
|
g_printerr("Unable to unlink %s: %s",
|
|
|
|
pathsrc, strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QFILE_MONITOR_TEST_OP_TOUCH:
|
2019-03-14 15:45:08 +01:00
|
|
|
if (debug) {
|
|
|
|
g_printerr("Touch %s\n", pathsrc);
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
ubuf.actime = 1024;
|
|
|
|
ubuf.modtime = 1025;
|
|
|
|
if (utime(pathsrc, &ubuf) < 0) {
|
|
|
|
g_printerr("Unable to touch %s: %s",
|
|
|
|
pathsrc, strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-03-13 18:36:18 +01:00
|
|
|
case QFILE_MONITOR_TEST_OP_MKDIR:
|
|
|
|
if (debug) {
|
|
|
|
g_printerr("Mkdir %s\n", pathsrc);
|
|
|
|
}
|
|
|
|
if (mkdir(pathsrc, 0700) < 0) {
|
|
|
|
g_printerr("Unable to mkdir %s: %s",
|
|
|
|
pathsrc, strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QFILE_MONITOR_TEST_OP_RMDIR:
|
|
|
|
if (debug) {
|
|
|
|
g_printerr("Rmdir %s\n", pathsrc);
|
|
|
|
}
|
|
|
|
if (rmdir(pathsrc) < 0) {
|
|
|
|
g_printerr("Unable to rmdir %s: %s",
|
|
|
|
pathsrc, strerror(errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-06-08 18:24:57 +02:00
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(pathsrc);
|
|
|
|
g_free(pathdst);
|
|
|
|
pathsrc = pathdst = NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:47:47 +01:00
|
|
|
g_assert_cmpint(g_hash_table_size(ids), ==, 0);
|
|
|
|
|
2018-06-08 18:24:57 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
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) {
|
2019-03-14 15:45:08 +01:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(ops); i++) {
|
|
|
|
const QFileMonitorTestOp *op = &(ops[i]);
|
|
|
|
char *path = g_strdup_printf("%s/%s",
|
|
|
|
dir, op->filesrc);
|
2019-03-13 18:36:18 +01:00
|
|
|
if (op->type == QFILE_MONITOR_TEST_OP_MKDIR) {
|
|
|
|
rmdir(path);
|
|
|
|
g_free(path);
|
|
|
|
} else {
|
2019-03-14 15:45:08 +01:00
|
|
|
unlink(path);
|
|
|
|
g_free(path);
|
2019-03-13 18:36:18 +01:00
|
|
|
if (op->filedst) {
|
|
|
|
path = g_strdup_printf("%s/%s",
|
|
|
|
dir, op->filedst);
|
|
|
|
unlink(path);
|
|
|
|
g_free(path);
|
|
|
|
}
|
2019-03-14 15:45:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rmdir(dir) < 0) {
|
|
|
|
g_printerr("Failed to remove %s: %s\n",
|
|
|
|
dir, strerror(errno));
|
|
|
|
abort();
|
|
|
|
}
|
2018-06-08 18:24:57 +02:00
|
|
|
}
|
2019-03-19 16:47:47 +01:00
|
|
|
g_hash_table_unref(ids);
|
2018-06-08 18:24:57 +02:00
|
|
|
g_free(dir);
|
|
|
|
g_assert(err == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
|
|
|
qemu_init_main_loop(&error_abort);
|
|
|
|
|
|
|
|
qemu_mutex_init(&evlock);
|
|
|
|
|
2019-03-14 15:45:08 +01:00
|
|
|
debug = getenv("FILEMONITOR_DEBUG") != NULL;
|
|
|
|
g_test_add_func("/util/filemonitor", test_file_monitor_events);
|
2018-06-08 18:24:57 +02:00
|
|
|
|
|
|
|
return g_test_run();
|
|
|
|
}
|