-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlfuse_internal_tests.c
1184 lines (952 loc) · 33.9 KB
/
sqlfuse_internal_tests.c
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Unit tests for sqlfuse, which run sqlfuse code in a separate thread.
//
// These tests don't allow closing and reopening of the same database file,
// because the files are opened in exclusive mode, and in this mode sqlite3
// doesn't release file locks when the database is closed.
//
// Note: these tests create temporary directories named /tmp/test-xxx-yy, and
// mounts the filesystem for testing at /tmp/test-xxx-yyy/mnt. If a test fails,
// it may leave the test directory behind. You'll have to clean it up manually.
// To unmount, use: fusermount -u /tmp/test-xxx-yyy/mnt.
//
// To use a different directory, set the TMPDIR environmental variable to the
// directory root to use instead of /tmp.
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "fuse.h"
#include "fuse_lowlevel.h"
#include "intmap.h"
#include "logging.h"
#include "sqlfs.h"
#include "sqlfuse.h"
#include "test_common.h"
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
static bool enable_fuse_debug_logging;
static char *sqlite_path_for_dump;
static char *testdir;
static char *mountpoint;
static char *database;
static struct sqlfs *sqlfs;
static struct intmap *lookups;
static struct sqlfuse_userdata sqlfuse_userdata;
static struct fuse_args fuse_args;
static struct fuse_chan *fuse_chan;
static struct fuse_session *fuse_session;
static pthread_t fuse_thread;
// Returns a temporary string formed as: mountpoint + "/" + relpath.
// The string should NOT be freed by the caller.
static char *makepath(const char *relpath) {
char *path = aprintf("%s/%s", mountpoint, relpath);
defer_free(path);
return path;
}
static bool timespec_eq(const struct timespec *a, const struct timespec *b) {
return a->tv_sec == b->tv_sec && a->tv_nsec == b->tv_nsec;
}
static bool timespec_le(const struct timespec *a, const struct timespec *b) {
return a->tv_sec < b->tv_sec || (a->tv_sec == b->tv_sec && a->tv_nsec <= b->tv_nsec);
}
static void global_setup() {
const char *tempdir = getenv("TMPDIR");
if (tempdir == NULL || *tempdir == '\0') {
tempdir = "/tmp";
}
testdir = aprintf("%s/test-%d-%06d", tempdir, (int)getpid(), (int)(time(NULL)%1000000));
mountpoint = aprintf("%s/mnt", testdir);
database = aprintf("%s/db", testdir);
CHECK(mkdir(testdir, 0700) == 0);
CHECK(mkdir(mountpoint, 0700) == 0);
CHECK(strlen(mountpoint) + 1000 < PATH_MAX);
}
static void global_teardown() {
CHECK(rmdir(mountpoint) == 0);
free(mountpoint);
mountpoint = NULL;
CHECK(rmdir(testdir) == 0);
free(testdir);
testdir = NULL;
free(database);
database = NULL;
}
static void *fuse_thread_func(void *arg) {
return (void*)(ssize_t)fuse_session_loop((struct fuse_session*)arg);
}
static void setup() {
const struct sqlfs_options options = {
.filepath = database,
.password = NULL,
.uid = geteuid(),
.gid = getegid(),
.umask = 0022};
CHECK(sqlfs_create(&options) == 0);
sqlfs = sqlfs_open(SQLFS_OPEN_MODE_READWRITE, &options);
CHECK(sqlfs);
lookups = intmap_create();
CHECK(lookups);
sqlfuse_userdata.sqlfs = sqlfs;
sqlfuse_userdata.lookups = lookups;
char *argv[2] = {"test", NULL};
memset(&fuse_args, 0, sizeof(fuse_args));
fuse_args.argc = 1;
fuse_args.argv = argv;
fuse_opt_parse(&fuse_args, NULL, NULL, NULL);
if (enable_fuse_debug_logging) {
fuse_opt_add_arg(&fuse_args, "-d");
}
fuse_chan = fuse_mount(mountpoint, &fuse_args);
CHECK(fuse_chan);
fuse_session = fuse_lowlevel_new(&fuse_args, &sqlfuse_ops, sizeof(sqlfuse_ops), &sqlfuse_userdata);
CHECK(fuse_session);
fuse_session_add_chan(fuse_session, fuse_chan);
fuse_set_signal_handlers(fuse_session);
CHECK(pthread_create(&fuse_thread, NULL /* attr */, fuse_thread_func, fuse_session /* arg */) == 0);
}
static void teardown() {
// Wait for 0.1 second for any lingering releasedir()/forget() calls.
// This is a pretty ugly hack! To do this properly, we should have a way to
// wait on the sqlfs implementation to close any open handles.
usleep(100000);
pthread_kill(fuse_thread, SIGHUP);
void *retval = NULL;
CHECK(pthread_join(fuse_thread, &retval) == 0);
EXPECT_EQ((int)(ssize_t)retval, 0);
fuse_remove_signal_handlers(fuse_session);
fuse_session_remove_chan(fuse_chan);
fuse_session_destroy(fuse_session);
fuse_unmount(mountpoint, fuse_chan);
fuse_opt_free_args(&fuse_args);
fuse_chan = NULL;
fuse_session = NULL;
memset(&fuse_args, 0, sizeof(fuse_args));
sqlfs_close(sqlfs);
sqlfuse_userdata.sqlfs = sqlfs = NULL;
EXPECT_EQ(intmap_size(lookups), 0);
intmap_destroy(lookups);
sqlfuse_userdata.lookups = lookups = NULL;
if (sqlite_path_for_dump) {
pid_t pid = fork();
if (pid < 0) {
perror("fork() failed");
} else if (pid == 0) {
close(0); // close stdin
dup2(2, 1); // redirect stdout to stderr
execlp(sqlite_path_for_dump, sqlite_path_for_dump, database, ".dump", (char*) NULL);
perror("exec() failed");
exit(1);
} else { // pid > 0
int status = 0;
waitpid(pid, &status, 0);
EXPECT_EQ(status, 0);
}
}
CHECK(unlink(database) == 0);
free_deferred();
}
static void test_basic() {
setup();
// Doesn't do anything. Just verifies the test framework works.
teardown();
}
// This test may fail if the realtime clock rewinds during the test!
static void test_rootdir() {
struct timespec time_before_setup;
CHECK(clock_gettime(CLOCK_REALTIME, &time_before_setup) == 0);
setup();
struct timespec time_after_setup;
CHECK(clock_gettime(CLOCK_REALTIME, &time_after_setup) == 0);
struct stat st = {0};
stat(mountpoint, &st);
EXPECT_EQ(stat(mountpoint, &st), 0);
EXPECT_EQ(st.st_ino, 1);
EXPECT_EQ(st.st_mode, 0755 | S_IFDIR);
EXPECT_EQ(st.st_nlink, 2);
EXPECT_EQ(st.st_uid, geteuid());
EXPECT_EQ(st.st_gid, getegid());
EXPECT_EQ(st.st_size, 0);
EXPECT_EQ(st.st_blksize, 4096);
EXPECT_EQ(st.st_blocks, 0);
EXPECT(timespec_eq(&st.st_atim, &st.st_mtim));
EXPECT(timespec_eq(&st.st_ctim, &st.st_mtim));
EXPECT(timespec_le(&time_before_setup, &st.st_mtim));
EXPECT(timespec_le(&st.st_mtim, &time_after_setup));
teardown();
}
static nlink_t nlinks(const char *path) {
struct stat st = {0};
if (stat(path, &st) != 0) {
fprintf(stderr, "stat([%s]) failed.\n", path);
test_fail();
return -1;
}
return st.st_nlink;
}
static void test_mkdir() {
setup();
EXPECT_EQ(nlinks(mountpoint), 2);
char *subdir = makepath("subdir");
EXPECT_EQ(mkdir(subdir, 0770), 0);
struct stat st = {0};
EXPECT_EQ(stat(subdir, &st), 0);
EXPECT_EQ(st.st_mode, 0750 | S_IFDIR); // umask has been applied
EXPECT_EQ(st.st_nlink, 2);
// Root directory link count has increased.
EXPECT_EQ(nlinks(mountpoint), 3);
// Cannot recreate directory that already exists.
EXPECT_EQ(mkdir(subdir, 0755), -1);
EXPECT_EQ(errno, EEXIST);
// Cannot create child directory in non-existent parent directory
EXPECT_EQ(mkdir(makepath("nonexistent/subdir"), 0770), -1);
EXPECT_EQ(errno, ENOENT);
// TODO: Maybe add test for mkdirat? What happens if parent dir is already deleted?
teardown();
}
static void test_rmdir() {
struct stat st;
setup();
char *path_foo = makepath("foo");
char *path_foo_bar = makepath("foo/bar");
char *path_foo_baz = makepath("foo/baz");
EXPECT_EQ(rmdir(path_foo), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(rmdir(path_foo_bar), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(mkdir(path_foo, 0700), 0);
EXPECT_EQ(mkdir(path_foo_bar, 0700), 0);
EXPECT_EQ(mkdir(path_foo_baz, 0700), 0);
EXPECT_EQ(nlinks(mountpoint), 3); // ".", "..", "foo"
EXPECT_EQ(nlinks(path_foo), 4); // "../foo", ".", "bar/..", "baz/.."
EXPECT_EQ(nlinks(path_foo_bar), 2); // ".", "../bar"
EXPECT_EQ(nlinks(path_foo_bar), 2); // ".", "../baz"
EXPECT_EQ(rmdir(path_foo), -1);
EXPECT_EQ(errno, ENOTEMPTY);
EXPECT_EQ(rmdir(path_foo_bar), 0);
EXPECT_EQ(stat(path_foo_bar, &st), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(nlinks(path_foo), 3);
EXPECT_EQ(rmdir(path_foo), -1);
EXPECT_EQ(errno, ENOTEMPTY);
EXPECT_EQ(rmdir(path_foo_baz), 0);
EXPECT_EQ(stat(path_foo_baz, &st), -1);
EXPECT_EQ(errno, ENOENT);
// Cannot rmdir() a file. Use unlink() instead.
EXPECT_EQ(mknod(path_foo_bar, S_IFREG | 0600, 0), 0);
EXPECT_EQ(rmdir(path_foo_bar), -1);
EXPECT_EQ(errno, ENOTDIR);
// Files do not increase parent directory's hardlink count...
EXPECT_EQ(nlinks(path_foo), 2);
// ... but they do prevent them from being deleted.
EXPECT_EQ(rmdir(path_foo), -1);
EXPECT_EQ(errno, ENOTEMPTY);
EXPECT_EQ(unlink(path_foo_bar), 0);
EXPECT_EQ(nlinks(path_foo), 2);
EXPECT_EQ(rmdir(path_foo), 0);
EXPECT_EQ(nlinks(mountpoint), 2);
teardown();
}
static void test_mknod_unlink() {
struct stat st;
setup();
const char *const path_foo = makepath("foo");
const char *const path_bar = makepath("bar");
const char *const path_baz = makepath("baz");
EXPECT_EQ(mknod(path_foo, S_IFREG | 0644, 0), 0);
EXPECT_EQ(stat(path_foo, &st), 0);
EXPECT_EQ(st.st_ino, 2);
EXPECT_EQ(st.st_mode, S_IFREG | 0644);
EXPECT_EQ(st.st_nlink, 1);
EXPECT_EQ(st.st_size, 0);
EXPECT(st.st_blksize > 0);
EXPECT_EQ(st.st_blocks, 0);
EXPECT_EQ(mknod(path_foo, S_IFREG | 0644, 0), -1);
EXPECT_EQ(errno, EEXIST);
// Invalid mode (symlinks not supported yet).
EXPECT_EQ(mknod(path_bar, S_IFLNK | 0644, 0), -1);
EXPECT_EQ(errno, EINVAL);
// Invalid mode (FIFO not supported at all).
EXPECT_EQ(mknod(path_bar, S_IFIFO | 0644, 0), -1);
EXPECT_EQ(errno, EINVAL);
EXPECT_EQ(mknod(makepath("foo/bar"), S_IFREG | 0644, 0), -1);
EXPECT_EQ(errno, ENOTDIR);
EXPECT_EQ(mknod(makepath("nonexistent/foo"), S_IFREG | 0644, 0), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(mknod(makepath("."), S_IFREG | 0644, 0), -1);
EXPECT_EQ(errno, EEXIST);
EXPECT_EQ(mknod(path_bar, S_IFREG | 0777, 0), 0);
EXPECT_EQ(stat(path_bar, &st), 0);
EXPECT_EQ(st.st_ino, 3);
EXPECT_EQ(st.st_mode, S_IFREG | 0755); // umask was applied
EXPECT_EQ(st.st_nlink, 1);
EXPECT_EQ(unlink(path_foo), 0);
EXPECT_EQ(stat(path_foo, &st), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(unlink(path_foo), -1);
EXPECT_EQ(errno, ENOENT);
// Can't unlink a directory. Use rmdir() instead.
EXPECT_EQ(mkdir(path_foo, 0775), 0);
EXPECT_EQ(unlink(path_foo), -1);
EXPECT_EQ(errno, EISDIR);
EXPECT_EQ(rmdir(path_foo), 0);
EXPECT_EQ(unlink(path_bar), 0);
// After files are deleted, their inode numbers may be re-used. Note: the fact
// that this actually happens should be considered an implementation detail.
EXPECT_EQ(mknod(path_baz, S_IFREG | 0600, 0), 0);
EXPECT_EQ(stat(path_baz, &st), 0);
EXPECT_EQ(st.st_ino, 2); // foo's old inode number
EXPECT_EQ(st.st_nlink, 1);
// TODO: readdir to verify directory contains /bar, /baz
// TODO: Maybe add test for mknodat? What happens if parent dir is already deleted?
// TODO: Maybe add test for unlinkat? What happens if parent dir is already deleted?
teardown();
}
static void verify_directory_contents(const char *relpath, struct dirent expected_entries[], int expected_size) {
DIR *dir = opendir(makepath(relpath));
struct dirent de, *de_ptr = NULL;
if (dir == NULL) {
fprintf(stderr, "Couldn't open directory [%s]!\n", relpath);
test_fail();
return;
}
for (int i = 0; i < expected_size; ++i) {
EXPECT_EQ(readdir_r(dir, &de, &de_ptr), 0);
if (de_ptr == NULL) {
fprintf(stderr, "Premature end of directory [%s] (after %d entries)\n", relpath, i);
closedir(dir);
return;
}
EXPECT(de_ptr == &de);
if (expected_entries[i].d_ino != de.d_ino) {
fprintf(stderr, "Entry %d in %s: expected ino %lld, read ino %lld\n",
i, relpath, (long long)expected_entries[i].d_ino, (long long)de.d_ino);
test_fail();
}
if (strcmp(expected_entries[i].d_name, de.d_name) != 0) {
fprintf(stderr, "Entry %d in %s: expected name [%s], read name [%s]\n",
i, relpath, expected_entries[i].d_name, de.d_name);
test_fail();
}
if (expected_entries[i].d_type != de.d_type) {
fprintf(stderr, "Entry %d in %s: expected type %d, read type %d\n",
i, relpath, (int)expected_entries[i].d_type, (int)de.d_type);
test_fail();
}
}
EXPECT_EQ(readdir_r(dir, &de, &de_ptr), 0);
if (de_ptr != NULL) {
test_fail();
fprintf(stderr, "Directory [%s] has extra directory entries (expected only %d entries):\n", relpath, expected_size);
do {
fprintf(stderr, "\tdirent{ino=%lld, name=[%s], type=%d}\n",
(long long)de.d_ino, de.d_name, (int)de.d_type);
EXPECT_EQ(readdir_r(dir, &de, &de_ptr), 0);
} while (de_ptr != NULL);
}
CHECK(de_ptr == NULL);
closedir(dir);
}
static void test_readdir() {
setup();
// ino path
// 1 /
// 2 /dir
// 3 /dir/sub
// 4 /dir/file1
// 5 /dir/file2
// 6 /dir/.-
mkdir(makepath("dir"), 0700);
mkdir(makepath("dir/sub"), 0700);
mknod(makepath("dir/file1"), 0600, 0);
mknod(makepath("dir/file2"), 0600, 0);
mknod(makepath("dir/.-"), 0600, 0);
EXPECT(opendir(makepath("nonexistent")) == NULL);
EXPECT_EQ(errno, ENOENT);
// read root
struct dirent root_entries[3] = {
{ .d_ino = 1, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = 1, .d_name = "..", .d_type = DT_DIR },
{ .d_ino = 2, .d_name = "dir", .d_type = DT_DIR }};
verify_directory_contents("", root_entries, 3);
verify_directory_contents(".", root_entries, 3);
// read empty subdir
struct dirent sub_entries[2] = {
{ .d_ino = 3, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = 2, .d_name = "..", .d_type = DT_DIR }};
verify_directory_contents("dir/sub", sub_entries, 2);
// read non-empty dir
struct dirent dir_entries[6] = {
{ .d_ino = 2, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = 1, .d_name = "..", .d_type = DT_DIR },
// Note that ".-" is lexicographically less tha "..", but "." and ".." are
// always the first and second entry.
{ .d_ino = 6, .d_name = ".-", .d_type = DT_REG },
{ .d_ino = 4, .d_name = "file1", .d_type = DT_REG },
{ .d_ino = 5, .d_name = "file2", .d_type = DT_REG },
{ .d_ino = 3, .d_name = "sub", .d_type = DT_DIR }};
verify_directory_contents("dir", dir_entries, 6);
// TODO: add a test for fdopendir?
teardown();
}
static void test_chmod() {
struct stat st = {0};
setup();
const char *const path_foo = makepath("foo");
const char *const path_bar = makepath("bar");
EXPECT_EQ(mknod(path_foo, 0644, 0), 0);
EXPECT_EQ(mknod(path_bar, 0644, 0), 0);
EXPECT_EQ(chmod(makepath("nonexistent"), 0644), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(chmod(path_foo, 0000), 0);
EXPECT_EQ(stat(path_foo, &st), 0);
EXPECT_EQ(st.st_mode, S_IFREG | 0000);
EXPECT_EQ(chmod(path_foo, 0123), 0);
EXPECT_EQ(stat(path_foo, &st), 0);
EXPECT_EQ(st.st_mode, S_IFREG | 0123);
// Only basic permission bits are affected.
EXPECT_EQ(chmod(path_foo, 07777), 0);
EXPECT_EQ(stat(path_foo, &st), 0);
EXPECT_EQ(st.st_mode, S_IFREG | 0777);
// Unrelated file is unaffected.
EXPECT_EQ(stat(path_bar, &st), 0);
EXPECT_EQ(st.st_mode, S_IFREG | 0644);
// TODO: add a test for fchmod()/fchmodat()?
teardown();
}
static void test_utime() {
setup();
struct stat st;
const char * const path_foo = makepath("foo");
EXPECT_EQ(mknod(path_foo, 0644, 0), 0);
struct utimbuf utimbuf = { .actime = 12345, .modtime = 4567 };
EXPECT_EQ(utime(path_foo, &utimbuf), 0);
EXPECT_EQ(stat(path_foo, &st), 0);
// The actime field is ignored. All timestamps are set to modtime.
EXPECT_EQ(st.st_atime, 4567);
EXPECT_EQ(st.st_ctime, 4567);
EXPECT_EQ(st.st_mtime, 4567);
time_t now = time(NULL);
utimbuf.modtime = now;
EXPECT_EQ(utime(path_foo, &utimbuf), 0);
EXPECT_EQ(stat(path_foo, &st), 0);
EXPECT(st.st_mtime >= now && st.st_mtime <= now + 10);
// There is a variety of similar functions with different resolution:
// utime, utimes, futimes futimesat, utimensat
// Probably not worth testing all of those separately, since either libc or
// libfuse maps them onto the same implementation.
teardown();
}
static void test_open() {
setup();
EXPECT_EQ(mknod(makepath("file"), 0600, 0), 0);
// Existing file can be opened.
int fd = open(makepath("file"), O_RDONLY);
EXPECT(fd >= 0);
EXPECT_EQ(close(fd), 0);
// Non-existent file cannot be opened.
EXPECT_EQ(open(makepath("nonexistent"), O_RDONLY), -1);
EXPECT_EQ(errno, ENOENT);
// Adding the O_CREAT file will cause the file to be created as with mknod().
EXPECT_EQ(open(makepath("nonexistent"), O_RDONLY | O_CREAT, 0600), fd);
// Now the file exists and can be opened.
EXPECT_EQ(open(makepath("nonexistent"), O_RDONLY), fd + 1);
EXPECT_EQ(close(fd + 1), 0);
EXPECT_EQ(close(fd), 0);
teardown();
}
static void verify_contents(const char *path, const char *expected_data, int expected_size) {
struct stat attr;
if (stat(path, &attr) != 0) {
perror(path);
test_fail();
return;
}
EXPECT_EQ(expected_size, attr.st_size);
if (expected_size != attr.st_size) {
return;
}
int fd = open(path, O_RDONLY);
if (fd < 0) {
perror(path);
test_fail();
return;
}
// Read 1 extra byte to detect EOF.
char *data = malloc(expected_size + 1);
CHECK(data);
ssize_t nread = read(fd, data, expected_size + 1);
EXPECT_EQ(nread, expected_size);
if (nread == expected_size) {
for (int i = 0; i < expected_size; ++i) {
if (expected_data[i] != data[i]) {
fprintf(stderr, "Mismatch at byte %d: expected 0x%02x, received 0x%02x.\n",
(int)i, expected_data[i] & 0xff, data[i] & 0xff);
test_fail();
break;
}
}
}
close(fd);
free(data);
}
static void set_mtime(const char *path, time_t timestamp) {
struct utimbuf utimbuf = { .modtime = timestamp, .actime = timestamp };
EXPECT_EQ(utime(path, &utimbuf), 0);
}
static time_t get_mtime(const char *path) {
struct stat attr;
attr.st_mtime = 0;
EXPECT_EQ(stat(path, &attr), 0);
return attr.st_mtime;
}
static void test_read_write() {
setup();
const char *path = makepath("file");
int fd = open(path, O_CREAT | O_RDWR, 0666);
EXPECT(fd >= 0);
char dummy;
EXPECT_EQ(read(fd, &dummy, 1), 0);
EXPECT_EQ(write(fd, "a", 1), 1);
EXPECT_EQ(write(fd, "bc", 2), 2);
EXPECT_EQ(write(fd, "def", 3), 3);
verify_contents(path, "abcdef", 6);
// Verify that writing updates mtime.
time_t past_time = time(NULL) - 1;
set_mtime(path, past_time);
EXPECT_EQ(get_mtime(path), past_time);
EXPECT_EQ(write(fd, "g", 1), 1);
EXPECT(get_mtime(path) > past_time);
// Write past the end of the file to extend it.
EXPECT_EQ(lseek(fd, 1, SEEK_CUR), 8);
EXPECT_EQ(write(fd, "h", 1), 1);
verify_contents(path, "abcdefg\0h", 9);
// (these should go into a separate test case)
close(fd);
teardown();
}
static void update_contents(const char *path, const char *data, size_t size) {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
EXPECT(fd >= 0);
EXPECT_EQ(write(fd, data, size), size);
close(fd);
}
static void test_truncate() {
setup();
struct stat st;
const char *const path = makepath("foo");
// Can only truncate regular files.
EXPECT_EQ(truncate(path, 0), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(mkdir(path, 0700), 0);
EXPECT_EQ(truncate(path, 0), -1);
EXPECT_EQ(errno, EISDIR);
EXPECT_EQ(rmdir(path), 0);
EXPECT_EQ(mknod(path, 0644, 0), 0);
EXPECT_EQ(truncate(path, 5000), 0);
EXPECT_EQ(stat(path, &st), 0);
// If the blocksize changes, updated this test.
EXPECT_EQ(st.st_blksize, 4096);
EXPECT_EQ(st.st_size, 5000);
// File contains only 0s
char contents[6000];
memset(contents, 0, 6000);
verify_contents(path, contents, 5000);
memset(contents, 'a', 5000); //-V512 (buffer underflow)
update_contents(path, contents, 5000);
EXPECT_EQ(truncate(path, 2500), 0);
verify_contents(path, contents, 2500);
EXPECT_EQ(truncate(path, 6000), 0);
memset(contents + 2500, 0, 3500);
verify_contents(path, contents, 6000);
time_t past_time = time(NULL) - 1;
set_mtime(path, past_time);
EXPECT_EQ(get_mtime(path), past_time);
EXPECT_EQ(truncate(path, 0), 0);
verify_contents(path, contents, 0);
// Truncation updates time.
EXPECT(get_mtime(path) > past_time);
// TODO: add a test for ftruncate?
teardown();
}
static void test_write_edgecases() {
// Tricky cases (to unit test!)
//
// 0123 4567 8901 2345 6789
// file: |----|----|--..|....|....|
//
// a. |....|----|....| exactly one temp_block
// b. |----|----|....| exactly two blocks
// c. |---.|....|....| first half of a block
// d. |.---|....|....| last half of a block
// e. |.--.|....|....| middle of a block
// f. |..--|--..|....| spanning two different blocks
// g. |...-|----|-...| write mixes whole and partial blocks
// h. |.---|----|---.| write extends old temp_block
// i. |....|....|....|.-..|....| write outside range
// j. |....|....|....|...-|--..| write outside range
//
// Note that case e doesn't extend beyond the end of the file, while f does.
// Note that the open space introduced by g will be zero-filled.
setup();
// Small blocksize to make these tests easier to write.
sqlfs_set_blocksize(sqlfs, 4);
EXPECT_EQ(sqlfs_get_blocksize(sqlfs), 4);
const char *path = makepath("file");
update_contents(path, "xxxxxxxxxx", 10);
int fd = open(path, O_WRONLY);
EXPECT_EQ(lseek(fd, 4, SEEK_SET), 4);
EXPECT_EQ(write(fd, "aaaa", 4), 4);
verify_contents(path, "xxxxaaaaxx", 10);
EXPECT_EQ(lseek(fd, 0, SEEK_SET), 0);
EXPECT_EQ(write(fd, "bbbbbbbb", 8), 8);
verify_contents(path, "bbbbbbbbxx", 10);
EXPECT_EQ(lseek(fd, 0, SEEK_SET), 0);
EXPECT_EQ(write(fd, "ccc", 3), 3);
verify_contents(path, "cccbbbbbxx", 10);
EXPECT_EQ(lseek(fd, 1, SEEK_SET), 1);
EXPECT_EQ(write(fd, "ddd", 3), 3);
verify_contents(path, "cdddbbbbxx", 10);
EXPECT_EQ(lseek(fd, 1, SEEK_SET), 1);
EXPECT_EQ(write(fd, "ee", 2), 2);
verify_contents(path, "ceedbbbbxx", 10);
EXPECT_EQ(lseek(fd, 2, SEEK_SET), 2);
EXPECT_EQ(write(fd, "ffff", 4), 4);
verify_contents(path, "ceffffbbxx", 10);
EXPECT_EQ(lseek(fd, 3, SEEK_SET), 3);
EXPECT_EQ(write(fd, "gggggg", 6), 6);
verify_contents(path, "cefggggggx", 10);
EXPECT_EQ(lseek(fd, 1, SEEK_SET), 1);
EXPECT_EQ(write(fd, "hhhhhhhhhh", 10), 10);
verify_contents(path, "chhhhhhhhhh", 11);
EXPECT_EQ(lseek(fd, 13, SEEK_SET), 13);
EXPECT_EQ(write(fd, "i", 1), 1);
verify_contents(path, "chhhhhhhhhh\0\0i", 14);
EXPECT_EQ(lseek(fd, 15, SEEK_SET), 15);
EXPECT_EQ(write(fd, "jjj", 3), 3);
verify_contents(path, "chhhhhhhhhh\0\0i\0jjj", 18);
close(fd);
teardown();
}
static void test_blocksize() {
setup();
// Default blocksize is 4K.
EXPECT_EQ(sqlfs_get_blocksize(sqlfs), 4096);
sqlfs_set_blocksize(sqlfs, 123);
EXPECT_EQ(sqlfs_get_blocksize(sqlfs), 123);
const char *path = makepath("file");
EXPECT_EQ(mknod(path, 0644, 0), 0);
EXPECT_EQ(truncate(path, 1000), 0);
struct stat attr;
EXPECT_EQ(stat(path, &attr), 0);
// FUSE will round down to the nearest power of 2.
EXPECT_EQ(attr.st_blksize, 64);
// Number of blocks is always in multiples of 512 blocks; it's not affected
// by the filedata blocksize.
EXPECT_EQ(attr.st_blocks, 2);
teardown();
}
static ino_t get_ino(const char *path) {
struct stat st;
int err = stat(path, &st);
EXPECT(err == 0 || errno == ENOENT);
if (err == 0) {
EXPECT(st.st_ino > 0);
return st.st_ino;
} else {
return 0;
}
}
static void test_rename() {
setup();
const char *const path_a = makepath("a");
const char *const path_a_file = makepath("a/file");
const char *const path_a_dir = makepath("a/dir");
const char *const path_a_dir_content = makepath("a/dir/dir-content");
const char *const path_b = makepath("b");
const char *const path_b_file = makepath("b/file");
const char *const path_b_dir = makepath("b/dir");
EXPECT_EQ(mkdir(path_a, 0700), 0);
EXPECT_EQ(mkdir(path_b, 0700), 0);
EXPECT_EQ(mknod(path_a_file, 0600, 0), 0);
EXPECT_EQ(mkdir(path_a_dir, 0700), 0);
// Add some content to the test file and directory, to be able to verify later
// that content was preserved across renames.
const char *file_content = "file-content";
update_contents(path_a_file, file_content, strlen(file_content));
EXPECT_EQ(mknod(path_a_dir_content, 0600, 0), 0);
const ino_t a_ino = get_ino(path_a);
const ino_t b_ino = get_ino(path_b);
const ino_t file_ino = get_ino(path_a_file);
const ino_t dir_ino = get_ino(path_a_dir);
const ino_t dir_content_ino = get_ino(path_a_dir_content);
EXPECT_EQ(rename(path_b_file, path_a_file), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(rename(path_b_dir, path_a_dir), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(rename(path_a_file, makepath("/nonexistent/a")), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(rename(path_a_file, path_b_file), 0);
EXPECT_EQ(rename(path_a_dir, path_b_dir), 0);
// Directory /a is now empty:
verify_directory_contents("a", (struct dirent[]){
{ .d_ino = a_ino, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = SQLFS_INO_ROOT, .d_name = "..", .d_type = DT_DIR },
}, 2);
// Directory /b contains the moved files:
verify_directory_contents("b", (struct dirent[]){
{ .d_ino = b_ino, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = SQLFS_INO_ROOT, .d_name = "..", .d_type = DT_DIR },
{ .d_ino = dir_ino, .d_name = "dir", .d_type = DT_DIR },
{ .d_ino = file_ino, .d_name = "file", .d_type = DT_REG },
}, 4);
const char *const path_c = makepath("c");
const char *const path_c_file = makepath("c/file");
const char *const path_c_file2 = makepath("c/file2");
const char *const path_c_dir = makepath("c/dir");
const char *const path_c_dir_file = makepath("c/dir/file");
EXPECT_EQ(mkdir(path_c, 0700), 0);
EXPECT_EQ(mknod(path_c_file, 0600, 0), 0);
EXPECT_EQ(mkdir(path_c_dir, 0700), 0);
EXPECT_EQ(mknod(path_c_dir_file, 0600, 0), 0);
const ino_t c_ino = get_ino(path_c);
// Moving b/dir to c/dir should fail, because c/dir is not empty.
EXPECT_EQ(rename(path_b_dir, path_c_dir), -1);
EXPECT_EQ(errno, ENOTEMPTY);
EXPECT_EQ(unlink(path_c_dir_file), 0);
// Cannot rename file to an existing directory.
EXPECT_EQ(rename(path_b_file, path_c_dir), -1);
EXPECT_EQ(errno, EISDIR);
// Cannot rename directory to an existing file.
EXPECT_EQ(rename(path_b_dir, path_c_file), -1);
EXPECT_EQ(errno, ENOTDIR);
// Can rename file to an existing file.
EXPECT_EQ(rename(path_b_file, path_c_file), 0);
// Can rename directory to an existing directory.
EXPECT_EQ(rename(path_b_dir, path_c_dir), 0);
// Directory /b is now empty:
verify_directory_contents("b", (struct dirent[]){
{ .d_ino = b_ino, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = SQLFS_INO_ROOT, .d_name = "..", .d_type = DT_DIR },
}, 2);
// Directory /c contains the moved files:
verify_directory_contents("c", (struct dirent[]){
{ .d_ino = c_ino, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = SQLFS_INO_ROOT, .d_name = "..", .d_type = DT_DIR },
{ .d_ino = dir_ino, .d_name = "dir", .d_type = DT_DIR },
{ .d_ino = file_ino, .d_name = "file", .d_type = DT_REG },
}, 4);
// Test renaming entry to itself. Should be no-op if it exists.
EXPECT_EQ(rename(path_c_file, path_c_file), 0); //-V549 (arguments equal)
EXPECT_EQ(rename(path_c_dir, path_c_dir), 0); //-V549 (arguments equal)
EXPECT_EQ(rename(path_c_file2, path_c_file2), -1); //-V549 (arguments equal)
EXPECT_EQ(errno, ENOENT);
// Renaming file within the same directory succeeds.
EXPECT_EQ(rename(path_c_file, path_c_file2), 0);
// TODO: once hard-links are supported, we should also test this behavior:
//
// If oldpath and newpath are existing hard links referring to the same file,
// then rename() does nothing, and returns a success status.
// Verify content is unchanged.
verify_contents(path_c_file2, file_content, strlen(file_content));
verify_directory_contents("c/dir", (struct dirent[]){
{ .d_ino = dir_ino, .d_name = ".", .d_type = DT_DIR },
{ .d_ino = c_ino, .d_name = "..", .d_type = DT_DIR },
{ .d_ino = dir_content_ino, .d_name = "dir-content", .d_type = DT_REG },
}, 3);
// Check link counts (since we moved subdirectories around).
struct stat attr;
EXPECT_EQ(stat(path_a, &attr), 0);
EXPECT_EQ(attr.st_nlink, 2);
EXPECT_EQ(stat(path_b, &attr), 0);
EXPECT_EQ(attr.st_nlink, 2);
EXPECT_EQ(stat(path_c, &attr), 0);
EXPECT_EQ(attr.st_nlink, 3);
// Can't move a directory into itself, or into a subdirectory of itself!
// Doing so would create a cycle in the directory structure.
const char *const path_a_subdir = makepath("a/subdir");
EXPECT_EQ(rename(path_a, path_a_subdir), -1);
EXPECT_EQ(errno, EINVAL);
EXPECT_EQ(mkdir(path_a_subdir, 0700), 0);
EXPECT_EQ(rename(path_a, path_a_subdir), -1);
EXPECT_EQ(errno, EINVAL);
teardown();
}
static void test_purge() {
setup();
const char *path_a = makepath("a");
const char *path_b = makepath("b");
update_contents(path_a, "a", 1);
update_contents(path_b, "b", 1);
int fd_a = open(path_a, O_RDONLY);
int fd_b = open(path_b, O_RDONLY);
EXPECT(fd_a >= 0);
EXPECT(fd_b >= 0);
ino_t ino_a = get_ino(path_a);
unlink(path_a);
unlink(path_b);
EXPECT_EQ(sqlfs_purge(sqlfs, ino_a), 0);
char dummy;
EXPECT_EQ(read(fd_a, &dummy, 1), -1);
EXPECT_EQ(errno, ENOENT);
EXPECT_EQ(read(fd_b, &dummy, 1), 1);
EXPECT_EQ(dummy, 'b');
// When logging is enabled, this will print "failed to purge ino=2 (err=2)!"
// because we circumvented sqlfuse by calling sqlfs_purge() directly.