iPXE
datauri_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 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 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 * Data URI tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <string.h>
36#include <ipxe/uri.h>
37#include <ipxe/datauri.h>
38#include <ipxe/test.h>
39
40/** Define inline data */
41#define DATA(...) { __VA_ARGS__ }
42
43/**
44 * Report result of data URI test
45 *
46 * @v uristring URI string
47 * @v expected Expected parsed data
48 * @v expected_len Length of expected parsed data
49 * @v file Test code file
50 * @v line Test code line
51 */
52static void datauri_okx ( const char *uristring, const void *expected,
53 size_t expected_len, const char *file,
54 unsigned int line ) {
55 uint8_t actual[ strlen ( uristring ) /* more than enough */ ];
56 struct uri *uri;
57 int len;
58
59 /* Parse URI */
60 uri = parse_uri ( uristring );
61 okx ( uri != NULL, file, line );
62 okx ( uri->scheme != NULL, file, line );
63 okx ( uri->opaque != NULL, file, line );
64 okx ( strcmp ( uri->scheme, "data" ) == 0, file, line );
65
66 /* Sanity check */
67 okx ( datauri_max_len ( uri ) <= sizeof ( actual ), file, line );
68 okx ( datauri_max_len ( uri ) >= expected_len, file, line );
69
70 /* Parse data URI */
71 len = datauri_parse ( uri, actual );
72 okx ( len >= 0, file, line );
73 okx ( ( ( size_t ) len ) == expected_len, file, line );
74 okx ( memcmp ( actual, expected, expected_len ) == 0, file, line );
75
76 /* Drop reference */
77 uri_put ( uri );
78}
79#define datauri_ok( uristring, EXPECTED ) do { \
80 static const uint8_t expected[] = EXPECTED; \
81 datauri_okx ( uristring, expected, sizeof ( expected ), \
82 __FILE__, __LINE__ ); \
83 } while ( 0 )
84
85/**
86 * Report result of data URI failure test
87 *
88 * @v uristring URI string
89 * @v file Test code file
90 * @v line Test code line
91 */
92static void datauri_fail_okx ( const char *uristring, const char *file,
93 unsigned int line ) {
94 uint8_t actual[ strlen ( uristring ) /* more than enough */ ];
95 struct uri *uri;
96 int len;
97
98 /* Parse URI */
99 uri = parse_uri ( uristring );
100 okx ( uri != NULL, file, line );
101 okx ( uri->scheme != NULL, file, line );
102 okx ( uri->opaque != NULL, file, line );
103 okx ( strcmp ( uri->scheme, "data" ) == 0, file, line );
104
105 /* Sanity check */
106 okx ( datauri_max_len ( uri ) <= sizeof ( actual ), file, line );
107
108 /* Parse data URI */
109 len = datauri_parse ( uri, actual );
110 okx ( len < 0, file, line );
111
112 /* Drop reference */
113 uri_put ( uri );
114}
115#define datauri_fail_ok( uristring ) \
116 datauri_fail_okx ( uristring, __FILE__, __LINE__ )
117
118/**
119 * Perform data URI self-test
120 *
121 */
122static void datauri_test_exec ( void ) {
123
124 /* Empty data */
125 datauri_ok ( "data:,", DATA() );
126 datauri_ok ( "data:;base64,", DATA() );
127 datauri_ok ( "data:text/plain,", DATA() );
128
129 /* Simple values */
130 datauri_ok ( "data:,hello%20world%21",
131 DATA ( 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
132 'd', '!' ) );
133 datauri_ok ( "data:text/plain,simple",
134 DATA ( 's', 'i', 'm', 'p', 'l', 'e' ) );
135 datauri_ok ( "data:text/plain;charset=US-ASCII;base64,ZW5jb2RlZA==",
136 DATA ( 'e', 'n', 'c', 'o', 'd', 'e', 'd' ) );
137 datauri_ok ( "data:application/octet-string;base64,d2l0aABudWw=",
138 DATA ( 'w', 'i', 't', 'h', 0x00, 'n', 'u', 'l' ) );
139
140 /* Invalid values */
141 datauri_fail_ok ( "data:" );
142 datauri_fail_ok ( "data:;base64,INVALID===" );
143}
144
145/** Data URI self-test */
146struct self_test datauri_test __self_test = {
147 .name = "datauri",
148 .exec = datauri_test_exec,
149};
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
#define DATA(...)
Define inline data.
Definition acpi_test.c:74
unsigned char uint8_t
Definition stdint.h:10
int datauri_parse(struct uri *uri, void *buf)
Parse data URI.
Definition datauri.c:62
Data URIs.
static size_t datauri_max_len(struct uri *uri)
Get maximum length of parsed data URI.
Definition datauri.h:23
static void datauri_test_exec(void)
Perform data URI self-test.
static void datauri_okx(const char *uristring, const void *expected, size_t expected_len, const char *file, unsigned int line)
Report result of data URI test.
static void datauri_fail_okx(const char *uristring, const char *file, unsigned int line)
Report result of data URI failure test.
#define DATA(...)
Define inline data.
#define datauri_ok(uristring, EXPECTED)
#define datauri_fail_ok(uristring)
ring len
Length.
Definition dwmac.h:226
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
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
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
A self-test set.
Definition test.h:15
A Uniform Resource Identifier.
Definition uri.h:65
const char * scheme
Scheme.
Definition uri.h:69
const char * opaque
Opaque part.
Definition uri.h:71
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
struct uri * parse_uri(const char *uri_string)
Parse URI.
Definition uri.c:297
Uniform Resource Identifiers.
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition uri.h:206