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

static const struct {
	uint64_t offset;
	size_t size;
	int fill;
} regions[] = {
	{       0, 4096, 'A' },
	{  262144, 4096, 'B' },
	{  524288, 4096, 'C' },
	{  786432, 4096, 'D' },
	{ 1048576, 4096, 'E' },
	{ 1310720, 4096, 'F' },
	{ 1572864, 4096, 'G' },
	{ 1835008, 4096, 'H' },
};

static int byte_from_offset(uint64_t offset)
{
	sqfs_u64 diff;
	size_t i;

	for (i = 0; i < sizeof(regions) / sizeof(regions[0]); ++i) {
		if (offset >= regions[i].offset) {
			diff = (offset - regions[i].offset);

			if (diff < regions[i].size)
				return regions[i].fill;
		}
	}

	return '\0';
}

int main(int argc, char **argv)
{
	unsigned char buffer[941];
	tar_header_decoded_t hdr;
	istream_t *fp, *ti;
	uint64_t offset;
	sqfs_s32 i, ret;
	(void)argc; (void)argv;

	fp = istream_open_file(STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE));
	TEST_NOT_NULL(fp);
	TEST_ASSERT(read_header(fp, &hdr) == 0);
	TEST_EQUAL_UI(hdr.mode, S_IFREG | 0644);
	TEST_EQUAL_UI(hdr.uid, 01750);
	TEST_EQUAL_UI(hdr.gid, 01750);
	TEST_EQUAL_UI(hdr.actual_size, 2097152);
	TEST_EQUAL_UI(hdr.record_size, 32768);
	TEST_STR_EQUAL(hdr.name, "input.bin");
	TEST_ASSERT(!hdr.unknown_record);

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

	offset = 0;

	for (;;) {
		ret = istream_read(ti, buffer, sizeof(buffer));
		TEST_ASSERT(ret >= 0);

		if (ret == 0)
			break;

		for (i = 0; i < ret; ++i) {
			int ref_byte = byte_from_offset(offset + i);

			if (buffer[i] != ref_byte) {
				fprintf(stderr, "Byte at offset %llu should "
					"be 0x%02X, but is 0x%02X\n",
					(unsigned long long)(offset + i),
					(unsigned int)ref_byte, buffer[i]);
				return EXIT_FAILURE;
			}
		}

		offset += ret;
		TEST_ASSERT(offset <= 2097152);
	}

	TEST_EQUAL_UI(offset, 2097152);

	sqfs_drop(ti);
	TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1);
	sqfs_drop(fp);
	return EXIT_SUCCESS;
}