1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "bios_setting.hpp"
16 #include "commands.hpp"
17 #include "helper.hpp"
18 
19 #include <stdplus/gtest/tmp.hpp>
20 
21 #include <fstream>
22 #include <ios>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 
27 #include <gtest/gtest.h>
28 
29 namespace google
30 {
31 namespace ipmi
32 {
33 
34 using testing::_;
35 using ::testing::ElementsAre;
36 
37 class BiosSettingTest : public stdplus::gtest::TestWithTmp
38 {
39   public:
40     std::string filename = std::format("{}/oem_bios_setting", CaseTmpDir());
41 
42     void writeTmpFile(std::vector<uint8_t> payload)
43     {
44         std::ofstream ofs;
45         ofs.open(filename, std::ios::trunc | std::ios::binary);
46         ofs.write(reinterpret_cast<char*>(payload.data()), payload.size());
47         ofs.close();
48     }
49 };
50 
51 TEST_F(BiosSettingTest, NoOrEmptyFileRead)
52 {
53     std::vector<uint8_t> request = {};
54 
55     HandlerMock hMock;
56     EXPECT_EQ(::ipmi::responseRetBytesUnavailable(),
57               readBiosSetting(request, &hMock));
58 
59     // Create an empty file
60     writeTmpFile({});
61     EXPECT_EQ(::ipmi::responseRetBytesUnavailable(),
62               readBiosSetting(request, &hMock, filename));
63     std::remove(filename.c_str());
64 }
65 
66 TEST_F(BiosSettingTest, SuccessfulRead)
67 {
68     std::vector<uint8_t> request = {};
69     // Ensure 0x0A which is a new line character '\n', is read properly
70     std::vector<uint8_t> payload = {0x0A, 0xDE, 0xAD, 0xBE, 0xEF, 0x0A};
71     std::vector<uint8_t> expectedReply = {6,    0x0A, 0xDE, 0xAD,
72                                           0xBE, 0xEF, 0x0A};
73 
74     writeTmpFile(payload);
75 
76     HandlerMock hMock;
77     auto reply = readBiosSetting(request, &hMock, filename);
78     auto result = ValidateReply(reply);
79     auto& data = result.second;
80 
81     EXPECT_EQ(SysOEMCommands::SysReadBiosSetting, result.first);
82     EXPECT_EQ(expectedReply.size() - 1, data.front());
83     EXPECT_EQ(expectedReply, data);
84     std::remove(filename.c_str());
85 }
86 
87 } // namespace ipmi
88 } // namespace google
89