0
0
mirror of https://github.com/libarchive/libarchive.git synced 2026-01-18 17:11:25 +01:00

Moving the tests' CRC-32 function to test_utils. (#2390)

Following up from #2284, putting the tests' CRC-32 implementation in
test_utils and having all tests use it.
This commit is contained in:
ljdarj
2025-03-08 04:21:23 +01:00
committed by GitHub
parent 3512329ba9
commit de77e6d671
14 changed files with 48 additions and 253 deletions

View File

@@ -34,7 +34,6 @@
#endif
#define __LIBARCHIVE_BUILD
#include <archive_crc32.h>
/*
* Extract a non-encoded file.
@@ -406,7 +405,7 @@ test_extract_file_zstd_bcj_nobjc(const char *refname)
la_ssize_t bytes_read = archive_read_data(a, buff, sizeof(buff));
assert(bytes_read >= 0);
if (bytes_read == 0) break;
computed_crc = crc32(computed_crc, buff, bytes_read);
computed_crc = bitcrc32(computed_crc, buff, bytes_read);
}
assertEqualInt(computed_crc, expected_crc);
@@ -1075,7 +1074,7 @@ test_arm_filter(const char *refname)
assertEqualString("hw-gnueabihf", archive_entry_pathname(ae));
assertEqualInt(sizeof(buff), archive_entry_size(ae));
assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
computed_crc = crc32(computed_crc, buff, sizeof(buff));
computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
assertEqualInt(computed_crc, expected_crc);
assertEqualInt(1, archive_file_count(a));
@@ -1149,7 +1148,7 @@ test_arm64_filter(const char *refname)
assertEqualString("hw-arm64", archive_entry_pathname(ae));
assertEqualInt(sizeof(buff), archive_entry_size(ae));
assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
computed_crc = crc32(computed_crc, buff, sizeof(buff));
computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
assertEqualInt(computed_crc, expected_crc);
assertEqualInt(1, archive_file_count(a));
@@ -1342,7 +1341,7 @@ test_riscv_filter(const char *refname)
assertEqualInt(sizeof(buff), archive_entry_size(ae));
assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff)));
computed_crc = crc32(computed_crc, buff, sizeof(buff));
computed_crc = bitcrc32(computed_crc, buff, sizeof(buff));
assertEqualInt(computed_crc, expected_crc);
assertEqualInt(1, archive_file_count(a));
@@ -1399,7 +1398,7 @@ test_sparc_filter(const char *refname)
assertEqualInt(expected_entry_size, archive_entry_size(ae));
assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size));
computed_crc = crc32(computed_crc, buff, expected_entry_size);
computed_crc = bitcrc32(computed_crc, buff, expected_entry_size);
assertEqualInt(computed_crc, expected_crc);
assertEqualInt(1, archive_file_count(a));
@@ -1472,7 +1471,7 @@ test_powerpc_filter(const char *refname)
assertEqualInt(expected_entry_size, archive_entry_size(ae));
assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size));
computed_crc = crc32(computed_crc, buff, expected_entry_size);
computed_crc = bitcrc32(computed_crc, buff, expected_entry_size);
assertEqualInt(computed_crc, expected_crc);
assertEqualInt(1, archive_file_count(a));

View File

@@ -27,7 +27,7 @@
/* Some tests will want to calculate some CRC32's, and this header can
* help. */
#define __LIBARCHIVE_BUILD
#include <archive_crc32.h>
#include <archive_endian.h>
#define PROLOGUE(reffile) \
struct archive_entry *ae; \
@@ -106,7 +106,7 @@ int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc) {
goto fn_exit;
}
computed_crc = crc32(0, buf, fsize);
computed_crc = bitcrc32(0, buf, fsize);
assertEqualInt(computed_crc, crc);
ret = 0;
@@ -335,7 +335,7 @@ DEFINE_TEST(test_read_format_rar5_blake2)
assertA(proper_size == archive_read_data(a, buf, proper_size));
/* To be extra pedantic, let's also check crc32 of the poem. */
assertEqualInt(crc32(0, buf, proper_size), 0x7E5EC49E);
assertEqualInt(bitcrc32(0, buf, proper_size), 0x7E5EC49E);
assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
EPILOGUE();
@@ -358,7 +358,7 @@ DEFINE_TEST(test_read_format_rar5_arm_filter)
/* Yes, RARv5 unpacker itself should calculate the CRC, but in case
* the DONT_FAIL_ON_CRC_ERROR define option is enabled during compilation,
* let's still fail the test if the unpacked data is wrong. */
assertEqualInt(crc32(0, buf, proper_size), 0x886F91EB);
assertEqualInt(bitcrc32(0, buf, proper_size), 0x886F91EB);
assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae));
EPILOGUE();
@@ -869,7 +869,7 @@ DEFINE_TEST(test_read_format_rar5_block_by_block)
if(bytes_read <= 0)
break;
computed_crc = crc32(computed_crc, buf, bytes_read);
computed_crc = bitcrc32(computed_crc, buf, bytes_read);
}
assertEqualInt(computed_crc, 0x7CCA70CD);

View File

@@ -26,7 +26,6 @@
#include "test.h"
#define __LIBARCHIVE_BUILD
#include <archive_crc32.h>
static
int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc)
@@ -47,7 +46,7 @@ int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc)
goto fn_exit;
}
computed_crc = crc32(0, buf, fsize);
computed_crc = bitcrc32(0, buf, fsize);
assertEqualInt(computed_crc, crc);
ret = 0;
@@ -84,7 +83,7 @@ int extract_one_using_blocks(struct archive* a, int block_size, uint32_t crc)
/* ok */
}
computed_crc = crc32(computed_crc, buf, bytes_read);
computed_crc = bitcrc32(computed_crc, buf, bytes_read);
}
assertEqualInt(computed_crc, crc);

View File

@@ -31,34 +31,6 @@
* written in streaming mode with Zip64 extensions enabled.
*/
static unsigned long
bitcrc32(unsigned long c, void *_p, size_t s)
{
/* This is a drop-in replacement for crc32() from zlib.
* Libarchive should be able to correctly generate
* uncompressed zip 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 correct, compact, and works well
* enough for this particular usage. Libarchive internally
* uses a much more efficient implementation. */
const unsigned char *p = _p;
int 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);
}
DEFINE_TEST(test_write_format_zip64_stream)
{
struct archive *a;

View File

@@ -7,35 +7,6 @@
#ifdef HAVE_BZLIB_H
#include <bzlib.h>
static unsigned long
bitcrc32(unsigned long c, const void *_p, size_t s)
{
/* This is a drop-in replacement for crc32() from zlib, given that
* libbzip2 doesn't expose its CRC32 function, as far as I'm aware.
* Libarchive should be able to correctly generate BZIP2-compressed
* zip 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 very,
* very obviously correct, compact, and works well for this
* particular usage. Libarchive internally uses a much more
* efficient implementation when zlib is unavailable */
const unsigned char *p = _p;
int 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);
}
/* File data */
static const char file_name[] = "file";
static const char file_data1[] = {'1', '2', '3', '4', '5', '6', '7', '8'};

View File

