json builder: fix error with buildHandlerConfigs
Problem: mistakenly defined the implementation of buildHandlerConfigs in
a separate cpp file. This breaks calling buildHandlerConfigs from a
derived class.
Solution: move implementation of buildHandlerConfigs into the header.
unit tests added:
firmware_json_unittest.cpp * add a test to parse json from file
* add a test to parse an invalid json file
Signed-off-by: Jason Ling <jasonling@google.com>
Change-Id: I5cd93ad01a329850a8ee516fae8a35339c991ae0
diff --git a/bmc/firmware-handler/test/firmware_json_unittest.cpp b/bmc/firmware-handler/test/firmware_json_unittest.cpp
index 235b33c..89d4741 100644
--- a/bmc/firmware-handler/test/firmware_json_unittest.cpp
+++ b/bmc/firmware-handler/test/firmware_json_unittest.cpp
@@ -13,8 +13,8 @@
{
using ::testing::IsEmpty;
+static constexpr auto TESTFNAME = "test.json";
using json = nlohmann::json;
-
TEST(FirmwareJsonTest, InvalidHandlerType)
{
auto j2 = R"(
@@ -615,5 +615,61 @@
EXPECT_FALSE(h[0].actions->update == nullptr);
}
+TEST(FirmwareJsonTest, BuildFromFile)
+{
+ std::ofstream testfile;
+ testfile.open(TESTFNAME, std::ios::out);
+ auto good = R"(
+ [{
+ "blob" : "/flash/image",
+ "handler" : {
+ "type" : "file",
+ "path" : "/run/initramfs/bmc-image"
+ },
+ "actions" : {
+ "preparation" : {
+ "type" : "skip"
+ },
+ "verification" : {
+ "type" : "skip"
+ },
+ "update" : {
+ "type" : "skip"
+ }
+ }
+ }]
+ )"_json;
+ testfile << good.dump(4);
+ testfile.flush();
+ FirmwareHandlersBuilder b;
+ auto h = b.buildHandlerConfigs("./");
+ EXPECT_EQ(h.size(), 1);
+ EXPECT_EQ(h[0].blobId, "/flash/image");
+ EXPECT_FALSE(h[0].handler == nullptr);
+ EXPECT_FALSE(h[0].actions == nullptr);
+ EXPECT_FALSE(h[0].actions->preparation == nullptr);
+ EXPECT_FALSE(h[0].actions->verification == nullptr);
+ EXPECT_FALSE(h[0].actions->update == nullptr);
+ if (std::remove(TESTFNAME) != 0)
+ {
+ fprintf(stderr, "warning: filecleanup of %s failed\n", TESTFNAME);
+ }
+}
+
+TEST(FirmwareJsonTest, BuildFromBadFile)
+{
+ std::ofstream testfile;
+ testfile.open(TESTFNAME, std::ios::out);
+ testfile << "{] a malformed json {{";
+ testfile.flush();
+ FirmwareHandlersBuilder b;
+ auto h = b.buildHandlerConfigs("./");
+ EXPECT_THAT(h, IsEmpty());
+ if (std::remove(TESTFNAME) != 0)
+ {
+ fprintf(stderr, "warning: filecleanup of %s failed\n", TESTFNAME);
+ }
+}
+
} // namespace
} // namespace ipmi_flash