summaryrefslogtreecommitdiff
path: root/unit/test-gobex-apparam.c
blob: 330a7819af65a42346bb3699878ad8f4aca9ea7e (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  OBEX library with GLib integration
 *
 *  Copyright (C) 2012  Intel Corporation.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>
#include <string.h>

#include "gobex/gobex.h"
#include "gobex/gobex-apparam.h"

#include "util.h"

#define TAG_U8 0x00
#define TAG_U16 0x01
#define TAG_U32 0x02
#define TAG_U64 0x03
#define TAG_STRING 0x04
#define TAG_BYTES 0x05

static uint8_t tag_nval_short[] = { TAG_U8 };
static uint8_t tag_nval_data[] = { TAG_U8, 0x01 };
static uint8_t tag_nval2_short[] = { TAG_U8, 0x01, 0x1, TAG_U16 };
static uint8_t tag_nval2_data[] = { TAG_U8, 0x01, 0x1, TAG_U16, 0x02 };
static uint8_t tag_uint8[] = { TAG_U8, 0x01, 0x01 };
static uint8_t tag_uint16[] = { TAG_U16, 0x02, 0x01, 0x02 };
static uint8_t tag_uint32[] = { TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04 };
static uint8_t tag_uint64[] = { TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
						0x05, 0x06, 0x07, 0x08 };
static uint8_t tag_string[] = { TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };
static uint8_t tag_bytes[257] = { TAG_BYTES, 0xFF };
static uint8_t tag_multi[] = { TAG_U8, 0x01, 0x01,
				TAG_U16, 0x02, 0x01, 0x02,
				TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04,
				TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
						0x05, 0x06, 0x07, 0x08,
				TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };


static GObexApparam *parse_and_decode(const void *data, gsize size)
{
	GObexApparam *apparam;
	guint8 encoded[1024];
	gsize len;

	apparam = g_obex_apparam_decode(data, size);

	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, encoded, sizeof(encoded));

	assert_memequal(data, size, encoded, len);

	return apparam;
}

static void test_apparam_nval_short(void)
{
	GObexApparam *apparam;

	apparam = g_obex_apparam_decode(tag_nval_short,
						sizeof(tag_nval_short));

	g_assert(apparam == NULL);
}

static void test_apparam_nval_data(void)
{
	GObexApparam *apparam;

	apparam = g_obex_apparam_decode(tag_nval_data,
						sizeof(tag_nval_data));

	g_assert(apparam == NULL);
}

static void test_apparam_nval2_short(void)
{
	GObexApparam *apparam;

	apparam = g_obex_apparam_decode(tag_nval2_short,
						sizeof(tag_nval2_short));

	g_assert(apparam == NULL);
}

static void test_apparam_nval2_data(void)
{
	GObexApparam *apparam;

	apparam = g_obex_apparam_decode(tag_nval2_data,
						sizeof(tag_nval2_data));

	g_assert(apparam == NULL);
}

static void test_apparam_get_uint8(void)
{
	GObexApparam *apparam;
	guint8 data;
	gboolean ret;

	apparam = parse_and_decode(tag_uint8, sizeof(tag_uint8));

	ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data);

	g_assert(ret == TRUE);
	g_assert(data == 0x01);

	g_obex_apparam_free(apparam);
}

static void test_apparam_get_uint16(void)
{
	GObexApparam *apparam;
	uint16_t data;
	gboolean ret;

	apparam = parse_and_decode(tag_uint16, sizeof(tag_uint16));

	ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data);

	g_assert(ret == TRUE);
	g_assert(data == 0x0102);

	g_obex_apparam_free(apparam);
}

static void test_apparam_get_uint32(void)
{
	GObexApparam *apparam;
	uint32_t data;
	gboolean ret;

	apparam = parse_and_decode(tag_uint32, sizeof(tag_uint32));

	ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data);

	g_assert(ret == TRUE);
	g_assert(data == 0x01020304);

	g_obex_apparam_free(apparam);
}

static void test_apparam_get_uint64(void)
{
	GObexApparam *apparam;
	uint64_t data;
	gboolean ret;

	apparam = parse_and_decode(tag_uint64, sizeof(tag_uint64));

	ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data);

	g_assert(ret == TRUE);
	g_assert(data == 0x0102030405060708);

	g_obex_apparam_free(apparam);
}

static void test_apparam_get_string(void)
{
	GObexApparam *apparam;
	char *string;

	apparam = parse_and_decode(tag_string, sizeof(tag_string));

	string = g_obex_apparam_get_string(apparam, TAG_STRING);

	g_assert(string != NULL);
	g_assert_cmpstr(string, ==, "ABC");

	g_free(string);
	g_obex_apparam_free(apparam);
}

static void test_apparam_get_bytes(void)
{
	GObexApparam *apparam;
	const uint8_t *data;
	gsize len;
	gboolean ret;

	apparam = parse_and_decode(tag_bytes, sizeof(tag_bytes));

	ret = g_obex_apparam_get_bytes(apparam, TAG_BYTES, &data, &len);

	g_assert(ret == TRUE);
	assert_memequal(tag_bytes + 2, sizeof(tag_bytes) - 2, data, len);

	g_obex_apparam_free(apparam);
}

static void test_apparam_get_multi(void)
{
	GObexApparam *apparam;
	char *string;
	uint8_t data8;
	uint16_t data16;
	uint32_t data32;
	uint64_t data64;
	gboolean ret;

	apparam = g_obex_apparam_decode(tag_multi, sizeof(tag_multi));

	g_assert(apparam != NULL);

	ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data8);

	g_assert(ret == TRUE);
	g_assert(data8 == 0x01);

	ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data16);

	g_assert(ret == TRUE);
	g_assert(data16 == 0x0102);

	ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data32);

	g_assert(ret == TRUE);
	g_assert(data32 == 0x01020304);

	ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data64);

	g_assert(ret == TRUE);
	g_assert(data64 == 0x0102030405060708);

	string = g_obex_apparam_get_string(apparam, TAG_STRING);

	g_assert(string != NULL);
	g_assert_cmpstr(string, ==, "ABC");

	g_free(string);

	g_obex_apparam_free(apparam);
}

static void test_apparam_set_uint8(void)
{
	GObexApparam *apparam;
	guint8 buf[1024];
	gsize len;

	apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);
	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
	assert_memequal(tag_uint8, sizeof(tag_uint8), buf, len);

	g_obex_apparam_free(apparam);
}

static void test_apparam_set_uint16(void)
{
	GObexApparam *apparam;
	guint8 buf[1024];
	gsize len;

	apparam = g_obex_apparam_set_uint16(NULL, TAG_U16, 0x0102);
	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
	assert_memequal(tag_uint16, sizeof(tag_uint16), buf, len);

	g_obex_apparam_free(apparam);
}

static void test_apparam_set_uint32(void)
{
	GObexApparam *apparam;
	guint8 buf[1024];
	gsize len;

	apparam = g_obex_apparam_set_uint32(NULL, TAG_U32, 0x01020304);
	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
	assert_memequal(tag_uint32, sizeof(tag_uint32), buf, len);

	g_obex_apparam_free(apparam);
}

static void test_apparam_set_uint64(void)
{
	GObexApparam *apparam;
	guint8 buf[1024];
	gsize len;

	apparam = g_obex_apparam_set_uint64(NULL, TAG_U64, 0x0102030405060708);
	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
	assert_memequal(tag_uint64, sizeof(tag_uint64), buf, len);

	g_obex_apparam_free(apparam);
}

static void test_apparam_set_string(void)
{
	GObexApparam *apparam;
	guint8 buf[1024];
	gsize len;

	apparam = g_obex_apparam_set_string(NULL, TAG_STRING, "ABC");
	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
	assert_memequal(tag_string, sizeof(tag_string), buf, len);

	g_obex_apparam_free(apparam);
}

static void test_apparam_set_bytes(void)
{
	GObexApparam *apparam;
	guint8 buf[1024];
	gsize len;

	apparam = g_obex_apparam_set_bytes(NULL, TAG_BYTES, tag_bytes + 2, 255);
	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
	assert_memequal(tag_bytes, sizeof(tag_bytes), buf, len);

	g_obex_apparam_free(apparam);
}

static void test_apparam_set_multi(void)
{
	GObexApparam *apparam;
	guint8 buf[1024];
	gsize len;

	apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);

	g_assert(apparam != NULL);

	apparam = g_obex_apparam_set_uint16(apparam, TAG_U16, 0x0102);

	g_assert(apparam != NULL);

	apparam = g_obex_apparam_set_uint32(apparam, TAG_U32, 0x01020304);

	g_assert(apparam != NULL);

	apparam = g_obex_apparam_set_uint64(apparam, TAG_U64,
							0x0102030405060708);

	g_assert(apparam != NULL);

	apparam = g_obex_apparam_set_string(apparam, TAG_STRING, "ABC");

	g_assert(apparam != NULL);

	len = g_obex_apparam_encode(apparam, buf, sizeof(buf));

	g_assert_cmpuint(len, ==, sizeof(tag_multi));

	g_obex_apparam_free(apparam);
}

int main(int argc, char *argv[])
{
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/gobex/test_apparam_nval_short",
						test_apparam_nval_short);
	g_test_add_func("/gobex/test_apparam_nval_data",
						test_apparam_nval_data);

	g_test_add_func("/gobex/test_apparam_nval2_short",
						test_apparam_nval2_short);
	g_test_add_func("/gobex/test_apparam_nval2_data",
						test_apparam_nval2_data);

	g_test_add_func("/gobex/test_apparam_get_uint8",
						test_apparam_get_uint8);
	g_test_add_func("/gobex/test_apparam_get_uint16",
						test_apparam_get_uint16);
	g_test_add_func("/gobex/test_apparam_get_uint32",
						test_apparam_get_uint32);
	g_test_add_func("/gobex/test_apparam_get_uint64",
						test_apparam_get_uint64);
	g_test_add_func("/gobex/test_apparam_get_string",
						test_apparam_get_string);
	g_test_add_func("/gobex/test_apparam_get_bytes",
						test_apparam_get_bytes);
	g_test_add_func("/gobex/test_apparam_get_multi",
						test_apparam_get_multi);

	g_test_add_func("/gobex/test_apparam_set_uint8",
						test_apparam_set_uint8);
	g_test_add_func("/gobex/test_apparam_set_uint16",
						test_apparam_set_uint16);
	g_test_add_func("/gobex/test_apparam_set_uint32",
						test_apparam_set_uint32);
	g_test_add_func("/gobex/test_apparam_set_uint64",
						test_apparam_set_uint64);
	g_test_add_func("/gobex/test_apparam_set_string",
						test_apparam_set_string);
	g_test_add_func("/gobex/test_apparam_set_bytes",
						test_apparam_set_bytes);
	g_test_add_func("/gobex/test_apparam_set_multi",
						test_apparam_set_multi);

	return g_test_run();
}