John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 1 | #include <cstring> |
John Wang | d8702f6 | 2019-10-31 10:06:35 +0800 | [diff] [blame] | 2 | #include <vector> |
| 3 | |
| 4 | #include "libpldm/utils.h" |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | TEST(Crc32, CheckSumTest) |
| 9 | { |
| 10 | const char* password = "123456789"; |
| 11 | auto checksum = crc32(password, 9); |
| 12 | EXPECT_EQ(checksum, 0xcbf43926); |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 13 | } |
| 14 | |
George Liu | a940d8e | 2019-11-26 11:07:42 +0800 | [diff] [blame] | 15 | TEST(Crc8, CheckSumTest) |
| 16 | { |
| 17 | const char* data = "123456789"; |
| 18 | auto checksum = crc8(data, 9); |
| 19 | EXPECT_EQ(checksum, 0xf4); |
| 20 | } |
| 21 | |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 22 | TEST(Ver2string, Ver2string) |
| 23 | { |
Manojkiran Eda | cb45467 | 2022-06-29 11:05:42 +0530 | [diff] [blame] | 24 | ver32_t version{0x61, 0x10, 0xf7, 0xf3}; |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 25 | const char* vstr = "3.7.10a"; |
| 26 | char buffer[1024]; |
| 27 | auto rc = ver2str(&version, buffer, sizeof(buffer)); |
crgeddes | 2550cee | 2020-05-05 17:51:28 +0000 | [diff] [blame] | 28 | EXPECT_EQ(rc, (signed)std::strlen(vstr)); |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 29 | EXPECT_STREQ(vstr, buffer); |
| 30 | |
Manojkiran Eda | cb45467 | 2022-06-29 11:05:42 +0530 | [diff] [blame] | 31 | version = {0x00, 0xf0, 0xf0, 0xf1}; |
| 32 | vstr = "1.0.0"; |
| 33 | rc = ver2str(&version, buffer, sizeof(buffer)); |
| 34 | EXPECT_EQ(rc, (signed)std::strlen(vstr)); |
| 35 | EXPECT_STREQ(vstr, buffer); |
| 36 | |
| 37 | version = {0x00, 0xf7, 0x01, 0x10}; |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 38 | vstr = "10.01.7"; |
| 39 | rc = ver2str(&version, buffer, sizeof(buffer)); |
crgeddes | 2550cee | 2020-05-05 17:51:28 +0000 | [diff] [blame] | 40 | EXPECT_EQ(rc, (signed)std::strlen(vstr)); |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 41 | EXPECT_STREQ(vstr, buffer); |
| 42 | |
Manojkiran Eda | cb45467 | 2022-06-29 11:05:42 +0530 | [diff] [blame] | 43 | version = {0x00, 0xff, 0xf1, 0xf3}; |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 44 | vstr = "3.1"; |
| 45 | rc = ver2str(&version, buffer, sizeof(buffer)); |
crgeddes | 2550cee | 2020-05-05 17:51:28 +0000 | [diff] [blame] | 46 | EXPECT_EQ(rc, (signed)std::strlen(vstr)); |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 47 | EXPECT_STREQ(vstr, buffer); |
| 48 | |
Manojkiran Eda | cb45467 | 2022-06-29 11:05:42 +0530 | [diff] [blame] | 49 | version = {0x61, 0xff, 0xf0, 0xf1}; |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 50 | vstr = "1.0a"; |
| 51 | rc = ver2str(&version, buffer, sizeof(buffer)); |
crgeddes | 2550cee | 2020-05-05 17:51:28 +0000 | [diff] [blame] | 52 | EXPECT_EQ(rc, (signed)std::strlen(vstr)); |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 53 | EXPECT_STREQ(vstr, buffer); |
| 54 | |
| 55 | rc = ver2str(&version, buffer, 3); |
| 56 | EXPECT_EQ(rc, 2); |
| 57 | EXPECT_STREQ("1.", buffer); |
| 58 | |
| 59 | rc = ver2str(&version, buffer, 1); |
| 60 | EXPECT_EQ(rc, 0); |
| 61 | |
| 62 | rc = ver2str(&version, buffer, 0); |
| 63 | EXPECT_EQ(rc, -1); |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST(BcdConversion, BcdCoversion) |
| 67 | { |
crgeddes | 2550cee | 2020-05-05 17:51:28 +0000 | [diff] [blame] | 68 | EXPECT_EQ(12u, bcd2dec8(0x12)); |
| 69 | EXPECT_EQ(99u, bcd2dec8(0x99)); |
| 70 | EXPECT_EQ(1234u, bcd2dec16(0x1234)); |
| 71 | EXPECT_EQ(9999u, bcd2dec16(0x9999)); |
| 72 | EXPECT_EQ(12345678u, bcd2dec32(0x12345678)); |
| 73 | EXPECT_EQ(99999999u, bcd2dec32(0x99999999)); |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 74 | |
crgeddes | 2550cee | 2020-05-05 17:51:28 +0000 | [diff] [blame] | 75 | EXPECT_EQ(0x12u, dec2bcd8(12)); |
| 76 | EXPECT_EQ(0x99u, dec2bcd8(99)); |
| 77 | EXPECT_EQ(0x1234u, dec2bcd16(1234)); |
| 78 | EXPECT_EQ(0x9999u, dec2bcd16(9999)); |
| 79 | EXPECT_EQ(0x12345678u, dec2bcd32(12345678)); |
| 80 | EXPECT_EQ(0x99999999u, dec2bcd32(99999999)); |
Xiaochao Ma | 39ae2a9 | 2019-11-12 20:30:34 +0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | TEST(TimeLegal, TimeLegal) |
| 84 | { |
| 85 | EXPECT_EQ(true, is_time_legal(30, 25, 16, 18, 8, 2019)); |
| 86 | EXPECT_EQ(true, is_time_legal(30, 25, 16, 29, 2, 2020)); // leap year |
| 87 | |
| 88 | EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 8, 1960)); // year illegal |
| 89 | EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 15, 2019)); // month illegal |
| 90 | EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 0, 2019)); // month illegal |
| 91 | EXPECT_EQ(false, is_time_legal(30, 25, 16, 0, 8, 2019)); // day illegal |
| 92 | EXPECT_EQ(false, is_time_legal(30, 25, 16, 29, 2, 2019)); // day illegal |
| 93 | EXPECT_EQ(false, is_time_legal(30, 25, 25, 18, 8, 2019)); // hours illegal |
| 94 | EXPECT_EQ(false, is_time_legal(30, 70, 16, 18, 8, 2019)); // minutes illegal |
| 95 | EXPECT_EQ(false, is_time_legal(80, 25, 16, 18, 8, 2019)); // seconds illegal |
crgeddes | 2550cee | 2020-05-05 17:51:28 +0000 | [diff] [blame] | 96 | } |