aboutsummaryrefslogtreecommitdiff
path: root/lib/tar/test/tar_istream2.c
blob: e5cdca80d29dbb089974ede7e7d9c0fd7b892aa7 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * tar_istream2.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "io/file.h"
#include "tar/tar.h"
#include "util/test.h"

int main(int argc, char **argv)
{
	tar_header_decoded_t hdr;
	char buffer[100];
	istream_t *fp;
	istream_t *ti;
	(void)argc; (void)argv;

	TEST_ASSERT(chdir(TEST_PATH) == 0);

	fp = istream_open_file("format-acceptance/link_filled.tar");
	TEST_NOT_NULL(fp);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);

	/* "deep" directory hierarchy containg 2 files */
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20_characters_here01/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20_characters_here01/20_characters_here02/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03/20_characters_here04/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFREG | 0777);
	TEST_STR_EQUAL(hdr.name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03/20_characters_here04/"
		       "errored_file_tst");
	TEST_EQUAL_UI(hdr.actual_size, 5);

	ti = tar_record_istream_create(fp, &hdr);
	TEST_NOT_NULL(ti);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1);
	clear_header(&hdr);

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

	ti = sqfs_drop(ti);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFREG | 0777);
	TEST_STR_EQUAL(hdr.name, "20_characters_here01/20_characters_here02/"
		       "20_characters_here03/20_characters_here04/"
		       "some_test_file");
	TEST_EQUAL_UI(hdr.actual_size, 5);

	ti = tar_record_istream_create(fp, &hdr);
	TEST_NOT_NULL(ti);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1);
	clear_header(&hdr);

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

	ti = sqfs_drop(ti);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);

	/* "deep" directory hierarchy containg a hard link */
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20CharsForLnkTest001/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20CharsForLnkTest001/20CharsForLnkTest002/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20CharsForLnkTest001/20CharsForLnkTest002/"
		       "20CharsForLnkTest003/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_EQUAL_UI(hdr.mode, S_IFDIR | 0777);
	TEST_STR_EQUAL(hdr.name, "20CharsForLnkTest001/20CharsForLnkTest002/"
		       "20CharsForLnkTest003/20CharsForLnkTest004/");
	clear_header(&hdr);

	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	TEST_STR_EQUAL(hdr.name, "20CharsForLnkTest001/20CharsForLnkTest002/"
		       "20CharsForLnkTest003/20CharsForLnkTest004/"
		       "01234567890123456789");
	TEST_ASSERT(hdr.is_hard_link);

	TEST_STR_EQUAL(hdr.link_target, "20_characters_here01/"
		       "20_characters_here02/20_characters_here03/"
		       "20_characters_here04/errored_file_tst");
	clear_header(&hdr);

	/* end of file */
	TEST_ASSERT(read_header(fp, &hdr) > 0);
	sqfs_drop(fp);
	return EXIT_SUCCESS;
}