xref: /linux/net/wireless/tests/util.h (revision b8e85e6f3a09fc56b0ff574887798962ef8a8f80)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Utilities for cfg80211 unit testing
4  *
5  * Copyright (C) 2023 Intel Corporation
6  */
7 #ifndef __CFG80211_UTILS_H
8 #define __CFG80211_UTILS_H
9 
10 #define CHAN2G(_freq)  { \
11 	.band = NL80211_BAND_2GHZ, \
12 	.center_freq = (_freq), \
13 	.hw_value = (_freq), \
14 }
15 
16 static const struct ieee80211_channel channels_2ghz[] = {
17 	CHAN2G(2412), /* Channel 1 */
18 	CHAN2G(2417), /* Channel 2 */
19 	CHAN2G(2422), /* Channel 3 */
20 	CHAN2G(2427), /* Channel 4 */
21 	CHAN2G(2432), /* Channel 5 */
22 	CHAN2G(2437), /* Channel 6 */
23 	CHAN2G(2442), /* Channel 7 */
24 	CHAN2G(2447), /* Channel 8 */
25 	CHAN2G(2452), /* Channel 9 */
26 	CHAN2G(2457), /* Channel 10 */
27 	CHAN2G(2462), /* Channel 11 */
28 	CHAN2G(2467), /* Channel 12 */
29 	CHAN2G(2472), /* Channel 13 */
30 	CHAN2G(2484), /* Channel 14 */
31 };
32 
33 struct t_wiphy_priv {
34 	struct kunit *test;
35 	struct cfg80211_ops *ops;
36 
37 	void *ctx;
38 
39 	struct ieee80211_supported_band band_2ghz;
40 	struct ieee80211_channel channels_2ghz[ARRAY_SIZE(channels_2ghz)];
41 };
42 
43 #define T_WIPHY(test, ctx) ({						\
44 		struct wiphy *__wiphy =					\
45 			kunit_alloc_resource(test, t_wiphy_init,	\
46 					     t_wiphy_exit,		\
47 					     GFP_KERNEL, &(ctx));	\
48 									\
49 		KUNIT_ASSERT_NOT_NULL(test, __wiphy);			\
50 		__wiphy;						\
51 	})
52 #define t_wiphy_ctx(wiphy) (((struct t_wiphy_priv *)wiphy_priv(wiphy))->ctx)
53 
54 int t_wiphy_init(struct kunit_resource *resource, void *data);
55 void t_wiphy_exit(struct kunit_resource *resource);
56 
57 #define t_skb_remove_member(skb, type, member)	do {				\
58 		memmove((skb)->data + (skb)->len - sizeof(type) +		\
59 			offsetof(type, member),					\
60 			(skb)->data + (skb)->len - sizeof(type) +		\
61 			offsetofend(type, member),				\
62 			offsetofend(type, member));				\
63 		skb_trim(skb, (skb)->len - sizeof_field(type, member));		\
64 	} while (0)
65 
66 #endif /* __CFG80211_UTILS_H */
67