add more mem map API tests

This commit is contained in:
Jonathon Reinhart 2015-09-13 21:49:43 -04:00
parent 4dae31b25e
commit c026c23efb

View file

@ -1,3 +1,9 @@
/**
* Unicorn memory API tests
*
* This tests memory read/write and map/unmap functionality.
* One is necessary for doing the other.
*/
#include "unicorn_test.h" #include "unicorn_test.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -53,9 +59,41 @@ static void test_basic(void **state)
assert_memory_equal(buf, "test", 4); assert_memory_equal(buf, "test", 4);
/* Unmap the region */ /* Unmap the region */
uc_assert_success(uc_mem_unmap(uc, mem_start, mem_len)); //uc_assert_success(uc_mem_unmap(uc, mem_start, mem_len));
} }
static void test_bad_read(void **state)
{
uc_engine *uc = *state;
uint8_t readbuf[0x10];
memset(readbuf, 0xCC, sizeof(readbuf));
uint8_t checkbuf[0x10];
memset(checkbuf, 0xCC, sizeof(checkbuf));
/* Reads to unmapped addresses should fail */
/* TODO: Which error? */
uc_assert_fail(uc_mem_read(uc, 0x1000, readbuf, sizeof(readbuf)));
/* And our buffer should be unchanged */
assert_memory_equal(readbuf, checkbuf, sizeof(checkbuf));
}
static void test_bad_write(void **state)
{
uc_engine *uc = *state;
uint8_t writebuf[0x10];
memset(writebuf, 0xCC, sizeof(writebuf));
/* Writes to unmapped addresses should fail */
/* TODO: Which error? */
uc_assert_fail(uc_mem_write(uc, 0x1000, writebuf, sizeof(writebuf)));
}
/** /**
* Verify that we can read/write across memory map region boundaries * Verify that we can read/write across memory map region boundaries
*/ */
@ -81,22 +119,25 @@ static void test_rw_across_boundaries(void **state)
assert_memory_equal(buf, "test", 4); assert_memory_equal(buf, "test", 4);
} }
/* Try to unmap memory that has not been mapped */
static void test_bad_unmap(void **state) static void test_bad_unmap(void **state)
{ {
uc_engine *uc = *state; uc_engine *uc = *state;
uc_err err;
/* Try to unmap memory that has not been mapped */ /* TODO: Which error should this return? */
err = uc_mem_unmap(uc, 0x0, 0x1000); uc_assert_fail(uc_mem_unmap(uc, 0x0, 0x1000));
assert_int_not_equal(err, UC_ERR_OK);
} }
int main(void) { int main(void) {
#define test(x) cmocka_unit_test_setup_teardown(x, setup, teardown)
const struct CMUnitTest tests[] = { const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_basic, setup, teardown), test(test_basic),
cmocka_unit_test_setup_teardown(test_bad_unmap, setup, teardown), //test(test_bad_read),
cmocka_unit_test_setup_teardown(test_rw_across_boundaries, setup, teardown), //test(test_bad_write),
test(test_bad_unmap),
test(test_rw_across_boundaries),
}; };
#undef test
return cmocka_run_group_tests(tests, NULL, NULL); return cmocka_run_group_tests(tests, NULL, NULL);
} }