aboutsummaryrefslogtreecommitdiff
path: root/lib/tar/test/tar_iterator.c
blob: b90d5ca2e2a378de07df74fb4c6747353f53c26d (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * tar_iterator.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"

#ifndef TESTUID
#define TESTUID 1000
#endif

#ifndef TESTGID
#define TESTGID TESTUID
#endif

#ifndef TESTFNAME
#define TESTFNAME input.txt
#endif

#ifndef TESTTS
#define TESTTS 1542905892
#endif

static const char *fname = STRVALUE(TESTFNAME);

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

	/* Open the file, create an iterator */
	iret = sqfs_istream_open_file(&fp,
				      STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE),
				      0);
	TEST_EQUAL_I(iret, 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);

	/* read entry */
	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 1);

	ts = TESTTS;
	TEST_EQUAL_UI(ent->mode, S_IFREG | 0644);
	TEST_EQUAL_UI(ent->uid, TESTUID);
	TEST_EQUAL_UI(ent->gid, TESTGID);
	TEST_EQUAL_UI(ent->mtime, ts);
	TEST_STR_EQUAL(ent->name, fname);
	free(ent);
	ent = NULL;

	/* Open file stream */
	ret = it->open_file_ro(it, &ti);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ti);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1);

	/* test that the iterator is now "locked" */
	ret = it->open_file_ro(it, &ti2);
	TEST_EQUAL_I(ret, SQFS_ERROR_SEQUENCE);
	TEST_NULL(ti2);
	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, SQFS_ERROR_SEQUENCE);
	TEST_NULL(ent);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)it)->refcount, 2);
	TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1);

	/* read the data from the stream */
	ret = sqfs_istream_read(ti, buffer, sizeof(buffer));
	TEST_EQUAL_I(ret, 5);
	buffer[5] = '\0';
	TEST_STR_EQUAL(buffer, "test\n");

	ret = sqfs_istream_read(ti, buffer, sizeof(buffer));
	TEST_EQUAL_I(ret, 0);

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

	/* read past EOF on the iterator */
	ret = it->next(it, &ent);
	TEST_ASSERT(ret > 0);
	TEST_NULL(ent);

	/* cleanup */
	sqfs_drop(it);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	sqfs_drop(fp);

	/* re-open the tar iterator */
	iret = sqfs_istream_open_file(&fp,
				      STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE),
				      0);
	TEST_EQUAL_I(iret, 0);
	TEST_NOT_NULL(fp);
	it = tar_open_stream(fp, NULL);
	TEST_NOT_NULL(it);

	/* read entry */
	ret = it->next(it, &ent);
	TEST_EQUAL_I(ret, 0);
	TEST_NOT_NULL(ent);

	ts = TESTTS;
	TEST_EQUAL_UI(ent->mode, S_IFREG | 0644);
	TEST_EQUAL_UI(ent->uid, TESTUID);
	TEST_EQUAL_UI(ent->gid, TESTGID);
	TEST_EQUAL_UI(ent->mtime, ts);
	TEST_STR_EQUAL(ent->name, fname);
	free(ent);
	ent = NULL;

	/* read next entry, mus treturn EOF */
	ret = it->next(it, &ent);
	TEST_ASSERT(ret > 0);
	TEST_NULL(ent);

	/* cleanup */
	sqfs_drop(it);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	sqfs_drop(fp);

	return EXIT_SUCCESS;
}