xref: /linux/net/wireless/tests/util.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1*9d027a35SBenjamin Berg // SPDX-License-Identifier: GPL-2.0-only
2*9d027a35SBenjamin Berg /*
3*9d027a35SBenjamin Berg  * KUnit fixture to have a (configurable) wiphy
4*9d027a35SBenjamin Berg  *
5*9d027a35SBenjamin Berg  * Copyright (C) 2023 Intel Corporation
6*9d027a35SBenjamin Berg  */
7*9d027a35SBenjamin Berg #include <linux/ieee80211.h>
8*9d027a35SBenjamin Berg #include <net/cfg80211.h>
9*9d027a35SBenjamin Berg #include <kunit/test.h>
10*9d027a35SBenjamin Berg #include <kunit/test-bug.h>
11*9d027a35SBenjamin Berg #include "util.h"
12*9d027a35SBenjamin Berg 
t_wiphy_init(struct kunit_resource * resource,void * ctx)13*9d027a35SBenjamin Berg int t_wiphy_init(struct kunit_resource *resource, void *ctx)
14*9d027a35SBenjamin Berg {
15*9d027a35SBenjamin Berg 	struct kunit *test = kunit_get_current_test();
16*9d027a35SBenjamin Berg 	struct cfg80211_ops *ops;
17*9d027a35SBenjamin Berg 	struct wiphy *wiphy;
18*9d027a35SBenjamin Berg 	struct t_wiphy_priv *priv;
19*9d027a35SBenjamin Berg 
20*9d027a35SBenjamin Berg 	ops = kzalloc(sizeof(*ops), GFP_KERNEL);
21*9d027a35SBenjamin Berg 	KUNIT_ASSERT_NOT_NULL(test, ops);
22*9d027a35SBenjamin Berg 
23*9d027a35SBenjamin Berg 	wiphy = wiphy_new_nm(ops, sizeof(*priv), "kunit");
24*9d027a35SBenjamin Berg 	KUNIT_ASSERT_NOT_NULL(test, wiphy);
25*9d027a35SBenjamin Berg 
26*9d027a35SBenjamin Berg 	priv = wiphy_priv(wiphy);
27*9d027a35SBenjamin Berg 	priv->ctx = ctx;
28*9d027a35SBenjamin Berg 	priv->ops = ops;
29*9d027a35SBenjamin Berg 
30*9d027a35SBenjamin Berg 	/* Initialize channels, feel free to add more here channels/bands */
31*9d027a35SBenjamin Berg 	memcpy(priv->channels_2ghz, channels_2ghz, sizeof(channels_2ghz));
32*9d027a35SBenjamin Berg 	wiphy->bands[NL80211_BAND_2GHZ] = &priv->band_2ghz;
33*9d027a35SBenjamin Berg 	priv->band_2ghz.channels = priv->channels_2ghz;
34*9d027a35SBenjamin Berg 	priv->band_2ghz.n_channels = ARRAY_SIZE(channels_2ghz);
35*9d027a35SBenjamin Berg 
36*9d027a35SBenjamin Berg 	resource->data = wiphy;
37*9d027a35SBenjamin Berg 	resource->name = "wiphy";
38*9d027a35SBenjamin Berg 
39*9d027a35SBenjamin Berg 	return 0;
40*9d027a35SBenjamin Berg }
41*9d027a35SBenjamin Berg 
t_wiphy_exit(struct kunit_resource * resource)42*9d027a35SBenjamin Berg void t_wiphy_exit(struct kunit_resource *resource)
43*9d027a35SBenjamin Berg {
44*9d027a35SBenjamin Berg 	struct t_wiphy_priv *priv;
45*9d027a35SBenjamin Berg 	struct cfg80211_ops *ops;
46*9d027a35SBenjamin Berg 
47*9d027a35SBenjamin Berg 	priv = wiphy_priv(resource->data);
48*9d027a35SBenjamin Berg 	ops = priv->ops;
49*9d027a35SBenjamin Berg 
50*9d027a35SBenjamin Berg 	/* Should we ensure anything about the state here?
51*9d027a35SBenjamin Berg 	 * e.g. full destruction or no calls to any ops on destruction?
52*9d027a35SBenjamin Berg 	 */
53*9d027a35SBenjamin Berg 
54*9d027a35SBenjamin Berg 	wiphy_free(resource->data);
55*9d027a35SBenjamin Berg 	kfree(ops);
56*9d027a35SBenjamin Berg }
57