This repository has been archived by the owner on Mar 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfstab_test.c
161 lines (144 loc) · 3.68 KB
/
fstab_test.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
/*
* Copyright (C) 2017 Intel Corporation
* SPDX-License-Identifier: MIT
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <macros.h>
/* This test is a white-box one to test mount.c function parse_fstab_mnt_options,
* hence this include of C file */
#include <mount.c>
struct test_data {
const char *name;
const char *mnt_options;
struct {
const char *remaining;
unsigned long flags;
bool success;
} expected;
} test_data[] = {
{
.name = "test1",
.mnt_options = "defaults",
.expected = {
.remaining = NULL,
.flags = MS_NOUSER,
.success = true
}
},
{
.name = "test2",
.mnt_options = "ro,fdata=dummy",
.expected = {
.remaining = "fdata=dummy",
.flags = MS_RDONLY,
.success = true
}
},
{
.name = "test3",
.mnt_options = "rw,fdata=dummy,sync,fdata2=dummy2",
.expected = {
.remaining = "fdata=dummy,fdata2=dummy2",
.flags = MS_SYNCHRONOUS,
.success = true
}
},
{
.name = "test4",
.mnt_options = "rw,fdata=dummy,sync,fdata2=dummy2,noatime",
.expected = {
.remaining = "fdata=dummy,fdata2=dummy2",
.flags = MS_SYNCHRONOUS | MS_NOATIME,
.success = true
}
},
{
.name = "test5",
.mnt_options = "defaults,rw,fdata=dummy,sync,fdata2=dummy2",
.expected = {
.remaining = "fdata=dummy,fdata2=dummy2",
.flags = MS_NOUSER | MS_SYNCHRONOUS,
.success = true
}
},
{
.name = "test6",
.mnt_options = "",
.expected = {
.remaining = NULL,
.flags = 0,
.success = false
}
},
{
.name = "test7",
.mnt_options = "rw",
.expected = {
.remaining = NULL,
.flags = 0,
.success = true
}
},
{
.name = "test8",
.mnt_options = "rw,noiversion,nofail",
.expected = {
.remaining = NULL,
.flags = 0,
.success = true
}
},
};
static bool not_equal(const char *a, const char *b)
{
if (a == b) {
return false;
}
if (a != NULL && b != NULL) {
return strcmp(a, b) != 0;
}
return true;
}
static bool perform_test(struct test_data *td)
{
assert(td);
bool b, result = true;
unsigned long flags;
char *remaining = NULL;
b = parse_fstab_mnt_options(td->mnt_options, &flags, &remaining);
if (b != td->expected.success) {
printf("TEST fstab (%s): Unexpected result for `parse_fstab_mnt_options`: %d Expected result %d\n",
td->name, b, td->expected.success);
result = false;
} else if (td->expected.success) {
if (td->expected.flags != flags) {
printf("TEST fstab (%s): Unexpected flags %ld Expected %ld\n",
td->name, flags, td->expected.flags);
result = false;
}
if (not_equal(td->expected.remaining, remaining)) {
printf("TEST fstab (%s): Unexpected remaining options [%s] Expected [%s]\n",
td->name, remaining, td->expected.remaining);
result = false;
}
}
free(remaining);
return result;
}
int main(void)
{
int i;
bool success = true;
for (i = 0; i < ARRAY_SIZE(test_data); i++) {
success &= perform_test(&test_data[i]);
}
if (success) {
printf("All tests OK\n");
} else {
printf("Some tests FAIL\n");
}
return success ? 0 : 1;
}