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