aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/libubi_test.c
blob: 35eb28f91badc35daea6391834886ac74992a075 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdarg.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <cmocka.h>

#include "libubi.h"
#include "test_lib.h"


static void test_libubi_open(void **state)
{
	libubi_t lib = NULL;
	expect_open(SYSFS_ROOT "/class/ubi", O_RDONLY, 4);
	expect_close(4,0);
	expect_open(SYSFS_ROOT "/class/ubi/version", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);

	lib = libubi_open();
	assert_non_null(lib);
	libubi_close(lib);
	(void) state;
}

static void test_ubi_vol_block_create(void **state)
{
	int mock_fd = 1;
	expect_ioctl_short(UBI_IOCVOLCRBLK, 0);
	int r = ubi_vol_block_create(mock_fd);
	assert_int_equal(r, 0);

	(void) state;
}

static void test_ubi_vol_block_remove(void **state)
{
	int mock_fd = 1;
	expect_ioctl_short(UBI_IOCVOLRMBLK, 0);
	int r = ubi_vol_block_remove(mock_fd);
	assert_int_equal(r, 0);

	(void) state;
}

static void test_ubi_leb_unmap(void **state)
{
	int mock_fd = 1;
	int lnum = 12;
	expect_ioctl(UBI_IOCEBUNMAP, 0, &lnum);
	int r = ubi_leb_unmap(mock_fd, lnum);
	assert_int_equal(r, 0);

	(void) state;
}

static void test_ubi_is_mapped(void **state)
{
	int mock_fd = 1;
	int lnum = 1;
	expect_ioctl(UBI_IOCEBISMAP, 0, &lnum);
	int r = ubi_is_mapped(mock_fd, lnum);
	assert_int_equal(r, 0);

	(void) state;
}

static void test_ubi_update_start(void **state)
{
	int mock_fd = 1;
	long long bytes = 0x1234;

	expect_ioctl(UBI_IOCVOLUP, 0, &bytes);
	int r = ubi_update_start(NULL, mock_fd, bytes);
	assert_int_equal(r, 0);
	(void) state;
}

