mirror of
https://github.com/pumpitupdev/pumptools.git
synced 2026-09-29 18:27:55 +03:00
Version 1.08 with public source code release.
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#include <cmocka/cmocka.h>
|
||||
|
||||
#include "capnhook/hook/lib.h"
|
||||
|
||||
#include "util/mem.h"
|
||||
|
||||
typedef int (*test_mock_function_t)(int a);
|
||||
|
||||
static int test_mock_function(int a)
|
||||
{
|
||||
return a * 2;
|
||||
}
|
||||
|
||||
static void test_cnh_lib_mock_init_unit_test(void** state)
|
||||
{
|
||||
struct cnh_lib_unit_test_func_mocks* mocks;
|
||||
size_t mocks_cnt;
|
||||
void* ptr;
|
||||
|
||||
mocks_cnt = 1;
|
||||
mocks = cnh_lib_allocate_func_mocks(mocks_cnt);
|
||||
|
||||
mocks[0].name = "test_mock_function";
|
||||
mocks[0].func = test_mock_function;
|
||||
|
||||
cnh_lib_init_unit_test(mocks, mocks_cnt);
|
||||
|
||||
ptr = cnh_lib_load("asdf");
|
||||
assert_ptr_not_equal(ptr, NULL);
|
||||
|
||||
ptr = cnh_lib_get_func_addr_handle(ptr, "test_mock_function");
|
||||
assert_ptr_equal(ptr, test_mock_function);
|
||||
|
||||
assert_int_equal(((test_mock_function_t) ptr)(2), 4);
|
||||
|
||||
ptr = cnh_lib_get_func_addr_handle(ptr, "asdf");
|
||||
assert_ptr_equal(ptr, NULL);
|
||||
|
||||
cnh_lib_unload(ptr);
|
||||
// Nothing happens here
|
||||
|
||||
ptr = cnh_lib_get_func_addr( "test_mock_function");
|
||||
assert_ptr_equal(ptr, test_mock_function);
|
||||
|
||||
cnh_lib_shutdown_unit_test();
|
||||
}
|
||||
|
||||
static void test_cnh_lib_real(void** state)
|
||||
{
|
||||
void* ptr;
|
||||
|
||||
cnh_lib_init();
|
||||
|
||||
ptr = cnh_lib_load("libc.so.6");
|
||||
|
||||
assert_ptr_not_equal(ptr, NULL);
|
||||
assert_ptr_equal(cnh_lib_load("asdfasdas"), NULL);
|
||||
|
||||
assert_ptr_not_equal(cnh_lib_get_func_addr_handle(ptr, "printf"), NULL);
|
||||
assert_ptr_not_equal(cnh_lib_get_func_addr("printf"), NULL);
|
||||
|
||||
assert_ptr_equal(cnh_lib_get_func_addr("asdasdas"), NULL);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const struct CMUnitTest tests[] ={
|
||||
cmocka_unit_test(test_cnh_lib_mock_init_unit_test),
|
||||
cmocka_unit_test(test_cnh_lib_real)
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <cmocka/cmocka.h>
|
||||
|
||||
#include "capnhook/hook/lib.h"
|
||||
|
||||
#include "hook/propatch/usb-fix.h"
|
||||
|
||||
#include "test-util/mem.h"
|
||||
|
||||
#include "util/log.h"
|
||||
#include "util/mem.h"
|
||||
|
||||
static const char* readlink_return_value = "../devices/pci0000:00/0000:00:14.0/usb2/2-2/2-2:1.0/host7/target7:0:0/7:0:0:0/block/sde";
|
||||
static const char* player1_match_buf = "../devices/pci0000:00/0000:00:14.0/usb2/1-1/1-1:1.0";
|
||||
static const char* player2_match_buf = "../devices/pci0000:00/0000:00:14.0/usb2/1-2/1-2:1.0";
|
||||
|
||||
extern ssize_t propatch_usb_fix_readlink(const char* path, char* buf, size_t len);
|
||||
|
||||
static uint32_t readlink_call_count = 0;
|
||||
|
||||
ssize_t readlink_mock(const char* path, char* buf, size_t len)
|
||||
{
|
||||
ssize_t len_return;
|
||||
|
||||
readlink_call_count++;
|
||||
|
||||
len_return = strlen(readlink_return_value);
|
||||
|
||||
assert_true(len >= len_return);
|
||||
|
||||
strcpy(buf, readlink_return_value);
|
||||
|
||||
return len_return;
|
||||
}
|
||||
|
||||
static int setup(void** state)
|
||||
{
|
||||
struct cnh_lib_unit_test_func_mocks* func_mocks;
|
||||
size_t func_mocks_cnt;
|
||||
|
||||
test_util_mem_install_mem_interface();
|
||||
|
||||
func_mocks_cnt = 1;
|
||||
func_mocks = cnh_lib_allocate_func_mocks(func_mocks_cnt);
|
||||
|
||||
func_mocks[0].name = "readlink";
|
||||
func_mocks[0].func = readlink_mock;
|
||||
|
||||
cnh_lib_init_unit_test(func_mocks, func_mocks_cnt);
|
||||
|
||||
readlink_call_count = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int teardown(void** state)
|
||||
{
|
||||
propatch_usb_fix_shutdown();
|
||||
|
||||
cnh_lib_shutdown_unit_test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_usb_fix_disabled(void** state)
|
||||
{
|
||||
ssize_t res;
|
||||
char buffer[256];
|
||||
|
||||
res = propatch_usb_fix_readlink(
|
||||
"/sys/block/sde/device",
|
||||
buffer,
|
||||
sizeof(buffer));
|
||||
|
||||
assert_int_equal(res, strlen(readlink_return_value));
|
||||
assert_string_equal(buffer, readlink_return_value);
|
||||
|
||||
assert_int_equal(readlink_call_count, 1);
|
||||
}
|
||||
|
||||
static void test_no_match_usb_fix(void** state)
|
||||
{
|
||||
ssize_t res;
|
||||
char buffer[256];
|
||||
|
||||
propatch_usb_fix_init("sdd,sdf", "2-1", "2-2");
|
||||
|
||||
res = propatch_usb_fix_readlink(
|
||||
"/sys/block/sde/device",
|
||||
buffer,
|
||||
sizeof(buffer));
|
||||
|
||||
assert_int_equal(res, strlen(readlink_return_value));
|
||||
|
||||
assert_int_equal(readlink_call_count, 1);
|
||||
}
|
||||
|
||||
static void test_match_player1_usb_fix(void** state)
|
||||
{
|
||||
ssize_t res;
|
||||
char buffer[256];
|
||||
|
||||
propatch_usb_fix_init("sde,sdf", "2-1", "2-2");
|
||||
|
||||
res = propatch_usb_fix_readlink(
|
||||
"/sys/block/sde/device",
|
||||
buffer,
|
||||
sizeof(buffer));
|
||||
|
||||
assert_int_equal(res, strlen(player1_match_buf));
|
||||
assert_string_equal(buffer, player1_match_buf);
|
||||
|
||||
assert_int_equal(readlink_call_count, 1);
|
||||
}
|
||||
|
||||
static void test_match_player2_usb_fix(void** state)
|
||||
{
|
||||
ssize_t res;
|
||||
char buffer[256];
|
||||
|
||||
propatch_usb_fix_init("sde,sdf", "2-2", "2-1");
|
||||
|
||||
res = propatch_usb_fix_readlink(
|
||||
"/sys/block/sde/device",
|
||||
buffer,
|
||||
sizeof(buffer));
|
||||
|
||||
assert_int_equal(res, strlen(player2_match_buf));
|
||||
assert_string_equal(buffer, player2_match_buf);
|
||||
|
||||
assert_int_equal(readlink_call_count, 1);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const struct CMUnitTest tests[] ={
|
||||
cmocka_unit_test_setup_teardown(test_usb_fix_disabled, setup, teardown),
|
||||
cmocka_unit_test_setup_teardown(test_no_match_usb_fix, setup, teardown),
|
||||
cmocka_unit_test_setup_teardown(test_match_player1_usb_fix, setup, teardown),
|
||||
cmocka_unit_test_setup_teardown(test_match_player2_usb_fix, setup, teardown)
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#define LOG_MODULE "test-util-mem"
|
||||
|
||||
#include <cmocka/cmocka.h>
|
||||
|
||||
#include "test-util/mem.h"
|
||||
|
||||
#include "util/log.h"
|
||||
#include "util/mem.h"
|
||||
|
||||
static void* _test_util_mem_xmalloc(size_t nbytes)
|
||||
{
|
||||
void* ptr;
|
||||
|
||||
ptr = test_malloc(nbytes);
|
||||
|
||||
log_debug("malloc(%d): %p", nbytes, ptr);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void* _test_util_mem_xrealloc(void* mem, size_t nbytes)
|
||||
{
|
||||
void* ptr;
|
||||
|
||||
ptr = test_realloc(mem, nbytes);
|
||||
|
||||
log_debug("realloc(%p %d): %p", mem, nbytes, ptr);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static void _test_util_mem_xfree(void** mem)
|
||||
{
|
||||
if (*mem != NULL) {
|
||||
log_debug("free(%p %p)", mem, *mem);
|
||||
test_free(*mem);
|
||||
*mem = NULL;
|
||||
} else {
|
||||
log_warn("free(%p NULL)", mem);
|
||||
}
|
||||
}
|
||||
|
||||
static const struct util_mem_interface _test_util_mem_interface_test = {
|
||||
.xmalloc = _test_util_mem_xmalloc,
|
||||
.xrealloc = _test_util_mem_xrealloc,
|
||||
.xfree = _test_util_mem_xfree,
|
||||
};
|
||||
|
||||
void test_util_mem_install_mem_interface()
|
||||
{
|
||||
util_mem_init(&_test_util_mem_interface_test);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* Install the memory test interface which is backed by cmocka for leak and error detection on unit tests.
|
||||
*/
|
||||
void test_util_mem_install_mem_interface();
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <cmocka/cmocka.h>
|
||||
|
||||
#include "test-util/mem.h"
|
||||
|
||||
#include "util/mem.h"
|
||||
|
||||
static void test_util_mem_mock_interface(void** state)
|
||||
{
|
||||
void* ptr;
|
||||
|
||||
test_util_mem_install_mem_interface();
|
||||
|
||||
ptr = util_xmalloc(100);
|
||||
assert_ptr_not_equal(ptr, NULL);
|
||||
|
||||
ptr = util_xrealloc(ptr, 200);
|
||||
assert_ptr_not_equal(ptr, NULL);
|
||||
|
||||
util_xfree(&ptr);
|
||||
assert_ptr_equal(ptr, NULL);
|
||||
}
|
||||
|
||||
static void test_util_mem_real(void** state)
|
||||
{
|
||||
void* ptr;
|
||||
|
||||
util_mem_init_default();
|
||||
|
||||
ptr = util_xmalloc(100);
|
||||
assert_ptr_not_equal(ptr, NULL);
|
||||
|
||||
ptr = util_xrealloc(ptr, 200);
|
||||
assert_ptr_not_equal(ptr, NULL);
|
||||
|
||||
util_xfree(&ptr);
|
||||
assert_ptr_equal(ptr, NULL);
|
||||
|
||||
// Should not crash
|
||||
util_xfree(&ptr);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const struct CMUnitTest tests[] ={
|
||||
cmocka_unit_test(test_util_mem_mock_interface),
|
||||
cmocka_unit_test(test_util_mem_real)
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
#include <cmocka/cmocka.h>
|
||||
|
||||
#include "test-util/mem.h"
|
||||
|
||||
#include "util/mem.h"
|
||||
#include "util/str.h"
|
||||
|
||||
static int setup(void** state)
|
||||
{
|
||||
test_util_mem_install_mem_interface();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_str_dup(void** state)
|
||||
{
|
||||
char* str;
|
||||
|
||||
str = util_str_dup("aasdf");
|
||||
assert_string_equal(str, "aasdf");
|
||||
|
||||
util_xfree((void**) &str);
|
||||
}
|
||||
|
||||
static void test_str_dup_empty(void** state)
|
||||
{
|
||||
char* str;
|
||||
|
||||
str = util_str_dup("");
|
||||
assert_string_equal(str, "");
|
||||
|
||||
util_xfree((void**) &str);
|
||||
}
|
||||
|
||||
static void test_str_split0(void** state)
|
||||
{
|
||||
size_t count;
|
||||
char** toks;
|
||||
|
||||
toks = util_str_split("", ",", &count);
|
||||
|
||||
assert_int_equal(count, 0);
|
||||
|
||||
util_str_free_split(toks, count);
|
||||
}
|
||||
|
||||
static void test_str_split1(void** state)
|
||||
{
|
||||
size_t count;
|
||||
char** toks;
|
||||
|
||||
toks = util_str_split("asd", ",", &count);
|
||||
|
||||
assert_int_equal(count, 1);
|
||||
assert_string_equal(toks[0], "asd");
|
||||
|
||||
util_str_free_split(toks, count);
|
||||
}
|
||||
|
||||
static void test_str_split2(void** state)
|
||||
{
|
||||
size_t count;
|
||||
char** toks;
|
||||
|
||||
toks = util_str_split("asd,123", ",", &count);
|
||||
|
||||
assert_int_equal(count, 2);
|
||||
assert_string_equal(toks[0], "asd");
|
||||
assert_string_equal(toks[1], "123");
|
||||
|
||||
util_str_free_split(toks, count);
|
||||
}
|
||||
|
||||
static void test_str_split3(void** state)
|
||||
{
|
||||
size_t count;
|
||||
char** toks;
|
||||
|
||||
toks = util_str_split("/asd/123/ddd/", "/", &count);
|
||||
|
||||
assert_int_equal(count, 3);
|
||||
assert_string_equal(toks[0], "asd");
|
||||
assert_string_equal(toks[1], "123");
|
||||
assert_string_equal(toks[2], "ddd");
|
||||
|
||||
util_str_free_split(toks, count);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
const struct CMUnitTest tests[] ={
|
||||
cmocka_unit_test_setup(test_str_dup, setup),
|
||||
cmocka_unit_test_setup(test_str_dup_empty, setup),
|
||||
cmocka_unit_test_setup(test_str_split0, setup),
|
||||
cmocka_unit_test_setup(test_str_split1, setup),
|
||||
cmocka_unit_test_setup(test_str_split2, setup),
|
||||
cmocka_unit_test_setup(test_str_split3, setup)
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user