test: vpnor: Rename write_flash_pnor to write_patch and clean up

Change-Id: I36f3946487d0b613fa0ada9bfa777e4d12444fac
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/test/vpnor/write_patch.cpp b/test/vpnor/write_patch.cpp
new file mode 100644
index 0000000..b0d5fa7
--- /dev/null
+++ b/test/vpnor/write_patch.cpp
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright (C) 2018 IBM Corp.
+
+#include <assert.h>
+#include <experimental/filesystem>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/syslog.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "common.h"
+#include "mbox.h"
+#include "mboxd_flash.h"
+
+#include "test/vpnor/tmpd.hpp"
+
+static constexpr auto BLOCK_SIZE = 0x1000;
+static constexpr auto DATA_SIZE = 8;
+
+const uint8_t data[DATA_SIZE] = {0xa0, 0xa1, 0xa2, 0xa3,
+                                 0xa4, 0xa5, 0xa6, 0xa7};
+
+const std::string toc[] = {
+    "partition01=TEST1,00001000,00002000,80,ECC,READWRITE",
+};
+
+int main(void)
+{
+    namespace fs = std::experimental::filesystem;
+    namespace test = openpower::virtual_pnor::test;
+
+    struct mbox_context _ctx, *ctx = &_ctx;
+    char src[DATA_SIZE]{0};
+    void *map;
+    int rc;
+    int fd;
+
+    /* Setup */
+    memset(ctx, 0, sizeof(mbox_context));
+
+    mbox_vlog = &mbox_log_console;
+    verbosity = (verbose)2;
+
+    test::VpnorRoot root(ctx, toc, BLOCK_SIZE);
+    root.write("TEST1", data, sizeof(data));
+    /* write_flash doesn't copy the file for us */
+    assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
+    fs::path patch = root.patch() / "TEST1";
+    assert(fs::copy_file(root.ro() / "TEST1", patch));
+
+    vpnor_create_partition_table_from_path(ctx, root.ro().c_str());
+
+    /* Test */
+    memset(src, 0x33, sizeof(src));
+    rc = write_flash(ctx, 0x1000, src, sizeof(src));
+    assert(rc == 0);
+
+    /* Check that RW file is unmodified after the patch write */
+    fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
+    map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
+    assert(map != MAP_FAILED);
+    rc = memcmp(data, map, sizeof(src));
+    assert(rc == 0);
+    munmap(map, sizeof(src));
+    close(fd);
+
+    /* Check that PATCH is modified with the new data */
+    fd = open(patch.c_str(), O_RDONLY);
+    map = mmap(NULL, sizeof(src), PROT_READ, MAP_SHARED, fd, 0);
+    assert(map != MAP_FAILED);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+    munmap(map, sizeof(src));
+    close(fd);
+
+    destroy_vpnor(ctx);
+    free(ctx->flash_bmap);
+
+    return rc;
+}