static libubi_t mock_libubi_open()
{
	expect_open(SYSFS_ROOT "/class/ubi", O_RDONLY, 4);
	expect_close(4,0);
	expect_open(SYSFS_ROOT "/class/ubi/version", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	libubi_t lib = libubi_open();
	assert_non_null(lib);
	return lib;
}

static void test_ubi_dev_present(void **state)
{
	libubi_t lib = mock_libubi_open();
	int r = ubi_dev_present(lib, 0);
	assert_int_equal(r, 1);

	libubi_close(lib);
	(void) state;
}

static void test_ubi_rsvol(void **state)
{
	const char *node = "/foo";
	int vol_id = 0;
	long long bytes = 0xadadaf;
	struct ubi_rsvol_req req;
	memset(&req, 0, sizeof(req));
	req.bytes = bytes;
	req.vol_id = vol_id;
	expect_open(node, O_RDONLY, 4);
	expect_ioctl(UBI_IOCRSVOL, 0, &req);
	expect_close(4, 0);
	int r = ubi_rsvol(NULL, node, vol_id, bytes);
	assert_int_equal(r, 0);

	(void) state;
}

static void test_ubi_rnvols(void **state)
{
	libubi_t lib = mock_libubi_open();
	const char *node = "/foo";
	struct ubi_rnvol_req req;
	memset(&req, 0xaf, sizeof(req));
	expect_open(node, O_RDONLY, 4);
	expect_ioctl(UBI_IOCRNVOL, 0, &req);
	expect_close(4, 0);
	int r = ubi_rnvols(lib, node, &req);
	assert_int_equal(r, 0);

	libubi_close(lib);
	(void) state;
}

static void test_ubi_rmvol(void **state)
{
	libubi_t lib = mock_libubi_open();
	const char *node = "/foo";
	int vol_id = 12;
	expect_open(node, O_RDONLY, 4);
	expect_ioctl(UBI_IOCRMVOL, 0, &vol_id);
	expect_close(4, 0);
	int r = ubi_rmvol(lib, node, vol_id);
	assert_int_equal(r, 0);

	libubi_close(lib);
	(void) state;
}

static void test_ubi_leb_change_start(void **state)
{
	libubi_t lib = mock_libubi_open();
	int mock_fd = 1;
	int lnum = 12;
	int bytes = 48;
	struct ubi_leb_change_req req;
	memset(&req, 0, sizeof(req));
	req.lnum = lnum;
	req.bytes = bytes;
	req.dtype = 3;
	expect_ioctl(UBI_IOCEBCH, 0, &req);
	int r = ubi_leb_change_start(lib, mock_fd, lnum, bytes);
	assert_int_equal(r, 0);

	libubi_close(lib);
	(void) state;
}

static void test_ubi_get_info(void **state)
{
	libubi_t lib = mock_libubi_open();
	struct ubi_info info;
	expect_open(SYSFS_ROOT "/class/misc/ubi_ctrl/dev", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_read(1,0);
	expect_close(3,1);
	expect_open(SYSFS_ROOT "/class/ubi/version", O_RDONLY, 0);
	expect_read_real(50,0);
	expect_close(3,1);
	int r = ubi_get_info(lib, &info);
	assert_int_equal(r, 0);
	assert_int_equal(info.dev_count, 1);

	libubi_close(lib);
	(void) state;
}

static void test_ubi_mkvol(void **state)
{
	libubi_t lib = mock_libubi_open();
	const char *node = "/foo";
	const char *vol_name = "testvol";
	int vol_id = 12;
	struct ubi_mkvol_request req;
	struct ubi_mkvol_req rr;
	memset(&rr, 0, sizeof(rr));
	memset(&req, 0, sizeof(req));
	req.vol_id = vol_id;
	req.name = vol_name;
	rr.vol_id = vol_id;
	rr.name_len = strlen(vol_name);
	strncpy(rr.name, vol_name, UBI_MAX_VOLUME_NAME + 1);
	expect_open(node, O_RDONLY, 3);
	expect_ioctl(UBI_IOCMKVOL, 0, &rr);
	expect_close(3,0);
	int r = ubi_mkvol(lib, node, &req);
	assert_int_equal(r, 0);
	assert_int_equal(req.vol_id, vol_id);

	libubi_close(lib);
	(void) state;
}

void test_ubi_remove_dev(void **state)
{
	const char *node = "/foo";
	libubi_t lib = mock_libubi_open();
	int ubi_dev = 0xAA;
	expect_open(node, O_RDONLY, 4);
	expect_ioctl(UBI_IOCDET, 0, &ubi_dev);
	expect_close(4,0);
	int r = ubi_remove_dev(lib, node, ubi_dev);
	assert_int_equal(r, 0);

	libubi_close(lib);
	(void) state;
}

void test_ubi_attach(void **state)
{
	const char *node = "/foo";
	struct ubi_attach_request req;
	struct ubi_attach_req rr;
	memset(&req, 0, sizeof(req));
	memset(&rr, 0, sizeof(rr));
	libubi_t lib = mock_libubi_open();
	req.dev_num = 1;
	req.mtd_num = 1;
	rr.ubi_num = 1;
	rr.mtd_num = 1;
	expect_open(node, O_RDONLY, 4);
	expect_ioctl(UBI_IOCATT, 0, &rr);
	expect_close(4,0);

	int r = ubi_attach(lib, node, &req);
	assert_int_equal(r, 0);

	libubi_close(lib);
	(void) state;
}

void test_ubi_set_property(void **state)
{
	int mock_fd = 1;
	uint8_t prop = 0xad;
	uint64_t val = 0xaabbccdd;
	struct ubi_set_vol_prop_req req;
	memset(&req, 0, sizeof(req));
	req.property = prop;
	req.value = val;
	expect_ioctl(UBI_IOCSETVOLPROP, 0, &req);
	int r = ubi_set_property(mock_fd, prop, val);
	assert_int_equal(r,0);

	(void)state;
}

/* functions to test
 * ubi_get_vol_info
 * ubi_get_vol_info1
 * ubi_get_vol_info1_nm
 * ubi_get_dev_info1
 * ubi_get_dev_info
 * ubi_probe_node
 * ubi_detach_mtd
 * ubi_detach
 * mtd_num2ubi_dev
 */


int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_libubi_open),
		cmocka_unit_test(test_ubi_vol_block_create),
		cmocka_unit_test(test_ubi_vol_block_remove),
		cmocka_unit_test(test_ubi_update_start),
		cmocka_unit_test(test_ubi_dev_present),
		cmocka_unit_test(test_ubi_rsvol),
		cmocka_unit_test(test_ubi_rmvol),
		cmocka_unit_test(test_ubi_rnvols),
		cmocka_unit_test(test_ubi_leb_change_start),
		cmocka_unit_test(test_ubi_get_info),
		cmocka_unit_test(test_ubi_mkvol),
		cmocka_unit_test(test_ubi_leb_unmap),
		cmocka_unit_test(test_ubi_is_mapped),
		cmocka_unit_test(test_ubi_remove_dev),
		cmocka_unit_test(test_ubi_attach),
		cmocka_unit_test(test_ubi_set_property),
	};

	return cmocka_run_group_tests(tests, NULL, NULL);
}