test_inet_pton.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /*! \file
3  * \brief Test of \c posix_inet_pton() implementation
4  *
5  * Copyright (c) 2017-2020 by the developers. See the LICENSE file for details.
6  */
7 
8 
9 /* ========================================================================== */
10 /* Include headers */
11 
12 #include "posix.h" /* Include this first because of feature test macros */
13 
14 #include <stdio.h>
15 
16 #include "config.h"
17 
18 #include "test.h"
19 #include "test_inet_pton.h"
20 
21 
22 /* ========================================================================== */
23 /*! \addtogroup TEST */
24 /*! @{ */
25 
26 
27 /* ========================================================================== */
28 /*! \brief Test \c posix_inet_pton() implementation
29  *
30  * The following cases are tested:
31  * - Valid IPv4 address with maximum length
32  * - Valid IPv4 address with minimum length
33  * - Invalid IPv4 address (must be rejected)
34  *
35  * \return
36  * - \c EXIT_SUCCESS on success
37  * - \c EXIT_FAILURE on error
38  */
39 
40 int test_inet_pton(void)
41 {
42  int res = POSIX_EXIT_SUCCESS;
43  const char* s;
44  CFG_UINT32_TYPE expected1 = (CFG_UINT32_TYPE) 3232261220UL;
45  CFG_UINT32_TYPE expected2 = (CFG_UINT32_TYPE) 16909060UL;
46  posix_in_addr_t valh, valn;
47  int rv;
48 
49  /*
50  * This test the following things:
51  * - Valid IPv4 address with maximum length
52  */
53  s = "192.168.100.100";
54  valn = 0;
55  rv = posix_inet_pton(POSIX_AF_INET, s, &valn);
56  if(!rv)
57  {
58  print_error("Conversion failed");
59  res = POSIX_EXIT_FAILURE;
60 
61  /* For debugging */
62  fprintf(stderr, TEST_TAB "Input: \"%s\"\n", s);
63  }
64  if(1 == rv)
65  {
66  valh = posix_ntohl(valn);
67  if(expected1 != valh)
68  {
69  print_error("Result is not correct");
70  res = POSIX_EXIT_FAILURE;
71 
72  /* For debugging */
73  fprintf(stderr, TEST_TAB "Input: \"%s\"\n", s);
74  fprintf(stderr, TEST_TAB "Result is: %lu\n",
75  (unsigned long int) valh);
76  fprintf(stderr, TEST_TAB "Should be: %lu\n",
77  (unsigned long int) expected1);
78  }
79  }
80 
81  if(!res)
82  {
83  /*
84  * This test the following things:
85  * - Valid IPv4 address with minimum length
86  */
87  s = "1.2.3.4";
88  valn = 0;
89  rv = posix_inet_pton(POSIX_AF_INET, s, &valn);
90  if(!rv)
91  {
92  print_error("Conversion failed");
93  res = POSIX_EXIT_FAILURE;
94 
95  /* For debugging */
96  fprintf(stderr, TEST_TAB "Input: \"%s\"\n", s);
97  }
98  if(1 == rv)
99  {
100  valh = posix_ntohl(valn);
101  if(expected2 != valh)
102  {
103  print_error("Result is not correct");
104  res = POSIX_EXIT_FAILURE;
105 
106  /* For debugging */
107  fprintf(stderr, TEST_TAB "Input: \"%s\"\n", s);
108  fprintf(stderr, TEST_TAB "Result is: %lu\n",
109  (unsigned long int) valh);
110  fprintf(stderr, TEST_TAB "Should be: %lu\n",
111  (unsigned long int) expected2);
112  }
113  }
114  }
115 
116  if(!res)
117  {
118  /*
119  * This test the following things:
120  * - Invalid IPv4 address
121  */
122  s = "x192.168.100.100";
123  valn = 0;
124  rv = posix_inet_pton(POSIX_AF_INET, s, &valn);
125  if(rv)
126  {
127  print_error("Conversion succeeded for invalid input");
128  res = POSIX_EXIT_FAILURE;
129 
130  /* For debugging */
131  fprintf(stderr, TEST_TAB "Input: \"%s\"\n", s);
132  }
133  }
134 
135  return(res);
136 }
137 
138 
139 /*! @} */
140 
141 /* EOF */
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
test_inet_pton
int test_inet_pton(void)
Test posix_inet_pton() implementation.
Definition: test_inet_pton.c:40
print_error
void print_error(const char *)
Print error message.
Definition: main.cxx:276

Generated at 2024-04-27 using  doxygen