nsprpub/pr/tests/addrstr.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/addrstr.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "prnetdb.h"
    1.10 +
    1.11 +#include <stdio.h>
    1.12 +#include <string.h>
    1.13 +#include <stdlib.h>
    1.14 +
    1.15 +const char *testaddrs[] = {
    1.16 +    "::", "::",
    1.17 +    "::1", "::1",
    1.18 +    "::ffff", "::ffff",
    1.19 +    "::1:0", "::0.1.0.0",
    1.20 +    "::127.0.0.1", "::127.0.0.1",
    1.21 +    "::FFFF:127.0.0.1", "::ffff:127.0.0.1",
    1.22 +    "::FFFE:9504:3501", "::fffe:9504:3501",
    1.23 +    "0:0:1:0:35c:0:0:0", "0:0:1:0:35c::",
    1.24 +    "0:0:3f4c:0:0:4552:0:0", "::3f4c:0:0:4552:0:0",
    1.25 +    "0:0:1245:0:0:0:0567:0", "0:0:1245::567:0", 
    1.26 +    "0:1:2:3:4:5:6:7", "0:1:2:3:4:5:6:7", 
    1.27 +    "1:2:3:0:4:5:6:7", "1:2:3:0:4:5:6:7", 
    1.28 +    "1:2:3:4:5:6:7:0", "1:2:3:4:5:6:7:0", 
    1.29 +    "1:2:3:4:5:6:7:8", "1:2:3:4:5:6:7:8", 
    1.30 +    "1:2:3:4:5:6::7", "1:2:3:4:5:6:0:7", 
    1.31 +    0
    1.32 +};
    1.33 +
    1.34 +const char *badaddrs[] = {
    1.35 +    "::.1.2.3",
    1.36 +    "ffff::.1.2.3",
    1.37 +    "1:2:3:4:5:6:7::8",
    1.38 +    "1:2:3:4:5:6::7:8",
    1.39 +    "::ff99.2.3.4",
    1.40 +    0
    1.41 +};
    1.42 +
    1.43 +int failed_already = 0;
    1.44 +
    1.45 +int main(int argc, char **argv)
    1.46 +{
    1.47 +    const char **nexttestaddr = testaddrs;
    1.48 +    const char **nextbadaddr = badaddrs;
    1.49 +    const char *in, *expected_out;
    1.50 +    PRNetAddr addr;
    1.51 +    char buf[256];
    1.52 +    PRStatus rv;
    1.53 +
    1.54 +    while ((in = *nexttestaddr++) != 0) {
    1.55 +	expected_out = *nexttestaddr++;
    1.56 +	rv = PR_StringToNetAddr(in, &addr);
    1.57 +	if (rv) {
    1.58 +	    printf("cannot convert %s to addr: %d\n", in, rv);
    1.59 +            failed_already = 1;
    1.60 +	    continue;
    1.61 +	}
    1.62 +	rv = PR_NetAddrToString(&addr, buf, sizeof(buf));
    1.63 +	if (rv) {
    1.64 +	    printf("cannot convert %s back to string: %d\n", in, rv);
    1.65 +            failed_already = 1;
    1.66 +	    continue;
    1.67 +	}
    1.68 +	if (strcmp(buf, expected_out)) {
    1.69 +            /* This is not necessarily an error */
    1.70 +	    printf("%s expected %s got %s\n", in, expected_out, buf);
    1.71 +	}
    1.72 +    }
    1.73 +    while ((in = *nextbadaddr++) != 0) {
    1.74 +        if (PR_StringToNetAddr(in, &addr) == PR_SUCCESS) {
    1.75 +            printf("converted bad addr %s\n", in);
    1.76 +            failed_already = 1;
    1.77 +        }
    1.78 +    }
    1.79 +    if (failed_already) {
    1.80 +        printf("FAIL\n");
    1.81 +        return 1;
    1.82 +    }
    1.83 +    printf("PASS\n");
    1.84 +    return 0;
    1.85 +}

mercurial