Blame view

test/bloblist.c 6.65 KB
919e7a8fb   Simon Glass   test: Add a simpl...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  // SPDX-License-Identifier: GPL-2.0+
  /*
   * Copyright (c) 2018, Google Inc. All rights reserved.
   */
  
  #include <common.h>
  #include <bloblist.h>
  #include <log.h>
  #include <mapmem.h>
  #include <test/suites.h>
  #include <test/test.h>
  #include <test/ut.h>
  
  DECLARE_GLOBAL_DATA_PTR;
  
  /* Declare a new compression test */
  #define BLOBLIST_TEST(_name, _flags) \
  		UNIT_TEST(_name, _flags, bloblist_test)
  
  enum {
  	TEST_TAG		= 1,
  	TEST_TAG2		= 2,
  	TEST_TAG_MISSING	= 3,
  
  	TEST_SIZE		= 10,
  	TEST_SIZE2		= 20,
5b044548f   Simon Glass   bloblist: Add a n...
27
  	TEST_SIZE_LARGE		= 0xe0,
919e7a8fb   Simon Glass   test: Add a simpl...
28
29
30
31
32
33
34
35
  
  	TEST_ADDR		= CONFIG_BLOBLIST_ADDR,
  	TEST_BLOBLIST_SIZE	= 0x100,
  };
  
  static struct bloblist_hdr *clear_bloblist(void)
  {
  	struct bloblist_hdr *hdr;
b83994dec   Simon Glass   bloblist: Zero re...
36
37
38
39
40
  	/*
  	 * Clear out any existing bloblist so we have a clean slate. Zero the
  	 * header so that existing records are removed, but set everything else
  	 * to 0xff for testing purposes.
  	 */
919e7a8fb   Simon Glass   test: Add a simpl...
41
  	hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
b83994dec   Simon Glass   bloblist: Zero re...
42
43
  	memset(hdr, '\xff', TEST_BLOBLIST_SIZE);
  	memset(hdr, '\0', sizeof(*hdr));
919e7a8fb   Simon Glass   test: Add a simpl...
44
45
46
  
  	return hdr;
  }
b83994dec   Simon Glass   bloblist: Zero re...
47
48
49
50
51
52
53
54
55
56
57
58
  static int check_zero(void *data, int size)
  {
  	u8 *ptr;
  	int i;
  
  	for (ptr = data, i = 0; i < size; i++, ptr++) {
  		if (*ptr)
  			return -EINVAL;
  	}
  
  	return 0;
  }
919e7a8fb   Simon Glass   test: Add a simpl...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  static int bloblist_test_init(struct unit_test_state *uts)
  {
  	struct bloblist_hdr *hdr;
  
  	hdr = clear_bloblist();
  	ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
  	hdr->version++;
  	ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR,
  						     TEST_BLOBLIST_SIZE));
  
  	ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0x10, 0));
  	ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0));
  	ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
  
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	ut_assertok(bloblist_finish());
  	ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	hdr->flags++;
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  
  	return 1;
  }
  BLOBLIST_TEST(bloblist_test_init, 0);
  
  static int bloblist_test_blob(struct unit_test_state *uts)
  {
  	struct bloblist_hdr *hdr;
  	struct bloblist_rec *rec, *rec2;
  	char *data;
  
  	/* At the start there should be no records */
  	hdr = clear_bloblist();
  	ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
  	ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
  
  	/* Add a record and check that we can find it */
  	data = bloblist_add(TEST_TAG, TEST_SIZE);
  	rec = (void *)(hdr + 1);
  	ut_asserteq_ptr(rec + 1, data);
  	data = bloblist_find(TEST_TAG, TEST_SIZE);
  	ut_asserteq_ptr(rec + 1, data);
b83994dec   Simon Glass   bloblist: Zero re...
101
102
  	/* Check the data is zeroed */
  	ut_assertok(check_zero(data, TEST_SIZE));
919e7a8fb   Simon Glass   test: Add a simpl...
103
104
105
106
  	/* Check the 'ensure' method */
  	ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
  	ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
  	rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
b83994dec   Simon Glass   bloblist: Zero re...
107
  	ut_assertok(check_zero(data, TEST_SIZE));
919e7a8fb   Simon Glass   test: Add a simpl...
108
109
110
111
112
113
114
115
116
  
  	/* Check for a non-existent record */
  	ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
  	ut_asserteq_ptr(rec2 + 1, bloblist_ensure(TEST_TAG2, TEST_SIZE2));
  	ut_assertnull(bloblist_find(TEST_TAG_MISSING, 0));
  
  	return 0;
  }
  BLOBLIST_TEST(bloblist_test_blob, 0);
