aboutsummaryrefslogtreecommitdiff
path: root/tests/blk_proc_order.c
blob: 9157262921bf92541c436a69aa2997cc9a86c5a6 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * add_by_path.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "sqfs/block_processor.h"
#include "sqfs/compress.h"

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

#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif

static ssize_t dummy_do_block(sqfs_compressor_t *cmp, const uint8_t *in,
			      size_t size, uint8_t *out, size_t outsize)
{
	int delay, result;
	(void)cmp; (void)size; (void)outsize;

	delay = ((int *)in)[0];
	result = ((int *)in)[1];

	memcpy(out, in, result);
	sleep(delay);
	return (size_t)result >= size ? 0 : result;
}

static sqfs_compressor_t *dummy_create_copy(sqfs_compressor_t *cmp)
{
	sqfs_compressor_t *cpy = malloc(sizeof(*cpy));
	assert(cpy != NULL);
	*cpy = *cmp;
	return cpy;
}

static void dummy_destroy(sqfs_compressor_t *cmp)
{
	free(cmp);
}

static sqfs_compressor_t *dummy_create(void)
{
	sqfs_compressor_t *cmp = calloc(1, sizeof(*cmp));
	assert(cmp != NULL);

	cmp->do_block = dummy_do_block;
	cmp->create_copy = dummy_create_copy;
	cmp->destroy = dummy_destroy;

	return cmp;
}

static unsigned int blk_index = 0;

#ifdef HAVE_PTHREAD
static pthread_t main_thread;
#endif

static int block_callback(void *user, sqfs_block_t *blk)
{
	assert(main_thread == pthread_self());
	assert(blk->index == blk_index++);

	if (blk->index == 4) {
		assert(blk->size == 4 * sizeof(int));
		assert(blk->flags & SQFS_BLK_DONT_COMPRESS);
		assert(blk->flags & SQFS_BLK_DONT_CHECKSUM);
		assert(!(blk->flags & SQFS_BLK_IS_COMPRESSED));
	} else {
		if (blk->index & 0x01) {
			assert(!(blk->flags & SQFS_BLK_IS_COMPRESSED));
			assert(blk->size == 4 * sizeof(int));
		} else {
			assert(blk->flags & SQFS_BLK_IS_COMPRESSED);
			assert(blk->size == 2 * sizeof(int));
		}
	}

	(void)user;
	return 0;
}

int main(void)
{
	sqfs_compressor_t *cmp = dummy_create();
	sqfs_block_processor_t *proc;
	sqfs_block_t *blk;
	int i;

#ifdef HAVE_PTHREAD
	main_thread = pthread_self();
#endif

	proc = sqfs_block_processor_create(4 * sizeof(int), cmp, 4, 10,
					   NULL, block_callback);
	assert(proc != NULL);

	for (i = 0; i < 4; ++i) {
		blk = calloc(1, sizeof(*blk) + 4 * sizeof(int));
		assert(blk != NULL);
		blk->size = 4 * sizeof(int);
		blk->index = i;
		((int *)blk->data)[0] = 4 - i;
		((int *)blk->data)[1] = (i & 0x01 ? 4 : 2) * sizeof(int);

		assert(sqfs_block_processor_enqueue(proc, blk) == 0);
	}

	blk = calloc(1, sizeof(*blk) + 4 * sizeof(int));
	assert(blk != NULL);
	blk->size = 4 * sizeof(int);
	blk->index = i;
	blk->flags |= SQFS_BLK_DONT_COMPRESS | SQFS_BLK_DONT_CHECKSUM;
	((int *)blk->data)[0] = 0;
	((int *)blk->data)[1] = (i & 0x01 ? 4 : 2) * sizeof(int);
	assert(sqfs_block_processor_enqueue(proc, blk) == 0);

	assert(sqfs_block_processor_finish(proc) == 0);

	sqfs_block_processor_destroy(proc);
	cmp->destroy(cmp);
	return EXIT_SUCCESS;
}