addrstr.c (2953B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "prnetdb.h" 7 8 #include <stdio.h> 9 #include <string.h> 10 #include <stdlib.h> 11 12 const char* testaddrs[] = {"::", 13 "::", 14 "::1", 15 "::1", 16 "::ffff", 17 "::ffff", 18 "::1:0", 19 "::0.1.0.0", 20 "::127.0.0.1", 21 "::127.0.0.1", 22 "::FFFF:127.0.0.1", 23 "::ffff:127.0.0.1", 24 "::FFFE:9504:3501", 25 "::fffe:9504:3501", 26 "0:0:1:0:35c:0:0:0", 27 "0:0:1:0:35c::", 28 "0:0:3f4c:0:0:4552:0:0", 29 "::3f4c:0:0:4552:0:0", 30 "0:0:1245:0:0:0:0567:0", 31 "0:0:1245::567:0", 32 "0:1:2:3:4:5:6:7", 33 "0:1:2:3:4:5:6:7", 34 "1:2:3:0:4:5:6:7", 35 "1:2:3:0:4:5:6:7", 36 "1:2:3:4:5:6:7:0", 37 "1:2:3:4:5:6:7:0", 38 "1:2:3:4:5:6:7:8", 39 "1:2:3:4:5:6:7:8", 40 "1:2:3:4:5:6::7", 41 "1:2:3:4:5:6:0:7", 42 0}; 43 44 const char* badaddrs[] = {"::.1.2.3", "ffff::.1.2.3", 45 "1:2:3:4:5:6:7::8", "1:2:3:4:5:6::7:8", 46 "::ff99.2.3.4", 0}; 47 48 int failed_already = 0; 49 50 int main(int argc, char** argv) { 51 const char** nexttestaddr = testaddrs; 52 const char** nextbadaddr = badaddrs; 53 const char *in, *expected_out; 54 PRNetAddr addr; 55 char buf[256]; 56 PRStatus rv; 57 58 while ((in = *nexttestaddr++) != 0) { 59 expected_out = *nexttestaddr++; 60 rv = PR_StringToNetAddr(in, &addr); 61 if (rv) { 62 printf("cannot convert %s to addr: %d\n", in, rv); 63 failed_already = 1; 64 continue; 65 } 66 rv = PR_NetAddrToString(&addr, buf, sizeof(buf)); 67 if (rv) { 68 printf("cannot convert %s back to string: %d\n", in, rv); 69 failed_already = 1; 70 continue; 71 } 72 if (strcmp(buf, expected_out)) { 73 /* This is not necessarily an error */ 74 printf("%s expected %s got %s\n", in, expected_out, buf); 75 } 76 } 77 while ((in = *nextbadaddr++) != 0) { 78 if (PR_StringToNetAddr(in, &addr) == PR_SUCCESS) { 79 printf("converted bad addr %s\n", in); 80 failed_already = 1; 81 } 82 } 83 if (failed_already) { 84 printf("FAIL\n"); 85 return 1; 86 } 87 printf("PASS\n"); 88 return 0; 89 }