5b044548f   Simon Glass   bloblist: Add a n...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  /* Check bloblist_ensure_size_ret() */
  static int bloblist_test_blob_ensure(struct unit_test_state *uts)
  {
  	void *data, *data2;
  	int size;
  
  	/* At the start there should be no records */
  	clear_bloblist();
  	ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
  
  	/* Test with an empty bloblist */
  	size = TEST_SIZE;
  	ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
  	ut_asserteq(TEST_SIZE, size);
b83994dec   Simon Glass   bloblist: Zero re...
131
  	ut_assertok(check_zero(data, TEST_SIZE));
5b044548f   Simon Glass   bloblist: Add a n...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  
  	/* Check that we get the same thing again */
  	ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data2));
  	ut_asserteq(TEST_SIZE, size);
  	ut_asserteq_ptr(data, data2);
  
  	/* Check that the size remains the same */
  	size = TEST_SIZE2;
  	ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
  	ut_asserteq(TEST_SIZE, size);
  
  	/* Check running out of space */
  	size = TEST_SIZE_LARGE;
  	ut_asserteq(-ENOSPC, bloblist_ensure_size_ret(TEST_TAG2, &size, &data));
  
  	return 0;
  }
  BLOBLIST_TEST(bloblist_test_blob_ensure, 0);
919e7a8fb   Simon Glass   test: Add a simpl...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  static int bloblist_test_bad_blob(struct unit_test_state *uts)
  {
  	struct bloblist_hdr *hdr;
  	void *data;
  
  	hdr = clear_bloblist();
  	ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
  	data = hdr + 1;
  	data += sizeof(struct bloblist_rec);
  	ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
  	ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
  
  	return 0;
  }
  BLOBLIST_TEST(bloblist_test_bad_blob, 0);
  
  static int bloblist_test_checksum(struct unit_test_state *uts)
  {
  	struct bloblist_hdr *hdr;
  	char *data, *data2;
  
  	hdr = clear_bloblist();
  	ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
  	ut_assertok(bloblist_finish());
  	ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  
  	/*
  	 * Now change things amd make sure that the checksum notices. We cannot
  	 * change the size or alloced fields, since that will crash the code.
  	 * It has to rely on these being correct.
  	 */
  	hdr->flags--;
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	hdr->flags++;
  
  	hdr->size--;
  	ut_asserteq(-EFBIG, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	hdr->size++;
  
  	hdr->spare++;
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	hdr->spare--;
  
  	hdr->chksum++;
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	hdr->chksum--;
  
  	/* Make sure the checksum changes when we add blobs */
  	data = bloblist_add(TEST_TAG, TEST_SIZE);
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  
  	data2 = bloblist_add(TEST_TAG2, TEST_SIZE2);
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	ut_assertok(bloblist_finish());
  
  	/* It should also change if we change the data */
  	ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	*data += 1;
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	*data -= 1;
  
  	ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	*data2 += 1;
  	ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	*data2 -= 1;
  
  	/*
  	 * Changing data outside the range of valid data should not affect
  	 * the checksum.
  	 */
  	ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  	data[TEST_SIZE]++;
  	data2[TEST_SIZE2]++;
  	ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
  
  	return 0;
  }
  
  BLOBLIST_TEST(bloblist_test_checksum, 0);
  
  int do_ut_bloblist(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  {
  	struct unit_test *tests = ll_entry_start(struct unit_test,
  						 bloblist_test);
  	const int n_ents = ll_entry_count(struct unit_test, bloblist_test);
4ad4edfe7   Philippe Reynes   cmd_ut: add a par...
235
236
  	return cmd_ut_category("bloblist", "bloblist_test_",
  			       tests, n_ents, argc, argv);
919e7a8fb   Simon Glass   test: Add a simpl...
237
  }