1 // SPDX-License-Identifier: GPL-2.0-or-later 2 // 3 // self-id-sequence-helper-test.c - An application of Kunit to test helpers of self ID sequence. 4 // 5 // Copyright (c) 2024 Takashi Sakamoto 6 7 #include <kunit/test.h> 8 9 #include "phy-packet-definitions.h" 10 11 static void test_self_id_sequence_enumerator_valid(struct kunit *test) 12 { 13 static const u32 valid_sequences[] = { 14 0x00000000, 15 0x00000001, 0x00800000, 16 0x00000001, 0x00800001, 0x00900000, 17 0x00000000, 18 }; 19 struct self_id_sequence_enumerator enumerator; 20 const u32 *entry; 21 unsigned int quadlet_count; 22 23 enumerator.cursor = valid_sequences; 24 enumerator.quadlet_count = ARRAY_SIZE(valid_sequences); 25 26 entry = self_id_sequence_enumerator_next(&enumerator, &quadlet_count); 27 KUNIT_EXPECT_PTR_EQ(test, entry, &valid_sequences[0]); 28 KUNIT_EXPECT_EQ(test, quadlet_count, 1); 29 KUNIT_EXPECT_EQ(test, enumerator.quadlet_count, 6); 30 31 entry = self_id_sequence_enumerator_next(&enumerator, &quadlet_count); 32 KUNIT_EXPECT_PTR_EQ(test, entry, &valid_sequences[1]); 33 KUNIT_EXPECT_EQ(test, quadlet_count, 2); 34 KUNIT_EXPECT_EQ(test, enumerator.quadlet_count, 4); 35 36 entry = self_id_sequence_enumerator_next(&enumerator, &quadlet_count); 37 KUNIT_EXPECT_PTR_EQ(test, entry, &valid_sequences[3]); 38 KUNIT_EXPECT_EQ(test, quadlet_count, 3); 39 KUNIT_EXPECT_EQ(test, enumerator.quadlet_count, 1); 40 41 entry = self_id_sequence_enumerator_next(&enumerator, &quadlet_count); 42 KUNIT_EXPECT_PTR_EQ(test, entry, &valid_sequences[6]); 43 KUNIT_EXPECT_EQ(test, quadlet_count, 1); 44 KUNIT_EXPECT_EQ(test, enumerator.quadlet_count, 0); 45 46 entry = self_id_sequence_enumerator_next(&enumerator, &quadlet_count); 47 KUNIT_EXPECT_EQ(test, PTR_ERR(entry), -ENODATA); 48 } 49 50 static void test_self_id_sequence_enumerator_invalid(struct kunit *test) 51 { 52 static const u32 invalid_sequences[] = { 53 0x00000001, 54 }; 55 struct self_id_sequence_enumerator enumerator; 56 const u32 *entry; 57 unsigned int count; 58 59 enumerator.cursor = invalid_sequences; 60 enumerator.quadlet_count = ARRAY_SIZE(invalid_sequences); 61 62 entry = self_id_sequence_enumerator_next(&enumerator, &count); 63 KUNIT_EXPECT_EQ(test, PTR_ERR(entry), -EPROTO); 64 } 65 66 static void test_self_id_sequence_get_port_status(struct kunit *test) 67 { 68 static const u32 expected[] = { 69 0x000000e5, 70 0x00839e79, 71 0x0091e79d, 72 0x00a279e4, 73 }; 74 u32 quadlets [] = { 75 0x00000001, 76 0x00800001, 77 0x00900001, 78 0x00a00000, 79 }; 80 enum phy_packet_self_id_port_status port_status[28]; 81 unsigned int port_capacity; 82 unsigned int port_index; 83 84 KUNIT_ASSERT_EQ(test, ARRAY_SIZE(expected), ARRAY_SIZE(quadlets)); 85 86 // With an extra port. 87 port_capacity = self_id_sequence_get_port_capacity(ARRAY_SIZE(expected)) + 1; 88 KUNIT_ASSERT_EQ(test, port_capacity, ARRAY_SIZE(port_status)); 89 90 for (port_index = 0; port_index < port_capacity; ++port_index) { 91 port_status[port_index] = 92 self_id_sequence_get_port_status(expected, ARRAY_SIZE(expected), port_index); 93 self_id_sequence_set_port_status(quadlets, ARRAY_SIZE(quadlets), port_index, 94 port_status[port_index]); 95 } 96 97 // Self ID zero. 98 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[0]); 99 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[1]); 100 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[2]); 101 102 // Self ID one. 103 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[3]); 104 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[4]); 105 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[5]); 106 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[6]); 107 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[7]); 108 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[8]); 109 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[9]); 110 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[10]); 111 112 // Self ID two. 113 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[11]); 114 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[12]); 115 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[13]); 116 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[14]); 117 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[15]); 118 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[16]); 119 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[17]); 120 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[18]); 121 122 // Self ID three. 123 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[19]); 124 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[20]); 125 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[21]); 126 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[22]); 127 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[23]); 128 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_CHILD, port_status[24]); 129 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_PARENT, port_status[25]); 130 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NCONN, port_status[26]); 131 132 // Our of order. 133 KUNIT_EXPECT_EQ(test, PHY_PACKET_SELF_ID_PORT_STATUS_NONE, port_status[27]); 134 135 KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected)); 136 } 137 138 static struct kunit_case self_id_sequence_helper_test_cases[] = { 139 KUNIT_CASE(test_self_id_sequence_enumerator_valid), 140 KUNIT_CASE(test_self_id_sequence_enumerator_invalid), 141 KUNIT_CASE(test_self_id_sequence_get_port_status), 142 {} 143 }; 144 145 static struct kunit_suite self_id_sequence_helper_test_suite = { 146 .name = "self-id-sequence-helper", 147 .test_cases = self_id_sequence_helper_test_cases, 148 }; 149 kunit_test_suite(self_id_sequence_helper_test_suite); 150 151 MODULE_DESCRIPTION("Unit test suite for helpers of self ID sequence"); 152 MODULE_LICENSE("GPL"); 153