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 <fstream> 17 #include <gtest/gtest.h> 18 #include <experimental/filesystem> 19 #include "callout.hpp" 20 #include "dbus.hpp" 21 22 using namespace ibm::logging; 23 24 class CalloutTest : public ::testing::Test 25 { 26 protected: 27 virtual void SetUp() 28 { 29 char dir[] = {"./calloutsXXXXXX"}; 30 31 persistDir = mkdtemp(dir); 32 } 33 34 virtual void TearDown() 35 { 36 fs::remove_all(persistDir); 37 } 38 39 fs::path persistDir; 40 }; 41 42 TEST_F(CalloutTest, TestPersist) 43 { 44 using namespace std::literals::string_literals; 45 46 auto bus = sdbusplus::bus::new_default(); 47 std::string objectPath{"/callout/path/0"}; 48 std::string calloutPath{"/some/inventory/object"}; 49 size_t id = 0; 50 uint64_t ts = 5; 51 52 DbusPropertyMap assetProps{{"BuildDate"s, Value{"Date42"s}}, 53 {"Manufacturer"s, Value{"Mfg42"s}}, 54 {"Model"s, Value{"Model42"s}}, 55 {"PartNumber"s, Value{"PN42"s}}, 56 {"SerialNumber"s, Value{"SN42"s}}}; 57 { 58 auto callout = std::make_unique<Callout>(bus, objectPath, calloutPath, 59 id, ts, assetProps); 60 callout->serialize(persistDir); 61 62 ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), true); 63 } 64 65 // Test object restoration 66 { 67 auto callout = std::make_unique<Callout>(bus, objectPath, id, ts); 68 69 ASSERT_EQ(callout->deserialize(persistDir), true); 70 71 ASSERT_EQ(callout->id(), id); 72 ASSERT_EQ(callout->ts(), ts); 73 ASSERT_EQ(callout->path(), calloutPath); 74 ASSERT_EQ(callout->buildDate(), 75 assetProps["BuildDate"].get<std::string>()); 76 ASSERT_EQ(callout->manufacturer(), 77 assetProps["Manufacturer"].get<std::string>()); 78 ASSERT_EQ(callout->model(), assetProps["Model"].get<std::string>()); 79 ASSERT_EQ(callout->partNumber(), 80 assetProps["PartNumber"].get<std::string>()); 81 ASSERT_EQ(callout->serialNumber(), 82 assetProps["SerialNumber"].get<std::string>()); 83 } 84 85 // Test a serialization failure due to a bad timestamp 86 { 87 auto callout = std::make_unique<Callout>(bus, objectPath, id, ts + 1); 88 89 ASSERT_EQ(callout->deserialize(persistDir), false); 90 ASSERT_EQ(fs::exists(persistDir / std::to_string(id)), false); 91 } 92 } 93