iPXE
uuid_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * UUID tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <string.h>
36#include <byteswap.h>
37#include <ipxe/uuid.h>
38#include <ipxe/test.h>
39
40/** Define an inline UUID value */
41#define UUID( A, B, C, D, E0, E1, E2, E3, E4, E5 ) { \
42 .a = htonl ( A ), \
43 .b = htons ( B ), \
44 .c = htons ( C ), \
45 .d = htons ( D ), \
46 .e = { E0, E1, E2, E3, E4, E5 }, \
47 }
48
49/**
50 * Report a uuid_ntoa() test result
51 *
52 * @v uuid UUID
53 * @v text Expected textual representation
54 * @v file Test code file
55 * @v line Test code line
56 */
57static void uuid_ntoa_okx ( const union uuid *uuid, const char *text,
58 const char *file, unsigned int line ) {
59 const char *actual;
60
61 /* Format address */
62 actual = uuid_ntoa ( uuid );
63 DBG ( "uuid_ntoa ( %08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x ) = "
64 "\"%s\"\n", ntohl ( uuid->canonical.a ),
68 uuid->canonical.e[4], uuid->canonical.e[5], actual );
69 okx ( strcmp ( actual, text ) == 0, file, line );
70}
71#define uuid_ntoa_ok( value, text ) do { \
72 static const union uuid uuid = { \
73 .canonical = value, \
74 }; \
75 uuid_ntoa_okx ( &uuid, text, __FILE__, __LINE__ ); \
76 } while ( 0 )
77
78/**
79 * Report a uuid_aton() test result
80 *
81 * @v text Textual representation
82 * @v uuid Expected UUID
83 * @v file Test code file
84 * @v line Test code line
85 */
86static void uuid_aton_okx ( const char *text, const union uuid *uuid,
87 const char *file, unsigned int line ) {
88 union uuid actual;
89
90 /* Parse address */
91 okx ( uuid_aton ( text, &actual ) == 0, file, line );
92 DBG ( "uuid_aton ( \"%s\" ) = %s\n", text, uuid_ntoa ( &actual ) );
93 okx ( memcmp ( &actual, uuid, sizeof ( actual ) ) == 0, file, line );
94};
95#define uuid_aton_ok( text, value ) do { \
96 static const union uuid uuid = { \
97 .canonical = value, \
98 }; \
99 uuid_aton_okx ( text, &uuid, __FILE__, __LINE__ ); \
100 } while ( 0 )
101
102/**
103 * Report a uuid_aton() failure test result
104 *
105 * @v text Textual representation
106 * @v file Test code file
107 * @v line Test code line
108 */
109static void uuid_aton_fail_okx ( const char *text, const char *file,
110 unsigned int line ) {
111 union uuid actual;
112
113 /* Attempt to parse address */
114 okx ( uuid_aton ( text, &actual ) != 0, file, line );
115}
116#define uuid_aton_fail_ok( text ) \
117 uuid_aton_fail_okx ( text, __FILE__, __LINE__ )
118
119/**
120 * Perform UUID self-tests
121 *
122 */
123static void uuid_test_exec ( void ) {
124
125 /* uuid_ntoa() tests */
126 uuid_ntoa_ok ( UUID ( 0x18725ca6, 0xd699, 0x4e4d, 0xb501,
127 0xc3, 0x80, 0x91, 0xd2, 0xa4, 0x33 ),
128 "18725ca6-d699-4e4d-b501-c38091d2a433" );
129 uuid_ntoa_ok ( UUID ( 0x1a969b23, 0xc7d5, 0x40fe, 0xb79a,
130 0xc9, 0x2e, 0xa3, 0x4a ,0xb4, 0x5b ),
131 "1a969b23-c7d5-40fe-b79a-c92ea34ab45b" );
132
133 /* uuid_aton() tests */
134 uuid_aton_ok ( "62b907a8-e1a7-460e-82f7-667d84270c84",
135 UUID ( 0x62b907a8, 0xe1a7, 0x460e, 0x82f7,
136 0x66, 0x7d, 0x84, 0x27, 0x0c, 0x84 ) );
137 uuid_aton_ok ( "F5D0349C-EF7C-4AD4-B40B-FC2E522A7327",
138 UUID ( 0xf5d0349c, 0xef7c, 0x4ad4, 0xb40b,
139 0xfc, 0x2e, 0x52, 0x2a, 0x73, 0x27 ) );
140 uuid_aton_ok ( "4edd80ff7b43465589a02b1e7cffa196",
141 UUID ( 0x4edd80ff, 0x7b43, 0x4655, 0x89a0,
142 0x2b, 0x1e, 0x7c, 0xff, 0xa1, 0x96 ) );
143
144 /* uuid_aton() failure tests */
145 uuid_aton_fail_ok ( "628d677b-cf38-471e-9ad9-c8a5d9220055b6" );
146 uuid_aton_fail_ok ( "5071ca26-fc5f-4580-887a-46d9a103e4" );
147 uuid_aton_fail_ok ( "453aee96:0fb5-4aeb-aecd-d060b2121218" );
148 uuid_aton_fail_ok ( "1ccb524a-b8b9-4b17-x5e2-7996867edc7d" );
149 uuid_aton_fail_ok ( "" );
150}
151
152/** UUID self-test */
153struct self_test uuid_test __self_test = {
154 .name = "uuid",
155 .exec = uuid_test_exec,
156};
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ntohl(value)
Definition byteswap.h:135
#define ntohs(value)
Definition byteswap.h:137
String functions.
int strcmp(const char *first, const char *second)
Compare strings.
Definition string.c:174
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
#define __self_test
Declare a self-test.
Definition test.h:32
A universally unique ID.
Definition uuid.h:16
uint16_t d
2 hex digits, big-endian
Definition uuid.h:26
uint8_t e[6]
12 hex digits, big-endian
Definition uuid.h:28
uint32_t a
8 hex digits, big-endian
Definition uuid.h:20
struct uuid::@036242100033237070006335026303064005125164242336 canonical
Canonical form (00000000-0000-0000-0000-000000000000)
uint16_t b
2 hex digits, big-endian
Definition uuid.h:22
uint16_t c
2 hex digits, big-endian
Definition uuid.h:24
const char * uuid_ntoa(const union uuid *uuid)
Convert UUID to printable string.
Definition uuid.c:46
int uuid_aton(const char *string, union uuid *uuid)
Parse UUID.
Definition uuid.c:67
Universally unique IDs.
#define uuid_aton_ok(text, value)
Definition uuid_test.c:95
#define uuid_ntoa_ok(value, text)
Definition uuid_test.c:71
#define UUID(A, B, C, D, E0, E1, E2, E3, E4, E5)
Define an inline UUID value.
Definition uuid_test.c:41
#define uuid_aton_fail_ok(text)
Definition uuid_test.c:116
static void uuid_ntoa_okx(const union uuid *uuid, const char *text, const char *file, unsigned int line)
Report a uuid_ntoa() test result.
Definition uuid_test.c:57
static void uuid_aton_okx(const char *text, const union uuid *uuid, const char *file, unsigned int line)
Report a uuid_aton() test result.
Definition uuid_test.c:86
static void uuid_test_exec(void)
Perform UUID self-tests.
Definition uuid_test.c:123
static void uuid_aton_fail_okx(const char *text, const char *file, unsigned int line)
Report a uuid_aton() failure test result.
Definition uuid_test.c:109