File: test-basic-reader.c

package info (click to toggle)
lhasa 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,364 kB
  • sloc: ansic: 6,796; sh: 1,938; makefile: 168; python: 129
file content (255 lines) | stat: -rw-r--r-- 7,256 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*

Copyright (c) 2011, 2012, Simon Howard

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "lib/lha_basic_reader.h"
#include "crc32.h"

static LHABasicReader *reader_for_file(char *filename, LHAInputStream **stream)
{
	LHABasicReader *reader;

	*stream = lha_input_stream_from(filename);

	assert(*stream != NULL);

	reader = lha_basic_reader_new(*stream);
	assert(reader != NULL);

	return reader;
}

static void test_create_free(void)
{
	LHAInputStream *stream;
	LHABasicReader *reader;
	uint8_t buf[16];

	reader = reader_for_file("archives/lha213/lh5.lzh", &stream);

	// Sensible start conditions:

	assert(lha_basic_reader_curr_file(reader) == NULL);
	assert(lha_basic_reader_decode(reader) == NULL);
	assert(lha_basic_reader_read_compressed(reader, buf, sizeof(buf)) == 0);

	lha_basic_reader_free(reader);
	lha_input_stream_free(stream);
}

// Check that the specified file contains an archived file with the
// specified name.

static void check_directory_for(char *filename, char *archived)
{
	LHAInputStream *stream;
	LHABasicReader *reader;
	LHAFileHeader *header;
	uint8_t buf[16];

	reader = reader_for_file(filename, &stream);

	// Should be a single file:

	header = lha_basic_reader_next_file(reader);
	assert(header != NULL);
	assert(!strcmp(header->filename, archived));

	assert(lha_basic_reader_curr_file(reader) == header);

	// Only one file.

	assert(lha_basic_reader_next_file(reader) == NULL);
	assert(lha_basic_reader_curr_file(reader) == NULL);

	// Can't read at end of file.

	assert(lha_basic_reader_read_compressed(reader, buf, sizeof(buf)) == 0);
	assert(lha_basic_reader_decode(reader) == NULL);

	lha_basic_reader_free(reader);
	lha_input_stream_free(stream);
}

static void test_read_directory(void)
{
	check_directory_for("archives/larc333/lz4.lzs",       "gpl-2.gz");
	check_directory_for("archives/larc333/lz5.lzs",       "gpl-2");

	check_directory_for("archives/lha213/lh0.lzh",        "gpl-2.gz");
	check_directory_for("archives/lha213/lh5.lzh",        "gpl-2");

	check_directory_for("archives/lha255e/lh0.lzh",       "gpl-2.gz");
	check_directory_for("archives/lha255e/lh5.lzh",       "gpl-2");

	check_directory_for("archives/lha_amiga_122/lh0.lzh", "gpl-2.gz");
	check_directory_for("archives/lha_amiga_122/lh1.lzh", "gpl-2");
	check_directory_for("archives/lha_amiga_122/lh4.lzh", "gpl-2");
	check_directory_for("archives/lha_amiga_122/lh5.lzh", "gpl-2");
	check_directory_for("archives/lha_amiga_212/lh1.lzh", "gpl-2");
	check_directory_for("archives/lha_amiga_212/lh6.lzh", "gpl-2");

	check_directory_for("archives/lha_unix114i/h1_lh0.lzh", "gpl-2.gz");
	check_directory_for("archives/lha_unix114i/h1_lh5.lzh", "gpl-2");
	check_directory_for("archives/lha_unix114i/h1_lh6.lzh", "gpl-2");
	check_directory_for("archives/lha_unix114i/h1_lh7.lzh", "gpl-2");

	check_directory_for("archives/lharc113/lh0.lzh",      "gpl-2.gz");
	check_directory_for("archives/lharc113/lh1.lzh",      "gpl-2");

	check_directory_for("archives/pmarc2/pm0.pma",        "gpl-2.gz");
	check_directory_for("archives/pmarc2/pm2.pma",        "gpl-2.");
}

