blob: 554ab74d9c53475cb59b5713e4109e3e0685ea59 [file] [log] [blame]
Josh Lehan482c45a2022-03-29 17:10:44 -07001#include "include/openbmc_dbus_rest.hpp"
2
Nan Zhoudf91ea72022-06-13 23:58:49 +00003#include "gmock/gmock.h"
Josh Lehan482c45a2022-03-29 17:10:44 -07004#include "gtest/gtest.h"
5
6// Also see redfish-core/ut/configfile_test.cpp
7TEST(OpenbmcDbusRestTest, ValidFilenameGood)
8{
9 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("GoodConfigFile"));
10 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("_Underlines_"));
11 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("8675309"));
12 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("-Dashes-"));
13 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("With Spaces"));
14 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("One.Dot"));
15 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("trailingdot."));
16 EXPECT_TRUE(crow::openbmc_mapper::validateFilename("-_ o _-"));
17 EXPECT_TRUE(crow::openbmc_mapper::validateFilename(" "));
18 EXPECT_TRUE(crow::openbmc_mapper::validateFilename(" ."));
19}
20
21// There is no length test yet because validateFilename() does not care yet
22TEST(OpenbmcDbusRestTest, ValidFilenameBad)
23{
24 EXPECT_FALSE(crow::openbmc_mapper::validateFilename(""));
25 EXPECT_FALSE(crow::openbmc_mapper::validateFilename("Bad@file"));
26 EXPECT_FALSE(
27 crow::openbmc_mapper::validateFilename("/../../../../../etc/badpath"));
28 EXPECT_FALSE(crow::openbmc_mapper::validateFilename("/../../etc/badpath"));
29 EXPECT_FALSE(crow::openbmc_mapper::validateFilename("/mydir/configFile"));
30 EXPECT_FALSE(crow::openbmc_mapper::validateFilename("/"));
31 EXPECT_FALSE(crow::openbmc_mapper::validateFilename(".leadingdot"));
32 EXPECT_FALSE(crow::openbmc_mapper::validateFilename("Two..Dots"));
33 EXPECT_FALSE(
34 crow::openbmc_mapper::validateFilename("../../../../../../etc/shadow"));
35 EXPECT_FALSE(crow::openbmc_mapper::validateFilename("."));
36}
Nan Zhoudf91ea72022-06-13 23:58:49 +000037
38TEST(OpenBmcDbusTest, TestArgSplit)
39{
40 // test the basic types
41 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("x"),
42 ::testing::ElementsAre("x"));
43 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("y"),
44 ::testing::ElementsAre("y"));
45 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("b"),
46 ::testing::ElementsAre("b"));
47 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("n"),
48 ::testing::ElementsAre("n"));
49 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("q"),
50 ::testing::ElementsAre("q"));
51 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("i"),
52 ::testing::ElementsAre("i"));
53 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("u"),
54 ::testing::ElementsAre("u"));
55 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("x"),
56 ::testing::ElementsAre("x"));
57 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("t"),
58 ::testing::ElementsAre("t"));
59 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("d"),
60 ::testing::ElementsAre("d"));
61 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("h"),
62 ::testing::ElementsAre("h"));
63 // test arrays
64 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("ai"),
65 ::testing::ElementsAre("ai"));
66 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("ax"),
67 ::testing::ElementsAre("ax"));
68 // test tuples
69 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("(sss)"),
70 ::testing::ElementsAre("(sss)"));
71 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("(sss)b"),
72 ::testing::ElementsAre("(sss)", "b"));
73 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("b(sss)"),
74 ::testing::ElementsAre("b", "(sss)"));
75
76 // Test nested types
77 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("a{si}b"),
78 ::testing::ElementsAre("a{si}", "b"));
79 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("a(sss)b"),
80 ::testing::ElementsAre("a(sss)", "b"));
81 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("aa{si}b"),
82 ::testing::ElementsAre("aa{si}", "b"));
83 EXPECT_THAT(crow::openbmc_mapper::dbusArgSplit("i{si}b"),
84 ::testing::ElementsAre("i", "{si}", "b"));
85}