File: test_utils.c

package info (click to toggle)
libarchive 3.8.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,896 kB
  • sloc: ansic: 172,236; sh: 6,022; makefile: 1,951; cpp: 1,535; awk: 770
file content (159 lines) | stat: -rw-r--r-- 4,942 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
/*
 * Copyright (c) 2003-2012 Tim Kientzle
 * Copyright (c) 2012 Andres Mejia
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "test_utils.h"

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

static inline uint64_t
xorshift64(uint64_t *state)
{
	uint64_t x = *state;
	x ^= x << 13;
	x ^= x >> 7;
	x ^= x << 17;
	*state = x;
	return (x);
}

/*
 * Fill a buffer with reproducible pseudo-random data using a simple xorshift
 * algorithm. Originally, most tests filled buffers with a loop that calls
 * rand() once for each byte. However, this initialization can be extremely
 * slow when running on emulated platforms such as QEMU where 16M calls to
 * rand() take a long time: Before the test_write_format_7zip_large_copy test
 * took ~22 seconds, whereas using a xorshift random number generator (that can
 * be inlined) reduces it to ~17 seconds on QEMU RISC-V.
 */
static void
fill_with_pseudorandom_data_seed(uint64_t seed, void *buffer, size_t size)
{
	uint64_t *aligned_buffer;
	size_t num_values;
	size_t i;
	size_t unaligned_suffix;
	size_t unaligned_prefix = 0;
	/*
	 * To avoid unaligned stores we only fill the aligned part of the buffer
	 * with pseudo-random data and fill the unaligned prefix with 0xab and
	 * the suffix with 0xcd.
	 */
	if ((uintptr_t)buffer % sizeof(uint64_t)) {
		unaligned_prefix =
		    sizeof(uint64_t) - (uintptr_t)buffer % sizeof(uint64_t);
		aligned_buffer =
		    (uint64_t *)((char *)buffer + unaligned_prefix);
		memset(buffer, 0xab, unaligned_prefix);
	} else {
		aligned_buffer = (uint64_t *)buffer;
	}
	assert((uintptr_t)aligned_buffer % sizeof(uint64_t) == 0);
	num_values = (size - unaligned_prefix) / sizeof(uint64_t);
	unaligned_suffix =
	    size - unaligned_prefix - num_values * sizeof(uint64_t);
	for (i = 0; i < num_values; i++) {
		aligned_buffer[i] = xorshift64(&seed);
	}
	if (unaligned_suffix) {
		memset((char *)buffer + size - unaligned_suffix, 0xcd,
		    unaligned_suffix);
	}
}

void
fill_with_pseudorandom_data(void *buffer, size_t size)
{
	uint64_t seed;
	const char* seed_str;
	/*
	 * Check if a seed has been specified in the environment, otherwise fall
	 * back to using rand() as a seed.
	 */
	if ((seed_str = getenv("TEST_RANDOM_SEED")) != NULL) {
		errno = 0;
		seed = strtoull(seed_str, NULL, 10);
		if (errno != 0) {
			fprintf(stderr, "strtoull(%s) failed: %s", seed_str,
			    strerror(errno));
			seed = rand();
		}
	} else {
		seed = rand();
	}
	fill_with_pseudorandom_data_seed(seed, buffer, size);
}

unsigned long
bitcrc32(unsigned long c, const void *_p, size_t s)
{
	/* This is a drop-in replacement for crc32() from zlib.
	 * Libarchive should be able to correctly read archives (including
	 * correct CRCs) even when zlib is unavailable, and this function
	 * helps us verify that. Yes, this is very, very slow and unsuitable
	 * for production use, but it's obviously correct, compact, and
	 * works well enough for this particular usage. Libarchive
	 * internally uses a much more efficient implementation if zlib is
	 * unavailable. */
	const unsigned char *p = _p;
	char bitctr;

	if (p == NULL)
		return (0);

	for (; s > 0; --s) {
		c ^= *p++;
		for (bitctr = 8; bitctr > 0; --bitctr) {
			if (c & 1) c = (c >> 1);
			else	   c = (c >> 1) ^ 0xedb88320;
			c ^= 0x80000000;
		}
	}
	return (c);
}

/* Read little-endian integers */
unsigned short
i2le(const void* p_)
{
	const unsigned char *p = p_;
	return (p[0] | (p[1] << 8));
}
unsigned int
i4le(const void* p_)
{
	const char *p = p_;
	return (i2le(p) | (i2le(p + 2) << 16));
}
unsigned long long
i8le(const void* p_)
{
	const char *p = p_;
	return (i4le(p) | ((unsigned long long)i4le(p + 4) << 32));
}