John Wang | 871c927 | 2019-12-09 18:02:15 +0800 | [diff] [blame] | 1 | #include <endian.h> |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 2 | #include <string.h> |
| 3 | |
| 4 | #include <cstring> |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 5 | #include <string> |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 6 | #include <utility> |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
| 9 | #include "libpldm/base.h" |
| 10 | #include "libpldm/bios.h" |
| 11 | #include "libpldm/bios_table.h" |
John Wang | 871c927 | 2019-12-09 18:02:15 +0800 | [diff] [blame] | 12 | #include "libpldm/utils.h" |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 13 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
| 16 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 17 | using testing::ElementsAreArray; |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 18 | using Table = std::vector<uint8_t>; |
| 19 | |
| 20 | void buildTable(Table& table) |
| 21 | { |
| 22 | auto padSize = ((table.size() % 4) ? (4 - table.size() % 4) : 0); |
| 23 | table.insert(table.end(), padSize, 0); |
John Wang | 871c927 | 2019-12-09 18:02:15 +0800 | [diff] [blame] | 24 | uint32_t checksum = crc32(table.data(), table.size()); |
| 25 | checksum = htole32(checksum); |
| 26 | uint8_t a[4]; |
| 27 | std::memcpy(a, &checksum, sizeof(checksum)); |
| 28 | table.insert(table.end(), std::begin(a), std::end(a)); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | template <typename First, typename... Rest> |
| 32 | void buildTable(Table& table, First& first, Rest&... rest) |
| 33 | { |
| 34 | table.insert(table.end(), first.begin(), first.end()); |
| 35 | buildTable(table, rest...); |
| 36 | } |
| 37 | |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 38 | TEST(AttrTable, HeaderDecodeTest) |
| 39 | { |
| 40 | std::vector<uint8_t> enumEntry{ |
| 41 | 2, 0, /* attr handle */ |
| 42 | 0, /* attr type */ |
| 43 | 1, 0, /* attr name handle (string handle) */ |
| 44 | 2, /* number of possible value */ |
| 45 | 2, 0, /* possible value handle */ |
| 46 | 3, 0, /* possible value handle */ |
| 47 | 1, /* number of default value */ |
| 48 | 0 /* defaut value string handle index */ |
| 49 | }; |
| 50 | auto entry = |
| 51 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data()); |
| 52 | auto attrHandle = pldm_bios_table_attr_entry_decode_attribute_handle(entry); |
| 53 | EXPECT_EQ(attrHandle, 2); |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 54 | auto attrType = pldm_bios_table_attr_entry_decode_attribute_type(entry); |
| 55 | EXPECT_EQ(attrType, 0); |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 56 | auto stringHandle = pldm_bios_table_attr_entry_decode_string_handle(entry); |
| 57 | EXPECT_EQ(stringHandle, 1); |
| 58 | } |
| 59 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 60 | TEST(AttrTable, EnumEntryDecodeTest) |
| 61 | { |
| 62 | std::vector<uint8_t> enumEntry{ |
| 63 | 0, 0, /* attr handle */ |
| 64 | 0, /* attr type */ |
| 65 | 1, 0, /* attr name handle */ |
| 66 | 2, /* number of possible value */ |
| 67 | 2, 0, /* possible value handle */ |
| 68 | 3, 0, /* possible value handle */ |
| 69 | 1, /* number of default value */ |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 70 | 1 /* defaut value string handle index */ |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | auto entry = |
| 74 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data()); |
| 75 | uint8_t pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 76 | EXPECT_EQ(pvNumber, 2); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 77 | pvNumber = 0; |
| 78 | auto rc = |
| 79 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 80 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 81 | EXPECT_EQ(pvNumber, 2); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 82 | |
| 83 | std::vector<uint16_t> pvHandles(pvNumber, 0); |
| 84 | pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_hdls( |
| 85 | entry, pvHandles.data(), pvHandles.size()); |
| 86 | EXPECT_EQ(pvNumber, 2); |
| 87 | EXPECT_EQ(pvHandles[0], 2); |
| 88 | EXPECT_EQ(pvHandles[1], 3); |
| 89 | pvHandles.resize(1); |
| 90 | pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_hdls( |
| 91 | entry, pvHandles.data(), pvHandles.size()); |
| 92 | EXPECT_EQ(pvNumber, 1); |
| 93 | EXPECT_EQ(pvHandles[0], 2); |
| 94 | |
| 95 | pvHandles.resize(2); |
| 96 | rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check( |
| 97 | entry, pvHandles.data(), pvHandles.size()); |
| 98 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 99 | EXPECT_EQ(pvHandles[0], 2); |
| 100 | EXPECT_EQ(pvHandles[1], 3); |
| 101 | rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check( |
| 102 | entry, pvHandles.data(), 1); |
| 103 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 104 | |
| 105 | uint8_t defNumber = pldm_bios_table_attr_entry_enum_decode_def_num(entry); |
| 106 | EXPECT_EQ(defNumber, 1); |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 107 | std::vector<uint8_t> defIndices(defNumber); |
| 108 | rc = pldm_bios_table_attr_entry_enum_decode_def_indices( |
| 109 | entry, defIndices.data(), defIndices.size()); |
| 110 | EXPECT_EQ(rc, defNumber); |
| 111 | EXPECT_THAT(defIndices, ElementsAreArray({1})); |
| 112 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 113 | defNumber = 0; |
| 114 | rc = |
| 115 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 116 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 117 | EXPECT_EQ(defNumber, 1); |
| 118 | |
| 119 | rc = |
| 120 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(nullptr, &pvNumber); |
| 121 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 122 | rc = pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, nullptr); |
| 123 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 124 | |
| 125 | entry->attr_type = PLDM_BIOS_STRING; |
| 126 | rc = pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 127 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 128 | |
| 129 | rc = |
| 130 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 131 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 132 | rc = |
| 133 | pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(entry, nullptr, 0); |
| 134 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 135 | } |
| 136 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 137 | TEST(AttrTable, EnumEntryEncodeTest) |
| 138 | { |
| 139 | std::vector<uint8_t> enumEntry{ |
| 140 | 0, 0, /* attr handle */ |
| 141 | 0, /* attr type */ |
| 142 | 1, 0, /* attr name handle */ |
| 143 | 2, /* number of possible value */ |
| 144 | 2, 0, /* possible value handle */ |
| 145 | 3, 0, /* possible value handle */ |
| 146 | 1, /* number of default value */ |
| 147 | 0 /* defaut value string handle index */ |
| 148 | }; |
| 149 | |
| 150 | std::vector<uint16_t> pv_hdls{2, 3}; |
| 151 | std::vector<uint8_t> defs{0}; |
| 152 | |
| 153 | struct pldm_bios_table_attr_entry_enum_info info = { |
| 154 | 1, /* name handle */ |
| 155 | false, /* read only */ |
| 156 | 2, /* pv number */ |
| 157 | pv_hdls.data(), /* pv handle */ |
| 158 | 1, /*def number */ |
| 159 | defs.data() /*def index*/ |
| 160 | }; |
| 161 | auto encodeLength = pldm_bios_table_attr_entry_enum_encode_length(2, 1); |
| 162 | EXPECT_EQ(encodeLength, enumEntry.size()); |
| 163 | |
| 164 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 165 | pldm_bios_table_attr_entry_enum_encode(encodeEntry.data(), |
| 166 | encodeEntry.size(), &info); |
| 167 | // set attr handle = 0 |
| 168 | encodeEntry[0] = 0; |
| 169 | encodeEntry[1] = 0; |
| 170 | |
| 171 | EXPECT_EQ(enumEntry, encodeEntry); |
| 172 | |
| 173 | EXPECT_DEATH(pldm_bios_table_attr_entry_enum_encode( |
| 174 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 175 | "length <= entry_length"); |
| 176 | auto rc = pldm_bios_table_attr_entry_enum_encode_check( |
| 177 | encodeEntry.data(), encodeEntry.size(), &info); |
| 178 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 179 | // set attr handle = 0 |
| 180 | encodeEntry[0] = 0; |
| 181 | encodeEntry[1] = 0; |
| 182 | |
| 183 | EXPECT_EQ(enumEntry, encodeEntry); |
| 184 | rc = pldm_bios_table_attr_entry_enum_encode_check( |
| 185 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 186 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 187 | } |
| 188 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 189 | TEST(AttrTable, StringEntryDecodeTest) |
| 190 | { |
| 191 | std::vector<uint8_t> stringEntry{ |
| 192 | 1, 0, /* attr handle */ |
| 193 | 1, /* attr type */ |
| 194 | 12, 0, /* attr name handle */ |
| 195 | 1, /* string type */ |
| 196 | 1, 0, /* minimum length of the string in bytes */ |
| 197 | 100, 0, /* maximum length of the string in bytes */ |
| 198 | 3, 0, /* length of default string in length */ |
| 199 | 'a', 'b', 'c' /* default string */ |
| 200 | }; |
| 201 | |
| 202 | auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>( |
| 203 | stringEntry.data()); |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 204 | auto stringType = |
| 205 | pldm_bios_table_attr_entry_string_decode_string_type(entry); |
| 206 | EXPECT_EQ(stringType, 1); |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 207 | auto minLength = pldm_bios_table_attr_entry_string_decode_min_length(entry); |
| 208 | EXPECT_EQ(minLength, 1); |
| 209 | auto maxLength = pldm_bios_table_attr_entry_string_decode_max_length(entry); |
| 210 | EXPECT_EQ(maxLength, 100); |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 211 | |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 212 | uint16_t defStringLength = |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 213 | pldm_bios_table_attr_entry_string_decode_def_string_length(entry); |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 214 | EXPECT_EQ(defStringLength, 3); |
| 215 | std::vector<char> defString(defStringLength + 1); |
| 216 | auto rc = pldm_bios_table_attr_entry_string_decode_def_string( |
| 217 | entry, defString.data(), defString.size()); |
| 218 | EXPECT_EQ(rc, 3); |
| 219 | EXPECT_STREQ(defString.data(), "abc"); |
| 220 | rc = pldm_bios_table_attr_entry_string_decode_def_string( |
| 221 | entry, defString.data(), defString.size() - 1); |
| 222 | EXPECT_EQ(rc, 2); |
| 223 | EXPECT_STREQ(defString.data(), "ab"); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 224 | |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 225 | defStringLength = 0; |
| 226 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 227 | entry, &defStringLength); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 228 | EXPECT_EQ(rc, PLDM_SUCCESS); |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 229 | EXPECT_EQ(defStringLength, 3); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 230 | |
| 231 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 232 | entry, nullptr); |
| 233 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 234 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 235 | nullptr, &defStringLength); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 236 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 237 | entry->attr_type = PLDM_BIOS_INTEGER; |
| 238 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 239 | entry, &defStringLength); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 240 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 241 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 242 | nullptr, &defStringLength); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 243 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 244 | } |
| 245 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 246 | TEST(AttrTable, StringEntryEncodeTest) |
| 247 | { |
| 248 | std::vector<uint8_t> stringEntry{ |
| 249 | 0, 0, /* attr handle */ |
| 250 | 1, /* attr type */ |
| 251 | 3, 0, /* attr name handle */ |
| 252 | 1, /* string type */ |
| 253 | 1, 0, /* min string length */ |
| 254 | 100, 0, /* max string length */ |
| 255 | 3, 0, /* default string length */ |
| 256 | 'a', 'b', 'c', /* defaul string */ |
| 257 | }; |
| 258 | |
| 259 | struct pldm_bios_table_attr_entry_string_info info = { |
| 260 | 3, /* name handle */ |
| 261 | false, /* read only */ |
| 262 | 1, /* string type ascii */ |
| 263 | 1, /* min length */ |
| 264 | 100, /* max length */ |
| 265 | 3, /* def length */ |
| 266 | "abc", /* def string */ |
| 267 | }; |
| 268 | auto encodeLength = pldm_bios_table_attr_entry_string_encode_length(3); |
| 269 | EXPECT_EQ(encodeLength, stringEntry.size()); |
| 270 | |
| 271 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 272 | pldm_bios_table_attr_entry_string_encode(encodeEntry.data(), |
| 273 | encodeEntry.size(), &info); |
| 274 | // set attr handle = 0 |
| 275 | encodeEntry[0] = 0; |
| 276 | encodeEntry[1] = 0; |
| 277 | |
| 278 | EXPECT_EQ(stringEntry, encodeEntry); |
| 279 | |
| 280 | EXPECT_DEATH(pldm_bios_table_attr_entry_string_encode( |
| 281 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 282 | "length <= entry_length"); |
| 283 | auto rc = pldm_bios_table_attr_entry_string_encode_check( |
| 284 | encodeEntry.data(), encodeEntry.size(), &info); |
| 285 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 286 | // set attr handle = 0 |
| 287 | encodeEntry[0] = 0; |
| 288 | encodeEntry[1] = 0; |
| 289 | |
| 290 | EXPECT_EQ(stringEntry, encodeEntry); |
| 291 | rc = pldm_bios_table_attr_entry_string_encode_check( |
| 292 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 293 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 294 | std::swap(info.max_length, info.min_length); |
John Wang | 827c5de | 2019-11-07 18:27:27 +0800 | [diff] [blame] | 295 | const char* errmsg; |
| 296 | rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg); |
| 297 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 298 | EXPECT_STREQ( |
| 299 | "MinimumStingLength should not be greater than MaximumStringLength", |
| 300 | errmsg); |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 301 | rc = pldm_bios_table_attr_entry_string_encode_check( |
| 302 | encodeEntry.data(), encodeEntry.size(), &info); |
| 303 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 304 | std::swap(info.max_length, info.min_length); |
| 305 | |
| 306 | std::vector<uint8_t> stringEntryLength0{ |
| 307 | 0, 0, /* attr handle */ |
| 308 | 1, /* attr type */ |
| 309 | 3, 0, /* attr name handle */ |
| 310 | 1, /* string type */ |
| 311 | 1, 0, /* min string length */ |
| 312 | 100, 0, /* max string length */ |
| 313 | 0, 0, /* default string length */ |
| 314 | }; |
| 315 | |
| 316 | info.def_length = 0; |
| 317 | info.def_string = nullptr; |
| 318 | |
| 319 | encodeLength = pldm_bios_table_attr_entry_string_encode_length(0); |
| 320 | EXPECT_EQ(encodeLength, stringEntryLength0.size()); |
| 321 | |
| 322 | encodeEntry.resize(encodeLength); |
| 323 | pldm_bios_table_attr_entry_string_encode(encodeEntry.data(), |
| 324 | encodeEntry.size(), &info); |
| 325 | // set attr handle = 0 |
| 326 | encodeEntry[0] = 0; |
| 327 | encodeEntry[1] = 0; |
| 328 | |
| 329 | EXPECT_EQ(stringEntryLength0, encodeEntry); |
| 330 | } |
| 331 | |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 332 | TEST(AttrTable, integerEntryEncodeTest) |
| 333 | { |
| 334 | std::vector<uint8_t> integerEntry{ |
| 335 | 0, 0, /* attr handle */ |
| 336 | 3, /* attr type */ |
| 337 | 1, 0, /* attr name handle */ |
| 338 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 339 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 340 | 2, 0, 0, 0, /* scalar increment */ |
| 341 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 342 | }; |
| 343 | |
| 344 | std::vector<uint16_t> pv_hdls{2, 3}; |
| 345 | std::vector<uint8_t> defs{0}; |
| 346 | |
| 347 | struct pldm_bios_table_attr_entry_integer_info info = { |
| 348 | 1, /* name handle */ |
| 349 | false, /* read only */ |
| 350 | 1, /* lower bound */ |
| 351 | 10, /* upper bound */ |
| 352 | 2, /* sacalar increment */ |
| 353 | 3 /* default value */ |
| 354 | }; |
| 355 | auto encodeLength = pldm_bios_table_attr_entry_integer_encode_length(); |
| 356 | EXPECT_EQ(encodeLength, integerEntry.size()); |
| 357 | |
| 358 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 359 | pldm_bios_table_attr_entry_integer_encode(encodeEntry.data(), |
| 360 | encodeEntry.size(), &info); |
| 361 | // set attr handle = 0 |
| 362 | encodeEntry[0] = 0; |
| 363 | encodeEntry[1] = 0; |
| 364 | |
| 365 | EXPECT_EQ(integerEntry, encodeEntry); |
| 366 | |
| 367 | EXPECT_DEATH(pldm_bios_table_attr_entry_integer_encode( |
| 368 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 369 | "length <= entry_length"); |
| 370 | |
| 371 | auto rc = pldm_bios_table_attr_entry_integer_encode_check( |
| 372 | encodeEntry.data(), encodeEntry.size(), &info); |
| 373 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 374 | // set attr handle = 0 |
| 375 | encodeEntry[0] = 0; |
| 376 | encodeEntry[1] = 0; |
| 377 | |
| 378 | EXPECT_EQ(integerEntry, encodeEntry); |
| 379 | |
| 380 | rc = pldm_bios_table_attr_entry_integer_encode_check( |
| 381 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 382 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 383 | |
| 384 | info.lower_bound = 100; |
| 385 | info.upper_bound = 50; |
John Wang | 827c5de | 2019-11-07 18:27:27 +0800 | [diff] [blame] | 386 | const char* errmsg; |
| 387 | rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg); |
| 388 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 389 | EXPECT_STREQ("LowerBound should not be greater than UpperBound", errmsg); |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 390 | rc = pldm_bios_table_attr_entry_integer_encode_check( |
| 391 | encodeEntry.data(), encodeEntry.size(), &info); |
| 392 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 393 | } |
| 394 | |
John Wang | 50218a6 | 2020-02-12 17:10:28 +0800 | [diff] [blame^] | 395 | TEST(AttrTable, integerEntryDecodeTest) |
| 396 | { |
| 397 | std::vector<uint8_t> integerEntry{ |
| 398 | 0, 0, /* attr handle */ |
| 399 | 3, /* attr type */ |
| 400 | 1, 0, /* attr name handle */ |
| 401 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 402 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 403 | 2, 0, 0, 0, /* scalar increment */ |
| 404 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 405 | }; |
| 406 | |
| 407 | uint64_t lower, upper, def; |
| 408 | uint32_t scalar; |
| 409 | auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>( |
| 410 | integerEntry.data()); |
| 411 | pldm_bios_table_attr_entry_integer_decode(entry, &lower, &upper, &scalar, |
| 412 | &def); |
| 413 | EXPECT_EQ(lower, 1); |
| 414 | EXPECT_EQ(upper, 10); |
| 415 | EXPECT_EQ(scalar, 2); |
| 416 | EXPECT_EQ(def, 3); |
| 417 | } |
| 418 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 419 | TEST(AttrTable, ItearatorTest) |
| 420 | { |
| 421 | std::vector<uint8_t> enumEntry{ |
| 422 | 0, 0, /* attr handle */ |
| 423 | 0, /* attr type */ |
| 424 | 1, 0, /* attr name handle */ |
| 425 | 2, /* number of possible value */ |
| 426 | 2, 0, /* possible value handle */ |
| 427 | 3, 0, /* possible value handle */ |
| 428 | 1, /* number of default value */ |
| 429 | 0 /* defaut value string handle index */ |
| 430 | }; |
| 431 | std::vector<uint8_t> stringEntry{ |
| 432 | 1, 0, /* attr handle */ |
| 433 | 1, /* attr type */ |
| 434 | 12, 0, /* attr name handle */ |
| 435 | 1, /* string type */ |
| 436 | 1, 0, /* minimum length of the string in bytes */ |
| 437 | 100, 0, /* maximum length of the string in bytes */ |
| 438 | 3, 0, /* length of default string in length */ |
| 439 | 'a', 'b', 'c' /* default string */ |
| 440 | }; |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 441 | std::vector<uint8_t> integerEntry{ |
| 442 | 0, 0, /* attr handle */ |
| 443 | 3, /* attr type */ |
| 444 | 1, 0, /* attr name handle */ |
| 445 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 446 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 447 | 2, 0, 0, 0, /* scalar increment */ |
| 448 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 449 | }; |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 450 | |
| 451 | Table table; |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 452 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 453 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 454 | PLDM_BIOS_ATTR_TABLE); |
| 455 | auto entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 456 | auto rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 457 | EXPECT_EQ(rc, 0); |
| 458 | |
| 459 | pldm_bios_table_iter_next(iter); |
| 460 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 461 | rc = std::memcmp(entry, stringEntry.data(), stringEntry.size()); |
| 462 | EXPECT_EQ(rc, 0); |
| 463 | |
| 464 | pldm_bios_table_iter_next(iter); |
| 465 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 466 | rc = std::memcmp(entry, integerEntry.data(), integerEntry.size()); |
| 467 | EXPECT_EQ(rc, 0); |
| 468 | |
| 469 | pldm_bios_table_iter_next(iter); |
| 470 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 471 | rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 472 | EXPECT_EQ(rc, 0); |
| 473 | |
| 474 | pldm_bios_table_iter_next(iter); |
| 475 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 476 | pldm_bios_table_iter_free(iter); |
| 477 | } |
| 478 | |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 479 | TEST(AttrTable, FindTest) |
| 480 | { |
| 481 | std::vector<uint8_t> enumEntry{ |
| 482 | 0, 0, /* attr handle */ |
| 483 | 0, /* attr type */ |
| 484 | 1, 0, /* attr name handle */ |
| 485 | 2, /* number of possible value */ |
| 486 | 2, 0, /* possible value handle */ |
| 487 | 3, 0, /* possible value handle */ |
| 488 | 1, /* number of default value */ |
| 489 | 0 /* defaut value string handle index */ |
| 490 | }; |
| 491 | std::vector<uint8_t> stringEntry{ |
| 492 | 1, 0, /* attr handle */ |
| 493 | 1, /* attr type */ |
| 494 | 12, 0, /* attr name handle */ |
| 495 | 1, /* string type */ |
| 496 | 1, 0, /* minimum length of the string in bytes */ |
| 497 | 100, 0, /* maximum length of the string in bytes */ |
| 498 | 3, 0, /* length of default string in length */ |
| 499 | 'a', 'b', 'c' /* default string */ |
| 500 | }; |
| 501 | std::vector<uint8_t> integerEntry{ |
| 502 | 0, 0, /* attr handle */ |
| 503 | 3, /* attr type */ |
| 504 | 1, 0, /* attr name handle */ |
| 505 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 506 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 507 | 2, 0, 0, 0, /* scalar increment */ |
| 508 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 509 | }; |
| 510 | |
| 511 | Table table; |
| 512 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 513 | |
| 514 | auto entry = |
| 515 | pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 1); |
| 516 | EXPECT_NE(entry, nullptr); |
| 517 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 518 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 519 | ElementsAreArray(stringEntry)); |
| 520 | |
| 521 | entry = pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 3); |
| 522 | EXPECT_EQ(entry, nullptr); |
| 523 | } |
| 524 | |
| 525 | TEST(AttrValTable, HeaderDecodeTest) |
| 526 | { |
| 527 | std::vector<uint8_t> enumEntry{ |
| 528 | 1, 0, /* attr handle */ |
| 529 | 0, /* attr type */ |
| 530 | 2, /* number of current value */ |
| 531 | 0, /* current value string handle index */ |
| 532 | 1, /* current value string handle index */ |
| 533 | }; |
| 534 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 535 | enumEntry.data()); |
| 536 | auto attrHandle = |
| 537 | pldm_bios_table_attr_value_entry_decode_attribute_handle(entry); |
| 538 | EXPECT_EQ(attrHandle, 1); |
| 539 | auto attrType = |
| 540 | pldm_bios_table_attr_value_entry_decode_attribute_type(entry); |
| 541 | EXPECT_EQ(attrType, 0); |
| 542 | } |
| 543 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 544 | TEST(AttrValTable, EnumEntryEncodeTest) |
| 545 | { |
| 546 | std::vector<uint8_t> enumEntry{ |
| 547 | 0, 0, /* attr handle */ |
| 548 | 0, /* attr type */ |
| 549 | 2, /* number of current value */ |
| 550 | 0, /* current value string handle index */ |
| 551 | 1, /* current value string handle index */ |
| 552 | }; |
| 553 | |
| 554 | auto length = pldm_bios_table_attr_value_entry_encode_enum_length(2); |
| 555 | EXPECT_EQ(length, enumEntry.size()); |
| 556 | std::vector<uint8_t> encodeEntry(length, 0); |
| 557 | uint8_t handles[] = {0, 1}; |
| 558 | pldm_bios_table_attr_value_entry_encode_enum( |
| 559 | encodeEntry.data(), encodeEntry.size(), 0, 0, 2, handles); |
| 560 | EXPECT_EQ(encodeEntry, enumEntry); |
| 561 | |
| 562 | EXPECT_DEATH( |
| 563 | pldm_bios_table_attr_value_entry_encode_enum( |
| 564 | encodeEntry.data(), encodeEntry.size() - 1, 0, 0, 2, handles), |
| 565 | "length <= entry_length"); |
| 566 | |
| 567 | auto rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 568 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_ENUMERATION, 2, |
| 569 | handles); |
| 570 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 571 | EXPECT_EQ(encodeEntry, enumEntry); |
| 572 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 573 | enumEntry.data()); |
| 574 | entry->attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY; |
| 575 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 576 | encodeEntry.data(), encodeEntry.size(), 0, |
| 577 | PLDM_BIOS_ENUMERATION_READ_ONLY, 2, handles); |
| 578 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 579 | EXPECT_EQ(encodeEntry, enumEntry); |
| 580 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 581 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 2, |
| 582 | handles); |
| 583 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 584 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 585 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_ENUMERATION, 2, |
| 586 | handles); |
| 587 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 588 | } |
| 589 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 590 | TEST(AttrValTable, EnumEntryDecodeTest) |
| 591 | { |
| 592 | std::vector<uint8_t> enumEntry{ |
| 593 | 0, 0, /* attr handle */ |
| 594 | 0, /* attr type */ |
| 595 | 2, /* number of current value */ |
| 596 | 0, /* current value string handle index */ |
| 597 | 1, /* current value string handle index */ |
| 598 | }; |
| 599 | |
| 600 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 601 | enumEntry.data()); |
| 602 | auto number = pldm_bios_table_attr_value_entry_enum_decode_number(entry); |
| 603 | EXPECT_EQ(2, number); |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 604 | |
| 605 | std::vector<uint8_t> handles(2, 0); |
| 606 | auto rc = pldm_bios_table_attr_value_entry_enum_decode_handles( |
| 607 | entry, handles.data(), handles.size()); |
| 608 | EXPECT_EQ(rc, 2); |
| 609 | EXPECT_EQ(handles[0], 0); |
| 610 | EXPECT_EQ(handles[1], 1); |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 611 | } |
| 612 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 613 | TEST(AttrValTable, stringEntryEncodeTest) |
| 614 | { |
| 615 | std::vector<uint8_t> stringEntry{ |
| 616 | 0, 0, /* attr handle */ |
| 617 | 1, /* attr type */ |
| 618 | 3, 0, /* current string length */ |
| 619 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 620 | }; |
| 621 | |
| 622 | auto length = pldm_bios_table_attr_value_entry_encode_string_length(3); |
| 623 | EXPECT_EQ(length, stringEntry.size()); |
| 624 | std::vector<uint8_t> encodeEntry(length, 0); |
| 625 | pldm_bios_table_attr_value_entry_encode_string( |
| 626 | encodeEntry.data(), encodeEntry.size(), 0, 1, 3, "abc"); |
| 627 | EXPECT_EQ(encodeEntry, stringEntry); |
| 628 | |
| 629 | EXPECT_DEATH( |
| 630 | pldm_bios_table_attr_value_entry_encode_string( |
| 631 | encodeEntry.data(), encodeEntry.size() - 1, 0, 1, 3, "abc"), |
| 632 | "length <= entry_length"); |
| 633 | |
| 634 | auto rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 635 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING, 3, "abc"); |
| 636 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 637 | EXPECT_EQ(encodeEntry, stringEntry); |
| 638 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 639 | stringEntry.data()); |
| 640 | entry->attr_type = PLDM_BIOS_STRING_READ_ONLY; |
| 641 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 642 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING_READ_ONLY, |
| 643 | 3, "abc"); |
| 644 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 645 | EXPECT_EQ(encodeEntry, stringEntry); |
| 646 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 647 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 3, |
| 648 | "abc"); |
| 649 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 650 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 651 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_STRING, 3, |
| 652 | "abc"); |
| 653 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 654 | } |
| 655 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 656 | TEST(AttrValTable, StringEntryDecodeTest) |
| 657 | { |
| 658 | std::vector<uint8_t> stringEntry{ |
| 659 | 0, 0, /* attr handle */ |
| 660 | 1, /* attr type */ |
| 661 | 3, 0, /* current string length */ |
| 662 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 663 | }; |
| 664 | |
| 665 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 666 | stringEntry.data()); |
| 667 | auto length = pldm_bios_table_attr_value_entry_string_decode_length(entry); |
| 668 | EXPECT_EQ(3, length); |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 669 | |
| 670 | auto handle = pldm_bios_table_attr_value_entry_decode_handle(entry); |
| 671 | EXPECT_EQ(0, handle); |
| 672 | |
John Wang | 8e877e0 | 2020-02-03 16:06:55 +0800 | [diff] [blame] | 673 | auto entryLength = pldm_bios_table_attr_value_entry_length(entry); |
| 674 | EXPECT_EQ(stringEntry.size(), entryLength); |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 675 | |
| 676 | variable_field currentString{}; |
| 677 | pldm_bios_table_attr_value_entry_string_decode_string(entry, |
| 678 | ¤tString); |
| 679 | EXPECT_THAT(std::vector<uint8_t>(currentString.ptr, |
| 680 | currentString.ptr + currentString.length), |
| 681 | ElementsAreArray(std::vector<uint8_t>{'a', 'b', 'c'})); |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 682 | } |
| 683 | |
John Wang | ca23082 | 2019-10-16 11:39:27 +0800 | [diff] [blame] | 684 | TEST(AttrValTable, integerEntryEncodeTest) |
| 685 | { |
| 686 | std::vector<uint8_t> integerEntry{ |
| 687 | 0, 0, /* attr handle */ |
| 688 | 3, /* attr type */ |
| 689 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 690 | }; |
| 691 | |
| 692 | auto length = pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 693 | EXPECT_EQ(length, integerEntry.size()); |
| 694 | std::vector<uint8_t> encodeEntry(length, 0); |
| 695 | pldm_bios_table_attr_value_entry_encode_integer( |
| 696 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10); |
| 697 | EXPECT_EQ(encodeEntry, integerEntry); |
| 698 | |
| 699 | EXPECT_DEATH(pldm_bios_table_attr_value_entry_encode_integer( |
| 700 | encodeEntry.data(), encodeEntry.size() - 1, 0, |
| 701 | PLDM_BIOS_INTEGER, 10), |
| 702 | "length <= entry_length"); |
| 703 | |
| 704 | auto rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 705 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10); |
| 706 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 707 | EXPECT_EQ(encodeEntry, integerEntry); |
| 708 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 709 | integerEntry.data()); |
| 710 | entry->attr_type = PLDM_BIOS_INTEGER_READ_ONLY; |
| 711 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 712 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER_READ_ONLY, |
| 713 | 10); |
| 714 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 715 | EXPECT_EQ(encodeEntry, integerEntry); |
| 716 | |
| 717 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 718 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 10); |
| 719 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 720 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 721 | encodeEntry.data(), encodeEntry.size() - 1, 0, |
| 722 | PLDM_BIOS_INTEGER_READ_ONLY, 10); |
| 723 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 724 | } |
| 725 | |
John Wang | b0da7a0 | 2020-01-07 16:55:00 +0800 | [diff] [blame] | 726 | TEST(AttrValTable, integerEntryDecodeTest) |
| 727 | { |
| 728 | std::vector<uint8_t> integerEntry{ |
| 729 | 0, 0, /* attr handle */ |
| 730 | 3, /* attr type */ |
| 731 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 732 | }; |
| 733 | |
| 734 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 735 | integerEntry.data()); |
| 736 | auto cv = pldm_bios_table_attr_value_entry_integer_decode_cv(entry); |
| 737 | EXPECT_EQ(cv, 10); |
| 738 | } |
| 739 | |
John Wang | 49484a1 | 2019-12-02 14:21:53 +0800 | [diff] [blame] | 740 | TEST(AttrValTable, IteratorTest) |
| 741 | { |
| 742 | std::vector<uint8_t> enumEntry{ |
| 743 | 0, 0, /* attr handle */ |
| 744 | 0, /* attr type */ |
| 745 | 2, /* number of current value */ |
| 746 | 0, /* current value string handle index */ |
| 747 | 1, /* current value string handle index */ |
| 748 | }; |
| 749 | std::vector<uint8_t> stringEntry{ |
| 750 | 0, 0, /* attr handle */ |
| 751 | 1, /* attr type */ |
| 752 | 3, 0, /* current string length */ |
| 753 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 754 | }; |
| 755 | std::vector<uint8_t> integerEntry{ |
| 756 | 0, 0, /* attr handle */ |
| 757 | 3, /* attr type */ |
| 758 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 759 | }; |
| 760 | |
| 761 | Table table; |
| 762 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 763 | |
| 764 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 765 | PLDM_BIOS_ATTR_VAL_TABLE); |
| 766 | auto entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 767 | |
| 768 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 769 | EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()), |
| 770 | ElementsAreArray(enumEntry)); |
| 771 | |
| 772 | pldm_bios_table_iter_next(iter); |
| 773 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 774 | p = reinterpret_cast<const uint8_t*>(entry); |
| 775 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 776 | ElementsAreArray(stringEntry)); |
| 777 | |
| 778 | pldm_bios_table_iter_next(iter); |
| 779 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 780 | p = reinterpret_cast<const uint8_t*>(entry); |
| 781 | EXPECT_THAT(std::vector<uint8_t>(p, p + integerEntry.size()), |
| 782 | ElementsAreArray(integerEntry)); |
| 783 | |
| 784 | pldm_bios_table_iter_next(iter); |
| 785 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 786 | p = reinterpret_cast<const uint8_t*>(entry); |
| 787 | EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()), |
| 788 | ElementsAreArray(enumEntry)); |
| 789 | |
| 790 | pldm_bios_table_iter_next(iter); |
| 791 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 792 | |
| 793 | pldm_bios_table_iter_free(iter); |
| 794 | } |
| 795 | |
John Wang | 3342adb | 2019-11-29 16:03:58 +0800 | [diff] [blame] | 796 | TEST(AttrValTable, FindTest) |
| 797 | { |
| 798 | std::vector<uint8_t> enumEntry{ |
| 799 | 0, 0, /* attr handle */ |
| 800 | 0, /* attr type */ |
| 801 | 2, /* number of current value */ |
| 802 | 0, /* current value string handle index */ |
| 803 | 1, /* current value string handle index */ |
| 804 | }; |
| 805 | std::vector<uint8_t> stringEntry{ |
| 806 | 1, 0, /* attr handle */ |
| 807 | 1, /* attr type */ |
| 808 | 3, 0, /* current string length */ |
| 809 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 810 | }; |
| 811 | std::vector<uint8_t> integerEntry{ |
| 812 | 2, 0, /* attr handle */ |
| 813 | 3, /* attr type */ |
| 814 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 815 | }; |
| 816 | |
| 817 | Table table; |
| 818 | buildTable(table, enumEntry, stringEntry, integerEntry); |
| 819 | |
| 820 | auto entry = pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 821 | table.size(), 1); |
| 822 | EXPECT_NE(entry, nullptr); |
| 823 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 824 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 825 | ElementsAreArray(stringEntry)); |
| 826 | |
| 827 | entry = pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 828 | table.size(), 3); |
| 829 | EXPECT_EQ(entry, nullptr); |
| 830 | |
| 831 | auto firstEntry = |
| 832 | reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(table.data()); |
| 833 | firstEntry->attr_type = PLDM_BIOS_PASSWORD; |
| 834 | EXPECT_DEATH(pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 835 | table.size(), 1), |
| 836 | "entry_length != NULL"); |
| 837 | } |
| 838 | |
John Wang | 871c927 | 2019-12-09 18:02:15 +0800 | [diff] [blame] | 839 | TEST(AttrValTable, CopyAndUpdateTest) |
| 840 | { |
| 841 | std::vector<uint8_t> enumEntry{ |
| 842 | 0, 0, /* attr handle */ |
| 843 | 0, /* attr type */ |
| 844 | 2, /* number of current value */ |
| 845 | 0, /* current value string handle index */ |
| 846 | 1, /* current value string handle index */ |
| 847 | }; |
| 848 | std::vector<uint8_t> stringEntry{ |
| 849 | 1, 0, /* attr handle */ |
| 850 | 1, /* attr type */ |
| 851 | 3, 0, /* current string length */ |
| 852 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 853 | }; |
| 854 | std::vector<uint8_t> integerEntry{ |
| 855 | 2, 0, /* attr handle */ |
| 856 | 3, /* attr type */ |
| 857 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 858 | }; |
| 859 | |
| 860 | Table srcTable; |
| 861 | buildTable(srcTable, enumEntry, stringEntry, integerEntry); |
| 862 | |
| 863 | std::vector<uint8_t> stringEntry1{ |
| 864 | 1, 0, /* attr handle */ |
| 865 | 1, /* attr type */ |
| 866 | 3, 0, /* current string length */ |
| 867 | 'd', 'e', 'f', /* defaut value string handle index */ |
| 868 | }; |
| 869 | |
| 870 | Table expectTable; |
| 871 | buildTable(expectTable, enumEntry, stringEntry1, integerEntry); |
| 872 | Table destTable(expectTable.size() + 10); |
| 873 | auto destLength = destTable.size(); |
| 874 | auto rc = pldm_bios_table_attr_value_copy_and_update( |
| 875 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 876 | stringEntry1.data(), stringEntry1.size()); |
| 877 | |
| 878 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 879 | EXPECT_EQ(destLength, expectTable.size()); |
| 880 | destTable.resize(destLength); |
| 881 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 882 | |
| 883 | std::vector<uint8_t> stringEntry2{ |
| 884 | 1, 0, /* attr handle */ |
| 885 | 1, /* attr type */ |
| 886 | 5, 0, /* current string length */ |
| 887 | 'd', 'e', 'f', 'a', 'b', /* defaut value string handle index */ |
| 888 | }; |
| 889 | expectTable.resize(0); |
| 890 | buildTable(expectTable, enumEntry, stringEntry2, integerEntry); |
| 891 | destTable.resize(expectTable.size() + 10); |
| 892 | destLength = destTable.size(); |
| 893 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 894 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 895 | stringEntry2.data(), stringEntry2.size()); |
| 896 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 897 | EXPECT_EQ(destLength, expectTable.size()); |
| 898 | destTable.resize(destLength); |
| 899 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 900 | |
| 901 | std::vector<uint8_t> stringEntry3{ |
| 902 | 1, 0, /* attr handle */ |
| 903 | 1, /* attr type */ |
| 904 | 1, 0, /* current string length */ |
| 905 | 'd', /* defaut value string handle index */ |
| 906 | }; |
| 907 | expectTable.resize(0); |
| 908 | buildTable(expectTable, enumEntry, stringEntry3, integerEntry); |
| 909 | destTable.resize(expectTable.size() + 10); |
| 910 | destLength = destTable.size(); |
| 911 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 912 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 913 | stringEntry3.data(), stringEntry3.size()); |
| 914 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 915 | EXPECT_EQ(destLength, expectTable.size()); |
| 916 | destTable.resize(destLength); |
| 917 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 918 | |
| 919 | destTable.resize(expectTable.size() - 1); |
| 920 | destLength = destTable.size(); |
| 921 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 922 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 923 | stringEntry3.data(), stringEntry3.size()); |
| 924 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 925 | } |
| 926 | |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 927 | TEST(StringTable, EntryEncodeTest) |
| 928 | { |
| 929 | std::vector<uint8_t> stringEntry{ |
| 930 | 0, 0, /* string handle*/ |
| 931 | 7, 0, /* string length */ |
| 932 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 933 | }; |
| 934 | |
| 935 | const char* str = "Allowed"; |
| 936 | auto str_length = std::strlen(str); |
| 937 | auto encodeLength = pldm_bios_table_string_entry_encode_length(str_length); |
| 938 | EXPECT_EQ(encodeLength, stringEntry.size()); |
| 939 | |
| 940 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 941 | pldm_bios_table_string_entry_encode(encodeEntry.data(), encodeEntry.size(), |
| 942 | str, str_length); |
| 943 | // set string handle = 0 |
| 944 | encodeEntry[0] = 0; |
| 945 | encodeEntry[1] = 0; |
| 946 | |
| 947 | EXPECT_EQ(stringEntry, encodeEntry); |
| 948 | |
| 949 | EXPECT_DEATH(pldm_bios_table_string_entry_encode(encodeEntry.data(), |
| 950 | encodeEntry.size() - 1, |
| 951 | str, str_length), |
| 952 | "length <= entry_length"); |
| 953 | auto rc = pldm_bios_table_string_entry_encode_check( |
| 954 | encodeEntry.data(), encodeEntry.size() - 1, str, str_length); |
| 955 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 956 | } |
| 957 | |
| 958 | TEST(StringTable, EntryDecodeTest) |
| 959 | { |
| 960 | std::vector<uint8_t> stringEntry{ |
| 961 | 4, 0, /* string handle*/ |
| 962 | 7, 0, /* string length */ |
| 963 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 964 | }; |
| 965 | auto entry = reinterpret_cast<struct pldm_bios_string_table_entry*>( |
| 966 | stringEntry.data()); |
| 967 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 968 | EXPECT_EQ(handle, 4); |
| 969 | auto strLength = pldm_bios_table_string_entry_decode_string_length(entry); |
| 970 | EXPECT_EQ(strLength, 7); |
| 971 | |
| 972 | std::vector<char> buffer(strLength + 1, 0); |
| 973 | auto decodedLength = pldm_bios_table_string_entry_decode_string( |
| 974 | entry, buffer.data(), buffer.size()); |
| 975 | EXPECT_EQ(decodedLength, strLength); |
| 976 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
John Wang | 88a349c | 2020-01-20 14:19:49 +0800 | [diff] [blame] | 977 | decodedLength = pldm_bios_table_string_entry_decode_string( |
| 978 | entry, buffer.data(), 2 + 1 /* sizeof '\0'*/); |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 979 | EXPECT_EQ(decodedLength, 2); |
| 980 | EXPECT_EQ(std::strcmp("Al", buffer.data()), 0); |
| 981 | |
| 982 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 983 | entry, buffer.data(), buffer.size()); |
| 984 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 985 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
| 986 | |
| 987 | rc = pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(), |
| 988 | buffer.size() - 1); |
| 989 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 990 | } |
| 991 | |
| 992 | TEST(StringTable, IteratorTest) |
| 993 | { |
| 994 | std::vector<uint8_t> stringHello{ |
| 995 | 0, 0, /* string handle*/ |
| 996 | 5, 0, /* string length */ |
| 997 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 998 | }; |
| 999 | std::vector<uint8_t> stringWorld{ |
| 1000 | 1, 0, /* string handle*/ |
| 1001 | 6, 0, /* string length */ |
| 1002 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 1003 | }; |
| 1004 | |
| 1005 | Table table; |
| 1006 | buildTable(table, stringHello, stringWorld); |
| 1007 | |
| 1008 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 1009 | PLDM_BIOS_STRING_TABLE); |
| 1010 | auto entry = pldm_bios_table_iter_string_entry_value(iter); |
| 1011 | auto rc = std::memcmp(entry, stringHello.data(), stringHello.size()); |
| 1012 | EXPECT_EQ(rc, 0); |
| 1013 | pldm_bios_table_iter_next(iter); |
| 1014 | entry = pldm_bios_table_iter_string_entry_value(iter); |
| 1015 | rc = std::memcmp(entry, stringWorld.data(), stringWorld.size()); |
| 1016 | EXPECT_EQ(rc, 0); |
| 1017 | pldm_bios_table_iter_next(iter); |
| 1018 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 1019 | pldm_bios_table_iter_free(iter); |
| 1020 | } |
| 1021 | |
| 1022 | TEST(StringTable, FindTest) |
| 1023 | { |
| 1024 | std::vector<uint8_t> stringHello{ |
| 1025 | 1, 0, /* string handle*/ |
| 1026 | 5, 0, /* string length */ |
| 1027 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 1028 | }; |
| 1029 | std::vector<uint8_t> stringWorld{ |
| 1030 | 2, 0, /* string handle*/ |
| 1031 | 6, 0, /* string length */ |
| 1032 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 1033 | }; |
| 1034 | std::vector<uint8_t> stringHi{ |
| 1035 | 3, 0, /* string handle*/ |
| 1036 | 2, 0, /* string length */ |
| 1037 | 'H', 'i', /* string */ |
| 1038 | }; |
| 1039 | |
| 1040 | Table table; |
| 1041 | buildTable(table, stringHello, stringWorld, stringHi); |
| 1042 | |
| 1043 | auto entry = pldm_bios_table_string_find_by_string(table.data(), |
| 1044 | table.size(), "World!"); |
| 1045 | EXPECT_NE(entry, nullptr); |
| 1046 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 1047 | EXPECT_EQ(handle, 2); |
| 1048 | |
| 1049 | entry = pldm_bios_table_string_find_by_string(table.data(), table.size(), |
| 1050 | "Worl"); |
| 1051 | EXPECT_EQ(entry, nullptr); |
| 1052 | |
| 1053 | entry = |
| 1054 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 3); |
| 1055 | EXPECT_NE(entry, nullptr); |
| 1056 | auto str_length = pldm_bios_table_string_entry_decode_string_length(entry); |
| 1057 | EXPECT_EQ(str_length, 2); |
| 1058 | std::vector<char> strBuf(str_length + 1, 0); |
| 1059 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 1060 | entry, strBuf.data(), strBuf.size()); |
| 1061 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1062 | EXPECT_EQ(std::strcmp("Hi", strBuf.data()), 0); |
| 1063 | |
| 1064 | entry = |
| 1065 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 4); |
| 1066 | EXPECT_EQ(entry, nullptr); |
| 1067 | } |
| 1068 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 1069 | TEST(Itearator, DeathTest) |
| 1070 | { |
| 1071 | |
| 1072 | Table table(256, 0); |
| 1073 | |
| 1074 | /* first entry */ |
| 1075 | auto attr_entry = |
| 1076 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data()); |
| 1077 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 1078 | PLDM_BIOS_ATTR_TABLE); |
| 1079 | attr_entry->attr_type = PLDM_BIOS_PASSWORD; |
| 1080 | EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL"); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 1081 | pldm_bios_table_iter_free(iter); |
| 1082 | } |
John Wang | 79c37f1 | 2019-10-31 15:46:31 +0800 | [diff] [blame] | 1083 | |
| 1084 | TEST(PadAndChecksum, PadAndChecksum) |
| 1085 | { |
| 1086 | EXPECT_EQ(4, pldm_bios_table_pad_checksum_size(0)); |
| 1087 | EXPECT_EQ(7, pldm_bios_table_pad_checksum_size(1)); |
| 1088 | EXPECT_EQ(6, pldm_bios_table_pad_checksum_size(2)); |
| 1089 | EXPECT_EQ(5, pldm_bios_table_pad_checksum_size(3)); |
| 1090 | EXPECT_EQ(4, pldm_bios_table_pad_checksum_size(4)); |
| 1091 | |
| 1092 | // The table is borrowed from |
| 1093 | // https://github.com/openbmc/pldm/commit/69d3e7fb2d9935773f4fbf44326c33f3fc0a3c38 |
| 1094 | // refer to the commit message |
| 1095 | Table attrValTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, 0x66}; |
| 1096 | auto sizeWithoutPad = attrValTable.size(); |
| 1097 | attrValTable.resize(sizeWithoutPad + |
| 1098 | pldm_bios_table_pad_checksum_size(sizeWithoutPad)); |
| 1099 | pldm_bios_table_append_pad_checksum(attrValTable.data(), |
| 1100 | attrValTable.size(), sizeWithoutPad); |
| 1101 | Table expectedTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, |
| 1102 | 0x66, 0x00, 0x6d, 0x81, 0x4a, 0xb6}; |
| 1103 | EXPECT_EQ(attrValTable, expectedTable); |
| 1104 | } |