iPXE
Defines | 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.

Defines

#define IPV4(a, b, c, d)   htonl ( ( (a) << 24 ) | ( (b) << 16 ) | ( (c) << 8 ) | (d) )
 Define inline IPv4 address.
#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.
static void inet_aton_okx (const char *text, uint32_t addr, const char *file, unsigned int line)
 Report an inet_aton() test result.
static void inet_aton_fail_okx (const char *text, const char *file, unsigned int line)
 Report an inet_aton() failure test result.
static void ipv4_test_exec (void)
 Perform IPv4 self-tests.

Variables

struct self_test ipv4_test __self_test
 IPv4 self-test.

Detailed Description

IPv4 tests.

Definition in file ipv4_test.c.


Define Documentation

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

Define inline IPv4 address.

Definition at line 42 of file ipv4_test.c.

Referenced by ipv4_test_exec().

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

Definition at line 67 of file ipv4_test.c.

Referenced by ipv4_test_exec().

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

Definition at line 87 of file ipv4_test.c.

Referenced by ipv4_test_exec().

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

Definition at line 104 of file ipv4_test.c.

Referenced by ipv4_test_exec().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
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.

References DBG, inet_ntoa(), ntohl, okx, in_addr::s_addr, and strcmp().

                                                {
        struct in_addr in = { .s_addr = addr };
        char *actual;

        /* Format address */
        actual = inet_ntoa ( in );
        DBG ( "inet_ntoa ( %d.%d.%d.%d ) = %s\n",
              ( ( ntohl ( addr ) >> 24 ) & 0xff ),
              ( ( ntohl ( addr ) >> 16 ) & 0xff ),
              ( ( ntohl ( addr ) >> 8 ) & 0xff ),
              ( ( ntohl ( addr ) >> 0 ) & 0xff ), actual );
        okx ( strcmp ( actual, text ) == 0, file, line );
}
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.

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

                                                {
        struct in_addr actual;

        /* Parse address */
        okx ( inet_aton ( text, &actual ) != 0, file, line );
        DBG ( "inet_aton ( \"%s\" ) = %s\n", text, inet_ntoa ( actual ) );
        okx ( actual.s_addr == addr, file, line );
};
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.

References inet_aton(), and okx.

                                                     {
        struct in_addr actual;

        /* Attempt to parse address */
        okx ( inet_aton ( text, &actual ) == 0, file, line );
}
static void ipv4_test_exec ( void  ) [static]

Perform IPv4 self-tests.

Definition at line 111 of file ipv4_test.c.

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.

                                    {

        /* Address testing macros */
        ok (   IN_IS_CLASSA ( IPV4 ( 10, 0, 0, 1 ) ) );
        ok ( ! IN_IS_CLASSB ( IPV4 ( 10, 0, 0, 1 ) ) );
        ok ( ! IN_IS_CLASSC ( IPV4 ( 10, 0, 0, 1 ) ) );
        ok ( ! IN_IS_CLASSA ( IPV4 ( 172, 16, 0, 1 ) ) );
        ok (   IN_IS_CLASSB ( IPV4 ( 172, 16, 0, 1 ) ) );
        ok ( ! IN_IS_CLASSC ( IPV4 ( 172, 16, 0, 1 ) ) );
        ok ( ! IN_IS_CLASSA ( IPV4 ( 192, 168, 0, 1 ) ) );
        ok ( ! IN_IS_CLASSB ( IPV4 ( 192, 168, 0, 1 ) ) );
        ok (   IN_IS_CLASSC ( IPV4 ( 192, 168, 0, 1 ) ) );
        ok ( ! IN_IS_MULTICAST ( IPV4 ( 127, 0, 0, 1 ) ) );
        ok ( ! IN_IS_MULTICAST ( IPV4 ( 8, 8, 8, 8 ) ) );
        ok ( ! IN_IS_MULTICAST ( IPV4 ( 0, 0, 0, 0 ) ) );
        ok ( ! IN_IS_MULTICAST ( IPV4 ( 223, 0, 0, 1 ) ) );
        ok ( ! IN_IS_MULTICAST ( IPV4 ( 240, 0, 0, 1 ) ) );
        ok (   IN_IS_MULTICAST ( IPV4 ( 224, 0, 0, 1 ) ) );
        ok (   IN_IS_MULTICAST ( IPV4 ( 231, 89, 0, 2 ) ) );
        ok (   IN_IS_MULTICAST ( IPV4 ( 239, 6, 1, 17 ) ) );

        /* inet_ntoa() tests */
        inet_ntoa_ok ( IPV4 ( 127, 0, 0, 1 ), "127.0.0.1" );
        inet_ntoa_ok ( IPV4 ( 0, 0, 0, 0 ), "0.0.0.0" );
        inet_ntoa_ok ( IPV4 ( 255, 255, 255, 255 ), "255.255.255.255" );
        inet_ntoa_ok ( IPV4 ( 212, 13, 204, 60 ), "212.13.204.60" );

        /* inet_aton() tests */
        inet_aton_ok ( "212.13.204.60", IPV4 ( 212, 13, 204, 60 ) );
        inet_aton_ok ( "127.0.0.1", IPV4 ( 127, 0, 0, 1 ) );

        /* inet_aton() failure tests */
        inet_aton_fail_ok ( "256.0.0.1" ); /* Byte out of range */
        inet_aton_fail_ok ( "212.13.204.60.1" ); /* Too long */
        inet_aton_fail_ok ( "127.0.0" ); /* Too short */
        inet_aton_fail_ok ( "1.2.3.a" ); /* Invalid characters */
        inet_aton_fail_ok ( "127.0..1" ); /* Missing bytes */
}

Variable Documentation

struct self_test ipv4_test __self_test
Initial value:
 {
        .name = "ipv4",
        .exec = ipv4_test_exec,
}

IPv4 self-test.

Definition at line 151 of file ipv4_test.c.