libpldm: bios: implement en/decode for SetDateTime
Add encode/decode functions.
Add unit-test.
Signed-off-by: Xiaochao Ma <maxiaochao@inspur.com>
Change-Id: Ide5563ea5b6446b7300e0d1a9f2b64275217c820
diff --git a/test/libpldm_bios_test.cpp b/test/libpldm_bios_test.cpp
index d841da6..3e0938f 100644
--- a/test/libpldm_bios_test.cpp
+++ b/test/libpldm_bios_test.cpp
@@ -4,6 +4,7 @@
#include "libpldm/base.h"
#include "libpldm/bios.h"
+#include "libpldm/utils.h"
#include <gtest/gtest.h>
@@ -113,6 +114,219 @@
ASSERT_EQ(year, retYear);
}
+TEST(SetDateTime, testGoodEncodeResponse)
+{
+ uint8_t instanceId = 0;
+ uint8_t completionCode = PLDM_SUCCESS;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+
+ auto rc = encode_set_date_time_resp(instanceId, completionCode, response,
+ responseMsg.size() - hdrSize);
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+
+ struct pldm_only_cc_resp* resp =
+ reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
+ EXPECT_EQ(completionCode, resp->completion_code);
+}
+
+TEST(SetDateTime, testBadEncodeResponse)
+{
+
+ uint8_t instanceId = 10;
+ uint8_t completionCode = PLDM_SUCCESS;
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ auto rc = encode_set_date_time_resp(instanceId, completionCode, nullptr,
+ responseMsg.size() - hdrSize);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+ rc = encode_set_date_time_resp(instanceId, completionCode, response,
+ responseMsg.size() - hdrSize - 1);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetDateTime, testGoodDecodeResponse)
+{
+ uint8_t completionCode = PLDM_SUCCESS;
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ struct pldm_only_cc_resp* resp =
+ reinterpret_cast<struct pldm_only_cc_resp*>(response->payload);
+
+ resp->completion_code = completionCode;
+
+ uint8_t retCompletionCode;
+ auto rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize,
+ &retCompletionCode);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+ EXPECT_EQ(completionCode, retCompletionCode);
+}
+
+TEST(SetDateTime, testBadDecodeResponse)
+{
+ uint8_t completionCode = PLDM_SUCCESS;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_only_cc_resp)>
+ responseMsg{};
+ struct pldm_msg* response =
+ reinterpret_cast<struct pldm_msg*>(responseMsg.data());
+ auto rc = decode_set_date_time_resp(nullptr, responseMsg.size() - hdrSize,
+ &completionCode);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = decode_set_date_time_resp(response, responseMsg.size() - hdrSize - 1,
+ &completionCode);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetDateTime, testGoodEncodeRequset)
+{
+ uint8_t instanceId = 0;
+ uint8_t seconds = 50;
+ uint8_t minutes = 20;
+ uint8_t hours = 10;
+ uint8_t day = 11;
+ uint8_t month = 11;
+ uint16_t year = 2019;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+
+ auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+ auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
+ month, year, request,
+ requestMsg.size() - hdrSize);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+
+ struct pldm_set_date_time_req* req =
+ reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
+ EXPECT_EQ(seconds, bcd2dec8(req->seconds));
+ EXPECT_EQ(minutes, bcd2dec8(req->minutes));
+ EXPECT_EQ(hours, bcd2dec8(req->hours));
+ EXPECT_EQ(day, bcd2dec8(req->day));
+ EXPECT_EQ(month, bcd2dec8(req->month));
+ EXPECT_EQ(year, le16toh(bcd2dec16(req->year)));
+}
+
+TEST(SetDateTime, testBadEncodeRequset)
+{
+ uint8_t instanceId = 0;
+
+ uint8_t seconds = 50;
+ uint8_t minutes = 20;
+ uint8_t hours = 10;
+ uint8_t day = 13;
+ uint8_t month = 11;
+ uint16_t year = 2019;
+
+ uint8_t erday = 43;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+
+ auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
+
+ auto rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
+ month, year, nullptr,
+ requestMsg.size() - hdrSize);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, erday,
+ month, year, request,
+ requestMsg.size() - hdrSize);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = encode_set_date_time_req(instanceId, seconds, minutes, hours, day,
+ month, year, request,
+ requestMsg.size() - hdrSize - 4);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(SetDateTime, testGoodDecodeRequest)
+{
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+ uint8_t seconds = 0x50;
+ uint8_t minutes = 0x20;
+ uint8_t hours = 0x10;
+ uint8_t day = 0x11;
+ uint8_t month = 0x11;
+ uint16_t year = 0x2019;
+
+ auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+ struct pldm_set_date_time_req* req =
+ reinterpret_cast<struct pldm_set_date_time_req*>(request->payload);
+ req->seconds = seconds;
+ req->minutes = minutes;
+ req->hours = hours;
+ req->day = day;
+ req->month = month;
+ req->year = htole16(year);
+
+ uint8_t retseconds;
+ uint8_t retminutes;
+ uint8_t rethours;
+ uint8_t retday;
+ uint8_t retmonth;
+ uint16_t retyear;
+
+ auto rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize,
+ &retseconds, &retminutes, &rethours,
+ &retday, &retmonth, &retyear);
+
+ EXPECT_EQ(rc, PLDM_SUCCESS);
+ EXPECT_EQ(retseconds, 50);
+ EXPECT_EQ(retminutes, 20);
+ EXPECT_EQ(rethours, 10);
+ EXPECT_EQ(retday, 11);
+ EXPECT_EQ(retmonth, 11);
+ EXPECT_EQ(retyear, 2019);
+}
+
+TEST(SetDateTime, testBadDecodeRequest)
+{
+ uint8_t seconds = 0x50;
+ uint8_t minutes = 0x20;
+ uint8_t hours = 0x10;
+ uint8_t day = 0x11;
+ uint8_t month = 0x11;
+ uint16_t year = 0x2019;
+
+ std::array<uint8_t, hdrSize + sizeof(struct pldm_set_date_time_req)>
+ requestMsg{};
+
+ auto request = reinterpret_cast<struct pldm_msg*>(requestMsg.data());
+
+ decode_set_date_time_req(request, requestMsg.size() - hdrSize, &seconds,
+ &minutes, &hours, &day, &month, &year);
+
+ auto rc =
+ decode_set_date_time_req(nullptr, requestMsg.size() - hdrSize, &seconds,
+ &minutes, &hours, &day, &month, &year);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize, nullptr,
+ nullptr, nullptr, nullptr, nullptr, nullptr);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+
+ rc = decode_set_date_time_req(request, requestMsg.size() - hdrSize - 4,
+ &seconds, &minutes, &hours, &day, &month,
+ &year);
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
+}
+
TEST(GetBIOSTable, testGoodEncodeResponse)
{
std::array<uint8_t,
diff --git a/test/libpldm_utils_test.cpp b/test/libpldm_utils_test.cpp
index b391f79..a40aec1 100644
--- a/test/libpldm_utils_test.cpp
+++ b/test/libpldm_utils_test.cpp
@@ -65,4 +65,19 @@
EXPECT_EQ(0x9999, dec2bcd16(9999));
EXPECT_EQ(0x12345678, dec2bcd32(12345678));
EXPECT_EQ(0x99999999, dec2bcd32(99999999));
+}
+
+TEST(TimeLegal, TimeLegal)
+{
+ EXPECT_EQ(true, is_time_legal(30, 25, 16, 18, 8, 2019));
+ EXPECT_EQ(true, is_time_legal(30, 25, 16, 29, 2, 2020)); // leap year
+
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 8, 1960)); // year illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 15, 2019)); // month illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 18, 0, 2019)); // month illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 0, 8, 2019)); // day illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 16, 29, 2, 2019)); // day illegal
+ EXPECT_EQ(false, is_time_legal(30, 25, 25, 18, 8, 2019)); // hours illegal
+ EXPECT_EQ(false, is_time_legal(30, 70, 16, 18, 8, 2019)); // minutes illegal
+ EXPECT_EQ(false, is_time_legal(80, 25, 16, 18, 8, 2019)); // seconds illegal
}
\ No newline at end of file