Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * File: str2addr.c |
michael@0 | 8 | * Description: a test for PR_StringToNetAddr |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "nspr.h" |
michael@0 | 12 | |
michael@0 | 13 | #include <stdio.h> |
michael@0 | 14 | #include <stdlib.h> |
michael@0 | 15 | |
michael@0 | 16 | /* Address string to convert */ |
michael@0 | 17 | #define DEFAULT_IPV4_ADDR_STR "207.200.73.41" |
michael@0 | 18 | |
michael@0 | 19 | /* Expected conversion result, in network byte order */ |
michael@0 | 20 | static unsigned char default_ipv4_addr[] = {207, 200, 73, 41}; |
michael@0 | 21 | |
michael@0 | 22 | int main(int argc, char **argv) |
michael@0 | 23 | { |
michael@0 | 24 | PRNetAddr addr; |
michael@0 | 25 | const char *addrStr; |
michael@0 | 26 | unsigned char *bytes; |
michael@0 | 27 | int idx; |
michael@0 | 28 | |
michael@0 | 29 | addrStr = DEFAULT_IPV4_ADDR_STR; |
michael@0 | 30 | if (PR_StringToNetAddr(addrStr, &addr) == PR_FAILURE) { |
michael@0 | 31 | fprintf(stderr, "PR_StringToNetAddr failed\n"); |
michael@0 | 32 | exit(1); |
michael@0 | 33 | } |
michael@0 | 34 | if (addr.inet.family != PR_AF_INET) { |
michael@0 | 35 | fprintf(stderr, "addr.inet.family should be %d but is %d\n", |
michael@0 | 36 | PR_AF_INET, addr.inet.family); |
michael@0 | 37 | exit(1); |
michael@0 | 38 | } |
michael@0 | 39 | bytes = (unsigned char *) &addr.inet.ip; |
michael@0 | 40 | for (idx = 0; idx < 4; idx++) { |
michael@0 | 41 | if (bytes[idx] != default_ipv4_addr[idx]) { |
michael@0 | 42 | fprintf(stderr, "byte %d of IPv4 addr should be %d but is %d\n", |
michael@0 | 43 | idx, default_ipv4_addr[idx], bytes[idx]); |
michael@0 | 44 | exit(1); |
michael@0 | 45 | } |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | printf("PASS\n"); |
michael@0 | 49 | return 0; |
michael@0 | 50 | } |