Skip to content

Commit

Permalink
Merge pull request #1800 from private-octopus/code-coverage-202411
Browse files Browse the repository at this point in the history
Remove dead code and add tests for util.c
  • Loading branch information
huitema authored Dec 5, 2024
2 parents 0f7a066 + cd9742c commit f3db0d7
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 53 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ else()
endif()

project(picoquic
VERSION 1.1.28.10
VERSION 1.1.29.0
DESCRIPTION "picoquic library"
LANGUAGES C CXX)

Expand Down
16 changes: 15 additions & 1 deletion UnitTest1/unittest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,27 @@ namespace UnitTest1
Assert::AreEqual(ret, 0);
}

TEST_METHOD(sprintf)
TEST_METHOD(util_sprintf)
{
int ret = util_sprintf_test();

Assert::AreEqual(ret, 0);
}

TEST_METHOD(util_debug_print)
{
int ret = util_debug_print_test();

Assert::AreEqual(ret, 0);
}

TEST_METHOD(util_uint8_to_str)
{
int ret = util_uint8_to_str_test();

Assert::AreEqual(ret, 0);
}

TEST_METHOD(memcmp)
{
int ret = util_memcmp_test();
Expand Down
5 changes: 3 additions & 2 deletions picoquic/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ int picoquic_parse_long_packet_header(
ph->epoch = picoquic_epoch_handshake;
break;
case picoquic_packet_retry: /* Retry */
default:
/* No default branch in this statement, because there are only 4 possible types
* parsed in picoquic_parse_long_packet_type */
ph->pc = picoquic_packet_context_initial;
ph->epoch = picoquic_epoch_initial;
break;
/* No default branch in this statement, because there are only 4 possible types
* parsed in picoquic_parse_long_packet_type */
}

if (ph->ptype == picoquic_packet_retry) {
Expand Down
2 changes: 1 addition & 1 deletion picoquic/picoquic.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
extern "C" {
#endif

#define PICOQUIC_VERSION "1.1.28.10"
#define PICOQUIC_VERSION "1.1.29.0"
#define PICOQUIC_ERROR_CLASS 0x400
#define PICOQUIC_ERROR_DUPLICATE (PICOQUIC_ERROR_CLASS + 1)
#define PICOQUIC_ERROR_AEAD_CHECK (PICOQUIC_ERROR_CLASS + 3)
Expand Down
8 changes: 6 additions & 2 deletions picoquic/picoquic_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ extern "C" {


void debug_set_stream(FILE *F);
#if 0
void debug_set_callback(void (*cb)(const char *msg, void *argp), void *argp);
#endif
void debug_printf(const char* fmt, ...);
void debug_printf_push_stream(FILE* f);
void debug_printf_pop_stream(void);
void debug_printf_suspend(void);
void debug_printf_resume(void);
int debug_printf_reset(int suspended);
#ifdef _DEBUG
void debug_dump(const void * x, int len);
#endif

/* utilities */
char* picoquic_string_create(const char* original, size_t len);
Expand All @@ -88,12 +92,10 @@ int picoquic_is_connection_id_null(const picoquic_connection_id_t * cnx_id);
int picoquic_compare_connection_id(const picoquic_connection_id_t * cnx_id1, const picoquic_connection_id_t * cnx_id2);
uint64_t picoquic_connection_id_hash(const picoquic_connection_id_t * cid);
uint64_t picoquic_val64_connection_id(picoquic_connection_id_t cnx_id);
void picoquic_set64_connection_id(picoquic_connection_id_t * cnx_id, uint64_t val64);
uint64_t picoquic_hash_addr(const struct sockaddr* addr);
size_t picoquic_parse_hexa(char const* hex_input, size_t input_length, uint8_t* bin_output, size_t output_max);
uint8_t picoquic_parse_connection_id_hexa(char const * hex_input, size_t input_length, picoquic_connection_id_t * cnx_id);
int picoquic_print_connection_id_hexa(char* buf, size_t buf_len, const picoquic_connection_id_t* cnxid);
uint8_t picoquic_create_packet_header_cnxid_lengths(uint8_t dest_len, uint8_t srce_len);

int picoquic_compare_addr(const struct sockaddr* expected, const struct sockaddr* actual);
int picoquic_compare_ip_addr(const struct sockaddr* expected, const struct sockaddr* actual);
Expand Down Expand Up @@ -170,8 +172,10 @@ const uint8_t* picoquic_frames_cid_decode(const uint8_t * bytes, const uint8_t *
#define VARINT_LEN(bytes) (((uint8_t)1) << ((bytes[0] >> 6)&3))
#define VARINT_LEN_T(bytes, t_len) (((t_len)1) << ((bytes[0] >> 6)&3))

#if 0
/* Predict length of a varint encoding */
size_t picoquic_frames_varint_encode_length(uint64_t n64);
#endif

/* Encoding functions of the form uint8_t * picoquic_frame_XXX_encode(uint8_t * bytes, uint8_t * bytes-max, ...)
*/
Expand Down
90 changes: 47 additions & 43 deletions picoquic/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,51 +92,55 @@ char* picoquic_string_free(char* str)
return NULL;
}

char* picoquic_strip_endofline(char* buf, size_t bufmax, char const* line)
{
for (size_t i = 0; i < bufmax; i++) {
int c = line[i];

if (c == 0 || c == '\r' || c == '\n') {
buf[i] = 0;
break;
}
else {
buf[i] = (char) c;
}
}

buf[bufmax - 1] = 0;
return buf;
}

static FILE* debug_out = NULL;
#if 0
void (*debug_callback)(const char *msg, void *arg) = NULL;
void *debug_callback_argp = NULL;
#endif
static int debug_suspended = 0;

void debug_set_stream(FILE *F)
{
debug_out = F;
#if 0
debug_callback = NULL;
debug_callback_argp = NULL;
#endif
}

FILE* get_debug_out()
{
return debug_out;
}

int get_debug_suspended()
{
return debug_suspended;
}
#if 0
void debug_set_callback(void (*cb)(const char *msg, void *argp), void *argp)
{
debug_callback = cb;
debug_callback_argp = argp;
debug_out = NULL;
}
#endif

void debug_printf(const char* fmt, ...)
{
#if 1
if (debug_suspended == 0 && debug_out != NULL) {
#else
if (debug_suspended == 0 && (debug_out != NULL || debug_callback != NULL)) {
#endif
if (debug_out) {
va_list args;
va_start(args, fmt);
vfprintf(debug_out, fmt, args);
va_end(args);
#if 1
}
#else
} else {
char message[1024];
size_t message_length;
Expand All @@ -153,12 +157,18 @@ void debug_printf(const char* fmt, ...)
}
debug_callback(message, debug_callback_argp);
}
#endif
}
}

#ifdef _DEBUG
void debug_dump(const void * x, int len)
{
#if 1
if (debug_suspended == 0 && debug_out != NULL){
#else
if (debug_suspended == 0 && (debug_out != NULL || debug_callback != NULL)) {
#endif
char msg[64];
size_t mlen;
uint8_t * bytes = (uint8_t *)x;
Expand All @@ -173,12 +183,17 @@ void debug_dump(const void * x, int len)
}
if (debug_out) {
fprintf(debug_out, "%s\n", msg);
#if 1
}
#else
} else {
debug_callback(msg, debug_callback_argp);
}
#endif
}
}
}
#endif

void debug_printf_push_stream(FILE* f)
{
Expand Down Expand Up @@ -303,17 +318,6 @@ uint8_t picoquic_parse_connection_id_hexa(char const * hex_input, size_t input_l
return (cnx_id->id_len);
}

uint8_t picoquic_create_packet_header_cnxid_lengths(uint8_t dest_len, uint8_t srce_len)
{
uint8_t ret;

ret = (dest_len < 4) ? 0 : (dest_len - 3);
ret <<= 4;
ret |= (srce_len < 4) ? 0 : (srce_len - 3);

return ret;
}

uint8_t picoquic_format_connection_id(uint8_t* bytes, size_t bytes_max, picoquic_connection_id_t cnx_id)
{
uint8_t copied = cnx_id.id_len;
Expand Down Expand Up @@ -404,18 +408,6 @@ uint64_t picoquic_val64_connection_id(picoquic_connection_id_t cnx_id)
return val64;
}

void picoquic_set64_connection_id(picoquic_connection_id_t * cnx_id, uint64_t val64)
{
for (int i = 7; i >= 0; i--) {
cnx_id->id[i] = (uint8_t)(val64 & 0xFF);
val64 >>= 8;
}
for (size_t i = 8; i < sizeof(cnx_id->id); i++) {
cnx_id->id[i] = 0;
}
cnx_id->id_len = 8;
}

uint64_t picoquic_hash_addr(const struct sockaddr* addr)
{
uint64_t h;
Expand All @@ -433,7 +425,7 @@ uint64_t picoquic_hash_addr(const struct sockaddr* addr)

return h;
}

#if 0
int picoquic_compare_addr(const struct sockaddr * expected, const struct sockaddr * actual)
{
int ret = -1;
Expand Down Expand Up @@ -464,7 +456,7 @@ int picoquic_compare_addr(const struct sockaddr * expected, const struct sockadd

return ret;
}

#endif

int picoquic_compare_ip_addr(const struct sockaddr* expected, const struct sockaddr* actual)
{
Expand Down Expand Up @@ -497,6 +489,16 @@ uint16_t picoquic_get_addr_port(const struct sockaddr* addr)
return port;
}

int picoquic_compare_addr(const struct sockaddr* expected, const struct sockaddr* actual)
{
int ret = picoquic_compare_ip_addr(expected, actual);
if (ret == 0 &&
picoquic_get_addr_port(expected) != picoquic_get_addr_port(actual)) {
ret = -1;
}
return ret;
}

void picoquic_set_addr_port(const struct sockaddr* addr, uint16_t port)
{
if (addr->sa_family == AF_INET6) {
Expand Down Expand Up @@ -830,6 +832,7 @@ const uint8_t* picoquic_frames_cid_decode(const uint8_t* bytes, const uint8_t* b
return bytes;
}

#if 0
/* Predict length of a varint encoding */
size_t picoquic_frames_varint_encode_length(uint64_t n64)
{
Expand All @@ -849,6 +852,7 @@ size_t picoquic_frames_varint_encode_length(uint64_t n64)

return len;
}
#endif

/* Encoding functions of the form uint8_t * picoquic_frame_XXX_encode(uint8_t * bytes, uint8_t * bytes-max, ...)
*/
Expand Down
6 changes: 4 additions & 2 deletions picoquic_t/picoquic_t.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ typedef enum {
static const picoquic_test_def_t test_table[] = {
{ "connection_id_print", util_connection_id_print_test },
{ "connection_id_parse", util_connection_id_parse_test },
{ "sprintf", util_sprintf_test },
{ "memcmp", util_memcmp_test },
{ "util_sprintf", util_sprintf_test },
{ "util_debug_print", util_debug_print_test },
{ "util_uint8_to_str", util_uint8_to_str_test },
{ "util_memcmp", util_memcmp_test },
{ "threading", util_threading_test },
{ "picohash", picohash_test },
{ "picohash_embedded", picohash_embedded_test },
Expand Down
2 changes: 2 additions & 0 deletions picoquictest/picoquictest.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extern size_t picoquic_stress_nb_clients; /* number of stress clients, defaults
int util_connection_id_print_test();
int util_connection_id_parse_test();
int util_sprintf_test();
int util_debug_print_test();
int util_uint8_to_str_test();
int util_memcmp_test();
int util_threading_test();
int picohash_test();
Expand Down
Loading

0 comments on commit f3db0d7

Please sign in to comment.