Skip to content

Commit

Permalink
Move fuzzer tests to fuzzer file.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed May 8, 2020
1 parent 3531868 commit 56ad235
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 253 deletions.
2 changes: 1 addition & 1 deletion src/c/tst/Makefile
@@ -1,7 +1,7 @@
TESTS += test_detools.c
TESTS += test_dump_restore.c
TESTS += test_command_line.c
TESTS += test_fuzzer_corpus.c
TESTS += test_fuzzer.c
INC += ../heatshrink
SRC += ../detools.c
SRC += ../heatshrink/heatshrink_decoder.c
Expand Down
166 changes: 0 additions & 166 deletions src/c/tst/test_detools.c
Expand Up @@ -961,169 +961,3 @@ TEST(error_as_string)
ASSERT_EQ(detools_error_as_string(123456),
"Unknown error.");
}

static const uint8_t from_buf[256] = { 0, };
static int from_offset = 0;

static int from_read(void *arg_p, uint8_t *buf_p, size_t size)
{
(void)arg_p;

if (((size_t)from_offset + size) > 256) {
return (-1);
}

memcpy(buf_p, &from_buf[from_offset], size);
from_offset += size;

return (0);
}

static int from_seek(void *arg_p, int offset)
{
(void)arg_p;

if ((from_offset + offset) < 0) {
return (-1);
}

from_offset += offset;

return (0);
}

static int to_write(void *arg_p, const uint8_t *buf_p, size_t size)
{
(void)arg_p;
(void)buf_p;
(void)size;

return (0);
}

static void test_fuzzer(const uint8_t *patch_buf_p,
size_t patch_size,
int process_res,
int finalize_res)
{
struct detools_apply_patch_t apply_patch;
int res;

res = detools_apply_patch_init(&apply_patch,
from_read,
from_seek,
patch_size,
to_write,
NULL);
ASSERT_EQ(res, 0);

res = detools_apply_patch_process(&apply_patch, patch_buf_p, patch_size);

WITH_MESSAGE("Failed with '%s' (%d).", detools_error_as_string(res), res) {
ASSERT_EQ(res, process_res);
}

res = detools_apply_patch_finalize(&apply_patch);

WITH_MESSAGE("Failed with '%s' (%d).", detools_error_as_string(res), res) {
ASSERT_EQ(res, finalize_res);
}
}

TEST(fuzzer_size_overflow)
{
const uint8_t patch[] = {
0x04, 0x0c, 0xfd, 0xff, 0x00, 0x00, 0x00, 0x5d, 0x00, 0x2b,
0x06, 0x66
};

test_fuzzer(&patch[0],
sizeof(patch),
-DETOOLS_CORRUPT_PATCH_OVERFLOW,
-DETOOLS_ALREADY_FAILED);
}

TEST(fuzzer_infinite_loop)
{
const uint8_t patch[] = {
0x02, 0x3a, 0x01, 0xce, 0xce, 0xce, 0xfe, 0xff, 0x00, 0x00,
0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

test_fuzzer(&patch[0],
sizeof(patch),
-DETOOLS_CORRUPT_PATCH_OVERFLOW,
-DETOOLS_ALREADY_FAILED);
}

TEST(fuzzer_lzma_decode_failure)
{
const uint8_t patch[] = {
0x01, 0x5b, 0x00, 0x00, 0x00, 0x00, 0xe2, 0xff, 0x8c, 0x8c,
0x8c, 0x00, 0x00, 0x00, 0x0f, 0x02
};

test_fuzzer(&patch[0],
sizeof(patch),
-DETOOLS_LZMA_DECODE,
-DETOOLS_ALREADY_FAILED);
}

TEST(fuzzer_corrupt_crle_idle_out_of_data)
{
const uint8_t patch[] = {
0x02, 0x7a
};

test_fuzzer(&patch[0], sizeof(patch), 0, -DETOOLS_NOT_ENOUGH_PATCH_DATA);
}

TEST(fuzzer_dfpatch_not_implemented)
{
const uint8_t patch[] = {
0x02, 0x08, 0x00, 0x40, 0x05, 0xfe
};

test_fuzzer(&patch[0],
sizeof(patch),
-DETOOLS_NOT_IMPLEMENTED,
-DETOOLS_ALREADY_FAILED);
}

TEST(fuzzer_corrupt_crle_kind)
{
const uint8_t patch[] = {
0x02, 0x0a, 0x3d
};

test_fuzzer(&patch[0],
sizeof(patch),
-DETOOLS_CORRUPT_PATCH_CRLE_KIND,
-DETOOLS_ALREADY_FAILED);
}

TEST(fuzzer_bad_from_read_error)
{
const uint8_t patch[] = {
0x04, 0xee, 0xee, 0x18, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00,
0xce, 0xc1, 0x27, 0x28, 0x09, 0xcf, 0xee, 0xce, 0xc1, 0x27,
0x28, 0x09, 0xcf
};

test_fuzzer(&patch[0],
sizeof(patch),
-DETOOLS_IO_FAILED,
-DETOOLS_ALREADY_FAILED);
}

TEST(fuzzer_size_overflow_header)
{
const uint8_t patch[] = {
0x04, 0xfc, 0xf7, 0xfe, 0xfb, 0x04
};

test_fuzzer(&patch[0],
sizeof(patch),
-DETOOLS_CORRUPT_PATCH_OVERFLOW,
-DETOOLS_ALREADY_FAILED);
}

0 comments on commit 56ad235

Please sign in to comment.