added gentoo ebuilds
[libmixp.git] / test / addr_parse.c
blobe94f475f12c796167eb630a53687ed11563055cd
2 #include <stdio.h>
3 #include <string.h>
4 #include <9p-mixp/srv_addr.h>
6 void dumpaddr(MIXP_SERVER_ADDRESS* a, const char* prefix, const char* uri)
8 if (!a)
10 printf("%s FAIL URI = %s\n", prefix, uri);
11 return;
14 switch (a->proto)
16 case P9_PROTO_TCP:
17 printf("%s proto = TCP\n", prefix);
18 break;
19 case P9_PROTO_UNIX:
20 printf("%s proto = UNIX\n", prefix);
21 break;
22 default:
23 printf("%s proto = %d\n", prefix, a->proto);
26 printf("%s addrstr = %s\n", prefix, a->addrstr);
27 printf("%s port = %d\n", prefix, a->port);
28 printf("%s hostname = %s\n", prefix, a->hostname);
29 if (a->username)
30 printf("%s username = %s\n", prefix, a->username);
31 if (a->key)
32 printf("%s key = %s\n", prefix, a->key);
33 printf("%s path = %s\n", prefix, a->path);
36 int test_parse_tcp(const char* str, const char* want_server, int want_port, const char* want_path)
38 MIXP_SERVER_ADDRESS* addr = mixp_srv_addr_parse(str);
40 printf("[TCP] Testing address: %s\n", str);
42 if (addr==NULL)
44 fprintf(stderr,"[test-tcp] could not parse: %s\n", str);
45 return 0;
48 dumpaddr(addr, "", str);
50 if (addr->proto != P9_PROTO_TCP)
51 fprintf(stderr,"[test-tcp] wrong proto: %d\n", addr->proto);
53 if (strcmp(addr->hostname, want_server)!=0)
54 fprintf(stderr,"[test-tcp] wrong hostname - should be \"%s\"\n", want_server);
56 if (addr->port != want_port)
57 fprintf(stderr,"[test-tcp] wrong port - should be %d\n", want_port);
59 if (strcmp(addr->path, want_path)!=0)
60 fprintf(stderr,"[test-tcp] wrong path - should be \"%s\"\n", want_path);
62 printf("\n");
63 return 1;
66 int test_parse_unix(const char* url, const char* test_path)
68 MIXP_SERVER_ADDRESS* addr = mixp_srv_addr_parse(url);
70 printf("[UNIX] Testing address: %s\n", url);
72 if (addr==NULL)
74 fprintf(stderr,"[test unix] could not parse: %s\n", url);
75 return 0;
78 dumpaddr(addr, "", url);
80 if (addr->proto != P9_PROTO_UNIX)
81 fprintf(stderr,"[test unix] wrong proto: %d\n", addr->proto);
83 if (strcmp(addr->path, test_path)!=0)
84 fprintf(stderr,"[test unix] pathname mismatch - should be \"%s\"\n", test_path);
86 if (addr->port != -1)
87 fprintf(stderr,"[test unix] wrong port - should be -1\n");
89 if (strlen(addr->hostname))
90 fprintf(stderr,"[test unix] wrong hostname - should be \"\"");
92 printf("\n");
94 return 1;
98 int main()
100 printf("\n");
101 test_parse_tcp ("9p://server:1234/prefix", "server", 1234, "/prefix");
102 test_parse_tcp ("9p:/server:99", "server", 99, "/");
103 test_parse_tcp ("9p:tcp://srv:0023/path/name", "srv", 23, "/path/name");
104 test_parse_unix ("9p:unix://my/local/path", "/my/local/path");
105 test_parse_unix ("9p:unix:/tmp/foo/bar", "/tmp/foo/bar");
106 return 0;