blob: a623cf526e69f8723109e8fd11cdbdb8984e997a [file] [log] [blame]
Lei YU5e0dcb32019-08-02 18:04:34 +08001#include "utils.hpp"
2
3#include <sdbusplus/test/sdbus_mock.hpp>
4
5#include <gmock/gmock.h>
6#include <gtest/gtest.h>
7
8using ::testing::_;
9using ::testing::Invoke;
10using ::testing::IsNull;
11using ::testing::Return;
12using ::testing::StrEq;
13
14TEST(Utils, GetPSUInventoryPath)
15{
16 sdbusplus::SdBusMock sdbusMock;
17 auto bus = sdbusplus::get_mocked_new(&sdbusMock);
18
19 EXPECT_CALL(sdbusMock, sd_bus_message_new_method_call(
20 _, _, _, _, _, StrEq("GetSubTreePaths")));
21
22 EXPECT_CALL(sdbusMock, sd_bus_message_ref(IsNull()))
23 .WillOnce(Return(nullptr));
24 sdbusplus::message::message msg(nullptr, &sdbusMock);
25
26 const char* path0 = "/com/example/chassis/powersupply0";
27 const char* path1 = "/com/example/chassis/powersupply1";
28
29 // std::vector
30 EXPECT_CALL(sdbusMock,
31 sd_bus_message_enter_container(IsNull(), 'a', StrEq("s")))
32 .WillOnce(Return(1));
33
34 // while !at_end()
35 EXPECT_CALL(sdbusMock, sd_bus_message_at_end(IsNull(), 0))
36 .WillOnce(Return(0))
37 .WillOnce(Return(0))
38 .WillOnce(Return(1)); // So it exits the loop after reading two strings.
39
40 // std::string
41 EXPECT_CALL(sdbusMock, sd_bus_message_read_basic(IsNull(), 's', _))
42 .WillOnce(Invoke([&](sd_bus_message*, char, void* p) {
43 const char** s = static_cast<const char**>(p);
44 // Read the first parameter, the string.
45 *s = path0;
46 return 0;
47 }))
48 .WillOnce(Invoke([&](sd_bus_message*, char, void* p) {
49 const char** s = static_cast<const char**>(p);
50 // Read the first parameter, the string.
51 *s = path1;
52 return 0;
53 }));
54
55 EXPECT_CALL(sdbusMock, sd_bus_message_exit_container(IsNull()))
56 .WillOnce(Return(0)); /* end of std::vector */
57
58 auto ret = utils::getPSUInventoryPath(bus);
59 EXPECT_EQ(2u, ret.size());
60 EXPECT_EQ(path0, ret[0]);
61 EXPECT_EQ(path1, ret[1]);
62}
Lei YUad90ad52019-08-06 11:19:28 +080063
64TEST(Utils, GetVersionID)
65{
66
67 auto ret = utils::getVersionId("");
68 EXPECT_EQ("", ret);
69
70 ret = utils::getVersionId("some version");
71 EXPECT_EQ(8u, ret.size());
72}