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