@@ -9,11 +9,8 @@
/* File data */
static const char file_name[] = "file";
/* We have liblzma's lzma_crc32() so no need to rely on a handmade
* CRC-32...but it requires a uint8_t* for its data, hence the change
* compared to the usual */
static const uint8_t file_data1[] = {'.', ';', ':', '!', '?', ',', '"', '\'', ')', '(', '*'};
static const uint8_t file_data2[] = {'-', '/', '>', '$', '\\', '#', '@', '+', '=', '{', ']', '[', '}', '&', '<', '%'};
static const char file_data1[] = {'.', ';', ':', '!', '?', ',', '"', '\'', ')', '(', '*'};
static const char file_data2[] = {'-', '/', '>', '$', '\\', '#', '@', '+', '=', '{', ']', '[', '}', '&', '<', '%'};
static const int file_perm = 00644;
static const short file_uid = 10;
static const short file_gid = 20;
@@ -125,8 +122,8 @@ static void verify_xz_lzma(const char *buff, size_t used, uint16_t id,
assertEqualInt(i2le(p + 10), id); /* Compression method */
assertEqualInt(i2le(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
assertEqualInt(i2le(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
crc = lzma_crc32(file_data1, sizeof(file_data1), 0);
crc = lzma_crc32(file_data2, sizeof(file_data2), crc);
crc = bitcrc32(0, file_data1, sizeof(file_data1));
crc = bitcrc32(crc, file_data2, sizeof(file_data2));
assertEqualInt(i4le(p + 16), crc); /* CRC-32 */
assertEqualInt(i4le(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
assertEqualInt(i2le(p + 28), strlen(file_name)); /* Pathname length */

View File

@@ -45,34 +45,6 @@ static const short folder_gid = 40;
static time_t now;
static 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 generate
* uncompressed zip 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 correct, compact, and works well
* enough for this particular usage. Libarchive internally
* uses a much more efficient implementation. */
const unsigned char *p = _p;
int 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);
}
static void verify_write_uncompressed(struct archive *a)
{
struct archive_entry *entry;

View File

@@ -7,35 +7,6 @@
#ifdef HAVE_ZSTD_H
#include <zstd.h>
static unsigned long
bitcrc32(unsigned long c, const void *_p, size_t s)
{
/* This is a drop-in replacement for crc32() from zlib, given that
* libzstd doesn't use CRC32 in the first place, let alone has a
* function for it. Libarchive should be able to correctly generate
* ZSTD-compressed zip 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 very, very obviously correct, compact, and works well for
* this particular usage. Libarchive internally uses a much more
* efficient implementation when zlib is unavailable */
const unsigned char *p = _p;
int 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);
}
/* File data */
static const char file_name[] = "file";
static const char file_data1[] = {'~', 'Z', '`', '^', 'Y', 'X', 'N', 'W', 'V', 'G', 'H', 'I', 'J'};

View File

@@ -42,38 +42,6 @@ static const short folder_gid = 40;
#define ZIP_ENTRY_FLAG_LENGTH_AT_END (1 << 3)
static 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 generate
* uncompressed zip 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 correct, compact, and works well
* enough for this particular usage. Libarchive internally
* uses a much more efficient implementation. */
const unsigned char *p = _p;
int 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);
}
static void write_archive(struct archive *a)
{
struct archive_entry *entry = archive_entry_new();

View File

@@ -35,34 +35,6 @@
* with a single file written to it.
*/
static unsigned long
bitcrc32(unsigned long c, void *_p, size_t s)
{
/* This is a drop-in replacement for crc32() from zlib.
* Libarchive should be able to correctly generate
* uncompressed zip 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 correct, compact, and works well
* enough for this particular usage. Libarchive internally
* uses a much more efficient implementation. */
const unsigned char *p = _p;
int 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);
}
DEFINE_TEST(test_write_format_zip_file)
{
struct archive *a;

View File

@@ -35,34 +35,6 @@
* with a single file written to it that uses Zip64 extensions.
*/
static unsigned long
bitcrc32(unsigned long c, void *_p, size_t s)
{
/* This is a drop-in replacement for crc32() from zlib.
* Libarchive should be able to correctly generate
* uncompressed zip 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 correct, compact, and works well
* enough for this particular usage. Libarchive internally
* uses a much more efficient implementation. */
const unsigned char *p = _p;
int 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);
}
DEFINE_TEST(test_write_format_zip_file_zip64)
{
struct archive *a;

View File

@@ -31,34 +31,6 @@
* written in streaming mode WITHOUT Zip64 extensions enabled.
*/
static unsigned long
bitcrc32(unsigned long c, void *_p, size_t s)
{
/* This is a drop-in replacement for crc32() from zlib.
* Libarchive should be able to correctly generate
* uncompressed zip 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 correct, compact, and works well
* enough for this particular usage. Libarchive internally
* uses a much more efficient implementation. */
const unsigned char *p = _p;
int 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);
}
DEFINE_TEST(test_write_format_zip_stream)
{
struct archive *a;

View File

@@ -110,6 +110,34 @@ fill_with_pseudorandom_data(void *buffer, size_t size)
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_)

View File

@@ -31,7 +31,9 @@
#include <stdint.h>
/* Fill a buffer with pseudorandom data */
void fill_with_pseudorandom_data(void* buffer, size_t size);
void fill_with_pseudorandom_data(void*, size_t);
/* A simplistic CRC-32 function for testing purposes */
unsigned long bitcrc32(unsigned long, const void*, size_t);
/* Read little-endian integers */
unsigned short i2le(const void*);
unsigned int i4le(const void*);