blob: a40aec1a7ef2b88cdb36241bd3699babe4a1d238 [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
15TEST(Ver2string, Ver2string)
16{
17 ver32_t version{0xf3, 0xf7, 0x10, 0x61};
18 const char* vstr = "3.7.10a";
19 char buffer[1024];
20 auto rc = ver2str(&version, buffer, sizeof(buffer));
21 EXPECT_EQ(rc, std::strlen(vstr));
22 EXPECT_STREQ(vstr, buffer);
23
24 version = {0x10, 0x01, 0xf7, 0x00};
25 vstr = "10.01.7";
26 rc = ver2str(&version, buffer, sizeof(buffer));
27 EXPECT_EQ(rc, std::strlen(vstr));
28 EXPECT_STREQ(vstr, buffer);
29
30 version = {0xf3, 0xf1, 0xff, 0x00};
31 vstr = "3.1";
32 rc = ver2str(&version, buffer, sizeof(buffer));
33 EXPECT_EQ(rc, std::strlen(vstr));
34 EXPECT_STREQ(vstr, buffer);
35
36 version = {0xf1, 0xf0, 0xff, 0x61};
37 vstr = "1.0a";
38 rc = ver2str(&version, buffer, sizeof(buffer));
39 EXPECT_EQ(rc, std::strlen(vstr));
40 EXPECT_STREQ(vstr, buffer);
41
42 rc = ver2str(&version, buffer, 3);
43 EXPECT_EQ(rc, 2);
44 EXPECT_STREQ("1.", buffer);
45
46 rc = ver2str(&version, buffer, 1);
47 EXPECT_EQ(rc, 0);
48
49 rc = ver2str(&version, buffer, 0);
50 EXPECT_EQ(rc, -1);
John Wang5bdb30a2019-11-20 15:28:37 +080051}
52
53TEST(BcdConversion, BcdCoversion)
54{
55 EXPECT_EQ(12, bcd2dec8(0x12));
56 EXPECT_EQ(99, bcd2dec8(0x99));
57 EXPECT_EQ(1234, bcd2dec16(0x1234));
58 EXPECT_EQ(9999, bcd2dec16(0x9999));
59 EXPECT_EQ(12345678, bcd2dec32(0x12345678));
60 EXPECT_EQ(99999999, bcd2dec32(0x99999999));
61
62 EXPECT_EQ(0x12, dec2bcd8(12));
63 EXPECT_EQ(0x99, dec2bcd8(99));
64 EXPECT_EQ(0x1234, dec2bcd16(1234));
65 EXPECT_EQ(0x9999, dec2bcd16(9999));
66 EXPECT_EQ(0x12345678, dec2bcd32(12345678));
67 EXPECT_EQ(0x99999999, dec2bcd32(99999999));
Xiaochao Ma39ae2a92019-11-12 20:30:34 +080068}
69
70TEST(TimeLegal, TimeLegal)
71{
72 EXPECT_EQ(true, is_time_legal(30, 25, 16, 18, 8, 2019));
73 EXPECT_EQ(true, is_time_legal(30, 25, 16, 29, 2, 2020)); // leap year
74
75 EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 8, 1960)); // year illegal
76 EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 15, 2019)); // month illegal
77 EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 0, 2019)); // month illegal
78 EXPECT_EQ(false, is_time_legal(30, 25, 16, 0, 8, 2019)); // day illegal
79 EXPECT_EQ(false, is_time_legal(30, 25, 16, 29, 2, 2019)); // day illegal
80 EXPECT_EQ(false, is_time_legal(30, 25, 25, 18, 8, 2019)); // hours illegal
81 EXPECT_EQ(false, is_time_legal(30, 70, 16, 18, 8, 2019)); // minutes illegal
82 EXPECT_EQ(false, is_time_legal(80, 25, 16, 18, 8, 2019)); // seconds illegal
John Wangd8702f62019-10-31 10:06:35 +080083}