nsprpub/pr/tests/str2addr.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/str2addr.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,50 @@
     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 +/*
    1.10 + * File: str2addr.c
    1.11 + * Description: a test for PR_StringToNetAddr
    1.12 + */
    1.13 +
    1.14 +#include "nspr.h"
    1.15 +
    1.16 +#include <stdio.h>
    1.17 +#include <stdlib.h>
    1.18 +
    1.19 +/* Address string to convert */
    1.20 +#define DEFAULT_IPV4_ADDR_STR "207.200.73.41"
    1.21 +
    1.22 +/* Expected conversion result, in network byte order */
    1.23 +static unsigned char default_ipv4_addr[] = {207, 200, 73, 41};
    1.24 +
    1.25 +int main(int argc, char **argv)
    1.26 +{
    1.27 +    PRNetAddr addr;
    1.28 +    const char *addrStr;
    1.29 +    unsigned char *bytes;
    1.30 +    int idx;
    1.31 +
    1.32 +    addrStr = DEFAULT_IPV4_ADDR_STR;
    1.33 +    if (PR_StringToNetAddr(addrStr, &addr) == PR_FAILURE) {
    1.34 +        fprintf(stderr, "PR_StringToNetAddr failed\n");
    1.35 +        exit(1);
    1.36 +    }
    1.37 +    if (addr.inet.family != PR_AF_INET) {
    1.38 +        fprintf(stderr, "addr.inet.family should be %d but is %d\n",
    1.39 +                PR_AF_INET, addr.inet.family);
    1.40 +        exit(1);
    1.41 +    }
    1.42 +    bytes = (unsigned char *) &addr.inet.ip;
    1.43 +    for (idx = 0; idx < 4; idx++) {
    1.44 +        if (bytes[idx] != default_ipv4_addr[idx]) {
    1.45 +            fprintf(stderr, "byte %d of IPv4 addr should be %d but is %d\n",
    1.46 +                    idx, default_ipv4_addr[idx], bytes[idx]);
    1.47 +            exit(1);
    1.48 +        }
    1.49 +    }
    1.50 +
    1.51 +    printf("PASS\n");
    1.52 +    return 0;
    1.53 +}

mercurial