summaryrefslogtreecommitdiff
path: root/test/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/test.c')
-rw-r--r--test/test.c198
1 files changed, 189 insertions, 9 deletions
diff --git a/test/test.c b/test/test.c
index 7984cba..eb197bb 100644
--- a/test/test.c
+++ b/test/test.c
@@ -4,16 +4,196 @@
#include <unistd.h>
#include <time.h>
-#include <linux/in.h>
-#include <linux/if_ether.h>
-#include <linux/ip.h>
-#include <linux/ipv6.h>
-#include <linux/tcp.h>
-
-#include <sys/socket.h>
-
-#include <pcap.h>
+#include "cJSON.h"
#include "osfp.h"
#include "osfp_fingerprint.h"
#include "osfp_score_db.h"
+#include "osfp_log.h"
+#include "osfp_common.h"
+
+#define DATA_FILE_PATH "./data.json"
+#define DB_FILE_PATH "./db.json"
+#define TEST_FILE_PATH "./test.json"
+#define LOG_FILE_PATH "./osfp_test.log"
+
+unsigned char *data_file_path = DATA_FILE_PATH;
+unsigned char *db_file_path;
+unsigned char *test_file_path;
+
+FILE *data_file_ptr;
+FILE *db_file_ptr;
+FILE *test_file_ptr;
+FILE *log_file_ptr;
+
+unsigned int debug_enable;
+
+void test_data_prepare()
+{
+ char *file_buffer;
+
+ if (test_file_path == NULL) {
+ file_buffer = osfp_score_db_read_file(data_file_path);
+ if (file_buffer == NULL) {
+ osfp_log_error("read file: '%s'\n", data_file_path);
+ exit(1);
+ }
+
+ cJSON *data;
+ data = cJSON_Parse(file_buffer);
+ if (data == NULL) {
+ exit(1);
+ }
+
+ cJSON *test;
+ test = cJSON_CreateArray();
+
+ unsigned int data_count = cJSON_GetArraySize(data);
+ unsigned int db_count = data_count * 8 / 10;
+ unsigned int test_count = data_count - db_count;
+ srand(time(0));
+
+ int i;
+ for (i = 0; i < test_count; i++) {
+ cJSON_AddItemToArray(test, cJSON_DetachItemFromArray(data, rand() % data_count));
+ data_count--;
+ }
+
+ db_file_ptr = fopen(DB_FILE_PATH, "w");
+ test_file_ptr = fopen(TEST_FILE_PATH, "w");
+
+ fprintf(db_file_ptr, "%s", cJSON_Print(data));
+ fprintf(test_file_ptr, "%s", cJSON_Print(test));
+
+ fflush(db_file_ptr);
+ fflush(test_file_ptr);
+
+ db_file_path = DB_FILE_PATH;
+ test_file_path = TEST_FILE_PATH;
+ }else{
+ db_file_path = data_file_path;
+ }
+
+}
+
+void test_miss_rate()
+{
+ int i;
+ unsigned int other_count = 0;
+ unsigned int unknown_count = 0;
+ unsigned int identify_failed_count = 0;
+ unsigned int wrong_count = 0;
+ unsigned int verified_count = 0;
+ unsigned int fingerprint_count = 0;
+ char *file_buffer;
+
+ file_buffer = osfp_score_db_read_file(test_file_path);
+ if (file_buffer == NULL) {
+ osfp_log_error("read file: '%s'\n", test_file_path);
+ exit(1);
+ }
+
+ cJSON *root = cJSON_Parse(file_buffer);
+ if (root == NULL) {
+ exit(1);
+ }
+
+ fingerprint_count = cJSON_GetArraySize(root);
+
+ if (debug_enable) {
+ osfp_log_level_set(OSFP_LOG_LEVEL_DEBUG);
+ }
+
+ struct osfp_db *osfp_db = osfp_db_new(db_file_path);
+ if (osfp_db == NULL) {
+ printf("could not create osfp context. fingerprints file: %s\n", db_file_path);
+ exit(1);
+ }
+
+ //osfp_score_db_debug_print(osfp_db->score_db);
+
+ FILE *log_file_ptr = fopen(LOG_FILE_PATH, "w");
+
+ for (i = 0; i < fingerprint_count; i++) {
+ cJSON *entry = cJSON_GetArrayItem(root, i);
+ if (entry) {
+ cJSON *os_class_json = cJSON_GetObjectItem(entry, "os");
+ int os_class = osfp_os_class_name_to_id(os_class_json->valuestring);
+
+ const char *fp_str = cJSON_PrintUnformatted(entry);
+
+ struct osfp_result *result = osfp_json_identify(osfp_db, fp_str);
+ if (result == NULL) {
+ identify_failed_count++;
+ continue;
+ }
+
+ if (os_class == result->likely_os_class) {
+ verified_count++;
+ osfp_result_free(result);
+ continue;
+ }
+
+ wrong_count++;
+
+ if (result->likely_os_class == OSFP_OS_CLASS_OTHERS) {
+ other_count++;
+ }
+
+ if (result->likely_os_class == OSFP_OS_CLASS_UNKNOWN) {
+ unknown_count++;
+ }
+
+ fprintf(log_file_ptr, "expect: %s, result: %s\n", os_class_json->valuestring, osfp_result_os_name_get(result));
+
+ char *result_json = osfp_result_score_detail_export(result);
+ if (result_json) {
+ fprintf(log_file_ptr, "%s\n", result_json);
+ } else {
+ fprintf(log_file_ptr, "result detail error:%p\n", result);
+ }
+ fflush(log_file_ptr);
+ osfp_result_free(result);
+ }
+ }
+
+ printf("total %u, failed %u, pass %u, wrong %u, other %u, unknown %u\n",
+ fingerprint_count, identify_failed_count, verified_count, wrong_count, other_count, unknown_count);
+
+ printf("miss rate: %d%%\n", 100 - (verified_count * 100 / fingerprint_count));
+
+ printf("details in: %s\n", LOG_FILE_PATH);
+
+ osfp_db_free(osfp_db);
+}
+
+int main(int argc, char **argv)
+{
+ int r;
+
+ while ((r = getopt(argc, argv, "+f:t:o:d")) != -1) {
+ switch(r) {
+ case 'f':
+ data_file_path = (unsigned char*)optarg;
+ break;
+ case 't':
+ test_file_path = (unsigned char*)optarg;
+ break;
+ case 'd':
+ debug_enable = 1;
+ break;
+ default:
+ break;
+ }
+ }
+
+ assert(0 == test_osfp_fingerprinting_ipv4());
+ assert(0 == test_osfp_fingerprinting_ipv6());
+ assert(0 == test_osfp_fingerprinting_tcp_option());
+ assert(0 == test_osfp_score_db());
+
+ test_data_prepare();
+ test_miss_rate();
+
+ return 0;
+}