📄 authorized_test.c
字号:
/* $Id: authorized_test.c,v 1.3 2000/04/06 07:26:53 jm Exp $ * Tests on authorized module * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2000, 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. *//* * This test program tests the authorized.c module that contains the * routines to handle authorized networks (IP addresses) * in the Dynamics project software. */#include <stdio.h>#include <stddef.h>#include <assert.h>#include <arpa/inet.h>#include "authorized.h" /* FUNCTIONS TO BE TESTED: *//* * struct list *authorized_init(void); * int authorized_add(struct list *list, struct in_addr network, * struct in_addr netmask); * int authorized_check(struct list *list, struct in_addr addr); * void authorized_destroy(struct list *list); * *//* defines */#define ASSERT assert#define IP2INT(a,b,c,d) (a << 24 | b << 16 | c << 8 | d)#ifndef FALSE#define FALSE 0#endif#ifndef TRUE#define TRUE 1#endif/* structures */struct auth_test { struct node node1; struct in_addr network; struct in_addr netmask;};/* show nets */static void show_nets(struct list *list){ int i; struct node *node; struct auth_test *auth_test; node = list_get_first(list); i = 1; if (node == NULL) { printf("<EMPTY LIST>\n"); } while(node != NULL) { auth_test = (struct auth_test *) node; printf("item #%d:\n",i); printf("network: %s\n",inet_ntoa(auth_test->network)); printf("netmask: %s\n",inet_ntoa(auth_test->netmask)); node = list_get_next(node); i++; }}/* main */int main(void){ int i, testresult; /* struct list list1, list2; */ /* struct auth_test auth_test1, auth_test2, auth_test3;*/ /* struct node *node;*/ /* struct auth_test *auth_test;*/ struct in_addr net1, net2, net3; struct in_addr mask1, mask2, mask3; struct in_addr addr1, addr2, addr3; struct in_addr wrong_addr1, wrong_addr2; struct list *auth_list; testresult = FALSE; net1.s_addr = htonl(IP2INT(10,16,0,0)); net2.s_addr = htonl(IP2INT(130,233,192,0)); net3.s_addr = htonl(IP2INT(194,252,88,156)); mask1.s_addr = htonl(IP2INT(255,240,0,0)); mask2.s_addr = htonl(IP2INT(255,255,252,0)); mask3.s_addr = htonl(IP2INT(255,255,255,224)); addr1.s_addr = htonl(IP2INT(10,17,1,19)); addr2.s_addr = htonl(IP2INT(130,233,193,2)); addr3.s_addr = htonl(IP2INT(194,252,88,157)); wrong_addr1.s_addr = htonl(IP2INT(200,200,100,100)); wrong_addr2.s_addr = htonl(IP2INT(133,233,225,6)); printf("Testing authorized-module...\n"); printf("\n\nListing the used networks, netmasks and IPs:\n"); printf("net1 : %s\n", inet_ntoa(net1 )); printf("net2 : %s\n", inet_ntoa(net2 )); printf("net3 : %s\n", inet_ntoa(net3 )); printf("mask1 : %s\n", inet_ntoa(mask1 )); printf("mask2 : %s\n", inet_ntoa(mask2 )); printf("mask3 : %s\n", inet_ntoa(mask3 )); printf("addr1 : %s\n", inet_ntoa(addr1 )); printf("addr2 : %s\n", inet_ntoa(addr2 )); printf("addr3 : %s\n", inet_ntoa(addr3 )); printf("wrong_addr1 : %s\n", inet_ntoa(wrong_addr1 )); printf("wrong_addr2 : %s\n\n\n", inet_ntoa(wrong_addr2 )); /* M01 */ printf("Test case M01: Initialize an authorized list structure\n"); auth_list=authorized_init(); ASSERT(auth_list != NULL); printf("M01 test cases (1) OK\n"); /* M02 */ printf("Test case M02: Give a NULL argument as a list " "pointer to the authorized_add function:\n"); testresult = authorized_add(NULL, net1, mask1); printf("If the add to a NULL list fails, " "the test M02 is successful.\n"); ASSERT(testresult == FALSE); /* The add should fail! */ printf("M02 test cases (1) OK\n"); /* M03 */ printf("Test case M03: Add a network address to empty " "authorized list\n"); i = list_is_empty(auth_list); ASSERT(i == TRUE); testresult = authorized_add(auth_list, net1, mask1); ASSERT(testresult == TRUE); printf("M03 test cases (1) OK\n"); /* M04 */ printf("Test case M04: Add 5 network addresses with masks " "to authorized list\n"); i = list_is_empty(auth_list); ASSERT(i == FALSE); testresult = authorized_add(auth_list, net2, mask2); ASSERT(testresult == TRUE); printf("Inserting illegal net/mask pair:\n"); printf(" net: %s\n", inet_ntoa(net1)); printf(" mask: %s\n", inet_ntoa(mask2)); testresult = authorized_add(auth_list, net1, mask2); ASSERT(testresult == TRUE); printf("Inserting illegal net/mask pair:\n"); printf(" net: %s\n", inet_ntoa(net1)); printf(" mask: %s\n", inet_ntoa(mask2)); testresult = authorized_add(auth_list, net2, mask1); ASSERT(testresult == TRUE); testresult = authorized_add(auth_list, net3, mask3); ASSERT(testresult == TRUE); printf("Inserting illegal net/mask pair:\n"); printf(" net: %s\n", inet_ntoa(net1)); printf(" mask: %s\n", inet_ntoa(mask2)); testresult = authorized_add(auth_list, net3, mask1); ASSERT(testresult == TRUE); printf("M04 test cases (5) OK\n"); /* M05 */ printf("Test case M05: " "Test authorized_check with NULL list pointer argument:\n"); testresult = authorized_check(NULL, addr1); ASSERT(testresult == FALSE); printf("M05 test cases (1) OK\n"); /* M06 */ printf("Test case M06: " "The address is from an authorized network\n"); testresult = authorized_check(auth_list, addr1); ASSERT(testresult == TRUE); testresult = authorized_check(auth_list, addr2); ASSERT(testresult == TRUE); testresult = authorized_check(auth_list, addr3); ASSERT(testresult == TRUE); printf("M06 test cases (3) OK\n"); /* M07 */ printf("Test case M07: " "The address is not from any authorized network\n"); testresult = authorized_check(auth_list, wrong_addr2); ASSERT(testresult == FALSE); testresult = authorized_check(auth_list, wrong_addr1); ASSERT(testresult == FALSE); printf("M07 test cases (5) OK\n"); /* M08 */ printf("Test case M08: " "Call authorized_destroy with NULL list pointer argument\n"); authorized_destroy(NULL); printf("M08 test cases (1) OK\n"); /* M09 */ printf("Test case M09: " "Call authorized_destroy with a valid list pointer argument\n"); printf("\n The networks currently in the authorized list:\n"); show_nets(auth_list); authorized_destroy(auth_list); printf("M09 test cases (1) OK\n"); printf("\n\n\t ********************************************* \n" "\t * *\n" "\t * Authorized module tests run. *\n" "\t * Check the results. *\n" "\t * Verify tests failure/success. *\n" "\t * *\n" "\t *********************************************\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -