File: test-band.c

package info (click to toggle)
iwd 3.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,164 kB
  • sloc: ansic: 140,705; sh: 5,514; makefile: 714
file content (776 lines) | stat: -rw-r--r-- 23,451 bytes parent folder | download | duplicates (3)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/*
 *
 *  Wireless daemon for Linux
 *
 *  Copyright (C) 2021  Intel Corporation. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; 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 <string.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <ell/ell.h>

#include "src/band.h"
#include "src/netdev.h"
#include "src/ie.h"

static struct band *new_band()
{
	/* Band with VHT/80, short GI, NSS:2 and VHT MCS 0-9 */
	static const uint8_t vht_mcs_set[] = {
		0xfa, 0xff, 0x00, 0x00, 0xfa, 0xff, 0x00, 0x20,
	};
	static const uint8_t vht_capabilities[] = {
		0xa0, 0x71, 0x80, 0x03,
	};
	static const uint8_t ht_mcs_set[] = {
		0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x2c, 0x01, 0x01, 0x00, 0x00, 0x00,
	};
	static const uint8_t ht_capabilities[] = {
		0xee, 0x11,
	};
	/* band + 8 basic rates */
	struct band *band = l_malloc(sizeof(struct band) + 8);

	memset(band, 0, sizeof(struct band) + 8);

	band->supported_rates_len = 8;
	band->supported_rates[0] = 12;
	band->supported_rates[1] = 18;
	band->supported_rates[2] = 24;
	band->supported_rates[3] = 36;
	band->supported_rates[4] = 48;
	band->supported_rates[5] = 72;
	band->supported_rates[6] = 96;
	band->supported_rates[7] = 108;

	band->ht_supported = true;
	band->vht_supported = true;

	memcpy(band->vht_mcs_set, vht_mcs_set, sizeof(band->vht_mcs_set));
	memcpy(band->vht_capabilities, vht_capabilities,
					sizeof(band->vht_capabilities));
	memcpy(band->ht_mcs_set, ht_mcs_set, sizeof(band->ht_mcs_set));
	memcpy(band->ht_capabilities, ht_capabilities,
					sizeof(band->ht_capabilities));

	return band;
}

static void band_test_nonht_1(const void *data)
{
	uint8_t supported_rates[] = { 1, 8,
			0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c };
	struct band *band = new_band();
	uint64_t data_rate;
	int ret;

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-50, &data_rate);
	assert(ret == 0);
	assert(data_rate == 54000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-66, &data_rate);
	assert(ret == 0);
	assert(data_rate == 48000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-70, &data_rate);
	assert(ret == 0);
	assert(data_rate == 36000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-74, &data_rate);
	assert(ret == 0);
	assert(data_rate == 24000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-77, &data_rate);
	assert(ret == 0);
	assert(data_rate == 18000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-79, &data_rate);
	assert(ret == 0);
	assert(data_rate == 12000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-81, &data_rate);
	assert(ret == 0);
	assert(data_rate == 9000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-82, &data_rate);
	assert(ret == 0);
	assert(data_rate == 6000000);

	ret = band_estimate_nonht_rate(band, supported_rates, NULL,
							-83, &data_rate);
	assert(ret < 0);

	band_free(band);
}

static void band_test_ht_1(const void *data)
{
	/* HT40 */
	uint8_t hto[] = { 61, 22,
				0x95, 0x0d, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	/* HT40, MCS 0-23, 40/20Mhz SGI */
	uint8_t htc[] = { 45, 26,
				0xef, 0x09, 0x17, 0xff, 0xff, 0xff, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00 };
	struct band *band = new_band();
	uint64_t data_rate;
	int ret;

	ret = band_estimate_ht_rx_rate(band, htc, hto, -51, &data_rate);
	assert(ret == 0);
	assert(data_rate == 300000000);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -62, &data_rate);
	assert(ret == 0);
	assert(data_rate == 270000000);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -63, &data_rate);
	assert(ret == 0);
	assert(data_rate == 240000000);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -66, &data_rate);
	assert(ret == 0);
	assert(data_rate == 180000000);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -71, &data_rate);
	assert(ret == 0);
	assert(data_rate == 120000000);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -74, &data_rate);
	assert(ret == 0);
	assert(data_rate == 90000000);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -76, &data_rate);
	assert(ret == 0);
	assert(data_rate == 60000000);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -79, &data_rate);
	assert(ret == 0);
	assert(data_rate == 30000000);

	/* We should now fall back to HT20 */
	ret = band_estimate_ht_rx_rate(band, htc, hto, -82, &data_rate);
	assert(ret == 0);
	assert(data_rate == 14444440);

	ret = band_estimate_ht_rx_rate(band, htc, hto, -83, &data_rate);
	assert(ret < 0);

	band_free(band);
}

