iPXE
Macros | Functions | Variables
ipv4_test.c File Reference

IPv4 tests. More...

#include <stdint.h>
#include <string.h>
#include <byteswap.h>
#include <ipxe/in.h>
#include <ipxe/test.h>

Go to the source code of this file.

Macros

#define IPV4(a, b, c, d)   htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
 Define inline IPv4 address. More...
 
#define inet_ntoa_ok(addr, text)   inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )
 
#define inet_aton_ok(text, addr)   inet_aton_okx ( text, addr, __FILE__, __LINE__ )
 
#define inet_aton_fail_ok(text)   inet_aton_fail_okx ( text, __FILE__, __LINE__ )
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void inet_ntoa_okx (uint32_t addr, const char *text, const char *file, unsigned int line)
 Report an inet_ntoa() test result. More...
 
static void inet_aton_okx (const char *text, uint32_t addr, const char *file, unsigned int line)
 Report an inet_aton() test result. More...
 
static void inet_aton_fail_okx (const char *text, const char *file, unsigned int line)
 Report an inet_aton() failure test result. More...
 
static void ipv4_test_exec (void)
 Perform IPv4 self-tests. More...
 

Variables

struct self_test ipv4_test __self_test
 IPv4 self-test. More...
 

Detailed Description

IPv4 tests.

Definition in file ipv4_test.c.

Macro Definition Documentation

◆ IPV4

#define IPV4 (   a,
  b,
  c,
  d 
)    htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )

Define inline IPv4 address.

Definition at line 42 of file ipv4_test.c.

◆ inet_ntoa_ok

#define inet_ntoa_ok (   addr,
  text 
)    inet_ntoa_okx ( addr, text, __FILE__, __LINE__ )

Definition at line 67 of file ipv4_test.c.

◆ inet_aton_ok

#define inet_aton_ok (   text,
  addr 
)    inet_aton_okx ( text, addr, __FILE__, __LINE__ )

Definition at line 87 of file ipv4_test.c.

◆ inet_aton_fail_ok

#define inet_aton_fail_ok (   text)    inet_aton_fail_okx ( text, __FILE__, __LINE__ )

Definition at line 104 of file ipv4_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ inet_ntoa_okx()

static void inet_ntoa_okx ( uint32_t  addr,
const char *  text,
const char *  file,
unsigned int  line 
)
static

Report an inet_ntoa() test result.

Parameters
addrIPv4 address
textExpected textual representation
fileTest code file
lineTest code line

Definition at line 53 of file ipv4_test.c.

54  {
55  struct in_addr in = { .s_addr = addr };
56  char *actual;
57 
58  /* Format address */
59  actual = inet_ntoa ( in );
60  DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
61  ( ( ntohl ( addr ) >> 24 ) & 0xff ),
62  ( ( ntohl ( addr ) >> 16 ) & 0xff ),
63  ( ( ntohl ( addr ) >> 8 ) & 0xff ),
64  ( ( ntohl ( addr ) >> 0 ) & 0xff ), actual );
65  okx ( strcmp ( actual, text ) == 0, file, line );
66 }
__be32 in[4]
Definition: CIB_PRM.h:35
#define ntohl(value)
Definition: byteswap.h:134
#define okx(success, file, line)
Report test result.
Definition: test.h:44
IP address structure.
Definition: in.h:39
char * inet_ntoa(struct in_addr in)
Convert IPv4 address to dotted-quad notation.
Definition: ipv4.c:658
u32 addr
Definition: sky2.h:8
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498

References addr, DBG, in, inet_ntoa(), ntohl, okx, and strcmp().

◆ inet_aton_okx()

static void inet_aton_okx ( const char *  text,
uint32_t  addr,
const char *  file,
unsigned int  line 
)
static

Report an inet_aton() test result.

Parameters
textTextual representation
addrExpected IPv4 address
fileTest code file
lineTest code line

Definition at line 78 of file ipv4_test.c.

