File: test-provision.c

package info (click to toggle)
ofono 2.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,064 kB
  • sloc: ansic: 224,979; sh: 5,012; python: 4,040; makefile: 956
file content (308 lines) | stat: -rw-r--r-- 7,795 bytes parent folder | download | duplicates (2)
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
/*
 *  oFono - Open Source Telephony
 *  Copyright (C) 2023  Cruise, LLC
 *
 *  SPDX-License-Identifier: GPL-2.0-only
 */

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

#include <stddef.h>
#include <assert.h>
#include <errno.h>
#include <ell/ell.h>

#include <ofono/types.h>
#include <ofono/gprs-context.h>

#include "provisiondb.h"

static struct provision_db *pdb;

static void null_provision_db(const void *data)
{
	struct provision_db_entry *items;
	size_t n_items;
	int r;

	r = provision_db_lookup(NULL, "123", "345", NULL, NULL,
				&items, &n_items);
	assert(r == -EBADF);
}

static void invalid_mcc_mnc(const void *data)
{
	struct provision_db_entry *items;
	size_t n_items;
	int r;

	r = provision_db_lookup(pdb, "3444", "33", NULL, NULL,
				&items, &n_items);
	assert(r == -EINVAL);

	r = provision_db_lookup(pdb, "3ab", "33", NULL, NULL, &items, &n_items);
	assert(r == -EINVAL);

	r = provision_db_lookup(pdb, "333", "3", NULL, NULL, &items, &n_items);
	assert(r == -EINVAL);

	r = provision_db_lookup(pdb, "333", "3334", NULL, NULL,
			&items, &n_items);
	assert(r == -EINVAL);
}

struct provision_test {
	const char *mcc;
	const char *mnc;
	const char *spn;
	const char *tags;
	int result;
	size_t n_items;
	const struct provision_db_entry *items;
};

static const struct provision_db_entry alpha_contexts[] = {
	{
		.name = "Internet",
		.type = OFONO_GPRS_CONTEXT_TYPE_INTERNET,
		.proto = OFONO_GPRS_PROTO_IP,
		.apn = "internet",
		.auth_method = OFONO_GPRS_AUTH_METHOD_NONE,
	},
	{
		.name = "IMS+MMS",
		.type = OFONO_GPRS_CONTEXT_TYPE_IMS |
			OFONO_GPRS_CONTEXT_TYPE_MMS |
			OFONO_GPRS_CONTEXT_TYPE_IA,
		.apn = "imsmms",
		.proto = OFONO_GPRS_PROTO_IPV6,
		.auth_method = OFONO_GPRS_AUTH_METHOD_PAP,
		.message_center = "foobar.mmsc:80",
		.message_proxy = "mms.proxy.net",
	},
};

static const struct provision_db_entry zyx_contexts[] = {
	{
		.name = "ZYX",
		.apn = "zyx",
		.type = OFONO_GPRS_CONTEXT_TYPE_INTERNET |
			OFONO_GPRS_CONTEXT_TYPE_IA,
		.auth_method = OFONO_GPRS_AUTH_METHOD_NONE,
		.proto = OFONO_GPRS_PROTO_IP,
	},
};

static const struct provision_db_entry beta_contexts[] = {
	{
		.type = OFONO_GPRS_CONTEXT_TYPE_INTERNET |
			OFONO_GPRS_CONTEXT_TYPE_IA,
		.proto = OFONO_GPRS_PROTO_IPV4V6,
		.apn = "beta.internet",
		.auth_method = OFONO_GPRS_AUTH_METHOD_CHAP,
	},
};

static const struct provision_db_entry charlie_contexts[] = {
	{
		.type = OFONO_GPRS_CONTEXT_TYPE_INTERNET |
			OFONO_GPRS_CONTEXT_TYPE_IA,
		.proto = OFONO_GPRS_PROTO_IPV4V6,
		.apn = "charlie.internet",
		.auth_method = OFONO_GPRS_AUTH_METHOD_CHAP,
	},
};

static const struct provision_db_entry xyz_lte_contexts[] = {
	{
		.type = OFONO_GPRS_CONTEXT_TYPE_INTERNET |
			OFONO_GPRS_CONTEXT_TYPE_IA,
		.proto = OFONO_GPRS_PROTO_IPV4V6,
		.apn = "xyz",
		.auth_method = OFONO_GPRS_AUTH_METHOD_CHAP,
	}
};

static const struct provision_db_entry xyz_5g_contexts[] = {
	{
		.type = OFONO_GPRS_CONTEXT_TYPE_INTERNET |
			OFONO_GPRS_CONTEXT_TYPE_IA,
		.proto = OFONO_GPRS_PROTO_IPV4V6,
		.apn = "xyz.5g",
		.auth_method = OFONO_GPRS_AUTH_METHOD_CHAP,
	}
};

/* Make sure mccmnc not in the database isn't found */
static const struct provision_test unknown_mcc_mnc = {
	.mcc = "994",
	.mnc = "42",
	.result = -ENOENT,
};

/* Successful lookup of 'Operator Beta' settings */
static const struct provision_test lookup_beta = {
	.mcc = "999",
	.mnc = "006",
	.result = 0,
	.n_items = L_ARRAY_SIZE(beta_contexts),
	.items = beta_contexts,
};