static void band_test_vht_1(const void *data)
{
	/* VHT operating on 80 Mhz */
	uint8_t vhto[] = { 192, 5, 0x01, 0x9b, 0x00, 0x00, 0x00 };
	/* VHT80, NSS:3, MCS 0-9, 80Mhz SGI */
	uint8_t vhtc[] = { 191, 12,
				0xb2, 0x59, 0x82, 0x0f, 0xea, 0xff, 0x00, 0x00,
				0xea, 0xff, 0x00, 0x00 };
	/* HT40 */
	uint8_t hto[] = { 61, 22,
				0x95, 0x0d, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
	/* HT40, MCS 0-23, 40/20Mhz SGI */
	uint8_t htc[] = { 45, 26,
				0xef, 0x09, 0x17, 0xff, 0xff, 0xff, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00 };
	struct band *band = new_band();
	uint64_t data_rate;
	int ret;

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-51, &data_rate);
	assert(ret == 0);
	assert(data_rate == 866666660);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-53, &data_rate);
	assert(ret == 0);
	assert(data_rate == 780000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-56, &data_rate);
	assert(ret == 0);
	assert(data_rate == 650000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-59, &data_rate);
	assert(ret == 0);
	assert(data_rate == 585000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-60, &data_rate);
	assert(ret == 0);
	assert(data_rate == 520000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-63, &data_rate);
	assert(ret == 0);
	assert(data_rate == 390000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-67, &data_rate);
	assert(ret == 0);
	assert(data_rate == 260000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-70, &data_rate);
	assert(ret == 0);
	assert(data_rate == 195000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-73, &data_rate);
	assert(ret == 0);
	assert(data_rate == 130000000);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-76, &data_rate);
	assert(ret == 0);
	assert(data_rate == 65000000);

	/* We should now fall back to HT40 */
	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-79, &data_rate);
	assert(ret == 0);
	assert(data_rate == 30000000);

	/* And only enough for HT20 */
	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-82, &data_rate);
	assert(ret == 0);
	assert(data_rate == 14444440);

	ret = band_estimate_vht_rx_rate(band, vhtc, vhto, htc, hto,
					-83, &data_rate);
	assert(ret < 0);

	band_free(band);
}

struct he_test_data {
	enum band_freq freq;
	int32_t rssi;
	uint64_t expected_rate;
	int expected_return;
	/* Own capabilities */
	struct band_he_capabilities capabilities;
	/* Peer HE Capabilities IE */
	uint8_t he_capabilities[31];

};

/* IWD doesn't look at this */
#define HE_MAC_CAPA 0, 0, 0, 0, 0, 0
/* IWD only cares about the width set byte */
#define HE_PHY_CAPA(wset) wset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

#define MCS7		0
#define MCS9		1
#define MCS11		2
#define MCS_UNSUP	0xff, 0xff
/* A readable macro for defining MCS sets */
#define HE_MCS_SET(mcs, nss) \
	(nss >= 1 ? mcs << 0 : 3 << 0) | \
	(nss >= 2 ? mcs << 2 : 3 << 2) | \
	(nss >= 3 ? mcs << 4 : 3 << 4) | \
	(nss >= 4 ? mcs << 6 : 3 << 6),  \
	(nss >= 5 ? mcs << 0 : 3 << 0) | \
	(nss >= 6 ? mcs << 2 : 3 << 2) | \
	(nss >= 7 ? mcs << 4 : 3 << 4) | \
	(nss >= 8 ? mcs << 6 : 3 << 6)

/* 2.4GHz, 20MHz, MCS 7, NSS 1 */
const struct he_test_data he_test_2_4_20mhz_mcs_7_nss_1 = {
	.freq = BAND_FREQ_2_4_GHZ,
	.rssi = -20,
	.expected_rate = 86000000ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS7, 1), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x00) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		22, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x00), MCS_UNSUP, HE_MCS_SET(MCS7, 1),
	},
};

/* 2.4GHz, 40MHz, MCS 7, NSS 1 */
const struct he_test_data he_test_2_4_40mhz_mcs_7_nss_1 = {
	.freq = BAND_FREQ_2_4_GHZ,
	.rssi = -20,
	.expected_rate = 172000000ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS7, 1), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x02) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		22, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x02), MCS_UNSUP, HE_MCS_SET(MCS7, 1),
	},
};

/* 5GHz, 20MHz, MCS 7, NSS 1 */
const struct he_test_data he_test_5_20mhz_mcs_7_nss_1 = {
	.freq = BAND_FREQ_5_GHZ,
	.rssi = -20,
	.expected_rate = 86000000ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS7, 1), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x00) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		22, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x00), MCS_UNSUP, HE_MCS_SET(MCS7, 1)
	},
};

/* 5GHz, 80MHz, MCS 7, NSS 1 */
const struct he_test_data he_test_5_80mhz_mcs_7_nss_1 = {
	.freq = BAND_FREQ_5_GHZ,
	.rssi = -20,
	.expected_rate = 360300000ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS7, 1), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x04) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		22, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x04), MCS_UNSUP, HE_MCS_SET(MCS7, 1)
	},
};

/* 5GHz, 160MHz, MCS 7, NSS 1 */
const struct he_test_data he_test_5_160mhz_mcs_7_nss_1 = {
	.freq = BAND_FREQ_5_GHZ,
	.rssi = -20,
	.expected_rate = 720600000ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS7, 1), MCS_UNSUP,
				HE_MCS_SET(MCS7, 1), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x0c) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		26, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x0c), MCS_UNSUP, HE_MCS_SET(MCS7, 1),
				MCS_UNSUP, HE_MCS_SET(MCS7, 1)
	},
};

/* 5GHz, 160/80+80MHz, MCS 7, NSS 1 */
const struct he_test_data he_test_5_160_80_P_80_mhz_mcs_7_nss_1 = {
	.freq = BAND_FREQ_5_GHZ,
	.rssi = -20,
	.expected_rate = 720600000ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS7, 1), MCS_UNSUP,
				HE_MCS_SET(MCS7, 1), MCS_UNSUP,
				HE_MCS_SET(MCS7, 1), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x1c) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		30, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x1c), MCS_UNSUP, HE_MCS_SET(MCS7, 1),
				MCS_UNSUP, HE_MCS_SET(MCS7, 1),
				MCS_UNSUP, HE_MCS_SET(MCS7, 1)
	},
};

/* 5GHz, max data rate */
const struct he_test_data he_test_5_max_data_rate = {
	.freq = BAND_FREQ_5_GHZ,
	.rssi = -20,
	.expected_rate = 1201000000ULL * 8ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS11, 8), MCS_UNSUP,
				HE_MCS_SET(MCS11, 8), MCS_UNSUP,
				HE_MCS_SET(MCS11, 8), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x1c) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		30, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x1c), MCS_UNSUP, HE_MCS_SET(MCS11, 8),
				MCS_UNSUP, HE_MCS_SET(MCS11, 8),
				MCS_UNSUP, HE_MCS_SET(MCS11, 8)
	},
};

const struct he_test_data he_all_mcs_unsupported = {
	.freq = BAND_FREQ_5_GHZ,
	.rssi = -20,
	.expected_rate = 1201000000ULL * 8ULL,
	.expected_return = -ENETUNREACH,
	.capabilities = {
		.he_mcs_set = { MCS_UNSUP, MCS_UNSUP,
				MCS_UNSUP, MCS_UNSUP,
				MCS_UNSUP, MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x1c) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		30, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x1c), MCS_UNSUP, MCS_UNSUP,
				MCS_UNSUP, MCS_UNSUP,
				MCS_UNSUP, MCS_UNSUP
	},
};

/* 5GHz, max data rate, low-rssi */
const struct he_test_data he_test_5_low_rssi = {
	.freq = BAND_FREQ_5_GHZ,
	.rssi = -80, /* Should force 20MHz/MCS0 width to be used */
	.expected_rate = 8600000ULL * 8ULL,
	.capabilities = {
		.he_mcs_set = { HE_MCS_SET(MCS11, 8), MCS_UNSUP,
				HE_MCS_SET(MCS11, 8), MCS_UNSUP,
				HE_MCS_SET(MCS11, 8), MCS_UNSUP },
		.he_phy_capa = { HE_PHY_CAPA(0x1c) },
		.iftypes = 1 << NETDEV_IFTYPE_STATION,
	},
	.he_capabilities = {
		30, IE_TYPE_HE_CAPABILITIES - 256, HE_MAC_CAPA,
		HE_PHY_CAPA(0x1c), MCS_UNSUP, HE_MCS_SET(MCS11, 8),
				MCS_UNSUP, HE_MCS_SET(MCS11, 8),
				MCS_UNSUP, HE_MCS_SET(MCS11, 8)
	},
};

