1 #include "config.h"
2 
3 #include "utils.hpp"
4 
5 #include <sdbusplus/test/sdbus_mock.hpp>
6 
7 #include <gmock/gmock.h>
8 #include <gtest/gtest.h>
9 
10 using ::testing::_;
11 using ::testing::Invoke;
12 using ::testing::IsNull;
13 using ::testing::Return;
14 using ::testing::StrEq;
15 
16 TEST(Utils, GetPSUInventoryPath)
17 {
18     sdbusplus::SdBusMock sdbusMock;
19     auto bus = sdbusplus::get_mocked_new(&sdbusMock);
20 
21     EXPECT_CALL(sdbusMock, sd_bus_message_new_method_call(
22                                _, _, _, _, _, StrEq("GetSubTreePaths")));
23 
24     EXPECT_CALL(sdbusMock, sd_bus_message_ref(IsNull()))
25         .WillOnce(Return(nullptr));
26     sdbusplus::message_t msg(nullptr, &sdbusMock);
27 
28     const char* path0 = "/com/example/chassis/powersupply0";
29     const char* path1 = "/com/example/chassis/powersupply1";
30 
31     // std::vector
32     EXPECT_CALL(sdbusMock,
33                 sd_bus_message_enter_container(IsNull(), 'a', StrEq("s")))
34         .WillOnce(Return(1));
35 
36     // while !at_end()
37     EXPECT_CALL(sdbusMock, sd_bus_message_at_end(IsNull(), 0))
38         .WillOnce(Return(0))
39         .WillOnce(Return(0))
40         .WillOnce(Return(1)); // So it exits the loop after reading two strings.
41 
42     // std::string
43     EXPECT_CALL(sdbusMock, sd_bus_message_read_basic(IsNull(), 's', _))
44         .WillOnce(Invoke([&](sd_bus_message*, char, void* p) {
45             const char** s = static_cast<const char**>(p);
46             // Read the first parameter, the string.
47             *s = path0;
48             return 0;
49         }))
50         .WillOnce(Invoke([&](sd_bus_message*, char, void* p) {
51             const char** s = static_cast<const char**>(p);
52             // Read the first parameter, the string.
53             *s = path1;
54             return 0;
55         }));
56 
57     EXPECT_CALL(sdbusMock, sd_bus_message_exit_container(IsNull()))
58         .WillOnce(Return(0)); /* end of std::vector */
59 
60     auto ret = utils::getPSUInventoryPath(bus);
61     EXPECT_EQ(2U, ret.size());
62     EXPECT_EQ(path0, ret[0]);
63     EXPECT_EQ(path1, ret[1]);
64 }
65 
66 TEST(Utils, GetVersionID)
67 {
68     auto ret = utils::getVersionId("");
69     EXPECT_EQ("", ret);
70 
71     ret = utils::getVersionId("some version");
72     EXPECT_EQ(8U, ret.size());
73 }
74 
75 TEST(Utils, IsAssociated)
76 {
77     std::string path = "/com/example/chassis/powersupply0";
78     utils::AssociationList assocs = {
79         {ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
80          "a-different-path"}};
81 
82     EXPECT_FALSE(utils::isAssociated(path, assocs));
83 
84     assocs.emplace_back(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
85                         path);
86     EXPECT_TRUE(utils::isAssociated(path, assocs));
87 }
88