xref: /linux/drivers/gpu/drm/imagination/pvr_test.c (revision 939faf71cf7ca9ab3d1bd2912ac0e203d4d7156a)
1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2025 Imagination Technologies Ltd. */
3 
4 #include "pvr_device.h"
5 
6 #include <linux/errno.h>
7 #include <linux/stddef.h>
8 #include <linux/string.h>
9 #include <linux/types.h>
10 
11 #include <kunit/test.h>
12 #include <kunit/visibility.h>
13 
14 static void decode_gpuid_string(struct kunit *test)
15 {
16 	const struct pvr_gpu_id bad_gpuid = { 0xdead, 0xbeef, 0xcafe, 0xface };
17 	const u64 packed_bad_gpuid = pvr_gpu_id_to_packed_bvnc(&bad_gpuid);
18 
19 #define GPUID_TEST_CASE(str_, err_, value_)					\
20 	do {									\
21 		struct pvr_gpu_id _gpuid_out = bad_gpuid;			\
22 		int _err;							\
23 		_err = pvr_gpuid_decode_string(NULL, str_, &_gpuid_out);	\
24 		KUNIT_EXPECT_EQ(test, _err, err_);				\
25 		KUNIT_EXPECT_EQ(test,						\
26 				pvr_gpu_id_to_packed_bvnc(&_gpuid_out),		\
27 				value_);					\
28 	} while (0)
29 
30 #define GPUID_TEST_CASE_OK(str_, b_, v_, n_, c_) \
31 	GPUID_TEST_CASE(str_, 0, PVR_PACKED_BVNC(b_, v_, n_, c_))
32 
33 #define GPUID_TEST_CASE_INVAL(str_) \
34 	GPUID_TEST_CASE(str_, -EINVAL, packed_bad_gpuid)
35 
36 	GPUID_TEST_CASE_OK("12.34.56.78", 12, 34, 56, 78);
37 	GPUID_TEST_CASE_OK("0.0.0.0", 0, 0, 0, 0);
38 
39 	GPUID_TEST_CASE_INVAL("");
40 	GPUID_TEST_CASE_INVAL("42.foobar-invalid.gpuid.bvnc");
41 
42 	/* String longer than PVR_GPUID_STRING_MAX_LENGTH. */
43 	GPUID_TEST_CASE_INVAL("12.34.56.789012345678901234567890123456");
44 
45 	/* Single value overflowing u16. */
46 	GPUID_TEST_CASE_INVAL("12.34.56.999999");
47 
48 	/* Wrong number of parts and/or dots. */
49 	GPUID_TEST_CASE_INVAL("12.34.56.78.90");
50 	GPUID_TEST_CASE_INVAL("12.34.56..78");
51 	GPUID_TEST_CASE_INVAL("12.34..56");
52 	GPUID_TEST_CASE_INVAL("12.34.56");
53 
54 #undef GPUID_TEST_CASE_INVAL
55 #undef GPUID_TEST_CASE_OK
56 #undef GPUID_TEST_CASE
57 }
58 
59 static struct kunit_case pvr_tests_cases[] = {
60 	KUNIT_CASE(decode_gpuid_string),
61 	{},
62 };
63 
64 static struct kunit_suite pvr_tests_suite = {
65 	.name = "pvr_tests",
66 	.test_cases = pvr_tests_cases,
67 };
68 kunit_test_suite(pvr_tests_suite);
69 
70 MODULE_AUTHOR("Imagination Technologies Ltd.");
71 MODULE_LICENSE("Dual MIT/GPL");
72 MODULE_DESCRIPTION("pvr kunit tests");
73 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
74