aboutsummaryrefslogtreecommitdiff
path: root/lib/tar/test/tar_iterator3.c
blob: 05940a2548bc2ceeee711d636099e3e401064480 (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

/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * tar_iterator3.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "tar/tar.h"
#include "util/test.h"
#include "sqfs/error.h"
#include "sqfs/io.h"

int main(int argc, char **argv)
{
	sqfs_istream_t *fp, *ti;
	sqfs_dir_iterator_t *it;
	char buffer[100];
	sqfs_dir_entry_t *ent;
	char *link;
	int ret;
	(void)argc; (void)argv;

	TEST_ASSERT(chdir(TEST_PATH) == 0);

	ret = sqfs_istream_open_file(&fp, "format-acceptance/link_filled.tar",
				     0);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(fp);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	it = tar_open_stream(fp, NULL);
	TEST_NOT_NULL(it);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 1);
	sqfs_drop(fp);

	/* "deep" directory hierarchy containg 2 files */
	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20_characters_here01");
	free(ent);

	ret = it->read_link(it, &link);
	TEST_EQUAL_I(ret, SQFS_ERROR_NO_ENTRY);
	TEST_NULL(link);
	ret = it->open_file_ro(it, &ti);
	TEST_EQUAL_I(ret, SQFS_ERROR_NOT_FILE);
	TEST_NULL(ti);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20_characters_here01/20_characters_here02");
	free(ent);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03");
	free(ent);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03/20_characters_here04");
	free(ent);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFREG | 0777);
	TEST_STR_EQUAL(ent->name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03/20_characters_here04/"
		       "errored_file_tst");
	free(ent);

	ret = it->read_link(it, &link);
	TEST_EQUAL_I(ret, SQFS_ERROR_NO_ENTRY);
	TEST_NULL(link);
	ret = it->open_file_ro(it, &ti);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ti);

	TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 5);
	buffer[5] = '\0';
	TEST_STR_EQUAL(buffer, "test\n");
	TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 0);
	ti = sqfs_drop(ti);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFREG | 0777);
	TEST_STR_EQUAL(ent->name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03/20_characters_here04/"
		       "some_test_file");
	free(ent);

	ret = it->open_file_ro(it, &ti);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ti);

	TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 5);
	buffer[5] = '\0';
	TEST_STR_EQUAL(buffer, "test\n");
	TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 0);
	ti = sqfs_drop(ti);

	/* "deep" directory hierarchy containg a hard link */
	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20CharsForLnkTest001");
	free(ent);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20CharsForLnkTest001/20CharsForLnkTest002");
	free(ent);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20CharsForLnkTest001/20CharsForLnkTest002/"
		       "20CharsForLnkTest003");
	free(ent);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(ent->mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(ent->name, "20CharsForLnkTest001/20CharsForLnkTest002/"
		       "20CharsForLnkTest003/20CharsForLnkTest004");
	free(ent);

	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_STR_EQUAL(ent->name, "20CharsForLnkTest001/20CharsForLnkTest002/"
		       "20CharsForLnkTest003/20CharsForLnkTest004/"
		       "01234567890123456789");
	TEST_EQUAL_UI(ent->mode, S_IFLNK | 0777);
	TEST_ASSERT((ent->flags & SQFS_DIR_ENTRY_FLAG_HARD_LINK) != 0);
	free(ent);

	ret = it->read_link(it, &link);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(link);
	ret = it->open_file_ro(it, &ti);
	TEST_EQUAL_I(ret, SQFS_ERROR_NOT_FILE);
	TEST_NULL(ti);

	TEST_STR_EQUAL(link, "20_characters_here01/"
		       "20_characters_here02/20_characters_here03/"
		       "20_characters_here04/errored_file_tst");
	free(link);

	/* end of file */
	ret = it->next(it, &ent);
	TEST_ASSERT(ret > 0);
	TEST_NULL(ent);
	sqfs_drop(it);
	return EXIT_SUCCESS;
}