Make httpbf tests functional.

This commit is contained in:
Klaas Freitag 2013-04-17 15:21:38 +02:00
parent 35aaddda87
commit 2b971a09f3
5 changed files with 196 additions and 18 deletions

View file

@ -40,11 +40,7 @@
#define DEBUG_HBF(...) printf(__VA_ARGS__)
#endif
#define DEFAULT_BLOG_SIZE (10*1024*1024)
static int get_transfer_block_size() {
return DEFAULT_BLOG_SIZE;
}
#define DEFAULT_BLOCK_SIZE (10*1024*1024)
static int transfer_id( struct stat *sb ) {
struct timeval tp;
@ -81,6 +77,7 @@ hbf_transfer_t *hbf_init_transfer( const char *dest_uri ) {
transfer->status_code = 200;
transfer->error_string = NULL;
transfer->start_id = 0;
transfer->block_size = DEFAULT_BLOCK_SIZE;
return transfer;
}
@ -110,12 +107,12 @@ Hbf_State hbf_splitlist(hbf_transfer_t *transfer, int fd ) {
transfer->fd = fd;
transfer->stat_size = sb.st_size;
transfer->modtime = sb.st_mtime;
#ifdef NDEBUG
#ifndef NDEBUG
transfer->calc_size = 0;
#endif
/* calc the number of blocks to split in */
blk_size = get_transfer_block_size();
blk_size = transfer->block_size;
num_blocks = sb.st_size / blk_size;
/* there migth be a remainder. */
@ -155,7 +152,7 @@ Hbf_State hbf_splitlist(hbf_transfer_t *transfer, int fd ) {
/* store the block data into the result array in the transfer */
*((transfer->block_arr)+cnt) = block;
}
#ifdef NDEBUG
#ifndef NDEBUG
transfer->calc_size = overall;
#endif
}
@ -179,11 +176,8 @@ void hbf_free_transfer( hbf_transfer_t *transfer ) {
free( transfer );
}
/* keep this function hidden if non debug. Public for unit test. */
#ifndef NDEBUG
static
#endif
char* get_transfer_url( hbf_transfer_t *transfer, int indx ) {
static char* get_transfer_url( hbf_transfer_t *transfer, int indx ) {
char *res = NULL;
hbf_block_t *block = NULL;

View file

@ -77,7 +77,8 @@ struct hbf_transfer_s {
off_t stat_size;
time_t modtime;
#ifdef NDEBUG
off_t block_size;
#ifndef NDEBUG
off_t calc_size;
#endif
};
@ -98,8 +99,4 @@ const char *hbf_error_string( Hbf_State state );
*/
int hbf_fail_http_code( hbf_transfer_t *transfer );
#ifdef NDEBUG
char* get_transfer_url( hbf_transfer_t *transfer, int indx );
#endif
#endif

View file

@ -7,6 +7,7 @@ include_directories(
${CSTDLIB_PUBLIC_INCLUDE_DIRS}
${CMAKE_BINARY_DIR}
${CMOCKA_INCLUDE_DIR}
${HTTPBF_PUBLIC_INCLUDE_DIRS}
)
include_directories(${CHECK_INCLUDE_DIRS})
@ -56,5 +57,9 @@ add_cmocka_test(check_vio vio_tests/check_vio.c ${TEST_TARGET_LIBRARIES})
# sync
add_cmocka_test(check_csync_update csync_tests/check_csync_update.c ${TEST_TARGET_LIBRARIES})
# httpbf
set(TEST_HTTPBF_LIBRARIES ${TEST_TARGET_LIBRARIES} ${NEON_LIBRARIES})
add_cmocka_test(check_httpbf httpbf_tests/hbf_send_test.c ${TEST_HTTPBF_LIBRARIES} )
add_subdirectory(ownCloud)

View file

@ -0,0 +1,182 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <string.h>
#include <unistd.h>
#include <cmocka.h>
#include "config_test.h"
#include "httpbf.c"
// A test case that does nothing and succeeds.
static void null_test_success(void **state) {
(void) state;
}
static char* test_file( const char* name ) {
char path[260];
if( ! name ) return 0;
strcpy( path, TESTFILES_DIR);
if(path[strlen(TESTFILES_DIR)-1] != '/')
strcat( path, "/");
strcat( path, name );
return strdup(path);
}
static void test_get_transfer_url( void **state ) {
const char *url = "http://example.org/owncloud";
const char *turl = NULL;
int fd;
Hbf_State hbf_state;
hbf_transfer_t *list = NULL;
(void) state;
list = hbf_init_transfer( url );
assert_non_null( list );
/* open a file */
fd = open( test_file("church.jpg"), O_RDONLY );
assert_true(fd >= 0);
hbf_state = hbf_splitlist(list, fd);
assert_true( hbf_state == HBF_SUCCESS);
assert_true( list->block_cnt == 1);
turl = get_transfer_url( list, 0 );
assert_non_null( turl );
assert_string_equal( url, turl );
hbf_free_transfer( list );
}
static void test_get_transfer_url_bigfile( void **state ) {
const char *url = "http://example.org/big_file";
const char *turl = NULL;
char res[256];
int i, fd;
Hbf_State hbf_state;
hbf_transfer_t *list = NULL;
(void) state;
list = hbf_init_transfer( url );
assert_non_null( list );
list->block_size = (1024*1024); /* block size 1 MB */
/* open a file */
fd = open( test_file("church.jpg"), O_RDONLY );
assert_true(fd >= 0);
hbf_state = hbf_splitlist(list, fd);
assert_true( hbf_state == HBF_SUCCESS);
assert_true( list->block_cnt == 2 );
for( i=0; i < list->block_cnt; i++ ) {
turl = get_transfer_url( list, i );
assert_non_null(turl);
sprintf(res, "%s-chunking-%d-%d-%d", url, list->transfer_id,
list->block_cnt, i );
/* printf( "XX: %s\n", res ); */
assert_string_equal( turl, res );
}
hbf_free_transfer(list);
}
static void test_hbf_init_transfer( void **state ) {
hbf_transfer_t *list = NULL;
const char *url = "http://example.org/owncloud";
(void) state;
list = hbf_init_transfer( url );
assert_non_null( list );
assert_string_equal( url, list->url );
}
/* test with a file size that is not a multiply of the slize size. */
static void test_hbf_splitlist_odd( void **state ){
hbf_transfer_t *list = NULL;
const char *dest_url = "http://localhost/ocm/remote.php/webdav/big/church.jpg";
int prev_id = 0;
int i, fd;
Hbf_State hbf_state;
(void) state;
/* open a file */
fd = open(test_file("church.jpg"), O_RDONLY);
assert_true(fd >= 0);
/* do a smoke test for uniqueness */
for( i=0; i < 10000; i++) {
list = hbf_init_transfer(dest_url);
assert_non_null(list);
usleep(1);
hbf_state = hbf_splitlist(list, fd);
assert_int_not_equal(list->transfer_id, prev_id);
prev_id = list->transfer_id;
hbf_free_transfer(list);
}
list = hbf_init_transfer(dest_url);
assert_non_null(list);
hbf_state = hbf_splitlist(list, fd);
assert_non_null(list);
assert_int_equal(list->calc_size, list->stat_size);
assert_int_not_equal(list->block_cnt, 0);
assert_true( hbf_state == HBF_SUCCESS);
/* checks on the block list */
if( 1 ) {
int seen_zero_seq = 0;
int prev_seq = -1;
off_t prev_block_end = -1;
for( i=0; i < list->block_cnt; i++) {
hbf_block_t *blk = list->block_arr[i];
assert_non_null(blk);
if( blk->seq_number == 0 ) seen_zero_seq++;
assert_int_equal(prev_seq, blk->seq_number -1 );
prev_seq = blk->seq_number;
assert_true((prev_block_end+1) == (blk->start));
prev_block_end = blk->start + blk->size;
}
/* Make sure we exactly saw blk->seq_number == 0 exactly one times */
assert_int_equal( seen_zero_seq, 1 );
}
hbf_free_transfer( list );
}
int main(void) {
const UnitTest tests[] = {
unit_test(null_test_success),
unit_test(test_hbf_splitlist_odd),
unit_test(test_hbf_init_transfer),
unit_test(test_get_transfer_url),
unit_test(test_get_transfer_url_bigfile)
};
return run_tests(tests);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB