summaryrefslogtreecommitdiff
path: root/unit/test-ndef-parse.c
blob: ec3607ffeb9f5da67254b1cb0d39ebb46a3d4965 (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
/*
 *  neard - Near Field Communication manager
 *
 *  Copyright (C) 2013  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

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

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include <glib.h>
#include <glib/gprintf.h>

#include "src/near.h"
#include "include/ndef.h"

enum record_type {
	RECORD_TYPE_WKT_SMART_POSTER          =   0x01,
	RECORD_TYPE_WKT_URI                   =   0x02,
	RECORD_TYPE_WKT_TEXT                  =   0x03,
	RECORD_TYPE_WKT_SIZE                  =   0x04,
	RECORD_TYPE_WKT_TYPE                  =   0x05,
	RECORD_TYPE_WKT_ACTION                =   0x06,
	RECORD_TYPE_WKT_HANDOVER_REQUEST      =   0x07,
	RECORD_TYPE_WKT_HANDOVER_SELECT       =   0x08,
	RECORD_TYPE_WKT_HANDOVER_CARRIER      =   0x09,
	RECORD_TYPE_WKT_ALTERNATIVE_CARRIER   =   0x0a,
	RECORD_TYPE_WKT_COLLISION_RESOLUTION  =   0x0b,
	RECORD_TYPE_WKT_ERROR                 =   0x0c,
	RECORD_TYPE_MIME_TYPE                 =   0x0d,
	RECORD_TYPE_UNKNOWN                   =   0xfe,
	RECORD_TYPE_ERROR                     =   0xff
};

struct near_ndef_record_header {
	uint8_t mb;
	uint8_t me;
	uint8_t cf;
	uint8_t sr;
	uint8_t il;
	uint8_t tnf;
	uint8_t il_length;
	uint8_t *il_field;
	uint32_t payload_len;
	uint32_t offset;
	uint8_t	type_len;
	enum record_type rec_type;
	char *type_name;
	uint32_t header_len;
};

struct near_ndef_text_payload {
	char *encoding;
	char *language_code;
	char *data;
};

struct near_ndef_uri_payload {
	uint8_t identifier;

	uint32_t  field_length;
	uint8_t  *field;
};

struct near_ndef_sp_payload {
	struct near_ndef_uri_payload *uri;

	uint8_t number_of_title_records;
	struct near_ndef_text_payload **title_records;

	uint32_t size; /* from Size record*/
	char *type;    /* from Type record*/
	char *action;
};

struct near_ndef_record {
	char *path;

	struct near_ndef_record_header *header;

	/* specific payloads */
	struct near_ndef_text_payload *text;
	struct near_ndef_uri_payload  *uri;
	struct near_ndef_sp_payload   *sp;
 	struct near_ndef_mime_payload *mime;
	struct near_ndef_ho_payload   *ho;	/* handover payload */

	char *type;

	uint8_t *data;
	size_t data_len;
};

/* http://www.intel.com URI NDEF */
static uint8_t uri[] = {0xd1, 0x1, 0xa, 0x55, 0x1, 0x69, 0x6e, 0x74,
			0x65, 0x6c, 0x2e, 0x63, 0x6f, 0x6d};

/* 'hello' - UTF-8 - en-US Text NDEF */
static uint8_t text[] = {0xd1, 0x1, 0xb, 0x54, 0x5,  0x65, 0x6e, 0x2d,
			 0x55, 0x53, 0x68, 0x65, 0x6c, 0x6c, 0x6f};

/* Smart poster with a http://intel.com URI record */
static uint8_t single_sp[] = {0xd1, 0x2, 0xe, 0x53, 0x70, 0xd1, 0x1, 0xa,
			      0x55, 0x3, 0x69, 0x6e, 0x74, 0x65, 0x6c, 0x2e,
			      0x63, 0x6f, 0x6d};

/* Smart poster with a http://intel.com URI record and a 'Intel' title */
static uint8_t title_sp[] = {0xd1, 0x2, 0x1a, 0x53, 0x70, 0x91, 0x1, 0xa,
			     0x55, 0x3, 0x69, 0x6e, 0x74, 0x65, 0x6c, 0x2e,
			     0x63, 0x6f, 0x6d, 0x51, 0x1, 0x8, 0x54, 0x2,
			     0x65, 0x6e, 0x49, 0x6e, 0x74, 0x65, 0x6c};

static void test_ndef_free_record(struct near_ndef_record *record)
{
	g_free(record->header);
	g_free(record->type);
	g_free(record->data);
	g_free(record);

}

static void test_ndef_uri(void)
{
	GList *records;
	struct near_ndef_record *record;

	records = near_ndef_parse_msg(uri, sizeof(uri), NULL);

	g_assert(records);
	g_assert(g_list_length(records) == 1);

	record = (struct near_ndef_record *)(records->data);

	g_assert(record->header->rec_type == RECORD_TYPE_WKT_URI);
	g_assert(record->header->mb == 1);
	g_assert(record->header->me == 1);

	g_assert(record->uri);
	g_assert(record->uri->field_length == strlen("intel.com"));
	g_assert(strncmp((char *) record->uri->field, "intel.com",
					record->uri->field_length) == 0);

	if (g_test_verbose())
		g_print("NDEF URI field: %s\n", record->uri->field);

	g_free(record->uri->field);
	g_free(record->uri);
	test_ndef_free_record(record);
}

