summaryrefslogtreecommitdiff
path: root/egg/test-file-tracker.c
blob: 926b44852c0e2bf1b33e3eadf14f239ade201172 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* test-file-tracker.c: File tracker tests

   Copyright (C) 2007 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "egg/egg-file-tracker.h"

#include <glib/gstdio.h>

#define DATA "test-data"
#define SUBDIR "test-subdir"
#define WILDCARD "*.woo?"

typedef struct {
	EggFileTracker *the_tracker;
	gchar *test_dir;
	gchar *test_file;
	guint n_files_added;
	gchar* last_file_added;
	guint n_files_changed;
	gchar* last_file_changed;
	guint n_files_removed;
	gchar* last_file_removed;
} Test;

static void
file_added (EggFileTracker *tracker,
            const gchar *path,
            gpointer user_data)
{
	Test *test = user_data;

	g_assert ("should be a non-null path" && path != NULL);

	++test->n_files_added;
	g_free (test->last_file_added);
	test->last_file_added = g_strdup (path);
}

static void
file_changed (EggFileTracker *tracker,
              const gchar *path,
              gpointer user_data)
{
	Test *test = user_data;

	g_assert ("should be a non-null path" && path != NULL);

	++test->n_files_changed;
	g_free (test->last_file_changed);
	test->last_file_changed = g_strdup (path);
}

static void
file_removed (EggFileTracker *tracker,
              const gchar *path,
              gpointer user_data)
{
	Test *test = user_data;

	g_assert ("should be a non-null path" && path != NULL);

	++test->n_files_removed;
	g_free (test->last_file_removed);
	test->last_file_removed = g_strdup (path);
}

static void
file_reset_stats (Test *test)
{
	g_free (test->last_file_removed);
	g_free (test->last_file_added);
	g_free (test->last_file_changed);
	test->last_file_removed = test->last_file_added = test->last_file_changed = NULL;
	test->n_files_added = test->n_files_changed = test->n_files_removed = 0;
}

static void
setup (Test *test, gconstpointer unused)
{
	/* Make a test directory */
	test->test_dir = g_build_filename ("/tmp", SUBDIR, NULL);

	test->the_tracker = egg_file_tracker_new (test->test_dir, WILDCARD, NULL);
	g_signal_connect (test->the_tracker, "file-added", G_CALLBACK (file_added), test);
	g_signal_connect (test->the_tracker, "file-removed", G_CALLBACK (file_removed), test);
	g_signal_connect (test->the_tracker, "file-changed", G_CALLBACK (file_changed), test);

	/* Mtime must change so wait between tests */
	sleep (1);

	test->test_file = g_build_filename (test->test_dir, "my-file.woof", NULL);
	g_unlink (test->test_file);
}

static void
teardown (Test *test, gconstpointer unused)
{
	file_reset_stats (test);
	g_object_unref (test->the_tracker);
	g_free (test->test_dir);
	g_free (test->test_file);
}

static void
test_file_watch (Test *test, gconstpointer unused)
{
	/* A watch for an non-existant directory, should have no responses */
	egg_file_tracker_refresh (test->the_tracker, FALSE);

	g_assert_cmpint (0, ==, test->n_files_added);
	g_assert_cmpint (0, ==, test->n_files_changed);
	g_assert_cmpint (0, ==, test->n_files_removed);

	g_mkdir_with_parents (test->test_dir, 0700);

	/* Should still have no responses even though it exists */
	egg_file_tracker_refresh (test->the_tracker, FALSE);

	g_assert_cmpint (0, ==, test->n_files_added);
	g_assert_cmpint (0, ==, test->n_files_changed);
	g_assert_cmpint (0, ==, test->n_files_removed);
}

static void
test_watch_file (Test *test, gconstpointer unused)
{
	gboolean ret;

	/* Make sure things are clean */
	egg_file_tracker_refresh (test->the_tracker, FALSE);

	test->n_files_added = test->n_files_changed = test->n_files_removed = 0;
	test->last_file_added = test->last_file_changed = test->last_file_removed = 0;

	ret = g_file_set_contents (test->test_file, DATA, strlen (DATA), NULL);
	g_assert (ret);

	/* Now make sure that file is located */
	egg_file_tracker_refresh (test->the_tracker, FALSE);

	g_assert_cmpint (1, ==, test->n_files_added);
	g_assert_cmpint (0, ==, test->n_files_changed);
	g_assert_cmpint (0, ==, test->n_files_removed);

	/* The added one should match our file */
	g_assert_cmpstr (test->last_file_added, ==, test->test_file);

	file_reset_stats (test);
	sleep (1);

	/* Shouldn't find the file again */
	egg_file_tracker_refresh (test->the_tracker, FALSE);
	g_assert_cmpint (0, ==, test->n_files_added);
	g_assert_cmpint (0, ==, test->n_files_changed);
	g_assert_cmpint (0, ==, test->n_files_removed);

	/* But we should find the file if forced to */
	egg_file_tracker_refresh (test->the_tracker, TRUE);
	g_assert_cmpint (0, ==, test->n_files_added);
	g_assert_cmpint (1, ==, test->n_files_changed);
	g_assert_cmpint (0, ==, test->n_files_removed);
	g_assert_cmpstr (test->last_file_changed, ==, test->test_file);

	file_reset_stats (test);

	ret = g_file_set_contents (test->test_file, DATA, strlen (DATA), NULL);
	g_assert (ret);

	/* File was updated */
	egg_file_tracker_refresh (test->the_tracker, FALSE);
	g_assert_cmpint (0, ==, test->n_files_added);
	g_assert_cmpint (1, ==, test->n_files_changed);
	g_assert_cmpint (0, ==, test->n_files_removed);
	g_assert_cmpstr (test->last_file_changed, ==, test->test_file);

	file_reset_stats (test);
	g_unlink (test->test_file);

	/* Now file should be removed */
	egg_file_tracker_refresh (test->the_tracker, FALSE);

	g_assert_cmpint (0, ==, test->n_files_added);
	g_assert_cmpint (0, ==, test->n_files_changed);
	g_assert_cmpint (1, ==, test->n_files_removed);
	g_assert_cmpstr (test->last_file_removed, ==, test->test_file);
}

static void
test_nomatch (Test *test, gconstpointer unused)
{
	gchar *file = g_build_filename (test->test_dir, "my-file.toot", NULL);
	gboolean ret;

	/* Mtime must change so wait between tests */
	sleep (1);

	ret = g_file_set_contents (file, DATA, strlen (DATA), NULL);
	g_assert (ret);

	file_reset_stats (test);

	/* Now make sure that file is not located */
	egg_file_tracker_refresh (test->the_tracker, FALSE);

	g_assert_cmpint (0, ==, test->n_files_added);
	g_assert_cmpint (0, ==, test->n_files_changed);
	g_assert_cmpint (0, ==, test->n_files_removed);

	g_unlink (file);
	g_free (file);
}

int
main (int argc, char **argv)
{
#if !GLIB_CHECK_VERSION(2,35,0)
	g_type_init ();
#endif
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/egg/file-tracker/file_watch", Test, NULL, setup, test_file_watch, teardown);
	g_test_add ("/egg/file-tracker/watch_file", Test, NULL, setup, test_watch_file, teardown);
	g_test_add ("/egg/file-tracker/nomatch", Test, NULL, setup, test_nomatch, teardown);

	return g_test_run ();
}