blob: 383ed4bc3b0e9823c47891b1a4810d42dbd9f9c4 [file] [log] [blame]
Lei YU4b9ac392019-10-12 11:02:26 +08001#include "config.h"
2
Lei YU5e0dcb32019-08-02 18:04:34 +08003#include "utils.hpp"
4
5#include <sdbusplus/test/sdbus_mock.hpp>
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
10using ::testing::_;
11using ::testing::Invoke;
12using ::testing::IsNull;
13using ::testing::Return;
14using ::testing::StrEq;
15
16TEST(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));
Patrick Williams374fae52022-07-22 19:26:55 -050026 sdbusplus::message_t msg(nullptr, &sdbusMock);
Lei YU5e0dcb32019-08-02 18:04:34 +080027
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) {
Patrick Williams3f6c12b2023-10-20 11:19:11 -050045 const char** s = static_cast<const char**>(p);
46 // Read the first parameter, the string.
47 *s = path0;
48 return 0;
49 })).WillOnce(Invoke([&](sd_bus_message*, char, void* p) {
50 const char** s = static_cast<const char**>(p);
51 // Read the first parameter, the string.
52 *s = path1;
53 return 0;
54 }));
Lei YU5e0dcb32019-08-02 18:04:34 +080055
56 EXPECT_CALL(sdbusMock, sd_bus_message_exit_container(IsNull()))
57 .WillOnce(Return(0)); /* end of std::vector */
58
59 auto ret = utils::getPSUInventoryPath(bus);
60 EXPECT_EQ(2u, ret.size());
61 EXPECT_EQ(path0, ret[0]);
62 EXPECT_EQ(path1, ret[1]);
63}
Lei YUad90ad52019-08-06 11:19:28 +080064
65TEST(Utils, GetVersionID)
66{
Lei YUad90ad52019-08-06 11:19:28 +080067 auto ret = utils::getVersionId("");
68 EXPECT_EQ("", ret);
69
70 ret = utils::getVersionId("some version");
71 EXPECT_EQ(8u, ret.size());
72}
Lei YU4b9ac392019-10-12 11:02:26 +080073
74TEST(Utils, IsAssociated)
75{
76 std::string path = "/com/example/chassis/powersupply0";
77 utils::AssociationList assocs = {{ACTIVATION_FWD_ASSOCIATION,
78 ACTIVATION_REV_ASSOCIATION,
79 "a-different-path"}};
80
81 EXPECT_FALSE(utils::isAssociated(path, assocs));
82
83 assocs.emplace_back(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
84 path);
85 EXPECT_TRUE(utils::isAssociated(path, assocs));
86}