blob: 1b178d7d3a4a8f60698456c2d5783e50db9fb4d7 [file] [log] [blame]
John Wang72136232019-11-14 15:33:42 +08001#include <cstring>
John Wangd8702f62019-10-31 10:06:35 +08002#include <vector>
3
4#include "libpldm/utils.h"
5
6#include <gtest/gtest.h>
7
8TEST(Crc32, CheckSumTest)
9{
10 const char* password = "123456789";
11 auto checksum = crc32(password, 9);
12 EXPECT_EQ(checksum, 0xcbf43926);
John Wang72136232019-11-14 15:33:42 +080013}
14
George Liua940d8e2019-11-26 11:07:42 +080015TEST(Crc8, CheckSumTest)
16{
17 const char* data = "123456789";
18 auto checksum = crc8(data, 9);
19 EXPECT_EQ(checksum, 0xf4);
20}
21
John Wang72136232019-11-14 15:33:42 +080022TEST(Ver2string, Ver2string)
23{
24 ver32_t version{0xf3, 0xf7, 0x10, 0x61};
25 const char* vstr = "3.7.10a";
26 char buffer[1024];
27 auto rc = ver2str(&version, buffer, sizeof(buffer));
crgeddes2550cee2020-05-05 17:51:28 +000028 EXPECT_EQ(rc, (signed)std::strlen(vstr));
John Wang72136232019-11-14 15:33:42 +080029 EXPECT_STREQ(vstr, buffer);
30
31 version = {0x10, 0x01, 0xf7, 0x00};
32 vstr = "10.01.7";
33 rc = ver2str(&version, buffer, sizeof(buffer));
crgeddes2550cee2020-05-05 17:51:28 +000034 EXPECT_EQ(rc, (signed)std::strlen(vstr));
John Wang72136232019-11-14 15:33:42 +080035 EXPECT_STREQ(vstr, buffer);
36
37 version = {0xf3, 0xf1, 0xff, 0x00};
38 vstr = "3.1";
39 rc = ver2str(&version, buffer, sizeof(buffer));
crgeddes2550cee2020-05-05 17:51:28 +000040 EXPECT_EQ(rc, (signed)std::strlen(vstr));
John Wang72136232019-11-14 15:33:42 +080041 EXPECT_STREQ(vstr, buffer);
42
43 version = {0xf1, 0xf0, 0xff, 0x61};
44 vstr = "1.0a";
45 rc = ver2str(&version, buffer, sizeof(buffer));
crgeddes2550cee2020-05-05 17:51:28 +000046 EXPECT_EQ(rc, (signed)std::strlen(vstr));
John Wang72136232019-11-14 15:33:42 +080047 EXPECT_STREQ(vstr, buffer);
48
49 rc = ver2str(&version, buffer, 3);
50 EXPECT_EQ(rc, 2);
51 EXPECT_STREQ("1.", buffer);
52
53 rc = ver2str(&version, buffer, 1);
54 EXPECT_EQ(rc, 0);
55
56 rc = ver2str(&version, buffer, 0);
57 EXPECT_EQ(rc, -1);
John Wang5bdb30a2019-11-20 15:28:37 +080058}
59
60TEST(BcdConversion, BcdCoversion)
61{
crgeddes2550cee2020-05-05 17:51:28 +000062 EXPECT_EQ(12u, bcd2dec8(0x12));
63 EXPECT_EQ(99u, bcd2dec8(0x99));
64 EXPECT_EQ(1234u, bcd2dec16(0x1234));
65 EXPECT_EQ(9999u, bcd2dec16(0x9999));
66 EXPECT_EQ(12345678u, bcd2dec32(0x12345678));
67 EXPECT_EQ(99999999u, bcd2dec32(0x99999999));
John Wang5bdb30a2019-11-20 15:28:37 +080068
crgeddes2550cee2020-05-05 17:51:28 +000069 EXPECT_EQ(0x12u, dec2bcd8(12));
70 EXPECT_EQ(0x99u, dec2bcd8(99));
71 EXPECT_EQ(0x1234u, dec2bcd16(1234));
72 EXPECT_EQ(0x9999u, dec2bcd16(9999));
73 EXPECT_EQ(0x12345678u, dec2bcd32(12345678));
74 EXPECT_EQ(0x99999999u, dec2bcd32(99999999));
Xiaochao Ma39ae2a92019-11-12 20:30:34 +080075}
76
77TEST(TimeLegal, TimeLegal)
78{
79 EXPECT_EQ(true, is_time_legal(30, 25, 16, 18, 8, 2019));
80 EXPECT_EQ(true, is_time_legal(30, 25, 16, 29, 2, 2020)); // leap year
81
82 EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 8, 1960)); // year illegal
83 EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 15, 2019)); // month illegal
84 EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 0, 2019)); // month illegal
85 EXPECT_EQ(false, is_time_legal(30, 25, 16, 0, 8, 2019)); // day illegal
86 EXPECT_EQ(false, is_time_legal(30, 25, 16, 29, 2, 2019)); // day illegal
87 EXPECT_EQ(false, is_time_legal(30, 25, 25, 18, 8, 2019)); // hours illegal
88 EXPECT_EQ(false, is_time_legal(30, 70, 16, 18, 8, 2019)); // minutes illegal
89 EXPECT_EQ(false, is_time_legal(80, 25, 16, 18, 8, 2019)); // seconds illegal
crgeddes2550cee2020-05-05 17:51:28 +000090}