summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/imagination/pvr_test.c
blob: 506cfa5a02f1ecdfa7b2265d1018ebac4d60b10d (plain)
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
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/* Copyright (c) 2025 Imagination Technologies Ltd. */

#include "pvr_device.h"

#include <linux/errno.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/types.h>

#include <kunit/test.h>
#include <kunit/visibility.h>

static void decode_gpuid_string(struct kunit *test)
{
	const struct pvr_gpu_id bad_gpuid = { 0xdead, 0xbeef, 0xcafe, 0xface };
	const u64 packed_bad_gpuid = pvr_gpu_id_to_packed_bvnc(&bad_gpuid);

#define GPUID_TEST_CASE(str_, err_, value_)					\
	do {									\
		struct pvr_gpu_id _gpuid_out = bad_gpuid;			\
		int _err;							\
		_err = pvr_gpuid_decode_string(NULL, str_, &_gpuid_out);	\
		KUNIT_EXPECT_EQ(test, _err, err_);				\
		KUNIT_EXPECT_EQ(test,						\
				pvr_gpu_id_to_packed_bvnc(&_gpuid_out),		\
				value_);					\
	} while (0)

#define GPUID_TEST_CASE_OK(str_, b_, v_, n_, c_) \
	GPUID_TEST_CASE(str_, 0, PVR_PACKED_BVNC(b_, v_, n_, c_))

#define GPUID_TEST_CASE_INVAL(str_) \
	GPUID_TEST_CASE(str_, -EINVAL, packed_bad_gpuid)

	GPUID_TEST_CASE_OK("12.34.56.78", 12, 34, 56, 78);
	GPUID_TEST_CASE_OK("0.0.0.0", 0, 0, 0, 0);

	GPUID_TEST_CASE_INVAL("");
	GPUID_TEST_CASE_INVAL("42.foobar-invalid.gpuid.bvnc");

	/* String longer than PVR_GPUID_STRING_MAX_LENGTH. */
	GPUID_TEST_CASE_INVAL("12.34.56.789012345678901234567890123456");

	/* Single value overflowing u16. */
	GPUID_TEST_CASE_INVAL("12.34.56.999999");

	/* Wrong number of parts and/or dots. */
	GPUID_TEST_CASE_INVAL("12.34.56.78.90");
	GPUID_TEST_CASE_INVAL("12.34.56..78");
	GPUID_TEST_CASE_INVAL("12.34..56");
	GPUID_TEST_CASE_INVAL("12.34.56");

#undef GPUID_TEST_CASE_INVAL
#undef GPUID_TEST_CASE_OK
#undef GPUID_TEST_CASE
}

static struct kunit_case pvr_tests_cases[] = {
	KUNIT_CASE(decode_gpuid_string),
	{},
};

static struct kunit_suite pvr_tests_suite = {
	.name = "pvr_tests",
	.test_cases = pvr_tests_cases,
};
kunit_test_suite(pvr_tests_suite);

MODULE_AUTHOR("Imagination Technologies Ltd.");
MODULE_LICENSE("Dual MIT/GPL");
MODULE_DESCRIPTION("pvr kunit tests");
MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");