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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* istream_skip.c
*
* Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include "io/istream.h"
#include "util/test.h"
static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */
static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */
static const sqfs_u64 end2 = 941; /* region 3: filled with 'C' */
static sqfs_u8 buffer[103]; /* sliding window into the file */
static sqfs_u64 offset = 0; /* byte offset into the "file" */
static int dummy_precache(istream_t *strm);
static const char *dummy_get_filename(istream_t *strm);
static istream_t dummy = {
{
1,
NULL,
NULL,
},
0,
0,
false,
buffer,
dummy_precache,
dummy_get_filename,
};
static int dummy_precache(istream_t *strm)
{
sqfs_u8 x;
TEST_ASSERT(strm == &dummy);
while (strm->buffer_used < sizeof(buffer)) {
if (offset < end0) {
x = 'A';
} else if (offset < end1) {
x = 'B';
} else if (offset < end2) {
x = 'C';
} else {
strm->eof = true;
break;
}
strm->buffer[strm->buffer_used++] = x;
++offset;
}
return 0;
}
static const char *dummy_get_filename(istream_t *strm)
{
TEST_ASSERT(strm == &dummy);
return "dummy file";
}
int main(int argc, char **argv)
{
sqfs_u8 read_buffer[61];
sqfs_u64 read_off = 0;
const char *name;
(void)argc; (void)argv;
name = istream_get_filename(&dummy);
TEST_NOT_NULL(name);
TEST_STR_EQUAL(name, "dummy file");
/* region 1 */
while (read_off < end0) {
size_t read_diff = end0 - read_off;
if (read_diff > sizeof(read_buffer))
read_diff = sizeof(read_buffer);
int ret = istream_read(&dummy, read_buffer, read_diff);
TEST_ASSERT(ret > 0);
TEST_ASSERT((size_t)ret <= read_diff);
for (int i = 0; i < ret; ++i) {
TEST_EQUAL_UI(read_buffer[i], 'A');
}
read_off += ret;
}
/* region 2 */
{
int ret = istream_skip(&dummy, end2 - end1);
TEST_EQUAL_I(ret, 0);
read_off += (end2 - end1);
}
/* region 3 */
for (;;) {
size_t read_diff = sizeof(read_buffer);
int ret = istream_read(&dummy, read_buffer, read_diff);
TEST_ASSERT(ret >= 0);
TEST_ASSERT((size_t)ret <= read_diff);
if (ret == 0) {
TEST_EQUAL_UI(read_off, end2);
break;
}
for (int i = 0; i < ret; ++i) {
TEST_EQUAL_UI(read_buffer[i], 'C');
}
read_off += ret;
TEST_ASSERT(read_off <= end2);
}
return EXIT_SUCCESS;
}
|