testing: refactoring; fixes file_download
This commit is contained in:
+2
-1
@@ -1,3 +1,4 @@
|
||||
add_subdirectory(session)
|
||||
add_subdirectory(user)
|
||||
add_subdirectory(file_p)
|
||||
add_subdirectory(file_p)
|
||||
add_subdirectory(test_utils)
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
add_executable(clear_list
|
||||
clear_list.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(clear_list
|
||||
cmocka
|
||||
file_p
|
||||
main
|
||||
test_utils
|
||||
)
|
||||
|
||||
target_link_options(clear_list PRIVATE -Wl,--wrap=free)
|
||||
@@ -19,13 +19,13 @@ add_test(NAME file_p_clear_list_test_1 COMMAND clear_list)
|
||||
|
||||
add_executable(file_list
|
||||
file_list.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(file_list
|
||||
cmocka
|
||||
file_p
|
||||
main
|
||||
test_utils
|
||||
)
|
||||
|
||||
target_link_options(file_list PRIVATE -Wl,--wrap=db_get_files_data,--wrap=write)
|
||||
@@ -36,13 +36,13 @@ add_test(NAME file_p_file_list_test_1 COMMAND file_list)
|
||||
|
||||
add_executable(extract_names_from_hash
|
||||
extract_names_from_hash.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(extract_names_from_hash
|
||||
cmocka
|
||||
file_p
|
||||
main
|
||||
test_utils
|
||||
)
|
||||
|
||||
add_test(NAME file_p_extract_names_from_hash_test_1 COMMAND extract_names_from_hash)
|
||||
@@ -51,7 +51,6 @@ add_test(NAME file_p_extract_names_from_hash_test_1 COMMAND extract_names_from_h
|
||||
|
||||
add_executable(file_send_prepare
|
||||
file_send_prepare.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(file_send_prepare
|
||||
@@ -59,6 +58,7 @@ target_link_libraries(file_send_prepare
|
||||
file_p
|
||||
main
|
||||
utils
|
||||
test_utils
|
||||
)
|
||||
|
||||
target_link_options(file_send_prepare PRIVATE -Wl,--wrap=write,--wrap=print_log,--wrap=lseek,--wrap=open,--wrap=db_get_file)
|
||||
@@ -69,7 +69,6 @@ add_test(NAME file_p_file_send_prepare_test_1 COMMAND file_send_prepare)
|
||||
|
||||
add_executable(clear_file_from_sess
|
||||
clear_file_from_sess.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(clear_file_from_sess
|
||||
@@ -77,6 +76,7 @@ target_link_libraries(clear_file_from_sess
|
||||
file_p
|
||||
main
|
||||
utils
|
||||
test_utils
|
||||
)
|
||||
|
||||
target_link_options(clear_file_from_sess PRIVATE -Wl,--wrap=free)
|
||||
@@ -87,7 +87,6 @@ add_test(NAME file_p_clear_file_from_sess_test_1 COMMAND clear_file_from_sess)
|
||||
|
||||
add_executable(file_download
|
||||
file_download.c
|
||||
utils.c
|
||||
)
|
||||
|
||||
target_link_libraries(file_download
|
||||
@@ -96,6 +95,7 @@ target_link_libraries(file_download
|
||||
main
|
||||
utils
|
||||
client
|
||||
test_utils
|
||||
)
|
||||
|
||||
target_link_options(file_download PRIVATE -Wl,--wrap=print_log,--wrap=read,--wrap=write)
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <file_p.h>
|
||||
#include <main.h>
|
||||
#include <string.h>
|
||||
#include "utils.h"
|
||||
#include <test_utils.h>
|
||||
|
||||
fl_t fl_sample = {
|
||||
.name = "test_file", .description = "Hello test file", .owner = "user1234"};
|
||||
|
||||
+39
-29
@@ -9,6 +9,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <utils.h>
|
||||
#include <test_utils.h>
|
||||
|
||||
dbuf_t *dbuf = NULL;
|
||||
#define FNAME "Test file"
|
||||
@@ -16,29 +17,8 @@ dbuf_t *dbuf = NULL;
|
||||
#define FSIZE PACKAGE_SIZE *ITERATIONS
|
||||
int read_ret_zero = 0;
|
||||
int write_ret_m_1 = 0;
|
||||
char *print_str = NULL;
|
||||
void *print_str_arr = NULL;
|
||||
|
||||
int __wrap_print_log(FILE *output, enum pl_type type, const char *fmt, ...) {
|
||||
if (print_str != NULL) {
|
||||
assert_string_equal(print_str, fmt);
|
||||
} else if (print_str_arr != NULL) {
|
||||
char **ptrs = print_str_arr;
|
||||
va_list args;
|
||||
assert_string_equal(*ptrs, fmt);
|
||||
ptrs++;
|
||||
va_start(args, fmt);
|
||||
while (*ptrs != NULL) {
|
||||
char *out_str = va_arg(args, char *);
|
||||
assert_string_equal(out_str, *ptrs);
|
||||
ptrs++;
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
extern char *print_str;
|
||||
extern void *print_str_arr;
|
||||
|
||||
ssize_t __wrap_read(int __fd, void *__buf, size_t __nbytes) {
|
||||
if (read_ret_zero)
|
||||
@@ -66,20 +46,52 @@ void test__file_download__normal(void **state) {
|
||||
sess.file->path = malloc(sizeof(char) * INBUFSIZE);
|
||||
sess.file->description = malloc(sizeof(char) * INBUFSIZE);
|
||||
strcpy(sess.file->name, FNAME);
|
||||
char cont_mes[64];
|
||||
|
||||
sprintf(cont_mes, "continue %d\n", PACKAGE_SIZE);
|
||||
|
||||
const char *fmt_args[] = {"File %s is downloaded from the server\n", FNAME,
|
||||
NULL};
|
||||
print_str_arr = fmt_args;
|
||||
|
||||
while (sess.state != OP_WAIT) {
|
||||
for (int i = 0; (sess.state != OP_WAIT) && (i < 2000000000); i++) {
|
||||
file_download(&sess);
|
||||
if (sess.state == OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE) {
|
||||
download_confirm("continue", &sess, NULL);
|
||||
download_confirm(cont_mes, &sess, NULL);
|
||||
}
|
||||
}
|
||||
assert_int_equal(sess.state, OP_WAIT);
|
||||
assert_ptr_equal(sess.file, NULL);
|
||||
clear_file_from_sess(&sess);
|
||||
}
|
||||
|
||||
void test__file_download__normal_alt_buf_size(void **state) {
|
||||
dbuf = dbuf_init(INBUFSIZE);
|
||||
|
||||
session sess = {.state = OP_DOWNLOAD};
|
||||
sess.file = malloc(sizeof(s_file_t));
|
||||
sess.file->name = malloc(sizeof FNAME);
|
||||
sess.file->size = sess.file->rest = FSIZE;
|
||||
sess.file->package_rest = PACKAGE_SIZE;
|
||||
sess.file->path = malloc(sizeof(char) * INBUFSIZE);
|
||||
sess.file->description = malloc(sizeof(char) * INBUFSIZE);
|
||||
strcpy(sess.file->name, FNAME);
|
||||
char cont_mes[64];
|
||||
|
||||
sprintf(cont_mes, "continue %d\n", PACKAGE_SIZE / 3);
|
||||
|
||||
const char *fmt_args[] = {"File %s is downloaded from the server\n", FNAME,
|
||||
NULL};
|
||||
print_str_arr = fmt_args;
|
||||
|
||||
for (int i = 0; (sess.state != OP_WAIT) && (i < 2000000000); i++) {
|
||||
file_download(&sess);
|
||||
if (sess.state == OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE) {
|
||||
download_confirm(cont_mes, &sess, NULL);
|
||||
}
|
||||
}
|
||||
assert_int_equal(sess.state, OP_WAIT);
|
||||
assert_ptr_equal(sess.file, NULL);
|
||||
print_str_arr = NULL;
|
||||
clear_file_from_sess(&sess);
|
||||
}
|
||||
|
||||
@@ -108,7 +120,6 @@ void test__file_download__cancel(void **state) {
|
||||
}
|
||||
assert_int_equal(sess.state, OP_WAIT);
|
||||
assert_ptr_equal(sess.file, NULL);
|
||||
print_str_arr = NULL;
|
||||
}
|
||||
|
||||
void test__file_download__read_zero(void **state) {
|
||||
@@ -129,7 +140,6 @@ void test__file_download__read_zero(void **state) {
|
||||
assert_int_equal(sess.state, OP_WAIT);
|
||||
assert_ptr_equal(sess.file, NULL);
|
||||
read_ret_zero = false;
|
||||
print_str_arr = NULL;
|
||||
}
|
||||
|
||||
void test__file_download__read_write_m_1(void **state) {
|
||||
@@ -154,7 +164,6 @@ void test__file_download__read_write_m_1(void **state) {
|
||||
assert_int_equal(sess.state, ERR);
|
||||
assert_ptr_equal(sess.file, NULL);
|
||||
write_ret_m_1 = false;
|
||||
print_str_arr = NULL;
|
||||
}
|
||||
|
||||
int setup(void **state) { return 0; }
|
||||
@@ -163,6 +172,7 @@ int tear_down(void **state) { return 0; }
|
||||
int main(int argc, char **argv) {
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test__file_download__normal),
|
||||
cmocka_unit_test(test__file_download__normal_alt_buf_size),
|
||||
cmocka_unit_test(test__file_download__cancel),
|
||||
cmocka_unit_test(test__file_download__read_zero),
|
||||
cmocka_unit_test(test__file_download__read_write_m_1),
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <string.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include <unistd.h>
|
||||
#include <test_utils.h>
|
||||
|
||||
#define COUNT 20
|
||||
dbuf_t *dbuf;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <sys/cdefs.h>
|
||||
#include <unistd.h>
|
||||
#include <utils.h>
|
||||
#include <test_utils.h>
|
||||
|
||||
#define FSIZE 10233
|
||||
#define FSIZE_S "10233"
|
||||
@@ -60,11 +61,6 @@ int __wrap_mkdir(const char *__path, __mode_t __mode) { return mock_type(int); }
|
||||
|
||||
int __wrap_chdir(const char *__path) { return 0; }
|
||||
|
||||
int __wrap_print_log(FILE *output, enum pl_type type,
|
||||
const char *format_message, ...) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur {
|
||||
dbuf_write(__buf, __n, &dbuf);
|
||||
return __n;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <sys/cdefs.h>
|
||||
#include <unistd.h>
|
||||
#include <utils.h>
|
||||
#include <test_utils.h>
|
||||
|
||||
dbuf_t *dbuf;
|
||||
|
||||
@@ -23,11 +24,6 @@ dbuf_t *dbuf;
|
||||
#define FID 19
|
||||
#define FID_S "19"
|
||||
|
||||
int __wrap_print_log(FILE *output, enum pl_type type,
|
||||
const char *format_message, ...) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur {
|
||||
dbuf_write(__buf, __n, &dbuf);
|
||||
return __n;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
add_library(test_utils STATIC)
|
||||
target_link_libraries(test_utils cmocka main utils)
|
||||
target_sources(test_utils
|
||||
PRIVATE
|
||||
test_utils.c
|
||||
PUBLIC
|
||||
FILE_SET HEADERS
|
||||
FILES
|
||||
test_utils.h
|
||||
)
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "../../src/main.h"
|
||||
#include "cmocka.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "utils.h"
|
||||
#include "test_utils.h"
|
||||
#include <utils.h>
|
||||
|
||||
dbuf_t *dbuf_init(size_t init_sz) {
|
||||
dbuf_t *dbuf = malloc(sizeof(dbuf_t));
|
||||
@@ -78,4 +80,32 @@ void fill_list_with_samples(fl_t *fl_sample, fl_t **fl_st, fl_t **fl_cur,
|
||||
if (fl_cur != NULL) {
|
||||
*fl_cur = _fl_cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char *print_str = NULL;
|
||||
void *print_str_arr = NULL;
|
||||
|
||||
int __wrap_print_log(FILE *output, enum pl_type type, const char *fmt, ...) {
|
||||
if (print_str != NULL) {
|
||||
assert_string_equal(print_str, fmt);
|
||||
print_str = NULL;
|
||||
} else if (print_str_arr != NULL) {
|
||||
char **ptrs = print_str_arr;
|
||||
va_list args;
|
||||
assert_string_equal(*ptrs, fmt);
|
||||
ptrs++;
|
||||
va_start(args, fmt);
|
||||
|
||||
while (*ptrs != NULL) {
|
||||
char *out_str = va_arg(args, char *);
|
||||
assert_string_equal(out_str, *ptrs);
|
||||
ptrs++;
|
||||
}
|
||||
|
||||
print_str_arr = NULL;
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
return 0;
|
||||
};
|
||||
@@ -1,4 +1,9 @@
|
||||
#include "../../src/main.h"
|
||||
#ifndef TEST_UTILS_H
|
||||
#define TEST_UTILS_H
|
||||
|
||||
#include <main.h>
|
||||
#include <stdio.h>
|
||||
#include <utils.h>
|
||||
|
||||
typedef struct {
|
||||
char *ptr;
|
||||
@@ -10,4 +15,7 @@ void fill_list_with_samples(fl_t *fl_samples, fl_t **fl_st, fl_t **fl_cur,
|
||||
int amount);
|
||||
dbuf_t *dbuf_init(size_t init_sz);
|
||||
int32_t dbuf_write(const char *text, size_t len, dbuf_t **dbuf);
|
||||
int32_t dbuf_destroy(dbuf_t **dbuf);
|
||||
int32_t dbuf_destroy(dbuf_t **dbuf);
|
||||
int __wrap_print_log(FILE *output, enum pl_type type, const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user