xref: /openbmc/linux/drivers/firewire/uapi-test.c (revision 6db6b729)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // uapi_test.c - An application of Kunit to check layout of structures exposed to user space for
4 //		 FireWire subsystem.
5 //
6 // Copyright (c) 2023 Takashi Sakamoto
7 
8 #include <kunit/test.h>
9 #include <linux/firewire-cdev.h>
10 
11 // Known issue added at v2.6.27 kernel.
12 static void structure_layout_event_response(struct kunit *test)
13 {
14 #if defined(CONFIG_X86_32)
15 	// 4 bytes alignment for aggregate type including 8 bytes storage types.
16 	KUNIT_EXPECT_EQ(test, 20, sizeof(struct fw_cdev_event_response));
17 #else
18 	// 8 bytes alignment for aggregate type including 8 bytes storage types.
19 	KUNIT_EXPECT_EQ(test, 24, sizeof(struct fw_cdev_event_response));
20 #endif
21 
22 	KUNIT_EXPECT_EQ(test, 0, offsetof(struct fw_cdev_event_response, closure));
23 	KUNIT_EXPECT_EQ(test, 8, offsetof(struct fw_cdev_event_response, type));
24 	KUNIT_EXPECT_EQ(test, 12, offsetof(struct fw_cdev_event_response, rcode));
25 	KUNIT_EXPECT_EQ(test, 16, offsetof(struct fw_cdev_event_response, length));
26 	KUNIT_EXPECT_EQ(test, 20, offsetof(struct fw_cdev_event_response, data));
27 }
28 
29 // Added at v6.5.
30 static void structure_layout_event_request3(struct kunit *test)
31 {
32 	KUNIT_EXPECT_EQ(test, 56, sizeof(struct fw_cdev_event_request3));
33 
34 	KUNIT_EXPECT_EQ(test, 0, offsetof(struct fw_cdev_event_request3, closure));
35 	KUNIT_EXPECT_EQ(test, 8, offsetof(struct fw_cdev_event_request3, type));
36 	KUNIT_EXPECT_EQ(test, 12, offsetof(struct fw_cdev_event_request3, tcode));
37 	KUNIT_EXPECT_EQ(test, 16, offsetof(struct fw_cdev_event_request3, offset));
38 	KUNIT_EXPECT_EQ(test, 24, offsetof(struct fw_cdev_event_request3, source_node_id));
39 	KUNIT_EXPECT_EQ(test, 28, offsetof(struct fw_cdev_event_request3, destination_node_id));
40 	KUNIT_EXPECT_EQ(test, 32, offsetof(struct fw_cdev_event_request3, card));
41 	KUNIT_EXPECT_EQ(test, 36, offsetof(struct fw_cdev_event_request3, generation));
42 	KUNIT_EXPECT_EQ(test, 40, offsetof(struct fw_cdev_event_request3, handle));
43 	KUNIT_EXPECT_EQ(test, 44, offsetof(struct fw_cdev_event_request3, length));
44 	KUNIT_EXPECT_EQ(test, 48, offsetof(struct fw_cdev_event_request3, tstamp));
45 	KUNIT_EXPECT_EQ(test, 56, offsetof(struct fw_cdev_event_request3, data));
46 }
47 
48 // Added at v6.5.
49 static void structure_layout_event_response2(struct kunit *test)
50 {
51 	KUNIT_EXPECT_EQ(test, 32, sizeof(struct fw_cdev_event_response2));
52 
53 	KUNIT_EXPECT_EQ(test, 0, offsetof(struct fw_cdev_event_response2, closure));
54 	KUNIT_EXPECT_EQ(test, 8, offsetof(struct fw_cdev_event_response2, type));
55 	KUNIT_EXPECT_EQ(test, 12, offsetof(struct fw_cdev_event_response2, rcode));
56 	KUNIT_EXPECT_EQ(test, 16, offsetof(struct fw_cdev_event_response2, length));
57 	KUNIT_EXPECT_EQ(test, 20, offsetof(struct fw_cdev_event_response2, request_tstamp));
58 	KUNIT_EXPECT_EQ(test, 24, offsetof(struct fw_cdev_event_response2, response_tstamp));
59 	KUNIT_EXPECT_EQ(test, 32, offsetof(struct fw_cdev_event_response2, data));
60 }
61 
62 // Added at v6.5.
63 static void structure_layout_event_phy_packet2(struct kunit *test)
64 {
65 	KUNIT_EXPECT_EQ(test, 24, sizeof(struct fw_cdev_event_phy_packet2));
66 
67 	KUNIT_EXPECT_EQ(test, 0, offsetof(struct fw_cdev_event_phy_packet2, closure));
68 	KUNIT_EXPECT_EQ(test, 8, offsetof(struct fw_cdev_event_phy_packet2, type));
69 	KUNIT_EXPECT_EQ(test, 12, offsetof(struct fw_cdev_event_phy_packet2, rcode));
70 	KUNIT_EXPECT_EQ(test, 16, offsetof(struct fw_cdev_event_phy_packet2, length));
71 	KUNIT_EXPECT_EQ(test, 20, offsetof(struct fw_cdev_event_phy_packet2, tstamp));
72 	KUNIT_EXPECT_EQ(test, 24, offsetof(struct fw_cdev_event_phy_packet2, data));
73 }
74 
75 static struct kunit_case structure_layout_test_cases[] = {
76 	KUNIT_CASE(structure_layout_event_response),
77 	KUNIT_CASE(structure_layout_event_request3),
78 	KUNIT_CASE(structure_layout_event_response2),
79 	KUNIT_CASE(structure_layout_event_phy_packet2),
80 	{}
81 };
82 
83 static struct kunit_suite structure_layout_test_suite = {
84 	.name = "firewire-uapi-structure-layout",
85 	.test_cases = structure_layout_test_cases,
86 };
87 kunit_test_suite(structure_layout_test_suite);
88 
89 MODULE_LICENSE("GPL");
90