test: vpnor: Extract write_rw from write_flash_vpnor

Change-Id: Idec2747e2233eea5631740ed702623b5e4409a1c
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/test/vpnor/Makefile.am.include b/test/vpnor/Makefile.am.include
index b28f586..c2ebb1f 100644
--- a/test/vpnor/Makefile.am.include
+++ b/test/vpnor/Makefile.am.include
@@ -55,11 +55,22 @@
 test_vpnor_write_ro_LDFLAGS = $(OESDK_TESTCASE_FLAGS)
 test_vpnor_write_ro_LDADD = $(VPNOR_LDADD)
 
+test_vpnor_write_rw_SOURCES = \
+	$(TEST_MBOX_VPNOR_SRCS) \
+	mboxd_pnor_partition_table.cpp \
+	mboxd_flash_virtual.cpp \
+	mtd.c \
+	pnor_partition.cpp \
+	%reldir%/write_rw.cpp
+test_vpnor_write_rw_LDFLAGS = $(OESDK_TESTCASE_FLAGS)
+test_vpnor_write_rw_LDADD = $(VPNOR_LDADD)
+
 if VIRTUAL_PNOR_ENABLED
 check_PROGRAMS += \
 	%reldir%/create_pnor_partition_table \
 	%reldir%/create_read_window_vpnor \
 	%reldir%/write_flash_vpnor \
 	%reldir%/write_prsv \
-	%reldir%/write_ro
+	%reldir%/write_ro \
+	%reldir%/write_rw
 endif
diff --git a/test/vpnor/write_flash_vpnor.cpp b/test/vpnor/write_flash_vpnor.cpp
index 9b6fc89..5bb6f51 100644
--- a/test/vpnor/write_flash_vpnor.cpp
+++ b/test/vpnor/write_flash_vpnor.cpp
@@ -101,16 +101,6 @@
     // create the partition table
     vpnor_create_partition_table_from_path(ctx, root.ro().c_str());
 
-    // Write to the RW partition
-    memset(src, 0xbb, sizeof(src));
-    fd = open((root.rw() / "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);
@@ -119,6 +109,9 @@
     memset(src, 0xcc, sizeof(src));
     rc = write_flash(ctx, (OFFSET * 2), src, sizeof(src));
     assert(rc == 0);
+    fd = open((root.rw() / "TEST2").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);
 
diff --git a/test/vpnor/write_rw.cpp b/test/vpnor/write_rw.cpp
new file mode 100644
index 0000000..eff959b
--- /dev/null
+++ b/test/vpnor/write_rw.cpp
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright (C) 2018 IBM Corp.
+
+#include <assert.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "mbox.h"
+#include "mboxd_flash.h"
+
+#include "test/vpnor/tmpd.hpp"
+
+static constexpr auto BLOCK_SIZE = 0x1000;
+
+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;
+    uint8_t src[8] = {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);
+    /* write_flash() doesn't copy the file for us */
+    assert(fs::copy_file(root.ro() / "TEST1", root.rw() / "TEST1"));
+    vpnor_create_partition_table_from_path(ctx, root.ro().c_str());
+
+    /* Test */
+    memset(src, 0xbb, sizeof(src));
+    rc = write_flash(ctx, 0x1000, src, sizeof(src));
+    assert(rc == 0);
+    fd = open((root.rw() / "TEST1").c_str(), O_RDONLY);
+    map = mmap(NULL, sizeof(src), PROT_READ, MAP_PRIVATE, fd, 0);
+    assert(map != MAP_FAILED);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    /* Ensure single byte writes function */
+    memset(src, 0xcc, sizeof(src));
+    rc = write_flash(ctx, 0x1000, src, sizeof(src));
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    src[0] = 0xff;
+    rc = write_flash(ctx, 0x1000, src, 1);
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    src[1] = 0xff;
+    rc = write_flash(ctx, 0x1000 + 1, &src[1], 1);
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    src[2] = 0xff;
+    rc = write_flash(ctx, 0x1000 + 2, &src[2], 1);
+    assert(rc == 0);
+    rc = memcmp(src, map, sizeof(src));
+    assert(rc == 0);
+
+    /* Writes past the end of the partition should fail */
+    rc = write_flash(ctx, 0x1000 + 0xff9, src, sizeof(src));
+    assert(rc < 0);
+
+    /* Check that RW file is unmodified after the bad 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(src, map, sizeof(src));
+    assert(rc == 0);
+
+    munmap(map, sizeof(src));
+    close(fd);
+
+    destroy_vpnor(ctx);
+
+    return 0;
+}