79  {
80  struct in_addr actual;
81 
82  /* Parse address */
83  okx ( inet_aton ( text, &actual ) != 0, file, line );
84  DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
85  okx ( actual.s_addr == addr, file, line );
86 };
#define okx(success, file, line)
Report test result.
Definition: test.h:44
IP address structure.
Definition: in.h:39
int inet_aton(const char *string, struct in_addr *in)
Parse IPv4 address.
Definition: ipv4.c:631
char * inet_ntoa(struct in_addr in)
Convert IPv4 address to dotted-quad notation.
Definition: ipv4.c:658
u32 addr
Definition: sky2.h:8
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498

References addr, DBG, inet_aton(), inet_ntoa(), okx, and in_addr::s_addr.

◆ inet_aton_fail_okx()

static void inet_aton_fail_okx ( const char *  text,
const char *  file,
unsigned int  line 
)
static

Report an inet_aton() failure test result.

Parameters
textTextual representation
fileTest code file
lineTest code line

Definition at line 97 of file ipv4_test.c.

98  {
99  struct in_addr actual;
100 
101  /* Attempt to parse address */
102  okx ( inet_aton ( text, &actual ) == 0, file, line );
103 }
#define okx(success, file, line)
Report test result.
Definition: test.h:44
IP address structure.
Definition: in.h:39
int inet_aton(const char *string, struct in_addr *in)
Parse IPv4 address.
Definition: ipv4.c:631

References inet_aton(), and okx.

◆ ipv4_test_exec()

static void ipv4_test_exec ( void  )
static

Perform IPv4 self-tests.

Definition at line 111 of file ipv4_test.c.

111  {
112 
113  /* Address testing macros */
114  ok ( IN_IS_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
115  ok ( ! IN_IS_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
116  ok ( ! IN_IS_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
117  ok ( ! IN_IS_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
118  ok ( IN_IS_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
119  ok ( ! IN_IS_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
120  ok ( ! IN_IS_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
121  ok ( ! IN_IS_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
122  ok ( IN_IS_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
123  ok ( ! IN_IS_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
124  ok ( ! IN_IS_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
125  ok ( ! IN_IS_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
126  ok ( ! IN_IS_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
127  ok ( ! IN_IS_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
128  ok ( IN_IS_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
129  ok ( IN_IS_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
130  ok ( IN_IS_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );
131 
132  /* inet_ntoa() tests */
133  inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
134  inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
135  inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
136  inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );
137 
138  /* inet_aton() tests */
139  inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
140  inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );
141 
142  /* inet_aton() failure tests */
143  inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
144  inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
145  inet_aton_fail_ok ( "127.0.0" ); /* Too short */
146  inet_aton_fail_ok ( "1.2.3.a" ); /* Invalid characters */
147  inet_aton_fail_ok ( "127.0..1" ); /* Missing bytes */
148 }
#define IN_IS_CLASSA(addr)
Definition: in.h:27
#define IN_IS_CLASSB(addr)
Definition: in.h:29
#define inet_aton_ok(text, addr)
Definition: ipv4_test.c:87
#define inet_ntoa_ok(addr, text)
Definition: ipv4_test.c:67
#define IPV4(a, b, c, d)
Define inline IPv4 address.
Definition: ipv4_test.c:42
#define inet_aton_fail_ok(text)
Definition: ipv4_test.c:104
#define IN_IS_MULTICAST(addr)
Definition: in.h:33
#define IN_IS_CLASSC(addr)
Definition: in.h:31
#define ok(success)
Definition: test.h:46

References IN_IS_CLASSA, IN_IS_CLASSB, IN_IS_CLASSC, IN_IS_MULTICAST, inet_aton_fail_ok, inet_aton_ok, inet_ntoa_ok, IPV4, and ok.

Variable Documentation

◆ __self_test

struct self_test ipv4_test __self_test
Initial value:
= {
.name = "ipv4",
.exec = ipv4_test_exec,
}
static void ipv4_test_exec(void)
Perform IPv4 self-tests.
Definition: ipv4_test.c:111

IPv4 self-test.

Definition at line 151 of file ipv4_test.c.