|
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 /* |
|
7 * File: str2addr.c |
|
8 * Description: a test for PR_StringToNetAddr |
|
9 */ |
|
10 |
|
11 #include "nspr.h" |
|
12 |
|
13 #include <stdio.h> |
|
14 #include <stdlib.h> |
|
15 |
|
16 /* Address string to convert */ |
|
17 #define DEFAULT_IPV4_ADDR_STR "207.200.73.41" |
|
18 |
|
19 /* Expected conversion result, in network byte order */ |
|
20 static unsigned char default_ipv4_addr[] = {207, 200, 73, 41}; |
|
21 |
|
22 int main(int argc, char **argv) |
|
23 { |
|
24 PRNetAddr addr; |
|
25 const char *addrStr; |
|
26 unsigned char *bytes; |
|
27 int idx; |
|
28 |
|
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 } |
|
47 |
|
48 printf("PASS\n"); |
|
49 return 0; |
|
50 } |