chunk: Add functions to map file contents to a chunk

This commit is contained in:
Martin Willi
2014-01-23 15:55:32 +01:00
parent fa9b6e88a0
commit 595b6d9a82
3 changed files with 149 additions and 1 deletions
+43 -1
View File
@@ -14,9 +14,10 @@
* for more details.
*/
#include "test_suite.h"
#include <unistd.h>
#include <utils/chunk.h>
/*******************************************************************************
@@ -774,6 +775,43 @@ START_TEST(test_chunk_hash_static)
}
END_TEST
/*******************************************************************************
* test for chunk_map and friends
*/
START_TEST(test_chunk_map)
{
chunk_t *map, contents = chunk_from_chars(0x01,0x02,0x03,0x04,0x05);
char *path = "/tmp/strongswan-chunk-map-test";
ck_assert(chunk_write(contents, path, "chunk_map", 022, TRUE));
/* read */
map = chunk_map(path, FALSE);
ck_assert(map != NULL);
ck_assert_msg(chunk_equals(*map, contents), "%B", map);
/* altering mapped chunk should not hurt */
*map = chunk_empty;
ck_assert(chunk_unmap(map));
/* write */
map = chunk_map(path, TRUE);
ck_assert(map != NULL);
ck_assert_msg(chunk_equals(*map, contents), "%B", map);
map->ptr[0] = 0x06;
ck_assert(chunk_unmap(map));
/* verify write */
contents.ptr[0] = 0x06;
map = chunk_map(path, FALSE);
ck_assert(map != NULL);
ck_assert_msg(chunk_equals(*map, contents), "%B", map);
ck_assert(chunk_unmap(map));
unlink(path);
}
END_TEST
/*******************************************************************************
* printf_hook tests
*/
@@ -891,6 +929,10 @@ Suite *chunk_suite_create()
tcase_add_test(tc, test_chunk_hash_static);
suite_add_tcase(s, tc);
tc = tcase_create("chunk_map");
tcase_add_test(tc, test_chunk_map);
suite_add_tcase(s, tc);
tc = tcase_create("printf_hook");
tcase_add_loop_test(tc, test_printf_hook_hash, 0, countof(printf_hook_data));
tcase_add_loop_test(tc, test_printf_hook_plus, 0, countof(printf_hook_data));