nfs-ganesha 1.4

test_rpctools.c

Go to the documentation of this file.
00001 /*****
00002  * test the rpctools.c sockaddr_t functions.
00003  */
00004 
00005 #include "config.h"
00006 #include <stdio.h>
00007 #include <sys/types.h>
00008 #include <stdlib.h>  
00009 #include <netinet/in.h>
00010 #include <arpa/inet.h>
00011 #include "rpcal.h"
00012 
00013 int fridgethr_get( pthread_t * pthrid, void *(*thrfunc)(void*), void * thrarg )
00014 {
00015   return 0;
00016 }
00017 
00018 void *rpc_tcp_socket_manager_thread(void *Arg)
00019 {
00020   return NULL;
00021 }
00022 
00023 
00024 void Fatal(void) 
00025 {
00026     return;
00027 }
00028 
00029 int DisplayLogComponentLevel(log_components_t component,
00030                              char *function,
00031                              log_levels_t  level,
00032                              char *format, ...)
00033 {
00034     return 0;
00035 }
00036 
00037 void GetNameFunction(char *name, int len)
00038 {
00039 }
00040 
00041 #define EQUALS(a, b, msg) do {                    \
00042   if (a != b) {                             \
00043       printf(msg "\n");                           \
00044       exit(1);                                    \
00045     }                                             \
00046 } while(0)
00047 
00048 #define CMP(a, b, n, msg) do {               \
00049   if (strncmp(a, b, n) != 0) {               \
00050       printf(msg "\n");                           \
00051       exit(1);                                    \
00052     }                                             \
00053 } while(0)
00054 
00055 sockaddr_t ipv4a;
00056 sockaddr_t ipv4b;
00057 sockaddr_t ipv4c;
00058 sockaddr_t ipv6a;
00059 sockaddr_t ipv6b;
00060 sockaddr_t ipv6c;
00061 
00062 void create_ipv4(char * ip, int port, struct sockaddr_in * addr) 
00063 {
00064     memset(addr, 0, sizeof(struct sockaddr_in));
00065     addr->sin_family = AF_INET;
00066     addr->sin_port = port;
00067     inet_pton(AF_INET, ip, &(addr->sin_addr));
00068 }
00069 
00070 void create_ipv6(char * ip, int port, struct sockaddr_in6 * addr) 
00071 {
00072     memset(addr, 0, sizeof(struct sockaddr_in6));
00073     addr->sin6_family = AF_INET6;
00074     addr->sin6_port = port;
00075     inet_pton(AF_INET6, ip, &(addr->sin6_addr.s6_addr));
00076 }
00077 
00078 void init() {
00079     create_ipv4("10.10.5.1", 2048, (struct sockaddr_in * ) &ipv4a);
00080     create_ipv4("10.10.5.1", 2049, (struct sockaddr_in * ) &ipv4b);
00081     create_ipv4("10.10.5.2", 2048, (struct sockaddr_in * ) &ipv4c);
00082 
00083 #ifdef _USE_TIRPC
00084     create_ipv6("2001::1", 2048, (struct sockaddr_in6 *) &ipv6a);
00085     create_ipv6("2001::1", 2049, (struct sockaddr_in6 *) &ipv6b);
00086     create_ipv6("2001::f:1", 2048, (struct sockaddr_in6 *) &ipv6c);
00087 #endif    
00088 }
00089 
00090 void ipv4check() {
00091     printf("Value = %lu\n", hash_sockaddr(&ipv4a, 0));
00092     printf("Value = %lu\n", hash_sockaddr(&ipv4b, 0));
00093     printf("Value = %lu\n", hash_sockaddr(&ipv4c, 0));
00094     EQUALS(hash_sockaddr(&ipv4a, IGNORE_PORT), 17107466, "ipv4a doesn't hash as expected");
00095     EQUALS(hash_sockaddr(&ipv4b, IGNORE_PORT), 17107466, "ipv4b doesn't hash as expected");
00096     EQUALS(hash_sockaddr(&ipv4c, IGNORE_PORT), 33884682, "ipv4c doesn't hash as expected");
00097     EQUALS(hash_sockaddr(&ipv4a, CHECK_PORT), 151325194, "ipv4a doesn't hash as expected");
00098     EQUALS(hash_sockaddr(&ipv4b, CHECK_PORT), 151259658, "ipv4b doesn't hash as expected");
00099     EQUALS(hash_sockaddr(&ipv4c, CHECK_PORT), 168102410, "ipv4c doesn't hash as expected");
00100 }
00101 
00102 void ipv6check() {
00103     printf("Value = %lu\n", hash_sockaddr(&ipv6a, 0));
00104     printf("Value = %lu\n", hash_sockaddr(&ipv6b, 0));
00105     printf("Value = %lu\n", hash_sockaddr(&ipv6c, 0));
00106     EQUALS(hash_sockaddr(&ipv6a, CHECK_PORT), 150995232, "ipv6a doesn't hash as expected");
00107     EQUALS(hash_sockaddr(&ipv6b, CHECK_PORT), 151060768, "ipv6b doesn't hash as expected");
00108     EQUALS(hash_sockaddr(&ipv6c, CHECK_PORT), 150998560, "ipv6c doesn't hash as expected");
00109 }
00110 
00111 void ipv4print() {
00112     char buf[SOCK_NAME_MAX];
00113     sprint_sockip(&ipv4a, buf, sizeof(buf));
00114     // printf("Value = %s\n", buf);
00115     CMP("10.10.5.1", buf, strlen("10.10.5.1"), "ipv4a has the wrong ip value");
00116 
00117     sprint_sockip(&ipv4b, buf, sizeof(buf));
00118     // printf("Value = %s\n", buf);
00119     CMP("10.10.5.1", buf, strlen("10.10.5.1"), "ipv4b has the wrong ip value");
00120 
00121     sprint_sockip(&ipv4c, buf, sizeof(buf));
00122     // printf("Value = %s\n", buf);
00123     CMP("10.10.5.2", buf, strlen("10.10.5.2"), "ipv4c has the wrong ip value");
00124 }
00125 
00126 void ipv6print() {
00127     char buf[SOCK_NAME_MAX];
00128     sprint_sockip(&ipv6a, buf, sizeof(buf));
00129     // printf("Value = %s\n", buf);
00130     CMP("2001::1", buf, strlen("2001::1"), "ipv6a has the wrong ip value");
00131 
00132     sprint_sockip(&ipv6b, buf, sizeof(buf));
00133     // printf("Value = %s\n", buf);
00134     CMP("2001::1", buf, strlen("2001::1"), "ipv6b has the wrong ip value");
00135 
00136     sprint_sockip(&ipv6c, buf, sizeof(buf));
00137     // printf("Value = %s\n", buf);
00138     CMP("2001::f:1", buf, strlen("2001::f:1"), "ipv6c has the wrong ip value");
00139 }
00140 
00141 void ipv4cmp() {
00142     EQUALS(cmp_sockaddr(&ipv4a, &ipv4b, IGNORE_PORT), 1, "ipv4a comapred to ipv4b no port");
00143     EQUALS(cmp_sockaddr(&ipv4a, &ipv4b, CHECK_PORT), 0, "ipv4a comapred to ipv4b with port");
00144     EQUALS(cmp_sockaddr(&ipv4a, &ipv4c, IGNORE_PORT), 0, "ipv4a comapred to ipv4c no port");
00145     EQUALS(cmp_sockaddr(&ipv4a, &ipv4c, CHECK_PORT), 0, "ipv4a comapred to ipv4c with port");
00146 }
00147 
00148 void ipv6cmp() {
00149     EQUALS(cmp_sockaddr(&ipv6a, &ipv6b, IGNORE_PORT), 1, "ipv6a comapred to ipv6b no port");
00150     EQUALS(cmp_sockaddr(&ipv6a, &ipv6b, CHECK_PORT), 0, "ipv6a comapred to ipv6b with port");
00151     EQUALS(cmp_sockaddr(&ipv6a, &ipv6c, IGNORE_PORT), 0, "ipv6a comapred to ipv6c no port");
00152     EQUALS(cmp_sockaddr(&ipv6a, &ipv6c, CHECK_PORT), 0, "ipv6a comapred to ipv6c with port");
00153 }
00154 
00155 int main()
00156 {
00157     init();
00158     ipv4check();
00159     ipv4print();
00160     ipv4cmp();
00161 
00162 #ifdef _USE_TIRPC
00163     ipv6check();
00164     ipv6print();
00165     ipv6cmp();
00166 #endif
00167 
00168     return 0;
00169 }