1 /*
2  * Copyright (c) 2019 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  *
6  * Permission to use, copy, modify, and/or distribute this software for
7  * any purpose with or without fee is hereby granted, provided that the
8  * above copyright notice and this permission notice appear in all
9  * copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
12  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
13  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
14  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
16  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
17  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18  * PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #include "qdf_tracker.h"
22 #include "qdf_tracker_test.h"
23 #include "qdf_trace.h"
24 #include "qdf_types.h"
25 
26 #if defined(CONFIG_LEAK_DETECTION) && defined(WLAN_TRACKER_TEST)
27 #define qdf_ut_tracker_bits 4 /* 16 buckets */
28 #define qdf_ut_tracker_item_count 3
29 #define qdf_ut_tracker_declare(name) \
30 	qdf_tracker_declare(name, qdf_ut_tracker_bits, "unit-test leak", \
31 			    "unit-test alloc", "unit-test free")
32 
qdf_tracker_test_empty(void)33 static uint32_t qdf_tracker_test_empty(void)
34 {
35 	qdf_ut_tracker_declare(tracker);
36 	char func[QDF_TRACKER_FUNC_SIZE];
37 	uint32_t line;
38 
39 	/* a new tracker should ... */
40 	qdf_tracker_init(&tracker);
41 
42 	/* ... be empty */
43 	qdf_tracker_check_for_leaks(&tracker);
44 
45 	/* ... not contain an arbitrary pointer */
46 	QDF_BUG(!qdf_tracker_lookup(&tracker, &tracker, &func, &line));
47 
48 	qdf_tracker_deinit(&tracker);
49 
50 	return 0;
51 }
52 
qdf_tracker_test_add_remove(void)53 static uint32_t qdf_tracker_test_add_remove(void)
54 {
55 	qdf_ut_tracker_declare(tracker);
56 	bool items[qdf_ut_tracker_item_count];
57 	QDF_STATUS status;
58 	int i;
59 
60 	qdf_tracker_init(&tracker);
61 
62 	/* an empty tracker should track items */
63 	for (i = 0; i < qdf_ut_tracker_item_count; i++) {
64 		status = qdf_tracker_track(&tracker, items + i,
65 					   __func__, __LINE__);
66 		items[i] = QDF_IS_STATUS_SUCCESS(status);
67 	}
68 
69 	/* a non-empty tracker should find previously added items */
70 	for (i = 0; i < qdf_ut_tracker_item_count; i++) {
71 		char func[QDF_TRACKER_FUNC_SIZE];
72 		uint32_t line;
73 
74 		if (!items[i])
75 			continue;
76 
77 		QDF_BUG(qdf_tracker_lookup(&tracker, items + i, &func, &line));
78 		qdf_tracker_untrack(&tracker, items + i, __func__, __LINE__);
79 	}
80 
81 	/* a tracker should be empty after all items are untracked*/
82 	qdf_tracker_check_for_leaks(&tracker);
83 
84 	qdf_tracker_deinit(&tracker);
85 
86 	return 0;
87 }
88 
qdf_tracker_unit_test(void)89 uint32_t qdf_tracker_unit_test(void)
90 {
91 	uint32_t errors = 0;
92 
93 	errors += qdf_tracker_test_empty();
94 	errors += qdf_tracker_test_add_remove();
95 
96 	return errors;
97 }
98 #endif
99