test: Move related tests to vpnor directory

Change-Id: I5fe7f625ad642cef17fe53bd244dcde8fd99f3ad
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/test/vpnor/Makefile.am.include b/test/vpnor/Makefile.am.include
new file mode 100644
index 0000000..0b5b1c9
--- /dev/null
+++ b/test/vpnor/Makefile.am.include
@@ -0,0 +1,46 @@
+TEST_MBOX_VPNOR_SRCS = common.c pnor_partition_table.cpp
+
+test_vpnor_create_pnor_partition_table_SOURCES = \
+	$(TEST_MBOX_VPNOR_SRCS) \
+	%reldir%/create_pnor_partition_table.cpp
+test_vpnor_create_pnor_partition_table_LDFLAGS = $(OESDK_TESTCASE_FLAGS)
+test_vpnor_create_pnor_partition_table_LDADD = -lstdc++fs \
+	$(SDBUSPLUS_LIBS) \
+	$(PHOSPHOR_LOGGING_LIBS) \
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS)
+
+test_vpnor_create_read_window_vpnor_SOURCES = \
+	$(TEST_MBOX_VPNOR_SRCS) $(TEST_MOCK_SRCS) \
+	mboxd_msg.c \
+	mboxd_windows.c \
+	mboxd_lpc.c \
+	mboxd_lpc_virtual.cpp \
+	mboxd_pnor_partition_table.cpp \
+	mboxd_flash_virtual.cpp \
+	pnor_partition.cpp \
+	%reldir%/create_read_window_vpnor.cpp
+test_vpnor_create_read_window_vpnor_LDFLAGS = $(OESDK_TESTCASE_FLAGS)
+test_vpnor_create_read_window_vpnor_LDADD = -lstdc++fs \
+	$(SDBUSPLUS_LIBS) \
+	$(PHOSPHOR_LOGGING_LIBS) \
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS)
+
+test_vpnor_write_flash_vpnor_SOURCES = \
+	$(TEST_MBOX_VPNOR_SRCS) \
+	mboxd_pnor_partition_table.cpp \
+	mboxd_flash_virtual.cpp \
+	mtd.c \
+	pnor_partition.cpp \
+	%reldir%/write_flash_vpnor.cpp
+test_vpnor_write_flash_vpnor_LDFLAGS = $(OESDK_TESTCASE_FLAGS)
+test_vpnor_write_flash_vpnor_LDADD = -lstdc++fs \
+	$(SDBUSPLUS_LIBS) \
+	$(PHOSPHOR_LOGGING_LIBS) \
+	$(PHOSPHOR_DBUS_INTERFACES_LIBS)
+
+if VIRTUAL_PNOR_ENABLED
+check_PROGRAMS += \
+	%reldir%/create_pnor_partition_table \
+	%reldir%/create_read_window_vpnor \
+	%reldir%/write_flash_vpnor
+endif
diff --git a/test/vpnor/create_pnor_partition_table.cpp b/test/vpnor/create_pnor_partition_table.cpp
new file mode 100644
index 0000000..57bb675
--- /dev/null
+++ b/test/vpnor/create_pnor_partition_table.cpp
@@ -0,0 +1,79 @@
+#include "pnor_partition_table.hpp"
+#include "config.h"
+#include <assert.h>
+#include <string.h>
+#include <vector>
+#include <fstream>
+#include <experimental/filesystem>
+
+constexpr auto line = "partition01=HBB,00000000,00000400,80,ECC,PRESERVED";
+constexpr auto partitionName = "HBB";
+char tmplt[] = "/tmp/vpnor_partitions.XXXXXX";
+
+int main()
+{
+    namespace fs = std::experimental::filesystem;
+
+    char* tmpdir = mkdtemp(tmplt);
+    assert(tmpdir != nullptr);
+
+    fs::path tocFilePath{tmpdir};
+    tocFilePath /= PARTITION_TOC_FILE;
+    std::ofstream tocFile(tocFilePath.c_str());
+    tocFile.write(line, strlen(line));
+    tocFile.close();
+
+    fs::path partitionFilePath{tmpdir};
+    partitionFilePath /= partitionName;
+    std::ofstream partitionFile(partitionFilePath.c_str());
+    std::vector<char> empty(1, 0); // 1 byte file
+    partitionFile.write(empty.data(), empty.size());
+    partitionFile.close();
+
+    const openpower::virtual_pnor::partition::Table table(
+        fs::path{tmpdir}, 4 * 1024, 64 * 1024 * 1024);
+
+    pnor_partition_table expectedTable{};
+    expectedTable.data.magic = PARTITION_HEADER_MAGIC;
+    expectedTable.data.version = PARTITION_VERSION_1;
+    expectedTable.data.size = 1; // 1 block
+    expectedTable.data.entry_size = sizeof(pnor_partition);
+    expectedTable.data.entry_count = 1; // 1 partition
+    expectedTable.data.block_size = 4096;
+    expectedTable.data.block_count =
+        (64 * 1024 * 1024) / expectedTable.data.block_size;
+    expectedTable.checksum =
+        openpower::virtual_pnor::details::checksum(expectedTable.data);
+
+    pnor_partition expectedPartition{};
+    strcpy(expectedPartition.data.name, partitionName);
+    expectedPartition.data.base = 0;       // starts at offset 0
+    expectedPartition.data.size = 1;       // 1 block
+    expectedPartition.data.actual = 0x400; // 1024 bytes
+    expectedPartition.data.id = 1;
+    expectedPartition.data.pid = PARENT_PATITION_ID;
+    expectedPartition.data.type = PARTITION_TYPE_DATA;
+    expectedPartition.data.flags = 0;
+    expectedPartition.data.user.data[0] = PARTITION_ECC_PROTECTED;
+    expectedPartition.data.user.data[1] |= PARTITION_PRESERVED;
+    expectedPartition.data.user.data[1] |= PARTITION_VERSION_CHECK_SHA512;
+    expectedPartition.checksum =
+        openpower::virtual_pnor::details::checksum(expectedPartition.data);
+
+    const pnor_partition_table& result = table.getNativeTable();
+
+    fs::remove_all(fs::path{tmpdir});
+
+    auto rc = memcmp(&expectedTable, &result, sizeof(pnor_partition_table));
+    assert(rc == 0);
+
+    rc = memcmp(&expectedPartition, &result.partitions[0],
+                sizeof(pnor_partition));
+    assert(rc == 0);
+
+    const pnor_partition& first = table.partition(0); // Partition at offset 0
+    rc = memcmp(&first, &result.partitions[0], sizeof(pnor_partition));
+    assert(rc == 0);
+
+    return 0;
+}
diff --git a/test/vpnor/create_read_window_vpnor.cpp b/test/vpnor/create_read_window_vpnor.cpp
new file mode 100644
index 0000000..3ea6bb5
--- /dev/null
+++ b/test/vpnor/create_read_window_vpnor.cpp
@@ -0,0 +1,93 @@
+#include "config.h"
+#include "mboxd_pnor_partition_table.h"
+
+extern "C" {
+#include "test/mbox.h"
+#include "test/system.h"
+}
+
+#include <assert.h>
+#include <string.h>
+
+#include <vector>
+#include <fstream>
+#include <experimental/filesystem>
+
+// A read window assumes that the toc is located at offset 0,
+// so create dummy partition at arbitrary offset 0x100.
+constexpr auto line = "partition01=HBB,00000100,0001000,ECC,PRESERVED";
+constexpr auto partition = "HBB";
+char tmplt[] = "/tmp/create_read_test.XXXXXX";
+uint8_t data[8] = {0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab};
+
+#define BLOCK_SIZE 4096
+#define MEM_SIZE (BLOCK_SIZE * 2)
+#define ERASE_SIZE BLOCK_SIZE
+#define N_WINDOWS 1
+#define WINDOW_SIZE BLOCK_SIZE
+
+static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
+                                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                   0x00, 0x00, 0x00, 0x00};
+// offset 0x100 and size 6
+static const uint8_t create_read_window[] = {0x04, 0x01, 0x01, 0x00, 0x06, 0x00,
+                                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                                             0x00, 0x00, 0x00, 0x00};
+
+static const uint8_t response[] = {0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01,
+                                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
+
+namespace fs = std::experimental::filesystem;
+
+int main()
+{
+    char* tmpdir = mkdtemp(tmplt);
+    assert(tmpdir != nullptr);
+
+    // create the toc file
+    fs::path tocFilePath{tmpdir};
+    tocFilePath /= PARTITION_TOC_FILE;
+    std::ofstream tocFile(tocFilePath.c_str());
+    tocFile.write(line, strlen(line));
+    tocFile.close();
+
+    // create the partition file
+    fs::path partitionFilePath{tmpdir};
+    partitionFilePath /= partition;
+    std::ofstream partitionFile(partitionFilePath.c_str());
+
+    partitionFile.write((char*)data, sizeof(data));
+    partitionFile.close();
+
+    system_set_reserved_size(MEM_SIZE);
+    system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE);
+
+    struct mbox_context* ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
+    strcpy(ctx->paths.ro_loc, tmpdir);
+    strcpy(ctx->paths.rw_loc, tmpdir);
+    strcpy(ctx->paths.prsv_loc, tmpdir);
+
+    vpnor_create_partition_table_from_path(ctx, tmpdir);
+
+    int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info));
+    assert(rc == 1);
+
+    // send the request for partition1
+    rc = mbox_command_dispatch(ctx, create_read_window,
+                               sizeof(create_read_window));
+    assert(rc == 1);
+
+    rc = mbox_cmp(ctx, response, sizeof(response));
+    assert(rc == 0);
+
+    // Compare the reserved memory to the pnor
+    rc = memcmp(ctx->mem, data, 6);
+    assert(rc == 0);
+
+    // TODO: Add few more test cases for read from multiple partitions(PRSV/RW)
+    //      Read beyond the partition file size.
+    //      openbmc/openbmc#1868
+
+    fs::remove_all(fs::path{tmpdir});
+    return rc;
+}
diff --git a/test/vpnor/write_flash_vpnor.cpp b/test/vpnor/write_flash_vpnor.cpp
new file mode 100644
index 0000000..049cb94
--- /dev/null
+++ b/test/vpnor/write_flash_vpnor.cpp
@@ -0,0 +1,264 @@
+/*
+ * MBox Daemon Test File
+ *
+ * Copyright 2017 IBM
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+extern "C" {
+#include "config.h"
+#include "common.h"
+#include "mboxd_flash.h"
+#include "mboxd_pnor_partition_table.h"
+#include "mbox.h"
+#include "test/tmpf.h"
+}
+
+#include <assert.h>
+#include <unistd.h>
+
+#include <fstream>
+#include <experimental/filesystem>
+
+#include <sys/mman.h>
+#include <sys/syslog.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+
+uint8_t data[8] = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7};
+
+#define BLOCK_SIZE 4096
+#define OFFSET BLOCK_SIZE
+#define MEM_SIZE (BLOCK_SIZE * 2)
+#define DATA_SIZE sizeof(data)
+#define ERASE_SIZE BLOCK_SIZE
+#define BLOCK_SIZE_SHIFT 12
+
+void init(struct mbox_context* ctx)
+{
+
+    namespace fs = std::experimental::filesystem;
+    using namespace std::string_literals;
+
+    std::string tocData =
+        "partition01=TEST1,00001000,00001400,ECC,READONLY\n"s +
+        "partition02=TEST2,00002000,00002008,ECC,READWRITE\n"s +
+        "partition03=TEST3,00003000,00003400,ECC,PRESERVED"s;
+
+    std::vector<std::string> templatePaths = {
+        "/tmp/ro.XXXXXX", "/tmp/rw.XXXXXX", "/tmp/prsv.XXXXXX",
+        "/tmp/patch.XXXXXX"};
+
+    std::vector<std::string> partitions = {"TEST1", "TEST2", "TEST3"};
+
+    // create various partition directory
+
+    std::string tmpROdir = mkdtemp(const_cast<char*>(templatePaths[0].c_str()));
+    assert(tmpROdir.length() != 0);
+
+    std::string tmpRWdir = mkdtemp(const_cast<char*>(templatePaths[1].c_str()));
+    assert(tmpRWdir.length() != 0);
+
+    std::string tmpPRSVdir =
+        mkdtemp(const_cast<char*>(templatePaths[2].c_str()));
+    assert(tmpPRSVdir.length() != 0);
+
+    std::string tmpPATCHdir =
+        mkdtemp(const_cast<char*>(templatePaths[3].c_str()));
+    assert(tmpPATCHdir.length() != 0);
+
+    // create the toc file
+    fs::path tocFilePath = tmpROdir;
+
+    tocFilePath /= PARTITION_TOC_FILE;
+    std::ofstream tocFile(tocFilePath.c_str());
+    tocFile.write(tocData.c_str(), static_cast<int>(tocData.length()));
+    tocFile.close();
+
+    // create the partition files in the ro directory
+    for (auto partition : partitions)
+    {
+        fs::path partitionFilePath{tmpROdir};
+        partitionFilePath /= partition;
+        std::ofstream partitionFile(partitionFilePath.c_str());
+        partitionFile.write(reinterpret_cast<char*>(data), sizeof(data));
+        partitionFile.close();
+    }
+
+    // copy partition2 file from ro to rw
+    std::string roFile = tmpROdir + "/" + "TEST2";
+    std::string rwFile = tmpRWdir + "/" + "TEST2";
+    assert(fs::copy_file(roFile, rwFile) == true);
+
+    mbox_vlog = &mbox_log_console;
+    verbosity = (verbose)2;
+
+    // setting context parameters
+    ctx->erase_size_shift = BLOCK_SIZE_SHIFT;
+    ctx->block_size_shift = BLOCK_SIZE_SHIFT;
+    ctx->flash_bmap = reinterpret_cast<uint8_t*>(
+        calloc(MEM_SIZE >> ctx->erase_size_shift, sizeof(*ctx->flash_bmap)));
+
+    strcpy(ctx->paths.ro_loc, tmpROdir.c_str());
+    strcpy(ctx->paths.rw_loc, tmpRWdir.c_str());
+    strcpy(ctx->paths.prsv_loc, tmpPRSVdir.c_str());
+    strcpy(ctx->paths.patch_loc, tmpPATCHdir.c_str());
+}
+
+int main(void)
+{
+    namespace fs = std::experimental::filesystem;
+
+    int rc{};
+    char src[DATA_SIZE]{0};
+    struct mbox_context context;
+    struct mbox_context* ctx = &context;
+    memset(ctx, 0, sizeof(mbox_context));
+
+    // Initialize the context before running the test case.
+    init(ctx);
+
+    std::string tmpROdir = ctx->paths.ro_loc;
+    std::string tmpRWdir = ctx->paths.rw_loc;
+    std::string tmpPRSVdir = ctx->paths.prsv_loc;
+    std::string tmpPATCHdir = ctx->paths.patch_loc;
+
+    // create the partition table
+    vpnor_create_partition_table_from_path(ctx, tmpROdir.c_str());
+
+    // Write to psrv partition
+
+    // As file doesn't exist there, so it copies
+    // the file from RO to PRSV and write the file in PRSV partition.
+
+    memset(src, 0xaa, sizeof(src));
+
+    rc = write_flash(ctx, (OFFSET * 3), src, sizeof(src));
+    assert(rc == 0);
+
+    auto fd = open((tmpPRSVdir + "/" + "TEST3").c_str(), O_RDONLY);
+    auto map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
+    assert(map != MAP_FAILED);
+
+    // verify it is written
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+    munmap(map, MEM_SIZE);
+    close(fd);
+
+    // Write to the RO partition
+    memset(src, 0x55, sizeof(src));
+    fd = open((tmpROdir + "/" + "TEST1").c_str(), O_RDONLY);
+    map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
+    assert(map != MAP_FAILED);
+    rc = write_flash(ctx, (OFFSET), src, sizeof(src));
+    // Should not be allowed to write on RO
+    assert(rc != 0);
+
+    munmap(map, MEM_SIZE);
+    close(fd);
+
+    // Write to the RW partition
+    memset(src, 0xbb, sizeof(src));
+    fd = open((tmpRWdir + "/" + "TEST2").c_str(), O_RDONLY);
+    map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
+    assert(map != MAP_FAILED);
+    rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    // write beyond the partition length as the partition
+    // file length is 8 byte(TEST2).
+    rc = write_flash(ctx, (OFFSET * 2 + 3), src, sizeof(src) + 20);
+    assert(rc == -1);
+
+    memset(src, 0xcc, sizeof(src));
+    rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    src[0] = 0xff;
+    rc = write_flash(ctx, (OFFSET * 2), src, 1);
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    src[1] = 0xff;
+    rc = write_flash(ctx, (OFFSET * 2) + 1, &src[1], 1);
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    src[2] = 0xff;
+    rc = write_flash(ctx, (OFFSET * 2) + 2, &src[2], 1);
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    munmap(map, MEM_SIZE);
+    close(fd);
+
+    // START Test patch location - Patch dir has preference over other locations
+    // Copy partition2 file from ro to patch to simulate a patch file that is
+    // different from the one in rw (partition2 in rw was modified with the
+    // previous write test)
+    fs::path roFile(tmpROdir);
+    roFile /= "TEST2";
+    fs::path patchFile(tmpPATCHdir);
+    patchFile /= "TEST2";
+    assert(fs::copy_file(roFile, patchFile) == true);
+
+    // Write arbitrary data
+    char srcPatch[DATA_SIZE]{0};
+    memset(srcPatch, 0x33, sizeof(srcPatch));
+    rc = write_flash(ctx, (OFFSET * 2), srcPatch, sizeof(srcPatch));
+    assert(rc == 0);
+
+    // Check that partition file in RW location still contains the original data
+    fs::path rwFile(tmpRWdir);
+    rwFile /= "TEST2";
+    fd = open(rwFile.c_str(), O_RDONLY);
+    map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
+    assert(map != MAP_FAILED);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+    munmap(map, MEM_SIZE);
+    close(fd);
+
+    // Check that partition file in PATCH location was written with the new data
+    fd = open(patchFile.c_str(), O_RDONLY);
+    map = mmap(NULL, MEM_SIZE, PROT_READ, MAP_PRIVATE, fd, 0);
+    assert(map != MAP_FAILED);
+    rc = memcmp(srcPatch, map, sizeof(srcPatch));
+    assert(rc == 0);
+    munmap(map, MEM_SIZE);
+    close(fd);
+
+    // Remove patch file so that subsequent tests don't use it
+    fs::remove(patchFile);
+    // END Test patch location
+
+    fs::remove_all(fs::path{tmpROdir});
+    fs::remove_all(fs::path{tmpRWdir});
+    fs::remove_all(fs::path{tmpPRSVdir});
+    fs::remove_all(fs::path{tmpPATCHdir});
+
+    destroy_vpnor(ctx);
+    free(ctx->flash_bmap);
+
+    return rc;
+}