testing: refactoring; fixes file_download
This commit is contained in:
@@ -315,7 +315,14 @@ void file_download(session *sess) {
|
|||||||
int read_len;
|
int read_len;
|
||||||
|
|
||||||
if (sess->file->package_rest < INBUFSIZE) {
|
if (sess->file->package_rest < INBUFSIZE) {
|
||||||
|
if (sess->file->package_rest == 0) {
|
||||||
|
sess->state = OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE;
|
||||||
|
return;
|
||||||
|
}
|
||||||
read_len = sess->file->package_rest;
|
read_len = sess->file->package_rest;
|
||||||
|
} else if (sess->file->rest < INBUFSIZE) {
|
||||||
|
/* Do not trust read */
|
||||||
|
read_len = sess->file->rest;
|
||||||
} else {
|
} else {
|
||||||
read_len = INBUFSIZE;
|
read_len = INBUFSIZE;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
add_subdirectory(session)
|
add_subdirectory(session)
|
||||||
add_subdirectory(user)
|
add_subdirectory(user)
|
||||||
add_subdirectory(file_p)
|
add_subdirectory(file_p)
|
||||||
|
add_subdirectory(test_utils)
|
||||||
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
add_executable(clear_list
|
add_executable(clear_list
|
||||||
clear_list.c
|
clear_list.c
|
||||||
utils.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(clear_list
|
target_link_libraries(clear_list
|
||||||
cmocka
|
cmocka
|
||||||
file_p
|
file_p
|
||||||
main
|
main
|
||||||
|
test_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_options(clear_list PRIVATE -Wl,--wrap=free)
|
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
|
add_executable(file_list
|
||||||
file_list.c
|
file_list.c
|
||||||
utils.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(file_list
|
target_link_libraries(file_list
|
||||||
cmocka
|
cmocka
|
||||||
file_p
|
file_p
|
||||||
main
|
main
|
||||||
|
test_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_options(file_list PRIVATE -Wl,--wrap=db_get_files_data,--wrap=write)
|
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
|
add_executable(extract_names_from_hash
|
||||||
extract_names_from_hash.c
|
extract_names_from_hash.c
|
||||||
utils.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(extract_names_from_hash
|
target_link_libraries(extract_names_from_hash
|
||||||
cmocka
|
cmocka
|
||||||
file_p
|
file_p
|
||||||
main
|
main
|
||||||
|
test_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
add_test(NAME file_p_extract_names_from_hash_test_1 COMMAND extract_names_from_hash)
|
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
|
add_executable(file_send_prepare
|
||||||
file_send_prepare.c
|
file_send_prepare.c
|
||||||
utils.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(file_send_prepare
|
target_link_libraries(file_send_prepare
|
||||||
@@ -59,6 +58,7 @@ target_link_libraries(file_send_prepare
|
|||||||
file_p
|
file_p
|
||||||
main
|
main
|
||||||
utils
|
utils
|
||||||
|
test_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_options(file_send_prepare PRIVATE -Wl,--wrap=write,--wrap=print_log,--wrap=lseek,--wrap=open,--wrap=db_get_file)
|
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
|
add_executable(clear_file_from_sess
|
||||||
clear_file_from_sess.c
|
clear_file_from_sess.c
|
||||||
utils.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(clear_file_from_sess
|
target_link_libraries(clear_file_from_sess
|
||||||
@@ -77,6 +76,7 @@ target_link_libraries(clear_file_from_sess
|
|||||||
file_p
|
file_p
|
||||||
main
|
main
|
||||||
utils
|
utils
|
||||||
|
test_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_options(clear_file_from_sess PRIVATE -Wl,--wrap=free)
|
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
|
add_executable(file_download
|
||||||
file_download.c
|
file_download.c
|
||||||
utils.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(file_download
|
target_link_libraries(file_download
|
||||||
@@ -96,6 +95,7 @@ target_link_libraries(file_download
|
|||||||
main
|
main
|
||||||
utils
|
utils
|
||||||
client
|
client
|
||||||
|
test_utils
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_options(file_download PRIVATE -Wl,--wrap=print_log,--wrap=read,--wrap=write)
|
target_link_options(file_download PRIVATE -Wl,--wrap=print_log,--wrap=read,--wrap=write)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
#include <file_p.h>
|
#include <file_p.h>
|
||||||
#include <main.h>
|
#include <main.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "utils.h"
|
#include <test_utils.h>
|
||||||
|
|
||||||
fl_t fl_sample = {
|
fl_t fl_sample = {
|
||||||
.name = "test_file", .description = "Hello test file", .owner = "user1234"};
|
.name = "test_file", .description = "Hello test file", .owner = "user1234"};
|
||||||
|
|||||||
+39
-29
@@ -9,6 +9,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <utils.h>
|
#include <utils.h>
|
||||||
|
#include <test_utils.h>
|
||||||
|
|
||||||
dbuf_t *dbuf = NULL;
|
dbuf_t *dbuf = NULL;
|
||||||
#define FNAME "Test file"
|
#define FNAME "Test file"
|
||||||
@@ -16,29 +17,8 @@ dbuf_t *dbuf = NULL;
|
|||||||
#define FSIZE PACKAGE_SIZE *ITERATIONS
|
#define FSIZE PACKAGE_SIZE *ITERATIONS
|
||||||
int read_ret_zero = 0;
|
int read_ret_zero = 0;
|
||||||
int write_ret_m_1 = 0;
|
int write_ret_m_1 = 0;
|
||||||
char *print_str = NULL;
|
extern char *print_str;
|
||||||
void *print_str_arr = NULL;
|
extern void *print_str_arr;
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
ssize_t __wrap_read(int __fd, void *__buf, size_t __nbytes) {
|
ssize_t __wrap_read(int __fd, void *__buf, size_t __nbytes) {
|
||||||
if (read_ret_zero)
|
if (read_ret_zero)
|
||||||
@@ -66,20 +46,52 @@ void test__file_download__normal(void **state) {
|
|||||||
sess.file->path = malloc(sizeof(char) * INBUFSIZE);
|
sess.file->path = malloc(sizeof(char) * INBUFSIZE);
|
||||||
sess.file->description = malloc(sizeof(char) * INBUFSIZE);
|
sess.file->description = malloc(sizeof(char) * INBUFSIZE);
|
||||||
strcpy(sess.file->name, FNAME);
|
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,
|
const char *fmt_args[] = {"File %s is downloaded from the server\n", FNAME,
|
||||||
NULL};
|
NULL};
|
||||||
print_str_arr = fmt_args;
|
print_str_arr = fmt_args;
|
||||||
|
|
||||||
while (sess.state != OP_WAIT) {
|
for (int i = 0; (sess.state != OP_WAIT) && (i < 2000000000); i++) {
|
||||||
file_download(&sess);
|
file_download(&sess);
|
||||||
if (sess.state == OP_DOWNLOAD_WAIT_CONFIRM_PACKAGE) {
|
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_int_equal(sess.state, OP_WAIT);
|
||||||
assert_ptr_equal(sess.file, NULL);
|
assert_ptr_equal(sess.file, NULL);
|
||||||
print_str_arr = NULL;
|
|
||||||
clear_file_from_sess(&sess);
|
clear_file_from_sess(&sess);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +120,6 @@ void test__file_download__cancel(void **state) {
|
|||||||
}
|
}
|
||||||
assert_int_equal(sess.state, OP_WAIT);
|
assert_int_equal(sess.state, OP_WAIT);
|
||||||
assert_ptr_equal(sess.file, NULL);
|
assert_ptr_equal(sess.file, NULL);
|
||||||
print_str_arr = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test__file_download__read_zero(void **state) {
|
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_int_equal(sess.state, OP_WAIT);
|
||||||
assert_ptr_equal(sess.file, NULL);
|
assert_ptr_equal(sess.file, NULL);
|
||||||
read_ret_zero = false;
|
read_ret_zero = false;
|
||||||
print_str_arr = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test__file_download__read_write_m_1(void **state) {
|
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_int_equal(sess.state, ERR);
|
||||||
assert_ptr_equal(sess.file, NULL);
|
assert_ptr_equal(sess.file, NULL);
|
||||||
write_ret_m_1 = false;
|
write_ret_m_1 = false;
|
||||||
print_str_arr = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int setup(void **state) { return 0; }
|
int setup(void **state) { return 0; }
|
||||||
@@ -163,6 +172,7 @@ int tear_down(void **state) { return 0; }
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
const struct CMUnitTest tests[] = {
|
const struct CMUnitTest tests[] = {
|
||||||
cmocka_unit_test(test__file_download__normal),
|
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__cancel),
|
||||||
cmocka_unit_test(test__file_download__read_zero),
|
cmocka_unit_test(test__file_download__read_zero),
|
||||||
cmocka_unit_test(test__file_download__read_write_m_1),
|
cmocka_unit_test(test__file_download__read_write_m_1),
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <test_utils.h>
|
||||||
|
|
||||||
#define COUNT 20
|
#define COUNT 20
|
||||||
dbuf_t *dbuf;
|
dbuf_t *dbuf;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <utils.h>
|
#include <utils.h>
|
||||||
|
#include <test_utils.h>
|
||||||
|
|
||||||
#define FSIZE 10233
|
#define FSIZE 10233
|
||||||
#define FSIZE_S "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_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 {
|
ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur {
|
||||||
dbuf_write(__buf, __n, &dbuf);
|
dbuf_write(__buf, __n, &dbuf);
|
||||||
return __n;
|
return __n;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <utils.h>
|
#include <utils.h>
|
||||||
|
#include <test_utils.h>
|
||||||
|
|
||||||
dbuf_t *dbuf;
|
dbuf_t *dbuf;
|
||||||
|
|
||||||
@@ -23,11 +24,6 @@ dbuf_t *dbuf;
|
|||||||
#define FID 19
|
#define FID 19
|
||||||
#define FID_S "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 {
|
ssize_t __wrap_write(int __fd, const void *__buf, size_t __n) __wur {
|
||||||
dbuf_write(__buf, __n, &dbuf);
|
dbuf_write(__buf, __n, &dbuf);
|
||||||
return __n;
|
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 "../../src/main.h"
|
||||||
|
#include "cmocka.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.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_init(size_t init_sz) {
|
||||||
dbuf_t *dbuf = malloc(sizeof(dbuf_t));
|
dbuf_t *dbuf = malloc(sizeof(dbuf_t));
|
||||||
@@ -79,3 +81,31 @@ void fill_list_with_samples(fl_t *fl_sample, fl_t **fl_st, fl_t **fl_cur,
|
|||||||
*fl_cur = _fl_cur;
|
*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 {
|
typedef struct {
|
||||||
char *ptr;
|
char *ptr;
|
||||||
@@ -11,3 +16,6 @@ void fill_list_with_samples(fl_t *fl_samples, fl_t **fl_st, fl_t **fl_cur,
|
|||||||
dbuf_t *dbuf_init(size_t init_sz);
|
dbuf_t *dbuf_init(size_t init_sz);
|
||||||
int32_t dbuf_write(const char *text, size_t len, dbuf_t **dbuf);
|
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