static void test_ndef_text(void)
{
	GList *records;
	struct near_ndef_record *record;

	records = near_ndef_parse_msg(text, sizeof(text), NULL);

	g_assert(records);
	g_assert(g_list_length(records) == 1);

	record = (struct near_ndef_record *)(records->data);

	g_assert(record->header->rec_type == RECORD_TYPE_WKT_TEXT);
	g_assert(record->header->mb == 1);
	g_assert(record->header->me == 1);

	g_assert(record->text);
	g_assert(strcmp(record->text->data, "hello") == 0);
	g_assert(strcmp(record->text->encoding, "UTF-8") == 0);
	g_assert(strcmp(record->text->language_code, "en-US") == 0);

	if (g_test_verbose()) {
		g_print("NDEF Text data: %s\n", record->text->data);
		g_print("NDEF Text Encoding: %s\n", record->text->encoding);
		g_print("NDEF Text Language: %s\n",
						record->text->language_code);
	}

	g_free(record->text->data);
	g_free(record->text->encoding);
	g_free(record->text->language_code);
	g_free(record->text);
	test_ndef_free_record(record);
}

static void test_ndef_single_sp(void)
{
	GList *records;
	struct near_ndef_record *record;
	struct near_ndef_uri_payload *uri;

	records = near_ndef_parse_msg(single_sp, sizeof(single_sp), NULL);

	g_assert(records);
	g_assert(g_list_length(records) == 1);

	record = (struct near_ndef_record *) records->data;

	g_assert(record->header->rec_type == RECORD_TYPE_WKT_SMART_POSTER);
	g_assert(record->header->mb == 1);
	g_assert(record->header->me == 1);

	g_assert(record->sp);
	g_assert(record->sp->number_of_title_records == 0);
	g_assert(record->sp->type == NULL);
	g_assert(record->sp->action == NULL);
	g_assert(record->sp->size == 0);
	g_assert(record->sp->uri);

	uri = (struct near_ndef_uri_payload *) record->sp->uri;

	g_assert(uri->field_length == strlen("intel.com"));
	g_assert(strncmp((char *) uri->field, "intel.com",
					uri->field_length) == 0);

	if (g_test_verbose())
		g_print("NDEF SP URI field: %.*s\n", uri->field_length,
							(char *) uri->field);

	g_free(uri->field);
	g_free(uri);
	g_free(record->sp);
	test_ndef_free_record(record);
}

static void test_ndef_title_sp(void)
{
	GList *records;
	struct near_ndef_record *record;
	struct near_ndef_uri_payload *uri;
	struct near_ndef_text_payload *text;


	records = near_ndef_parse_msg(title_sp, sizeof(title_sp), NULL);

	g_assert(records);
	g_assert(g_list_length(records) == 1);

	record = (struct near_ndef_record *) records->data;

	g_assert(record->header->rec_type == RECORD_TYPE_WKT_SMART_POSTER);
	g_assert(record->header->mb == 1);
	g_assert(record->header->me == 1);

	g_assert(record->sp);
	g_assert(record->sp->number_of_title_records == 1);
	g_assert(record->sp->type == NULL);
	g_assert(record->sp->action == NULL);
	g_assert(record->sp->size == 0);
	g_assert(record->sp->uri);
	g_assert(record->sp->title_records[0]);

	uri = (struct near_ndef_uri_payload *) record->sp->uri;
	text = (struct near_ndef_text_payload *) record->sp->title_records[0];

	g_assert(uri->field_length == strlen("intel.com"));
	g_assert(strncmp((char *) uri->field, "intel.com",
					uri->field_length) == 0);

	if (g_test_verbose())
		g_print("NDEF SP URI field: %.*s\n", uri->field_length,
							(char *) uri->field);

	g_assert(strcmp(text->data, "Intel") == 0);
	g_assert(strcmp(text->encoding, "UTF-8") == 0);
	g_assert(strcmp(text->language_code, "en") == 0);

	if (g_test_verbose()) {
		g_print("NDEF SP Title data: %s\n", text->data);
		g_print("NDEF SP Title Encoding: %s\n", text->encoding);
		g_print("NDEF SP Title Language: %s\n", text->language_code);
	}

	g_free(uri->field);
	g_free(uri);

	g_free(text->data);
	g_free(text->encoding);
	g_free(text->language_code);
	g_free(text);

	g_free(record->sp);
	test_ndef_free_record(record);
}

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

	g_test_add_func("/testndefparse/Test URI NDEF", test_ndef_uri);
	g_test_add_func("/testndefparse/Test Text NDEF", test_ndef_text);
	g_test_add_func("/testndefparse/Test Single record SmartPoster NDEF",
							test_ndef_single_sp);
	g_test_add_func("/testndefparse/Test Title record SmartPoster NDEF",
							test_ndef_title_sp);

	return g_test_run();
}