summaryrefslogtreecommitdiffstats
path: root/extras/test/ld-preload-test/ld-preload-test.c
blob: 55dd98805b400eccb4130d262eec9f7857f872c1 (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
/*
   Copyright (c) 2007-2011 Gluster, Inc. <http://www.gluster.com>
   This file is part of GlusterFS.

   GlusterFS 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 3 of the License,
   or (at your option) any later version.

   GlusterFS 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 program.  If not, see
   <http://www.gnu.org/licenses/>.
*/


/*
 * LD PRELOAD Test Tool
 *
 * 1. The idea of this test tool is to check the sanity of the LD_PRELOAD
 * mechanism in libc for the system on which booster needs to run.
 *
 * 2. Basically, this test tool calls various system calls for which there is
 * support in the booster library. Combined with the logging output from this
 * tool and the preload-test-lib, one can determine whether the
 * pre-loading mechanism is working working in order for booster to initialize.
 *
 * 3. This tool does not test GlusterFS functionality running under booster
 * although the path specified to the tool can be a GlusterFS mountpoint but
 * that is not very useful.
 *
 * 4. This tool is incomplete without the preload-test-lib and the
 * accompanyung shell script that needs to be run for running the test.
 */
#define _XOPEN_SOURCE 500

#include <dlfcn.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <dirent.h>
#include <sys/statfs.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/uio.h>
#include <utime.h>
#include <sys/time.h>
#include <attr/xattr.h>
#include <sys/sendfile.h>


#define PRELOAD_ERRNO_VERF      6449
inline void
check_err(int ret, char *call, int tabs)
{
        while (tabs > 0) {
                fprintf (stdout, "\t");
                --tabs;
        }
        if (ret != -1) {
                fprintf (stdout, "Not intercepted: %s\n", call);
                return;
        }

        if (errno != PRELOAD_ERRNO_VERF) {
                fprintf (stdout, "Not intercepted: %s: err: %s\n", call,
                         strerror (errno));
                return;
        }

        fprintf (stdout, "Intercept verified: %s\n", call);
        return;
}

void
usage (FILE *fp)
{
        fprintf (fp, "Usage: ld-preload-test <Options>\n");
        fprintf (fp, "Options\n");
        fprintf (fp, "\t--path\t\tPathname is used as the file/directory"
                     " created for the test.\n");

}


int
run_file_tests (char *testfile)
{
        int             ret = -1;
        struct stat     buf;

        assert (testfile);
        fprintf (stdout, "Testing creat");
        ret = creat (testfile, S_IRWXU);
        check_err (ret, "creat", 2);

        fprintf (stdout, "Testing close");
        ret = close (ret);
        check_err (ret, "close", 2);

        fprintf (stdout, "Testing open");
        ret = open (testfile, O_RDONLY);
        check_err (ret, "open", 2);

        fprintf (stdout, "Testing read");
        ret = read (0, NULL, 0);
        check_err (ret, "read", 2);

        fprintf (stdout, "Testing readv");
        ret = readv (0, NULL, 0);
        check_err (ret, "readv", 2);

        fprintf (stdout, "Testing pread");
        ret = pread (0, NULL, 0, 0);
        check_err (ret, "pread", 2);

        fprintf (stdout, "Testing write");
        ret = write (0, NULL, 0);
        check_err (ret, "write", 2);

        fprintf (stdout, "Testing writev");
        ret = writev (0, NULL, 0);
        check_err (ret, "writev", 2);

        fprintf (stdout, "Testing pwrite");
        ret = pwrite (0, NULL, 0, 0);
        check_err (ret, "pwrite", 2);

        fprintf (stdout, "Testing lseek");
        ret = lseek (0, 0, 0);
        check_err (ret, "lseek", 2);

        fprintf (stdout, "Testing dup");
        ret = dup (0);
        check_err (ret, "dup", 2);

        fprintf (stdout, "Testing dup2");
        ret = dup2 (0, 0);
        check_err (ret, "dup2", 2);

        fprintf (stdout, "Testing fchmod");
        ret = fchmod (0, 0);
        check_err (ret, "fchmod", 2);

        fprintf (stdout, "Testing fchown");
        ret = fchown (0, 0, 0);
        check_err (ret, "fchown", 2);

        fprintf (stdout, "Testing fsync");
        ret = fsync (0);
        check_err (ret, "fsync", 2);

        fprintf (stdout, "Testing ftruncate");
        ret = ftruncate (0, 0);
        check_err (ret, "ftruncate", 1);

        fprintf (stdout, "Testing fstat");
        ret = fstat (0, &buf);
        check_err (ret, "fstat", 1);

        fprintf (stdout, "Testing sendfile");
        ret = sendfile (0, 0, NULL, 0);
        check_err (ret, "sendfile", 1);

        fprintf (stdout, "Testing fcntl");
        ret = fcntl (0, 0, NULL);
        check_err (ret, "fcntl", 2);

        fprintf (stdout, "Testing close");
        ret = close (ret);
        check_err (ret, "close", 2);

        fprintf (stdout, "Testing remove");
        ret = remove (testfile);
        check_err (ret, "remove", 2);

        return ret;
}


int
run_attr_tests (char *testfile)
{
        int             ret = -1;
        char            *res = NULL;
        struct stat     buf;
        struct statfs   sbuf;
        struct statvfs  svbuf;

        assert (testfile);

        fprintf (stdout, "Testing chmod");
        ret = chmod (testfile, 0);
        check_err (ret, "chmod", 2);

        fprintf (stdout, "Testing chown");
        ret = chown (testfile, 0, 0);
        check_err (ret, "chown", 2);

        fprintf (stdout, "Testing link");
        ret = link (testfile, testfile);
        check_err (ret, "link", 2);

        fprintf (stdout, "Testing rename");
        ret = rename (testfile, testfile);
        check_err (ret, "rename", 2);

        fprintf (stdout, "Testing utimes");
        ret = utimes (testfile, NULL);
        check_err (ret, "utimes", 2);

        fprintf (stdout, "Testing utime");
        ret = utime (testfile, NULL);
        check_err (ret, "utime", 2);

        fprintf (stdout, "Testing unlink");
        ret = unlink (testfile);
        check_err (ret, "unlink", 2);

        fprintf (stdout, "Testing symlink");
        ret = symlink (testfile, testfile);
        check_err (ret, "symlink", 2);

        fprintf (stdout, "Testing readlink");
        ret = readlink (testfile, testfile, 0);
        check_err (ret, "readlink", 2);

        fprintf (stdout, "Testing realpath");
        ret = 0;
        res = realpath ((const char *)testfile, testfile);
        if (!res)
                ret = -1;
        check_err (ret, "realpath", 2);

        fprintf (stdout, "Testing stat");
        ret = stat (testfile, &buf);
        check_err (ret, "stat", 1);

        fprintf (stdout, "Testing lstat");
        ret = lstat (testfile, &buf);
        check_err (ret, "lstat", 1);

        fprintf (stdout, "Testing statfs");
        ret = statfs (testfile, &sbuf);
        check_err (ret, "statfs", 2);

        fprintf (stdout, "Testing statvfs");
        ret = statvfs (testfile, &svbuf);
        check_err (ret, "statvfs", 1);

        fprintf (stdout, "Testing getxattr");
        ret = getxattr (testfile, NULL, NULL, 0);
        check_err (ret, "getxattr", 2);

        fprintf (stdout, "Testing lgetxattr");
        ret = lgetxattr (testfile, NULL, NULL, 0);
        check_err (ret, "lgetxattr", 1);

        fprintf (stdout, "Testing lchown");
        ret = lchown (testfile, 0, 0);
        check_err (ret, "lchown", 2);
        return 0;
}


int
run_dev_tests (char *testfile)
{
        int     ret = -1;

        assert (testfile);

        fprintf (stdout, "Testing mknod");
        ret = mknod (testfile, 0, 0);
        check_err (ret, "mknod", 2);

        fprintf (stdout, "Testing mkfifo");
        ret = mkfifo (testfile, 0);
        check_err (ret, "mkfifo", 2);
        return 0;
}

int
run_dir_tests (char *testpath)
{
        int             ret = -1;
        DIR             *dh = NULL;
        struct dirent   *dire = NULL;

        assert (testpath);

        fprintf (stdout, "Testing mkdir");
        ret = mkdir (testpath, 0);
        check_err (ret, "mkdir", 2);

        fprintf (stdout, "Testing rmdir");
        ret = rmdir (testpath);
        check_err (ret, "rmdir", 2);

        fprintf (stdout, "Testing opendir");
        ret = 0;
        dh = opendir (testpath);
        if (!dh)
                ret = -1;
        check_err (ret, "opendir", 2);

        fprintf (stdout, "Testing readdir");
        ret = 0;
        dire = readdir (dh);
        if (!dire)
                ret = -1;
        check_err (ret, "readdir", 1);

        fprintf (stdout, "Testing readdir_r");
        ret = readdir_r (dh, dire, &dire);
        check_err (ret, "readdir_r", 1);

        fprintf (stdout, "Testing rewinddir");
        rewinddir (dh);
        check_err (-1, "rewinddir", 1);

        fprintf (stdout, "Testing seekdir");
        seekdir (dh, 0);
        check_err (-1, "seekdir", 2);

        fprintf (stdout, "Testing telldir");
        ret = telldir (dh);
        check_err (ret, "telldir", 2);

        fprintf (stdout, "Testing closedir");
        ret = closedir (dh);
        check_err (ret, "closedir", 2);
        return 0;
}



int
main (int argc, char *argv[])
{
        char            *testpath = NULL;
        int             x = 0;

        for (;x < argc; ++x) {
                if (strcmp (argv[x], "--path") == 0) {
                        testpath = argv[x+1];
                        continue;
                }

        }

        if (!testpath) {
                fprintf (stderr, "--path not specified\n");
                usage (stderr);
                return -1;
        }

        run_file_tests (testpath);
        run_dir_tests (testpath);
        run_attr_tests (testpath);
        run_dev_tests (testpath);

        return 0;
}