/* Make sure two digit mnc is treated as != to 3 digit mnc */
static const struct provision_test two_digit_mnc = {
	.mcc = "999",
	.mnc = "06",
	.result = -ENOENT,
};

/*
 * Fallback to non-MVNO settings in case SPN doesn't match and an operator with
 * no SPN is found.  This follows legacy oFono behavior and allows provisioning
 * to work on modem drivers that do not support EFspn reading.
 */
static const struct provision_test fallback_no_spn = {
	.mcc = "999",
	.mnc = "005",
	.spn = "Bogus",
	.result = 0,
	.n_items = L_ARRAY_SIZE(beta_contexts),
	.items = beta_contexts,
};

/* Same as above, but with an MVNO entry for the same mcc/mnc */
static const struct provision_test fallback_no_spn_2 = {
	.mcc = "999",
	.mnc = "002",
	.spn = "Bogus",
	.result = 0,
	.n_items = L_ARRAY_SIZE(alpha_contexts),
	.items = alpha_contexts,
};

/* Successful lookup of Operator Alpha */
static const struct provision_test lookup_alpha = {
	.mcc = "999",
	.mnc = "001",
	.result = 0,
	.n_items = L_ARRAY_SIZE(alpha_contexts),
	.items = alpha_contexts,
};

/* Successful lookup of ZYX (MVNO on Alpha) */
static const struct provision_test lookup_zyx = {
	.mcc = "999",
	.mnc = "01",
	.spn = "ZYX",
	.result = 0,
	.n_items = L_ARRAY_SIZE(zyx_contexts),
	.items = zyx_contexts,
};

/*
 * Successful lookup of Charlie.  This has to be an exact SPN match since
 * no wildcard value is available
 */
static const struct provision_test lookup_charlie = {
	.mcc = "999",
	.mnc = "10",
	.spn = "Charlie",
	.result = 0,
	.n_items = L_ARRAY_SIZE(charlie_contexts),
	.items = charlie_contexts,
};

/* Successful lookup of XYZ (MVNO on Charlie) */
static const struct provision_test lookup_lte_xyz = {
	.mcc = "999",
	.mnc = "11",
	.spn = "XYZ",
	.tags = "lte,unused",
	.result = 0,
	.n_items = L_ARRAY_SIZE(xyz_lte_contexts),
	.items = xyz_lte_contexts,
};

/* Successful lookup of XYZ (MVNO on Charlie) */
static const struct provision_test lookup_5g_xyz = {
	.mcc = "999",
	.mnc = "11",
	.spn = "XYZ",
	.tags = "5g",
	.result = 0,
	.n_items = L_ARRAY_SIZE(xyz_5g_contexts),
	.items = xyz_5g_contexts,
};

/* No match with for an MCC/MNC present in the DB, but no wildcard entry */
static const struct provision_test lookup_no_match = {
	.mcc = "999",
	.mnc = "11",
	.result = -ENOENT,
};

static void provision_lookup(const void *data)
{
	const struct provision_test *test = data;
	struct provision_db_entry *items;
	_auto_(l_strv_free) char **tags = l_strsplit(test->tags, ',');
	size_t n_items;
	size_t i;
	int r;

	r = provision_db_lookup(pdb, test->mcc, test->mnc, test->spn, tags,
					&items, &n_items);
	assert(r == test->result);

	if (r < 0)
		return;

	assert(n_items == test->n_items);
	for (i = 0; i < n_items; i++) {
		const struct provision_db_entry *a = items + i;
		const struct provision_db_entry *b = test->items + i;

		assert(b->type == a->type);
		assert(b->proto == a->proto);
		assert(l_streq0(b->apn, a->apn));
		assert(l_streq0(b->name, a->name));
		assert(l_streq0(b->username, a->username));
		assert(l_streq0(b->password, a->password));
		assert(b->auth_method == a->auth_method);
		assert(l_streq0(b->message_proxy, a->message_proxy));
		assert(l_streq0(b->message_center, a->message_center));
	}

	l_free(items);
}

int main(int argc, char **argv)
{
	int r;

	l_test_init(&argc, &argv);

	l_test_add("Lookup on NULL provision db", null_provision_db, NULL);
	l_test_add("MCC/MNC input validation", invalid_mcc_mnc, NULL);
	l_test_add("Unknown MCC/MNC", provision_lookup, &unknown_mcc_mnc);
	l_test_add("Successful Lookup (Beta)", provision_lookup, &lookup_beta);
	l_test_add("Two digit MNC", provision_lookup, &two_digit_mnc);
	l_test_add("Fallback no-SPN", provision_lookup, &fallback_no_spn);
	l_test_add("Fallback no-SPN#2", provision_lookup, &fallback_no_spn_2);
	l_test_add("Successful lookup (Alpha)", provision_lookup, &lookup_alpha);
	l_test_add("Successful lookup (ZYX)", provision_lookup, &lookup_zyx);
	l_test_add("Exact match (Charlie)", provision_lookup, &lookup_charlie);
	l_test_add("Exact match (XYZ)", provision_lookup, &lookup_lte_xyz);
	l_test_add("Exact match (XYZ 5G)", provision_lookup, &lookup_5g_xyz);
	l_test_add("Exact math (no match)", provision_lookup, &lookup_no_match);

	pdb = provision_db_new(UNITDIR "test-provision.db");
	assert(pdb);

	r = l_test_run();
	provision_db_free(pdb);

	return r;
}