📄 fileio_test.c
字号:
/* $Id: fileio_test.c,v 1.9 2001/07/11 14:52:00 jm Exp $ * Test module for fileio * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2001, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#include <stdio.h>#include <stdlib.h>#include <string.h> /* for memset */#include <netinet/in.h>#include <assert.h>#include <syslog.h>#include "message.h"#include "fileio.h"/* defines */#define ASSERT assert#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endif#define MAXSTRINGLEN 64#define MAXHEXNUMLEN 16#define FILENAME_TEST "fileio_test.conf"/* structures */struct test_config { struct in_addr ip_addr; struct in_addr ip_mask; int prefix_len; unsigned char hex_table[MAXHEXNUMLEN]; int hex_table_len; char string[MAXSTRINGLEN + 1]; int string_len; int i; char nai[MAX_NAI_LEN + 1]; int nai_len;};struct load_test_data { struct test_config *cfg; int firsttime;};/* function prototypes */static int process_load_test(void *voidptr, char *key, char *data);static int load_test(struct test_config *test);/* global variables *//* load test */static int load_test(struct test_config *cfg){ FILE *file; struct load_test_data test; test.cfg = cfg; memset(cfg, '\0', sizeof(struct test_config)); test.firsttime = TRUE; file = fopen(FILENAME_TEST, "r"); if (file == NULL) return FALSE; if (load_data(&test, file, process_load_test) == FALSE) { fprintf(stderr, "load_test: Error while interpreting file '%s'!\n", FILENAME_TEST); fclose(file); return FALSE; } fclose(file); return TRUE;}/* Process loading of the test_data * Return values: -2: consistency error, -1: error, 0: ok, 1: end */static int process_load_test(void *voidptr, char *key, char *data){ int i; struct load_test_data *test; struct test_config *cfg; test = voidptr; cfg = test->cfg; if (test->firsttime == TRUE) { printf("Test case M02: Test blank lines\n"); printf("M02 test cases (1) OK\n"); printf("Test case M03: Test comments in the beginning of the " "line\n"); printf("M03 test cases (~ 20) OK\n"); printf("Test case M04: Test comments not in the beginning of " "the line\n"); printf("M04 test cases (1) OK\n"); test->firsttime = FALSE; } if (strcmp(key, "DummyValue") == 0) { printf("Test case M05: Configuration entry before a " "comment\n"); if (load_int(data, &cfg->i) == TRUE) { printf("M05 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "LongLine") == 0) { printf("Test case M06: Line is longer than default buffer\n"); i = strlen(data); printf("Line length: %d\n", i); if (i == 600 + 2 + 1 - 10) { printf("M06 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "SpacesBeforeKeyword") == 0) { printf("Test case M07: Spaces before the keyword\n"); if (load_int(data, &i) == TRUE) { ASSERT(i == 5); printf("M07 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IntHex") == 0) { printf("Test case M08: Hexadecimal number\n"); if (load_int(data, &cfg->i) == TRUE) { ASSERT(cfg->i == 0xabcdef12); printf("M08 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IntOct") == 0) { printf("Test case M09: Octal number\n"); if (load_int(data, &cfg->i) == TRUE) { ASSERT(cfg->i == 0177377); printf("M09 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IntDec") == 0) { printf("Test case M10: Decimal number\n"); if (load_int(data, &cfg->i) == TRUE) { ASSERT(cfg->i == -6543); printf("M10 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IntInvalid") == 0) { printf("Test case M11: Invalid number\n"); if (load_int(data, &cfg->i) == FALSE) { printf("M11 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "BoolTrue") == 0) { printf("Test case M12: Boolean TRUE\n"); if (load_bool(data, &cfg->i) == TRUE) { ASSERT(cfg->i == TRUE); printf("M12 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "BoolFalse") == 0) { printf("Test case M13: Boolean FALSE\n"); if (load_bool(data, &cfg->i) == TRUE) { ASSERT(cfg->i == FALSE); printf("M13 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "BoolInvalid") == 0) { printf("Test case M14: Invalid boolean\n"); if (load_bool(data, &cfg->i) == FALSE) { printf("M14 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPValid") == 0) { printf("Test case M15: Valid IP address\n"); if (load_ip_address(data, &cfg->ip_addr) == TRUE) { ASSERT(ntohl(cfg->ip_addr.s_addr) == ((123 << 24) | (45 << 16) | (67 << 8) | 89)); printf("M15 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPValidNetmask") == 0) { printf("Test case: Valid IP address with mask\n"); if (load_net_address(data, &cfg->ip_addr, &cfg->ip_mask) == TRUE) { ASSERT(ntohl(cfg->ip_addr.s_addr) == ((123 << 24) | (45 << 16) | (67 << 8) | 89)); ASSERT(ntohl(cfg->ip_mask.s_addr) == ((255 << 24) | (255 << 16) | (255 << 8) | 0)); printf("test case (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPValidPrefix") == 0) { printf("Test case: Valid IP prefix\n"); if (load_ip_prefix(data, &cfg->ip_addr, &cfg->prefix_len) == TRUE) { ASSERT(ntohl(cfg->ip_addr.s_addr) == ((123 << 24) | (45 << 16) | (67 << 8) | 89)); ASSERT(cfg->prefix_len == 24); printf("test case (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPNotNumber") == 0) { printf("Test case M16: IP byte is not a number\n"); if (load_ip_address(data, &cfg->ip_addr) == FALSE) { printf("M16 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPTooHigh") == 0) { printf("Test case M17: IP byte higher than 255\n"); if (load_ip_address(data, &cfg->ip_addr) == FALSE) { printf("M17 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPTooLow") == 0) { printf("Test case M18: IP byte lower than 0\n"); if (load_ip_address(data, &cfg->ip_addr) == FALSE) { printf("M18 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPNoDot") == 0) { printf("Test case M19: No separating dot\n"); if (load_ip_address(data, &cfg->ip_addr) == FALSE) { printf("M19 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPTooFew") == 0) { printf("Test case M20: Too few IP bytes\n"); if (load_ip_address(data, &cfg->ip_addr) == FALSE) { printf("M20 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPTooMany") == 0) { printf("Test case M21: Too many IP bytes\n"); if (load_ip_address(data, &cfg->ip_addr) == FALSE) { printf("M21 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "IPInvalid") == 0) { printf("Test case M22: IP address, something else\n"); if (load_ip_address(data, &cfg->ip_addr) == FALSE) { printf("M22 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "HexTableValid") == 0) { printf("Test case M23: Valid hex table\n"); if (load_hex_table(data, cfg->hex_table, MAXHEXNUMLEN, &cfg->hex_table_len) == TRUE) { ASSERT(cfg->hex_table[0] == 0x0c); ASSERT(cfg->hex_table[1] == 0x64); ASSERT(cfg->hex_table[15] == 0x41); printf("M23 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "HexTableInvalidOdd") == 0) { printf("Test case M24: Invalid odd character\n"); if (load_hex_table(data, cfg->hex_table, MAXHEXNUMLEN, &cfg->hex_table_len) == FALSE) { printf("M24 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "HexTableInvalidEven") == 0) { printf("Test case M25: Invalid even character\n"); if (load_hex_table(data, cfg->hex_table, MAXHEXNUMLEN, &cfg->hex_table_len) == FALSE) { printf("M25 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "HexTableIncomplete") == 0) { printf("Test case M26: Incomplete hex byte\n"); if (load_hex_table(data, cfg->hex_table, MAXHEXNUMLEN, &cfg->hex_table_len) == FALSE) { printf("M26 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "HexTableTooLong") == 0) { printf("Test case M27: Too long hex value\n"); if (load_hex_table(data, cfg->hex_table, MAXHEXNUMLEN, &cfg->hex_table_len) == FALSE) { printf("M27 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "HexTableLengthNULL") == 0) { printf("Test case M28: Hex table, length is NULL\n"); if (load_hex_table(data, cfg->hex_table, MAXHEXNUMLEN, &cfg->hex_table_len) == FALSE) { printf("M28 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "HexTableInvalid") == 0) { printf("Test case M29: Hex table, something else\n"); if (load_hex_table(data, cfg->hex_table, MAXHEXNUMLEN, &cfg->hex_table_len) == FALSE) { printf("M29 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "StringValid") == 0) { printf("Test case M30: Valid string\n"); if (load_char_table(data, cfg->string, MAXSTRINGLEN) == TRUE) { ASSERT(strcmp(cfg->string, "hihhulihei!") == 0); printf("M30 test cases (1) OK\n"); return 0; } return -1; } if (strcmp(key, "String2x") == 0) { printf("Test case M31: 2 x '\"' in string\n"); if (load_char_table(data, cfg->string, MAXSTRINGLEN) == TRUE) { ASSERT(strcmp(cfg->string, "lainausmerkeist
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -