nsprpub/pr/tests/str2addr.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     6 /*
     7  * File: str2addr.c
     8  * Description: a test for PR_StringToNetAddr
     9  */
    11 #include "nspr.h"
    13 #include <stdio.h>
    14 #include <stdlib.h>
    16 /* Address string to convert */
    17 #define DEFAULT_IPV4_ADDR_STR "207.200.73.41"
    19 /* Expected conversion result, in network byte order */
    20 static unsigned char default_ipv4_addr[] = {207, 200, 73, 41};
    22 int main(int argc, char **argv)
    23 {
    24     PRNetAddr addr;
    25     const char *addrStr;
    26     unsigned char *bytes;
    27     int idx;
    29     addrStr = DEFAULT_IPV4_ADDR_STR;
    30     if (PR_StringToNetAddr(addrStr, &addr) == PR_FAILURE) {
    31         fprintf(stderr, "PR_StringToNetAddr failed\n");
    32         exit(1);
    33     }
    34     if (addr.inet.family != PR_AF_INET) {
    35         fprintf(stderr, "addr.inet.family should be %d but is %d\n",
    36                 PR_AF_INET, addr.inet.family);
    37         exit(1);
    38     }
    39     bytes = (unsigned char *) &addr.inet.ip;
    40     for (idx = 0; idx < 4; idx++) {
    41         if (bytes[idx] != default_ipv4_addr[idx]) {
    42             fprintf(stderr, "byte %d of IPv4 addr should be %d but is %d\n",
    43                     idx, default_ipv4_addr[idx], bytes[idx]);
    44             exit(1);
    45         }
    46     }
    48     printf("PASS\n");
    49     return 0;
    50 }

mercurial