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