static void test_read_sfx(void)
{
	check_directory_for("archives/larc333/sfx.com",       "gpl-2.gz");
	check_directory_for("archives/lha213/sfx.exe",        "gpl-2");
	check_directory_for("archives/lha255e/sfx.exe",       "gpl-2");
	check_directory_for("archives/lha_amiga_122/sfx.run", "gpl-2");
	check_directory_for("archives/lharc113/sfx.com",      "gpl-2");
	check_directory_for("archives/lharc_atari_313a/sfx.tos", "gpl2");
	check_directory_for("archives/pmarc2/sfx.com",        "gpl-2.");
}

// Check CRC of compressed data.

static void check_crc_for(char *filename, uint32_t expected_crc,
                          size_t expected_len)
{
	LHAInputStream *stream;
	LHABasicReader *reader;
	LHAFileHeader *header;
	uint8_t buf[16];
	uint32_t crc;
	size_t len;

	reader = reader_for_file(filename, &stream);

	// Get the first archived file:

	header = lha_basic_reader_next_file(reader);
	assert(header != NULL);

	// Read all the compressed data:

	crc = 0; len = 0;

	for (;;) {
		size_t count;

		count = lha_basic_reader_read_compressed(reader, buf,
		                                         sizeof(buf));

		if (count == 0) {
			break;
		}

		len += count;

		crc32_buf(&crc, buf, count);
	}

	// Check final CRC / length:

	assert(crc == expected_crc);
	assert(len == expected_len);

	lha_basic_reader_free(reader);
	lha_input_stream_free(stream);
}

static void test_read_compressed(void)
{
	check_crc_for("archives/larc333/lz5.lzs",  0x2c1539b5, 8480);
	check_crc_for("archives/lha213/lh0.lzh",   0xe4690583, 6829);
	check_crc_for("archives/lha213/lh5.lzh",   0x45b943c8, 7004);

	check_crc_for("archives/lha213/sfx.exe",   0x45b943c8, 7004);
	check_crc_for("archives/pmarc2/sfx.com",   0x3751177e, 7098);
}

static void check_decode_for(char *filename)
{
	LHAInputStream *stream;
	LHABasicReader *reader;
	LHAFileHeader *header;
	LHADecoder *decoder;

	reader = reader_for_file(filename, &stream);

	// Get the first archived file:

	header = lha_basic_reader_next_file(reader);
	assert(header != NULL);

	// Check that it is possible to create a decoder for this file.

	decoder = lha_basic_reader_decode(reader);
	assert(decoder != NULL);
	lha_decoder_free(decoder);
	lha_basic_reader_free(reader);
	lha_input_stream_free(stream);
}

static void test_decode(void)
{
	check_decode_for("archives/larc333/lz4.lzs");
	check_decode_for("archives/larc333/lz5.lzs");

	check_decode_for("archives/lha213/lh0.lzh");
	check_decode_for("archives/lha213/lh5.lzh");

	check_decode_for("archives/lha255e/lh0.lzh");
	check_decode_for("archives/lha255e/lh5.lzh");

	check_decode_for("archives/lha_amiga_122/lh0.lzh");
	check_decode_for("archives/lha_amiga_122/lh1.lzh");
	check_decode_for("archives/lha_amiga_122/lh4.lzh");
	check_decode_for("archives/lha_amiga_122/lh5.lzh");
	check_decode_for("archives/lha_amiga_212/lh1.lzh");
	check_decode_for("archives/lha_amiga_212/lh6.lzh");

	check_decode_for("archives/lha_unix114i/h1_lh0.lzh");
	check_decode_for("archives/lha_unix114i/h1_lh5.lzh");
	check_decode_for("archives/lha_unix114i/h1_lh6.lzh");
	check_decode_for("archives/lha_unix114i/h1_lh7.lzh");

	check_decode_for("archives/lharc113/lh0.lzh");
	check_decode_for("archives/lharc113/lh1.lzh");

	check_decode_for("archives/pmarc2/pm0.pma");
	check_decode_for("archives/pmarc2/pm2.pma");
}

int main(int argc, char *argv[])
{
	test_create_free();
	test_read_directory();
	test_read_sfx();
	test_read_compressed();
	test_decode();

	return 0;
}