static void band_test_he(const void *data)
{
	const struct he_test_data *he_data = data;
	struct band *band;
	uint64_t rate = 0;
	int ret;

	band = new_band();
	band->freq = he_data->freq;
	band->he_capabilities = l_queue_new();

	l_queue_push_tail(band->he_capabilities,
				(void*)&(he_data->capabilities));

	assert(ie_validate_he_capabilities(he_data->he_capabilities + 2,
					he_data->he_capabilities[0]));

	ret = band_estimate_he_rx_rate(band, he_data->he_capabilities + 2,
					he_data->rssi, &rate);
	assert(ret == he_data->expected_return);

	if (ret == 0)
		assert(rate == he_data->expected_rate);

	l_queue_destroy(band->he_capabilities, NULL);
	l_free(band);
}

struct oci2freq_data {
	unsigned int op;
	unsigned int chan;
	int expected_freq;
};

static const struct oci2freq_data oci2freq_data_1 = { 129, 100, 5500 };
static const struct oci2freq_data oci2freq_data_2 = { 129, 108, 5540 };
static const struct oci2freq_data oci2freq_data_3 = { 129, 106, -EINVAL };
static const struct oci2freq_data oci2freq_data_4 = { 81, 1, 2412 };
static const struct oci2freq_data oci2freq_data_5 = { 82, 1, -EINVAL };
static const struct oci2freq_data oci2freq_data_6 = { 82, 14, 2484 };
static const struct oci2freq_data oci2freq_data_7 = { 88, 0, -ENOENT };
static const struct oci2freq_data oci2freq_data_8 = { 128, 161, 5805 };

static void test_oci2freq(const void *data)
{
	const struct oci2freq_data *test = data;
	int r;

	r = oci_to_frequency(test->op, test->chan);
	assert(r == test->expected_freq);
}

static const struct band_chandef cd_1 = {
	.frequency = 5540,
	.channel_width = BAND_CHANDEF_WIDTH_160,
	.center1_frequency = 5570,
};

static const struct band_chandef cd_2 = {
	.frequency = 5180,
	.channel_width = BAND_CHANDEF_WIDTH_80P80,
	.center1_frequency = 5210,
	.center2_frequency = 5775,
};

static const struct band_chandef cd_3 = {
	.frequency = 2437,
	.channel_width = BAND_CHANDEF_WIDTH_20NOHT,
	.center1_frequency = 2437,
};

static const struct band_chandef cd_4 = {
	.frequency = 2437,
	.channel_width = BAND_CHANDEF_WIDTH_40,
	.center1_frequency = 2427,
};

static const struct band_chandef cd_5 = {
	.frequency = 6235,
	.channel_width = BAND_CHANDEF_WIDTH_20,
};

static const struct band_chandef cd_6 = {
	.frequency = 6235,
	.channel_width =  BAND_CHANDEF_WIDTH_40,
};

static const struct band_chandef cd_7 = {
	.frequency = 6235,
	.channel_width =  BAND_CHANDEF_WIDTH_80,
};

static const struct band_chandef cd_8 = {
	.frequency = 6235,
	.channel_width =  BAND_CHANDEF_WIDTH_160,
};

static const struct band_chandef cd_9 = {
	.frequency = 6235,
	.channel_width =  BAND_CHANDEF_WIDTH_80P80,
	.center1_frequency = 6145,
	.center2_frequency = 6225,
};

struct oci_data {
	const struct band_chandef *cd;
	uint8_t oci[3];
	int expected_verify_error;
};

static const struct oci_data oci_data_1 = { &cd_1, { 129, 108, 0 } };
static const struct oci_data oci_data_2 = { &cd_2, { 130, 36, 155 } };
static const struct oci_data oci_data_3 = { &cd_3, { 81, 6, 0 } };
static const struct oci_data oci_data_4 = { &cd_4, { 84, 6, 0 } };
static const struct oci_data oci_data_5 = { &cd_5, { 131, 57, 0 } };
static const struct oci_data oci_data_6 = { &cd_6, { 132, 57, 0 } };
static const struct oci_data oci_data_7 = { &cd_7, { 133, 57, 0 } };
static const struct oci_data oci_data_8 = { &cd_8, { 134, 57, 0 } };
static const struct oci_data oci_data_9 = { &cd_9, { 135, 57, 55 } };

