xref: /openbmc/ibm-logging/test/test_callout.cpp (revision b5af3a3f)
1 /**
2  * Copyright © 2018 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "callout.hpp"
17 #include "dbus.hpp"
18 
19 #include <experimental/filesystem>
20 #include <fstream>
21 
22 #include <gtest/gtest.h>
23 
24 using namespace ibm::logging;
25 
26 class CalloutTest : public ::testing::Test
27 {
28   protected:
SetUp()29     virtual void SetUp()
30     {
31         char dir[] = {"./calloutsXXXXXX"};
32 
33         persistDir = mkdtemp(dir);
34     }
35 
TearDown()36     virtual void TearDown()
37     {
38         fs::remove_all(persistDir);
39     }
40 
41     fs::path persistDir;
42 };
43 
TEST_F(CalloutTest,TestPersist)44 TEST_F(CalloutTest, TestPersist)
45 {
46     using namespace std::literals::string_literals;
47 
48     auto bus = sdbusplus::bus::new_default();
49     std::string objectPath{"/callout/path/0"};
50     std::string calloutPath{"/some/inventory/object"};
51     size_t id = 0;
52     uint64_t ts = 5;
53 
54     DbusPropertyMap assetProps{{"BuildDate"s, Value{"Date42"s}},
55                                {"Manufacturer"s, Value{"Mfg42"s}},
56                                {"Model"s, Value{"Model42"s}},
57                                {"PartNumber"s, Value{"PN42"s}},
58                                {"SerialNumber"s, Value{"SN42"s}}};
59     {
60         auto callout = std::make_unique<Callout>(bus, objectPath, calloutPath,
61                                                  id, ts, assetProps);
62         callout->serialize(persistDir);
63 
64         ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), true);
65     }
66 
67     // Test object restoration
68     {
69         auto callout = std::make_unique<Callout>(bus, objectPath, id, ts);
70 
71         ASSERT_EQ(callout->deserialize(persistDir), true);
72 
73         ASSERT_EQ(callout->id(), id);
74         ASSERT_EQ(callout->ts(), ts);
75         ASSERT_EQ(callout->path(), calloutPath);
76         ASSERT_EQ(callout->buildDate(),
77                   std::get<std::string>(assetProps["BuildDate"]));
78         ASSERT_EQ(callout->manufacturer(),
79                   std::get<std::string>(assetProps["Manufacturer"]));
80         ASSERT_EQ(callout->model(), std::get<std::string>(assetProps["Model"]));
81         ASSERT_EQ(callout->partNumber(),
82                   std::get<std::string>(assetProps["PartNumber"]));
83         ASSERT_EQ(callout->serialNumber(),
84                   std::get<std::string>(assetProps["SerialNumber"]));
85     }
86 
87     // Test a serialization failure due to a bad timestamp
88     {
89         auto callout = std::make_unique<Callout>(bus, objectPath, id, ts + 1);
90 
91         ASSERT_EQ(callout->deserialize(persistDir), false);
92         ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), false);
93     }
94 }
95