diff options
author | Alfredo Cardigliano <alfredo.cardigliano@gmail.com> | 2020-09-21 17:24:06 +0200 |
---|---|---|
committer | Alfredo Cardigliano <alfredo.cardigliano@gmail.com> | 2020-09-21 17:24:06 +0200 |
commit | e6d206fd15a8e3ff97dcf4d13bd5e3df23e678a3 (patch) | |
tree | a2eb23a483db7be5e24cd4fa3df30f37b0ffad95 /tests/unit | |
parent | a263ac9024026b3b41378c05c21d873714fbc0a6 (diff) |
Add unit tests to travis. Move ndpi serializer tests to unit tests.
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/Makefile.in | 33 | ||||
-rw-r--r-- | tests/unit/unit.c | 245 |
2 files changed, 278 insertions, 0 deletions
diff --git a/tests/unit/Makefile.in b/tests/unit/Makefile.in new file mode 100644 index 000000000..4acb001c8 --- /dev/null +++ b/tests/unit/Makefile.in @@ -0,0 +1,33 @@ +CC=@CC@ +CXX=@CXX@ + +SRCHOME=../../src + +JSON_INC = $(shell pkg-config --cflags json-c) +JSON_LIB = $(shell pkg-config --libs json-c) + +CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include $(JSON_INC) @CFLAGS@ +LIBNDPI=$(SRCHOME)/lib/libndpi.a +LDFLAGS=$(LIBNDPI) @PCAP_LIB@ @LIBS@ @ADDITIONAL_LIBS@ $(JSON_LIB) -lpthread -lm @LDFLAGS@ +HEADERS=$(SRCHOME)/include/ndpi_api.h $(SRCHOME)/include/ndpi_typedefs.h $(SRCHOME)/include/ndpi_protocol_ids.h +OBJS=unit +PREFIX?=@prefix@ + +all: unit + +EXECUTABLE_SOURCES := unit.c +COMMON_SOURCES := $(filter-out $(EXECUTABLE_SOURCES),$(wildcard *.c )) + +unit: $(LIBNDPI) unit.o + $(CC) $(CFLAGS) unit.o -o $@ $(LDFLAGS) + +%.o: %.c $(HEADERS) Makefile + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + /bin/rm -f *.o unit + /bin/rm -f .*.o.cmd .*.o.d + /bin/rm -rf build + +distclean: clean + /bin/rm -f Makefile diff --git a/tests/unit/unit.c b/tests/unit/unit.c new file mode 100644 index 000000000..c7fb292de --- /dev/null +++ b/tests/unit/unit.c @@ -0,0 +1,245 @@ +/* + * unit.c + * + * Copyright (C) 2019-20 - ntop.org + * + * nDPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * nDPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with nDPI. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifdef linux +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <sched.h> +#endif /* linux */ + +#ifdef WIN32 +#include <winsock2.h> +#include <process.h> +#include <io.h> +#define getopt getopt____ +#else +#include <unistd.h> +#include <netinet/in.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <getopt.h> +#include <string.h> +#include <stdarg.h> +#include <search.h> +#include <pcap.h> +#include <signal.h> +#include <pthread.h> +#include <sys/socket.h> +#include <assert.h> +#include <math.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <libgen.h> + +#include "json.h" + +#include "ndpi_config.h" +#include "ndpi_api.h" + +static struct ndpi_detection_module_struct *ndpi_info_mod = NULL; +static int verbose = 0; + +/* *********************************************** */ + +int serializerUnitTest() { + ndpi_serializer serializer, deserializer; + int i, loop_id; + u_int8_t verbose = 0; + ndpi_serialization_format fmt; + u_int32_t buffer_len; + char *buffer; + enum json_tokener_error jerr; + json_object *j; + + for(loop_id=0; loop_id<3; loop_id++) { + switch(loop_id) { + case 0: + if (verbose) printf("--- TLV test ---\n"); + fmt = ndpi_serialization_format_tlv; + break; + + case 1: + if (verbose) printf("--- JSON test ---\n"); + fmt = ndpi_serialization_format_json; + break; + + case 2: + if (verbose) printf("--- CSV test ---\n"); + fmt = ndpi_serialization_format_csv; + break; + } + assert(ndpi_init_serializer(&serializer, fmt) != -1); + + for(i=0; i<16; i++) { + char kbuf[32], vbuf[32]; + snprintf(kbuf, sizeof(kbuf), "Key %d", i); + snprintf(vbuf, sizeof(vbuf), "Value %d", i); + assert(ndpi_serialize_uint32_uint32(&serializer, i, i*i) != -1); + assert(ndpi_serialize_uint32_string(&serializer, i, "Data") != -1); + assert(ndpi_serialize_string_string(&serializer, kbuf, vbuf) != -1); + assert(ndpi_serialize_string_uint32(&serializer, kbuf, i*i) != -1); + assert(ndpi_serialize_string_float(&serializer, kbuf, (float)(i*i), "%f") != -1); + if ((i&0x3) == 0x3) ndpi_serialize_end_of_record(&serializer); + } + + if (fmt == ndpi_serialization_format_json) { + assert(ndpi_serialize_start_of_list(&serializer, "List") != -1); + + for(i=0; i<4; i++) { + char kbuf[32], vbuf[32]; + snprintf(kbuf, sizeof(kbuf), "Ignored"); + snprintf(vbuf, sizeof(vbuf), "Item %d", i); + assert(ndpi_serialize_uint32_uint32(&serializer, i, i*i) != -1); + assert(ndpi_serialize_string_string(&serializer, kbuf, vbuf) != -1); + assert(ndpi_serialize_string_float(&serializer, kbuf, (float)(i*i), "%f") != -1); + } + assert(ndpi_serialize_end_of_list(&serializer) != -1); + assert(ndpi_serialize_string_string(&serializer, "Last", "Ok") != -1); + + buffer = ndpi_serializer_get_buffer(&serializer, &buffer_len); + + if(verbose) + printf("%s\n", buffer); + + /* Decoding JSON to validate syntax */ + jerr = json_tokener_success; + j = json_tokener_parse_verbose(buffer, &jerr); + if (j == NULL) { + printf("%s: ERROR (json validation failed)\n", __FUNCTION__); + return -1; + } else { + /* Validation ok */ + json_object_put(j); + } + + } else if (fmt == ndpi_serialization_format_csv) { + if(verbose) { + u_int32_t buffer_len = 0; + char *buffer; + + buffer = ndpi_serializer_get_header(&serializer, &buffer_len); + printf("%s\n", buffer); + + buffer = ndpi_serializer_get_buffer(&serializer, &buffer_len); + printf("%s\n", buffer); + } + + } else { + if(verbose) + printf("Serialization size: %u\n", ndpi_serializer_get_buffer_len(&serializer)); + + assert(ndpi_init_deserializer(&deserializer, &serializer) != -1); + + while(1) { + ndpi_serialization_type kt, et; + + et = ndpi_deserialize_get_item_type(&deserializer, &kt); + + if(et == ndpi_serialization_unknown) { + break; + } else if(et == ndpi_serialization_end_of_record) { + if (verbose) printf("EOR\n"); + } else { + u_int32_t k32, v32; + ndpi_string ks, vs; + float vf; + + switch(kt) { + case ndpi_serialization_uint32: + ndpi_deserialize_key_uint32(&deserializer, &k32); + if(verbose) printf("%u=", k32); + break; + case ndpi_serialization_string: + ndpi_deserialize_key_string(&deserializer, &ks); + if (verbose) { + u_int8_t bkp = ks.str[ks.str_len]; + ks.str[ks.str_len] = '\0'; + printf("%s=", ks.str); + ks.str[ks.str_len] = bkp; + } + break; + default: + printf("%s: ERROR (unsupported TLV key type %u)\n", __FUNCTION__, kt); + return -1; + } + + switch(et) { + case ndpi_serialization_uint32: + assert(ndpi_deserialize_value_uint32(&deserializer, &v32) != -1); + if(verbose) printf("%u\n", v32); + break; + + case ndpi_serialization_string: + assert(ndpi_deserialize_value_string(&deserializer, &vs) != -1); + if(verbose) { + u_int8_t bkp = vs.str[vs.str_len]; + vs.str[vs.str_len] = '\0'; + printf("%s\n", vs.str); + vs.str[vs.str_len] = bkp; + } + break; + + case ndpi_serialization_float: + assert(ndpi_deserialize_value_float(&deserializer, &vf) != -1); + if(verbose) printf("%f\n", vf); + break; + + default: + if (verbose) printf("\n"); + printf("%s: ERROR (unsupported type %u detected)\n", __FUNCTION__, et); + return -1; + } + } + + ndpi_deserialize_next(&deserializer); + } + } + + ndpi_term_serializer(&serializer); + } + + printf("%s: OK\n", __FUNCTION__); + return 0; +} + +/* *********************************************** */ + +int main(int argc, char **argv) { + + if (ndpi_get_api_version() != NDPI_API_VERSION) { + printf("nDPI Library version mismatch: please make sure this code and the nDPI library are in sync\n"); + return -1; + } + + ndpi_info_mod = ndpi_init_detection_module(ndpi_no_prefs); + + if (ndpi_info_mod == NULL) + return -1; + + /* Tests */ + if (serializerUnitTest() != 0) return -1; + + return 0; +} + |