test: Add temporary directory helper for virtual PNOR tests

Move the createVpnorTree() helper function out from the
create_pnor_partition_table test case into its own header. This way
multiple tests can make use of its function.

Change-Id: Ieb4149e736c7ff87ecdbf7aa586b58baf936cd97
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/test/vpnor/tmpd.hpp b/test/vpnor/tmpd.hpp
new file mode 100644
index 0000000..ab27377
--- /dev/null
+++ b/test/vpnor/tmpd.hpp
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+/* Copyright (C) 2018 IBM Corp. */
+
+#include <assert.h>
+#include <string.h>
+#include <vector>
+#include <fstream>
+#include <experimental/filesystem>
+
+#include "config.h"
+#include "pnor_partition_table.hpp"
+
+namespace fs = std::experimental::filesystem;
+
+namespace openpower
+{
+namespace virtual_pnor
+{
+namespace test
+{
+
+template <std::size_t N>
+void createVpnorTree(fs::path &root, const std::string (&toc)[N],
+                     size_t blockSize)
+{
+    fs::path tocFilePath{root};
+    tocFilePath /= PARTITION_TOC_FILE;
+    std::ofstream tocFile(tocFilePath.c_str());
+
+    for (const std::string &line : toc)
+    {
+        pnor_partition part;
+
+        openpower::virtual_pnor::parseTocLine(line, blockSize, part);
+
+        /* Populate the partition in the tree */
+        fs::path partitionFilePath{root};
+        partitionFilePath /= part.data.name;
+        std::ofstream partitionFile(partitionFilePath.c_str());
+        std::vector<char> empty(part.data.size, 0);
+        partitionFile.write(empty.data(), empty.size());
+        partitionFile.close();
+
+        /* Update the ToC if the partition file was created */
+        tocFile.write(line.c_str(), line.length());
+        tocFile.write("\n", 1);
+    }
+
+    tocFile.close();
+}
+
+} // test
+} // virtual_pnor
+} // openpower