summaryrefslogtreecommitdiff
path: root/tests/test-bluetooth-utils.c
blob: 2dfdea60f8bb9960da78179eea4efd96512c85ba (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
/*
 * Copyright (C) 2021 Bastien Nocera <hadess@hadess.net>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <locale.h>
#include <bluetooth-utils.c>

static void
test_appearance (void)
{
	g_assert_cmpint (bluetooth_appearance_to_type(0x03c2), ==, BLUETOOTH_TYPE_MOUSE);
}

static void
test_class (void)
{
	g_assert_cmpint (bluetooth_class_to_type(268), ==, BLUETOOTH_TYPE_COMPUTER);
}

static void
test_type_to_string (void)
{
	GFlagsClass *fclass;
	guint i;

	setlocale (LC_ALL, "en_US.UTF-8");

	g_type_ensure (BLUETOOTH_TYPE_TYPE);
	fclass = G_FLAGS_CLASS (g_type_class_ref (BLUETOOTH_TYPE_TYPE));

	/* This verifies that all BluetoothType types have a description */
	g_assert_cmpstr (bluetooth_type_to_string (1 << 0), ==, "Unknown");
	for (i = 1; i < fclass->n_values; i++) {
		const char *desc = bluetooth_type_to_string (1 << i);

		if (g_strcmp0 (desc, "Unknown") == 0) {
			g_test_message ("Type %s returned description %s",
					fclass->values[i].value_name,
					desc);
			g_assert_cmpstr (desc, !=, "Unknown");
		}
	}
	g_assert_cmpint (_BLUETOOTH_TYPE_NUM_TYPES, ==, fclass->n_values);
}

static void
test_verify (void)
{
	struct {
		const char *bdaddr;
		gboolean valid;
	} addresses[] = {
		{ "11:22:33:44:55:66", TRUE },
		{ "11:22:33:44:55", FALSE },
		{ "11:22:33:44:55:FF", TRUE },
		{ "11:22:33:44:55:6", FALSE },
		{ "foobar", FALSE },
	};
	guint i;

	for (i = 0; i < G_N_ELEMENTS(addresses); i++) {
		if (addresses[i].valid)
			g_assert_true (bluetooth_verify_address (addresses[i].bdaddr));
		else
			g_assert_false (bluetooth_verify_address (addresses[i].bdaddr));
	}
}

int main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);
	g_test_add_func ("/bluetooth/appearance", test_appearance);
	g_test_add_func ("/bluetooth/class", test_class);
	g_test_add_func ("/bluetooth/verify", test_verify);
	g_test_add_func ("/bluetooth/type_to_string", test_type_to_string);

	return g_test_run ();
}