John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame^] | 1 | #include <string.h> |
| 2 | |
| 3 | #include <cstring> |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "libpldm/base.h" |
| 7 | #include "libpldm/bios.h" |
| 8 | #include "libpldm/bios_table.h" |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | using Table = std::vector<uint8_t>; |
| 13 | |
| 14 | void buildTable(Table& table) |
| 15 | { |
| 16 | auto padSize = ((table.size() % 4) ? (4 - table.size() % 4) : 0); |
| 17 | table.insert(table.end(), padSize, 0); |
| 18 | table.insert(table.end(), sizeof(uint32_t) /*checksum*/, 0); |
| 19 | } |
| 20 | |
| 21 | template <typename First, typename... Rest> |
| 22 | void buildTable(Table& table, First& first, Rest&... rest) |
| 23 | { |
| 24 | table.insert(table.end(), first.begin(), first.end()); |
| 25 | buildTable(table, rest...); |
| 26 | } |
| 27 | |
| 28 | TEST(AttrTable, EnumEntryDecodeTest) |
| 29 | { |
| 30 | std::vector<uint8_t> enumEntry{ |
| 31 | 0, 0, /* attr handle */ |
| 32 | 0, /* attr type */ |
| 33 | 1, 0, /* attr name handle */ |
| 34 | 2, /* number of possible value */ |
| 35 | 2, 0, /* possible value handle */ |
| 36 | 3, 0, /* possible value handle */ |
| 37 | 1, /* number of default value */ |
| 38 | 0 /* defaut value string handle index */ |
| 39 | }; |
| 40 | |
| 41 | auto entry = |
| 42 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data()); |
| 43 | uint8_t pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 44 | EXPECT_EQ(pvNumber, 2); |
| 45 | uint8_t defNumber = pldm_bios_table_attr_entry_enum_decode_def_num(entry); |
| 46 | EXPECT_EQ(defNumber, 1); |
| 47 | |
| 48 | pvNumber = 0; |
| 49 | auto rc = |
| 50 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 51 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 52 | EXPECT_EQ(pvNumber, 2); |
| 53 | defNumber = 0; |
| 54 | rc = |
| 55 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 56 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 57 | EXPECT_EQ(defNumber, 1); |
| 58 | |
| 59 | rc = |
| 60 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(nullptr, &pvNumber); |
| 61 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 62 | rc = pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, nullptr); |
| 63 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 64 | |
| 65 | entry->attr_type = PLDM_BIOS_STRING; |
| 66 | rc = pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 67 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 68 | |
| 69 | rc = |
| 70 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 71 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 72 | } |
| 73 | |
| 74 | TEST(AttrTable, StringEntryDecodeTest) |
| 75 | { |
| 76 | std::vector<uint8_t> stringEntry{ |
| 77 | 1, 0, /* attr handle */ |
| 78 | 1, /* attr type */ |
| 79 | 12, 0, /* attr name handle */ |
| 80 | 1, /* string type */ |
| 81 | 1, 0, /* minimum length of the string in bytes */ |
| 82 | 100, 0, /* maximum length of the string in bytes */ |
| 83 | 3, 0, /* length of default string in length */ |
| 84 | 'a', 'b', 'c' /* default string */ |
| 85 | }; |
| 86 | |
| 87 | auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>( |
| 88 | stringEntry.data()); |
| 89 | uint16_t def_string_length = |
| 90 | pldm_bios_table_attr_entry_string_decode_def_string_length(entry); |
| 91 | EXPECT_EQ(def_string_length, 3); |
| 92 | |
| 93 | def_string_length = 0; |
| 94 | auto rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 95 | entry, &def_string_length); |
| 96 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 97 | EXPECT_EQ(def_string_length, 3); |
| 98 | |
| 99 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 100 | entry, nullptr); |
| 101 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 102 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 103 | nullptr, &def_string_length); |
| 104 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 105 | entry->attr_type = PLDM_BIOS_INTEGER; |
| 106 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 107 | entry, &def_string_length); |
| 108 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 109 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 110 | nullptr, &def_string_length); |
| 111 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 112 | } |
| 113 | |
| 114 | TEST(AttrTable, ItearatorTest) |
| 115 | { |
| 116 | std::vector<uint8_t> enumEntry{ |
| 117 | 0, 0, /* attr handle */ |
| 118 | 0, /* attr type */ |
| 119 | 1, 0, /* attr name handle */ |
| 120 | 2, /* number of possible value */ |
| 121 | 2, 0, /* possible value handle */ |
| 122 | 3, 0, /* possible value handle */ |
| 123 | 1, /* number of default value */ |
| 124 | 0 /* defaut value string handle index */ |
| 125 | }; |
| 126 | std::vector<uint8_t> stringEntry{ |
| 127 | 1, 0, /* attr handle */ |
| 128 | 1, /* attr type */ |
| 129 | 12, 0, /* attr name handle */ |
| 130 | 1, /* string type */ |
| 131 | 1, 0, /* minimum length of the string in bytes */ |
| 132 | 100, 0, /* maximum length of the string in bytes */ |
| 133 | 3, 0, /* length of default string in length */ |
| 134 | 'a', 'b', 'c' /* default string */ |
| 135 | }; |
| 136 | |
| 137 | Table table; |
| 138 | buildTable(table, enumEntry, stringEntry, enumEntry); |
| 139 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 140 | PLDM_BIOS_ATTR_TABLE); |
| 141 | auto entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 142 | auto rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 143 | EXPECT_EQ(rc, 0); |
| 144 | |
| 145 | pldm_bios_table_iter_next(iter); |
| 146 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 147 | rc = std::memcmp(entry, stringEntry.data(), stringEntry.size()); |
| 148 | EXPECT_EQ(rc, 0); |
| 149 | |
| 150 | pldm_bios_table_iter_next(iter); |
| 151 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 152 | rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 153 | EXPECT_EQ(rc, 0); |
| 154 | |
| 155 | pldm_bios_table_iter_next(iter); |
| 156 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 157 | pldm_bios_table_iter_free(iter); |
| 158 | } |
| 159 | |
| 160 | TEST(Itearator, DeathTest) |
| 161 | { |
| 162 | |
| 163 | Table table(256, 0); |
| 164 | |
| 165 | /* first entry */ |
| 166 | auto attr_entry = |
| 167 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data()); |
| 168 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 169 | PLDM_BIOS_ATTR_TABLE); |
| 170 | attr_entry->attr_type = PLDM_BIOS_PASSWORD; |
| 171 | EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL"); |
| 172 | attr_entry->attr_type = PLDM_BIOS_INTEGER; |
| 173 | EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL"); |
| 174 | pldm_bios_table_iter_free(iter); |
| 175 | } |