static const struct oci_data oci_err_1 = { &cd_1, { 129, 36, 0 }, -EPERM };
static const struct oci_data oci_err_2 = { &cd_1, { 121, 108, 0 }, -EPERM };
static const struct oci_data oci_err_3 = { &cd_1, { 130, 36, 155 }, -EPERM };
static const struct oci_data oci_err_4 = { &cd_3, { 81, 5 }, -EPERM };
static const struct oci_data oci_err_5 = { &cd_3, { 80, 1 }, -ENOENT };
static const struct oci_data oci_err_6 = { &cd_3, { 81, 15 }, -EINVAL };
static const struct oci_data oci_err_7 = { &cd_4, { 84, 5 }, -EPERM };
static const struct oci_data oci_err_8 = { &cd_4, { 83, 6 }, -EPERM };

static void test_oci_verify(const void *data)
{
	const struct oci_data *test = data;
	int r;

	r = oci_verify(test->oci, test->cd);
	assert(r == test->expected_verify_error);
}

static void test_oci_from_chandef(const void *data)
{
	const struct oci_data *test = data;
	uint8_t oci[3];
	int r;

	r = oci_from_chandef(test->cd, oci);
	assert(!r);

	assert(!memcmp(oci, test->oci, sizeof(oci)));
}

static void test_6ghz_channels(const void *data)
{
	unsigned int i;

	/* Test all channels for 6GHz */
	for (i = 1; i <= 233; i += 4)
		assert(band_channel_to_freq(i, BAND_FREQ_6_GHZ) != 0);
}

static void test_6ghz_freqs(const void *data)
{
	uint32_t i;
	enum band_freq band;

	for (i = 5955; i <= 7115; i += 20) {
		assert(band_freq_to_channel(i, &band) != 0);
		assert(band == BAND_FREQ_6_GHZ);
	}
}

static void test_conversions(const void *data)
{
	/*
	 * Test a few invalid channels/frequencies that appear valid but are
	 * not in the E-4 table. The checks in band.c seem to cover 2.4GHz and
	 * 6GHz very well since there are no gaps, but the 5GHz band has some
	 * segmentation.
	 */

	/* Gap in 5GHz channels between 68 and 96 */
	assert(!band_channel_to_freq(72, BAND_FREQ_5_GHZ));
	assert(!band_freq_to_channel(5360, NULL));

	/* Invalid channel using 4000mhz starting frequency */
	assert(!band_channel_to_freq(183, BAND_FREQ_5_GHZ));
	assert(!band_freq_to_channel(4915, NULL));

	assert(!band_channel_to_freq(192, BAND_FREQ_5_GHZ));
}

static void test_conversion_fallback(const void *data)
{
	enum band_freq band;
	const uint8_t cc[] = {'E', 'S', 0x04};

	/*
	 * Without a fallback, this would fail. There is no operclass 3 in the
	 * global operating table (E-4)
	 */
	band = band_oper_class_to_band(cc, 3);
	assert(band == BAND_FREQ_5_GHZ);
}

static void test_unsupported_freqs(const void *data)
{
	/*
	 * These are frequencies that were seen advertised by some drivers
	 * but that don't match any global operating classes IWD has. They may
	 * be valid, but IWD should not parse them correctly without added
	 * support in band.c.
	 */
	uint32_t unsupported[] = {
		5920, 5940, 5960, 5980, 6000, 6020, 6040, 6060, 6080,
	};
	unsigned int i;

	for (i = 0; i < L_ARRAY_SIZE(unsupported); i++) {
		uint8_t channel = band_freq_to_channel(unsupported[i], NULL);

		assert(!channel);
	}
}

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

	l_test_add("/band/non-HT/test1", band_test_nonht_1, NULL);

	l_test_add("/band/HT/test1", band_test_ht_1, NULL);

	l_test_add("/band/VHT/test1", band_test_vht_1, NULL);

	l_test_add("/band/HE/test/2.4GHz/20MHz/MCS7/NSS1", band_test_he,
					&he_test_2_4_20mhz_mcs_7_nss_1);
	l_test_add("/band/HE/test/2.4GHz/40MHz/MCS7/NSS1", band_test_he,
					&he_test_2_4_40mhz_mcs_7_nss_1);
	l_test_add("/band/HE/test/5GHz/20MHz/MCS7/NSS1", band_test_he,
					&he_test_5_20mhz_mcs_7_nss_1);
	l_test_add("/band/HE/test/5GHz/80MHz/MCS7/NSS1", band_test_he,
					&he_test_5_80mhz_mcs_7_nss_1);
	l_test_add("/band/HE/test/5GHz/160MHz/MCS7/NSS1", band_test_he,
					&he_test_5_160mhz_mcs_7_nss_1);
	l_test_add("/band/HE/test/5GHz/160/80+80MHz/MCS7/NSS1", band_test_he,
					&he_test_5_160_80_P_80_mhz_mcs_7_nss_1);
	l_test_add("/band/HE/test/5GHz/max data rate", band_test_he,
					&he_test_5_max_data_rate);
	l_test_add("/band/HE/test/all MCS unsupported", band_test_he,
					&he_all_mcs_unsupported);
	l_test_add("/band/HE/test/low RSSI", band_test_he,
					&he_test_5_low_rssi);

	l_test_add("/band/oci2freq 1", test_oci2freq, &oci2freq_data_1);
	l_test_add("/band/oci2freq 2", test_oci2freq, &oci2freq_data_2);
	l_test_add("/band/oci2freq 3", test_oci2freq, &oci2freq_data_3);
	l_test_add("/band/oci2freq 4", test_oci2freq, &oci2freq_data_4);
	l_test_add("/band/oci2freq 5", test_oci2freq, &oci2freq_data_5);
	l_test_add("/band/oci2freq 6", test_oci2freq, &oci2freq_data_6);
	l_test_add("/band/oci2freq 7", test_oci2freq, &oci2freq_data_7);
	l_test_add("/band/oci2freq 8", test_oci2freq, &oci2freq_data_8);

	l_test_add("/band/oci/verify 1", test_oci_verify, &oci_data_1);
	l_test_add("/band/oci/verify 2", test_oci_verify, &oci_data_2);
	l_test_add("/band/oci/verify 3", test_oci_verify, &oci_data_3);
	l_test_add("/band/oci/verify 4", test_oci_verify, &oci_data_4);
	l_test_add("/band/oci/verify 5", test_oci_verify, &oci_data_5);
	l_test_add("/band/oci/verify 6", test_oci_verify, &oci_data_6);
	l_test_add("/band/oci/verify 7", test_oci_verify, &oci_data_7);
	l_test_add("/band/oci/verify 8", test_oci_verify, &oci_data_8);
	l_test_add("/band/oci/verify 9", test_oci_verify, &oci_data_9);

	l_test_add("/band/oci/noverify 1", test_oci_verify, &oci_err_1);
	l_test_add("/band/oci/noverify 2", test_oci_verify, &oci_err_2);
	l_test_add("/band/oci/noverify 3", test_oci_verify, &oci_err_3);
	l_test_add("/band/oci/noverify 4", test_oci_verify, &oci_err_4);
	l_test_add("/band/oci/noverify 5", test_oci_verify, &oci_err_5);
	l_test_add("/band/oci/noverify 6", test_oci_verify, &oci_err_6);
	l_test_add("/band/oci/noverify 7", test_oci_verify, &oci_err_7);
	l_test_add("/band/oci/noverify 8", test_oci_verify, &oci_err_8);

	l_test_add("/band/oci/chandef 1", test_oci_from_chandef, &oci_data_1);
	l_test_add("/band/oci/chandef 2", test_oci_from_chandef, &oci_data_2);
	l_test_add("/band/oci/chandef 3", test_oci_from_chandef, &oci_data_3);
	l_test_add("/band/oci/chandef 4", test_oci_from_chandef, &oci_data_4);

	l_test_add("/band/6ghz/channels", test_6ghz_channels, NULL);
	l_test_add("/band/6ghz/freq", test_6ghz_freqs, NULL);

	l_test_add("/band/conversions", test_conversions, NULL);
	l_test_add("/band/conversion fallback", test_conversion_fallback, NULL);
	l_test_add("/band/unsupported freqs", test_unsupported_freqs, NULL);

	return l_test_run();
}