blob: 8d22f3fd68f45cb91824be8b79c6912b4fb42230 [file] [log] [blame]
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
4 *
5 * Unit tests for memory functions
6 *
7 * The architecture dependent implementations run through different lines of
8 * code depending on the alignment and length of memory regions copied or set.
9 * This has to be considered in testing.
10 */
11
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +010012#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +010014#include <test/lib.h>
15#include <test/test.h>
16#include <test/ut.h>
17
18/* Xor mask used for marking memory regions */
19#define MASK 0xA5
20/* Number of different alignment values */
21#define SWEEP 16
22/* Allow for copying up to 32 bytes */
23#define BUFLEN (SWEEP + 33)
24
Simon Glass4b2a18b2021-09-25 07:03:06 -060025#define TEST_STR "hello"
26
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +010027/**
28 * init_buffer() - initialize buffer
29 *
30 * The buffer is filled with incrementing values xor'ed with the mask.
31 *
32 * @buf: buffer
33 * @mask: xor mask
34 */
35static void init_buffer(u8 buf[], u8 mask)
36{
37 int i;
38
39 for (i = 0; i < BUFLEN; ++i)
40 buf[i] = i ^ mask;
41}
42
43/**
44 * test_memset() - test result of memset()
45 *
46 * @uts: unit test state
47 * @buf: buffer
48 * @mask: value set by memset()
49 * @offset: relative start of region changed by memset() in buffer
50 * @len: length of region changed by memset()
51 * Return: 0 = success, 1 = failure
52 */
53static int test_memset(struct unit_test_state *uts, u8 buf[], u8 mask,
54 int offset, int len)
55{
56 int i;
57
58 for (i = 0; i < BUFLEN; ++i) {
59 if (i < offset || i >= offset + len) {
60 ut_asserteq(i, buf[i]);
61 } else {
62 ut_asserteq(mask, buf[i]);
63 }
64 }
65 return 0;
66}
67
68/**
69 * lib_memset() - unit test for memset()
70 *
71 * Test memset() with varied alignment and length of the changed buffer.
72 *
73 * @uts: unit test state
74 * Return: 0 = success, 1 = failure
75 */
76static int lib_memset(struct unit_test_state *uts)
77{
78 u8 buf[BUFLEN];
79 int offset, len;
80 void *ptr;
81
82 for (offset = 0; offset <= SWEEP; ++offset) {
83 for (len = 1; len < BUFLEN - SWEEP; ++len) {
84 init_buffer(buf, 0);
85 ptr = memset(buf + offset, MASK, len);
86 ut_asserteq_ptr(buf + offset, (u8 *)ptr);
87 if (test_memset(uts, buf, MASK, offset, len)) {
88 debug("%s: failure %d, %d\n",
89 __func__, offset, len);
90 return CMD_RET_FAILURE;
91 }
92 }
93 }
94 return 0;
95}
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +010096LIB_TEST(lib_memset, 0);
97
98/**
99 * test_memmove() - test result of memcpy() or memmove()
100 *
101 * @uts: unit test state
102 * @buf: buffer
103 * @mask: xor mask used to initialize source buffer
104 * @offset1: relative start of copied region in source buffer
105 * @offset2: relative start of copied region in destination buffer
106 * @len: length of region changed by memset()
107 * Return: 0 = success, 1 = failure
108 */
109static int test_memmove(struct unit_test_state *uts, u8 buf[], u8 mask,
110 int offset1, int offset2, int len)
111{
112 int i;
113
114 for (i = 0; i < BUFLEN; ++i) {
115 if (i < offset2 || i >= offset2 + len) {
116 ut_asserteq(i, buf[i]);
117 } else {
118 ut_asserteq((i + offset1 - offset2) ^ mask, buf[i]);
119 }
120 }
121 return 0;
122}
123
124/**
125 * lib_memcpy() - unit test for memcpy()
126 *
127 * Test memcpy() with varied alignment and length of the copied buffer.
128 *
129 * @uts: unit test state
130 * Return: 0 = success, 1 = failure
131 */
132static int lib_memcpy(struct unit_test_state *uts)
133{
134 u8 buf1[BUFLEN];
135 u8 buf2[BUFLEN];
136 int offset1, offset2, len;
137 void *ptr;
138
139 init_buffer(buf1, MASK);
140
141 for (offset1 = 0; offset1 <= SWEEP; ++offset1) {
142 for (offset2 = 0; offset2 <= SWEEP; ++offset2) {
143 for (len = 1; len < BUFLEN - SWEEP; ++len) {
144 init_buffer(buf2, 0);
145 ptr = memcpy(buf2 + offset2, buf1 + offset1,
146 len);
147 ut_asserteq_ptr(buf2 + offset2, (u8 *)ptr);
148 if (test_memmove(uts, buf2, MASK, offset1,
149 offset2, len)) {
150 debug("%s: failure %d, %d, %d\n",
151 __func__, offset1, offset2, len);
152 return CMD_RET_FAILURE;
153 }
154 }
155 }
156 }
157 return 0;
158}
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100159LIB_TEST(lib_memcpy, 0);
160
161/**
162 * lib_memmove() - unit test for memmove()
163 *
164 * Test memmove() with varied alignment and length of the copied buffer.
165 *
166 * @uts: unit test state
167 * Return: 0 = success, 1 = failure
168 */
169static int lib_memmove(struct unit_test_state *uts)
170{
171 u8 buf[BUFLEN];
172 int offset1, offset2, len;
173 void *ptr;
174
175 for (offset1 = 0; offset1 <= SWEEP; ++offset1) {
176 for (offset2 = 0; offset2 <= SWEEP; ++offset2) {
177 for (len = 1; len < BUFLEN - SWEEP; ++len) {
178 init_buffer(buf, 0);
179 ptr = memmove(buf + offset2, buf + offset1,
180 len);
181 ut_asserteq_ptr(buf + offset2, (u8 *)ptr);
182 if (test_memmove(uts, buf, 0, offset1, offset2,
183 len)) {
184 debug("%s: failure %d, %d, %d\n",
185 __func__, offset1, offset2, len);
186 return CMD_RET_FAILURE;
187 }
188 }
189 }
190 }
191 return 0;
192}
Heinrich Schuchardtf77a6352019-01-30 07:53:31 +0100193LIB_TEST(lib_memmove, 0);
Simon Glass4b2a18b2021-09-25 07:03:06 -0600194
195/** lib_memdup() - unit test for memdup() */
196static int lib_memdup(struct unit_test_state *uts)
197{
198 char buf[BUFLEN];
199 size_t len;
200 char *p, *q;
201
202 /* Zero size should do nothing */
203 p = memdup(NULL, 0);
204 ut_assertnonnull(p);
205 free(p);
206
207 p = memdup(buf, 0);
208 ut_assertnonnull(p);
209 free(p);
210
211 strcpy(buf, TEST_STR);
212 len = sizeof(TEST_STR);
213 p = memdup(buf, len);
214 ut_asserteq_mem(p, buf, len);
215
216 q = memdup(p, len);
217 ut_asserteq_mem(q, buf, len);
218 free(q);
219 free(p);
220
221 return 0;
222}
223LIB_TEST(lib_memdup, 0);