blob: afcbb47336de95f476047811c95dfcd5dd17e527 [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));
26 sdbusplus::message::message 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}
Lei YUad90ad52019-08-06 11:19:28 +080065
66TEST(Utils, GetVersionID)
67{
Lei YUad90ad52019-08-06 11:19:28 +080068 auto ret = utils::getVersionId("");
69 EXPECT_EQ("", ret);
70
71 ret = utils::getVersionId("some version");
72 EXPECT_EQ(8u, ret.size());
73}
Lei YU4b9ac392019-10-12 11:02:26 +080074
75TEST(Utils, IsAssociated)
76{
77 std::string path = "/com/example/chassis/powersupply0";
78 utils::AssociationList assocs = {{ACTIVATION_FWD_ASSOCIATION,
79 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}