aboutsummaryrefslogtreecommitdiff
path: root/tests/libsqfs/table.c
blob: 5f240cca9d13098543ebfea3c191df06c8eb2352 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * table.c
 *
 * Copyright (C) 2021 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"
#include "compat.h"
#include "../test.h"

#include "sqfs/compressor.h"
#include "sqfs/error.h"
#include "sqfs/table.h"
#include "sqfs/io.h"

static sqfs_u8 file_data[32768];
static size_t file_used = 0;

static int dummy_read_at(sqfs_file_t *file, sqfs_u64 offset,
			 void *buffer, size_t size)
{
	(void)file;

	if (offset >= sizeof(file_data))
		return SQFS_ERROR_OUT_OF_BOUNDS;

	if (size > (sizeof(file_data) - offset))
		return SQFS_ERROR_OUT_OF_BOUNDS;

	memset(buffer, 0, size);

	if (offset < file_used) {
		if (size > (file_used - offset))
			size = file_used - offset;

		memcpy(buffer, file_data + offset, size);
	}
	return 0;
}

static int dummy_write_at(sqfs_file_t *file, sqfs_u64 offset,
			  const void *buffer, size_t size)
{
	(void)file;

	if (offset >= sizeof(file_data))
		return SQFS_ERROR_OUT_OF_BOUNDS;

	if (size > (sizeof(file_data) - offset))
		return SQFS_ERROR_OUT_OF_BOUNDS;

	if (offset > file_used)
		memset(file_data + file_used, 0, offset - file_used);

	if ((offset + size) > file_used)
		file_used = offset + size;

	memcpy(file_data + offset, buffer, size);
	return 0;
}

static sqfs_u64 dummy_get_size(const sqfs_file_t *file)
{
	(void)file;
	return file_used;
}

static sqfs_s32 dummy_compress(sqfs_compressor_t *cmp, const sqfs_u8 *in,
			       sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize)
{
	(void)cmp;
	memcpy(out, in, outsize < size ? outsize : size);
	return 0;
}

static sqfs_s32 dummy_uncompress(sqfs_compressor_t *cmp, const sqfs_u8 *in,
				 sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize)
{
	(void)cmp;
	if (outsize < size)
		return 0;
	memcpy(out, in, size);
	return size;
}

static sqfs_file_t dummy_file = {
	{ NULL, NULL },
	dummy_read_at,
	dummy_write_at,
	dummy_get_size,
	NULL,
};

static sqfs_compressor_t dummy_compressor = {
	{ NULL, NULL },
	NULL,
	NULL,
	NULL,
	dummy_compress,
};

static sqfs_compressor_t dummy_uncompressor = {
	{ NULL, NULL },
	NULL,
	NULL,
	NULL,
	dummy_uncompress,
};

/*****************************************************************************/

static sqfs_u64 table[4000];

int main(int argc, char **argv)
{
	sqfs_u64 start, value, locations[4], *copy;
	sqfs_u16 hdr;
	size_t i;
	int ret;
	(void)argc; (void)argv;

	/* fill the table with data */
	for (i = 0; i < sizeof(table) / sizeof(table[0]); ++i)
		table[i] = i;

	/* serialize the table */
	ret = sqfs_write_table(&dummy_file, &dummy_compressor,
			       table, sizeof(table), &start);
	TEST_EQUAL_I(ret, 0);

	TEST_EQUAL_UI(file_used, (3 * (8192 + 2) + (7424 + 2) + 4 * sizeof(sqfs_u64)));
	TEST_EQUAL_UI(start, (3 * (8192 + 2) + (7424 + 2)));

	/* check the location list */
	memcpy(locations, file_data + start, sizeof(locations));
	for (i = 0; i < 4; ++i)
		locations[i] = le64toh(locations[i]);

	TEST_EQUAL_UI(locations[0], 0);
	TEST_EQUAL_UI(locations[1], (1 * (8192 + 2)));
	TEST_EQUAL_UI(locations[2], (2 * (8192 + 2)));
	TEST_EQUAL_UI(locations[3], (3 * (8192 + 2)));

	/* check the individual blocks */
	memcpy(&hdr, file_data + locations[0], sizeof(hdr));
	hdr = le16toh(hdr);
	TEST_EQUAL_UI(hdr, (0x8000 | 8192));

	for (i = 0; i < 8192; i += sizeof(sqfs_u64)) {
		memcpy(&value, (file_data + locations[0] + 2) + i,
		       sizeof(value));

		TEST_EQUAL_UI(value, i / sizeof(sqfs_u64));
	}

	memcpy(&hdr, file_data + locations[1], sizeof(hdr));
	hdr = le16toh(hdr);
	TEST_EQUAL_UI(hdr, (0x8000 | 8192));

	for (i = 0; i < 8192; i += sizeof(sqfs_u64)) {
		memcpy(&value, (file_data + locations[1] + 2) + i,
		       sizeof(value));

		TEST_EQUAL_UI(value, (1024 + i / sizeof(sqfs_u64)));
	}

	memcpy(&hdr, file_data + locations[2], sizeof(hdr));
	hdr = le16toh(hdr);
	TEST_EQUAL_UI(hdr, (0x8000 | 8192));

	for (i = 0; i < 8192; i += sizeof(sqfs_u64)) {
		memcpy(&value, (file_data + locations[2] + 2) + i,
		       sizeof(value));

		TEST_EQUAL_UI(value, (2048 + i / sizeof(sqfs_u64)));
	}

	memcpy(&hdr, file_data + locations[3], sizeof(hdr));
	hdr = le16toh(hdr);
	TEST_EQUAL_UI(hdr, (0x8000 | 7424));

	for (i = 0; i < 7424; i += sizeof(sqfs_u64)) {
		memcpy(&value, (file_data + locations[3] + 2) + i,
		       sizeof(value));

		TEST_EQUAL_UI(value, (3072 + i / sizeof(sqfs_u64)));
	}

	/* read the table back */
	ret = sqfs_read_table(&dummy_file, &dummy_uncompressor,
			      sizeof(table), start, 0, start, (void **)&copy);
	TEST_EQUAL_I(ret, 0);

	ret = memcmp(copy, table, sizeof(table));
	TEST_EQUAL_I(ret, 0);

	free(copy);
	return EXIT_SUCCESS;
}