aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/test/stream_splice.c
blob: 6f40d1fe046ec78fbb7651a76253db64bfbd120c (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * stream_splice.c
 *
 * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "io/mem.h"
#include "util/test.h"
#include "sqfs/io.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 rd_buffer[941];

static sqfs_u8 byte_at_offset(sqfs_u64 off)
{
	if (off < end0)
		return 'A';
	if (off < end1)
		return 'B';
	return 'C';
}

static void init_rd_buffer(void)
{
	for (size_t i = 0; i < end2; ++i)
		rd_buffer[i] = byte_at_offset(i);
}

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

static int out_append(sqfs_ostream_t *strm, const void *data, size_t size);

static sqfs_u64 out_offset = 0;

static sqfs_ostream_t out = {
	{ 1, NULL, NULL },
	out_append,
	NULL,
	NULL,
};

static int out_append(sqfs_ostream_t *strm, const void *data, size_t size)
{
	const sqfs_u8 *ptr = data;

	TEST_ASSERT(strm == &out);
	TEST_ASSERT(size > 0);

	while (size--) {
		sqfs_u8 x = *(ptr++);
		sqfs_u8 y = byte_at_offset(out_offset++);

		TEST_EQUAL_UI(x, y);
		TEST_ASSERT(out_offset <= end2);
	}

	return 0;
}

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

int main(int argc, char **argv)
{
	sqfs_u64 total = 0;
	sqfs_istream_t *in;
	sqfs_s32 ret;
	(void)argc; (void)argv;

	init_rd_buffer();
	in = istream_memory_create("memory_in", 109,
				   rd_buffer, sizeof(rd_buffer));
	TEST_NOT_NULL(in);

	for (;;) {
		ret = sqfs_istream_splice(in, &out, 211);
		TEST_ASSERT(ret >= 0);

		if (ret == 0)
			break;

		total += ret;
		TEST_ASSERT(total <= end2);
		TEST_ASSERT(out_offset <= end2);
		TEST_EQUAL_UI(total, out_offset);
	}

	TEST_EQUAL_UI(total, end2);
	TEST_EQUAL_UI(out_offset, end2);
	sqfs_drop(in);
	return EXIT_SUCCESS;
}