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
|
/*
* libdpkg - Debian packaging suite library routines
* t-file.c - test file functions
*
* Copyright © 2018 Guillem Jover <guillem@debian.org>
*
* This 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 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 <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <compat.h>
#include <dpkg/test.h>
#include <dpkg/dpkg.h>
#include <dpkg/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
static const char ref_data[] =
"this is a test string\n"
"within a test file\n"
"containing multiple lines\n"
;
static void
test_file_slurp(void)
{
struct varbuf vb = VARBUF_INIT;
struct dpkg_error err = DPKG_ERROR_INIT;
char *test_file;
char *test_dir;
int fd;
test_pass(file_slurp("/nonexistent", &vb, &err) < 0);
test_pass(vb.used == 0);
test_pass(vb.buf == NULL);
test_pass(err.syserrno == ENOENT);
test_error(err);
varbuf_destroy(&vb);
test_dir = test_alloc(strdup("test.XXXXXX"));
test_pass(mkdtemp(test_dir) != NULL);
test_pass(file_slurp(test_dir, &vb, &err) < 0);
test_pass(vb.used == 0);
test_pass(vb.buf == NULL);
test_pass(err.syserrno == 0);
test_error(err);
varbuf_destroy(&vb);
test_pass(rmdir(test_dir) == 0);
test_file = test_alloc(strdup("test.XXXXXX"));
fd = mkstemp(test_file);
test_pass(fd >= 0);
test_pass(file_slurp(test_file, &vb, &err) == 0);
test_pass(vb.used == 0);
test_pass(vb.buf == NULL);
test_pass(err.syserrno == 0);
test_pass(err.type == DPKG_MSG_NONE);
varbuf_destroy(&vb);
test_pass(write(fd, ref_data, strlen(ref_data)) == (ssize_t)strlen(ref_data));
test_pass(lseek(fd, 0, SEEK_SET) == 0);
test_pass(file_slurp(test_file, &vb, &err) == 0);
test_pass(vb.used == strlen(ref_data));
test_mem(vb.buf, ==, ref_data, min(vb.used, strlen(ref_data)));
test_pass(err.syserrno == 0);
test_pass(err.type == DPKG_MSG_NONE);
varbuf_destroy(&vb);
test_fail(file_is_exec(test_dir));
test_fail(file_is_exec(test_file));
test_pass(chmod(test_file, 0755) == 0);
test_pass(file_is_exec(test_file));
test_pass(chmod(test_file, 0750) == 0);
test_pass(file_is_exec(test_file));
test_pass(unlink(test_file) == 0);
free(test_file);
free(test_dir);
}
TEST_ENTRY(test)
{
test_plan(32);
test_file_slurp();
}
|