refactoring; finishing integration of CMocka; several test cases covered
This commit is contained in:
@@ -1,12 +1,29 @@
|
||||
add_executable(session_app
|
||||
session.c
|
||||
# ---/ session_do_read /---------------------------------------------
|
||||
|
||||
add_executable(session_do_read
|
||||
session_do_read.c
|
||||
)
|
||||
|
||||
target_link_libraries(session_app
|
||||
target_link_libraries(session_do_read
|
||||
cmocka
|
||||
session
|
||||
main
|
||||
)
|
||||
|
||||
target_link_options(session_app PRIVATE -Wl,--wrap=read)
|
||||
target_link_options(session_do_read PRIVATE -Wl,--wrap=read)
|
||||
|
||||
add_test(session_test session_app)
|
||||
add_test(NAME session_test_1 COMMAND session_do_read)
|
||||
|
||||
# ---/ query_extract_from_buf /--------------------------------------
|
||||
|
||||
add_executable(query_extract_from_buf
|
||||
query_extract_from_buf.c
|
||||
)
|
||||
|
||||
target_link_libraries(query_extract_from_buf
|
||||
cmocka
|
||||
session
|
||||
main
|
||||
)
|
||||
|
||||
add_test(NAME session_test_2 COMMAND query_extract_from_buf)
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <cmocka.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <main.h>
|
||||
#include <session.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TEST_BUF "First line\nThe second line\r\nThe third line\nFourth line\0"
|
||||
#define SUBSTR_NUM 4
|
||||
|
||||
void test_query_extract_from_buf_filled(void **state) {
|
||||
session sess = {.buf = TEST_BUF, .buf_used = sizeof TEST_BUF};
|
||||
char *lines[SUBSTR_NUM];
|
||||
for (int i = 0; i < SUBSTR_NUM; i++) {
|
||||
query_extract_from_buf(&sess, &lines[i]);
|
||||
}
|
||||
assert_memory_equal("First line\n", lines[0], strlen(lines[0]));
|
||||
assert_memory_equal("The second line\n", lines[1], strlen(lines[1]));
|
||||
assert_memory_equal("The third line\n", lines[2], strlen(lines[2]));
|
||||
assert_memory_equal("Fourth line", lines[3], strlen(lines[3]));
|
||||
|
||||
for (int i = 0; i < SUBSTR_NUM; i++) {
|
||||
free(lines[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void test_query_extract_from_buf_empty(void **state) {
|
||||
session sess = {.buf = "", .buf_used = 0};
|
||||
char *line = NULL;
|
||||
int ret = query_extract_from_buf(&sess, &line);
|
||||
assert_null(line);
|
||||
assert_int_equal(ret, 0);
|
||||
}
|
||||
|
||||
int setup(void **state) { return 0; }
|
||||
int tear_down(void **state) { return 0; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_query_extract_from_buf_filled),
|
||||
cmocka_unit_test(test_query_extract_from_buf_empty)
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
#include <cmocka.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <session.h>
|
||||
#include "../../src/main.h"
|
||||
|
||||
#define TEST_BUF "Hello world!\n"
|
||||
|
||||
int __wrap_read(int fd, void *buf, size_t nbytes) {
|
||||
return sizeof TEST_BUF - 1;
|
||||
}
|
||||
|
||||
void test_session_do_read(void** state) {
|
||||
session sess = {
|
||||
.buf = TEST_BUF,
|
||||
.buf_used = sizeof TEST_BUF,
|
||||
.sd = 2,
|
||||
};
|
||||
char *str;
|
||||
session_do_read(NULL, &str);
|
||||
assert_memory_equal(str, TEST_BUF, sizeof (TEST_BUF)-1);
|
||||
}
|
||||
|
||||
int setup(void **state) { return 0; }
|
||||
int tear_down(void **state) { return 0; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {cmocka_unit_test(test_session_do_read)};
|
||||
|
||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <cmocka.h>
|
||||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <main.h>
|
||||
#include <session.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TEST_BUF_USED "Voluptas voluptate reprehenderit eum voluptatibus.\n"
|
||||
#define TEST_BUF_UNUSED \
|
||||
"Impedit et rerum ut aut vel nisi. Animi et vero nihil aut possimus " \
|
||||
"commodi. Autem dolorem et officia provident.\n"
|
||||
#define TEST_BUF TEST_BUF_USED TEST_BUF_UNUSED
|
||||
|
||||
int __wrap_read(int fd, void *buf, size_t nbytes) {
|
||||
return sizeof TEST_BUF - 1;
|
||||
}
|
||||
|
||||
void test_session_do_read(void **state) {
|
||||
session sess = {
|
||||
.buf = TEST_BUF,
|
||||
.buf_used = sizeof TEST_BUF,
|
||||
.sd = 2,
|
||||
};
|
||||
char *line;
|
||||
session_do_read(&sess, &line);
|
||||
assert_memory_equal(line, TEST_BUF_USED, sizeof(TEST_BUF_USED));
|
||||
free(line);
|
||||
}
|
||||
|
||||
int setup(void **state) { return 0; }
|
||||
int tear_down(void **state) { return 0; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {cmocka_unit_test(test_session_do_read)};
|
||||
|
||||
return cmocka_run_group_tests(tests, setup, tear_down);
|
||||
}
|
||||
Reference in New Issue
Block a user