test(slot buffer): add assertion tests

This patch also fixes the dest and src addresses not being checked for
ns_buffer_read() and ns_buffer_write() respectively as well as some
lines exceeding 80 cols on granule.cpp

Signed-off-by: Javier Almansa Sobrino <javier.almansasobrino@arm.com>
Change-Id: I71cac6e85294fac828c950b227cf9d9fb27164ae
diff --git a/lib/realm/tests/buffer.cpp b/lib/realm/tests/buffer.cpp
index 83e553c..6b04bec 100644
--- a/lib/realm/tests/buffer.cpp
+++ b/lib/realm/tests/buffer.cpp
@@ -11,6 +11,7 @@
 #include <buffer_private.h>
 #include <cpuid.h>
 #include <granule.h>
+#include <host_defs.h>
 #include <host_harness.h>
 #include <host_utils.h>
 #include <realm_test_utils.h>
@@ -30,8 +31,7 @@
 #define GRANULE_BLOCKS			(GRANULE_SIZE/GRANULE_BLOCK_SIZE)
 
 /*
- * Function to get a random address within the granules range.
- * The address will be aligned to granule size.
+ * Function to get a random granule address within the valid address range.
  */
 static inline uintptr_t get_rand_granule_addr(void) {
 	uintptr_t addr;
@@ -239,11 +239,6 @@
 			buffer_unmap((void *)slot_va[j]);
 		}
 	} /* NR_CPU_SLOTS */
-
-	/*
-	 * granule_map() asserts if the granule address is not aligned, so
-	 * skip that test.
-	 */
 };
 
 TEST(slot_buffer, granule_map_buffer_unmap_TC3)
@@ -272,6 +267,169 @@
 	TEST_EXIT;
 }
 
+ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC5)
+{
+	uintptr_t granule_addr;
+	struct granule *test_granule;
+	union test_harness_cbs cb;
+	unsigned int cpuid;
+
+	/******************************************************************
+	 * TEST CASE 5:
+	 *
+	 * For a random CPU, try to map a random granule to a SLOT_NS buffer.
+	 * The operation should generate an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	granule_addr = get_rand_granule_addr();
+	test_granule = addr_to_granule(granule_addr);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0, MAX_CPUS - 1);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	(void)granule_map(test_granule, SLOT_NS);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC6)
+{
+	union test_harness_cbs cb;
+	unsigned int cpuid;
+	enum buffer_slot slot;
+
+	/******************************************************************
+	 * TEST CASE 6:
+	 *
+	 * For a random CPU, try to map a NULL granule address to a random
+	 * slot type other than SLOT_NS.
+	 * The operation should generate an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
+						SLOT_NS + 1U, NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0, MAX_CPUS - 1);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	(void)granule_map((struct granule *)NULL, slot);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC7)
+{
+	union test_harness_cbs cb;
+	unsigned int cpuid;
+	enum buffer_slot slot;
+	struct granule *test_granule;
+
+	/******************************************************************
+	 * TEST CASE 7:
+	 *
+	 * For a random CPU, try to map a granule address less than the
+	 * start of valid granule addr range to a random slot type other
+	 * than SLOT_NS.
+	 * The operation should generate an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	test_granule = realm_test_util_granule_struct_base() - 1U;
+	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
+						SLOT_NS + 1U, NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0, MAX_CPUS - 1);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	(void)granule_map(test_granule, slot);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC8)
+{
+	union test_harness_cbs cb;
+	unsigned int cpuid;
+	enum buffer_slot slot;
+	struct granule *test_granule;
+
+	/******************************************************************
+	 * TEST CASE 8:
+	 *
+	 * For a random CPU, try to map a granule address over the end of
+	 * the granules array to a random slot type other than SLOT_NS.
+	 * The operation should generate an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	test_granule = realm_test_util_granule_struct_base() + \
+							HOST_NR_GRANULES;
+	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
+						SLOT_NS + 1U, NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0, MAX_CPUS - 1);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	(void)granule_map(test_granule, slot);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC9)
+{
+	uintptr_t granule_addr;
+	uintptr_t test_granule;
+	union test_harness_cbs cb;
+	unsigned int cpuid;
+	enum buffer_slot slot;
+
+	/******************************************************************
+	 * TEST CASE 9:
+	 *
+	 * For a random CPU, try to map an unaligned granule address to a
+	 * random slot type other than SLOT_NS.
+	 * The operation should generate an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	granule_addr = get_rand_granule_addr();
+	test_granule = (uintptr_t)addr_to_granule(granule_addr);
+	test_granule += test_helpers_get_rand_in_range(1,
+						sizeof(struct granule) - 1);
+
+	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
+						SLOT_NS + 1U, NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0, MAX_CPUS - 1);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	(void)granule_map((struct granule*)test_granule, slot);
+	test_helpers_fail_if_no_assert_failed();
+}
+
 TEST(slot_buffer, ns_buffer_write_TC1)
 {
 	uintptr_t granule_addrs[3];
@@ -459,17 +617,257 @@
 	 */
 	val = *(long *)granule_addrs[1];
 	CHECK_FALSE(val == pattern[0]);
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_write_TC4)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+	enum buffer_slot slot;
+
+	/******************************************************************
+	 * TEST CASE 4:
+	 *
+	 * for a random CPU, try to call ns_buffer_write() with a
+	 * random secure slot.
+	 * ns_buffer_write() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/* Get a random slot. Secure slots are after SLOT_NS */
+	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
+						SLOT_NS + 1U, NR_CPU_SLOTS);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_write(slot, addr_to_granule(granule_addrs[0]), 0U,
+			(size_t)GRANULE_SIZE, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_write_TC5)
+{
+	uintptr_t granule_addr;
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 5:
+	 *
+	 * for a random CPU, try to call ns_buffer_write() with a
+	 * NULL pointer to copy from.
+	 * ns_buffer_write() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	granule_addr = get_rand_granule_addr();
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_write(SLOT_NS, addr_to_granule(granule_addr), 0U,
+			(size_t)GRANULE_SIZE, NULL);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_write_TC6)
+{
+	uintptr_t granule_addr;
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 6:
+	 *
+	 * for a random CPU, try to call ns_buffer_write() with a
+	 * NULL granule to topy to.
+	 * ns_buffer_write() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	granule_addr = get_rand_granule_addr();
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_write(SLOT_NS, NULL, 0U,
+			(size_t)GRANULE_SIZE, (void *)granule_addr);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_write_TC7)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+	size_t size;
+
+	/******************************************************************
+	 * TEST CASE 7:
+	 *
+	 * for a random CPU, try to call ns_buffer_write() with a
+	 * size not aligned to 8 bytes.
+	 * ns_buffer_write() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/* Get a random size between 1 and 7 bytes */
+	size = (size_t)test_helpers_get_rand_in_range(1, 7);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
+			size, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_write_TC8)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+	unsigned int offset;
+
+	/******************************************************************
+	 * TEST CASE 8:
+	 *
+	 * for a random CPU, try to call ns_buffer_write() with an
+	 * offset not aligned to 8 bytes.
+	 * ns_buffer_write() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/* Get a random offset between 1 and 7 */
+	offset = test_helpers_get_rand_in_range(1, 7);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
+			GRANULE_SIZE, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_write_TC9)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 9:
+	 *
+	 * for a random CPU, try to call ns_buffer_write() with an
+	 * source not aligned to 8 bytes.
+	 * ns_buffer_write() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
 
 	/*
-	 * ns_buffer_write() will assert if:
-	 *	- The slot is not a non-secure one.
-	 *	- The granule to read from is NULL.
-	 *	- The size is not aligned to a byte size.
-	 *	- The offset is not aligned to a byte size.
-	 *	- The source is not aligned to a byte size.
-	 *	- The offset + size overflows the granule size.
-	 * So skip tests for these cases.
+	 * Misalign the address of the source.
+	 * test_helpers_get_rand_in_range() will never return an address for
+	 * the last granule, so we are safe increasing the address.
 	 */
+	granule_addrs[1] += test_helpers_get_rand_in_range(1, 7);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
+			GRANULE_SIZE, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_write_TC10)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	size_t size;
+	unsigned int offset;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 10:
+	 *
+	 * for a random CPU, try to call ns_buffer_write() with an
+	 * offset + size higher than GRANULE_SIZE.
+	 * ns_buffer_write() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/*
+	 * offset + granule = 1.5 * granule_size.
+	 * Both parameters are properly aligned.
+	 */
+	offset = GRANULE_SIZE >> 1U;
+	size = (size_t)GRANULE_SIZE;
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_write(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
+			size, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
 }
 
 TEST(slot_buffer, ns_buffer_read_TC1)
@@ -656,17 +1054,257 @@
 	 */
 	val = *(long *)granule_addrs[1];
 	CHECK_FALSE(val == dest[0]);
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_read_TC4)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+	enum buffer_slot slot;
+
+	/******************************************************************
+	 * TEST CASE 4:
+	 *
+	 * for a random CPU, try to call ns_buffer_read() with a
+	 * random secure slot.
+	 * ns_buffer_read() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/* Get a random slot. Secure slots are after SLOT_NS */
+	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
+						SLOT_NS + 1U, NR_CPU_SLOTS);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_read(slot, addr_to_granule(granule_addrs[0]), 0U,
+		       (size_t)GRANULE_SIZE, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_read_TC5)
+{
+	uintptr_t granule_addr;
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 5:
+	 *
+	 * for a random CPU, try to call ns_buffer_read() with a
+	 * NULL pointer to copy to.
+	 * ns_buffer_read() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	granule_addr = get_rand_granule_addr();
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_read(SLOT_NS, addr_to_granule(granule_addr), 0U,
+		       (size_t)GRANULE_SIZE, NULL);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_read_TC6)
+{
+	uintptr_t granule_addr;
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 6:
+	 *
+	 * for a random CPU, try to call ns_buffer_read() with a
+	 * NULL granule to copy from.
+	 * ns_buffer_read() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	granule_addr = get_rand_granule_addr();
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_read(SLOT_NS, NULL, 0U,
+		       (size_t)GRANULE_SIZE, (void *)granule_addr);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_read_TC7)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+	size_t size;
+
+	/******************************************************************
+	 * TEST CASE 7:
+	 *
+	 * for a random CPU, try to call ns_buffer_read() with a
+	 * size not aligned to 8 bytes.
+	 * ns_buffer_read() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/* Get a random size between 1 and 7 bytes */
+	size = (size_t)test_helpers_get_rand_in_range(1, 7);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
+		       size, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_read_TC8)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+	unsigned int offset;
+
+	/******************************************************************
+	 * TEST CASE 8:
+	 *
+	 * for a random CPU, try to call ns_buffer_read() with an
+	 * offset not aligned to 8 bytes.
+	 * ns_buffer_read() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/* Get a random offset between 1 and 7 */
+	offset = test_helpers_get_rand_in_range(1, 7);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
+		       GRANULE_SIZE, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_read_TC9)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 9:
+	 *
+	 * for a random CPU, try to call ns_buffer_read() with a
+	 * destination not aligned to 8 bytes.
+	 * ns_buffer_read() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
 
 	/*
-	 * ns_buffer_read() will assert if:
-	 *	- The slot is not a non-secure one.
-	 *	- The granule to read from is NULL.
-	 *	- The size is not aligned to a byte size.
-	 *	- The offset is not aligned to a byte size.
-	 *	- The dest is not aligned to a byte size.
-	 *	- The offset + size overflows the granule size.
-	 * So skip tests for these cases.
+	 * Misalign the address of the destination.
+	 * test_helpers_get_rand_in_range() will never return an address for
+	 * the last granule, so we are safe increasing the address.
 	 */
+	granule_addrs[1] += test_helpers_get_rand_in_range(1, 7);
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), 0U,
+		       GRANULE_SIZE, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+ASSERT_TEST(slot_buffer, ns_buffer_read_TC10)
+{
+	uintptr_t granule_addrs[2];
+	unsigned int cpuid;
+	size_t size;
+	unsigned int offset;
+	union test_harness_cbs cb;
+
+	/******************************************************************
+	 * TEST CASE 10:
+	 *
+	 * for a random CPU, try to call ns_buffer_read() with an
+	 * offset + size higher than GRANULE_SIZE.
+	 * ns_buffer_read() should cause an assertion failure.
+	 ******************************************************************/
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = test_buffer_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = test_buffer_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/* Get two random granules, one for destination and one for source. */
+	get_rand_granule_array(granule_addrs, 2U);
+
+	/*
+	 * offset + granule = 1.5 * granule_size.
+	 * Both parameters are properly aligned.
+	 */
+	offset = GRANULE_SIZE >> 1U;
+	size = (size_t)GRANULE_SIZE;
+
+	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	host_util_set_cpuid(cpuid);
+
+	test_helpers_expect_assert_fail(true);
+	ns_buffer_read(SLOT_NS, addr_to_granule(granule_addrs[0]), offset,
+		       size, (void *)granule_addrs[1]);
+	test_helpers_fail_if_no_assert_failed();
 }
 
 TEST(slot_buffer, slot_buf_setup_xlat_TC1)
diff --git a/lib/realm/tests/granule.cpp b/lib/realm/tests/granule.cpp
index a2b5029..553c431 100644
--- a/lib/realm/tests/granule.cpp
+++ b/lib/realm/tests/granule.cpp
@@ -89,15 +89,6 @@
 	return true;
 }
 
-/*
- * Function to return a pointer to the first granule structure.
- * This function relies on addr_to_granule().
- */
-static inline struct granule *get_granule_struct_base(void)
-{
-	return addr_to_granule(host_util_get_granule_base());
-}
-
 TEST_GROUP(granule) {
 
 
@@ -126,7 +117,7 @@
 		 * Clean RMM's internal struct granule array
 		 * for a clean state for the next tests.
 		 */
-		memset((void *)get_granule_struct_base(), 0,
+		memset((void *)realm_test_util_granule_struct_base(), 0,
 			sizeof(struct granule) *
 					test_helpers_get_nr_granules());
 
@@ -159,7 +150,8 @@
 
 	for (unsigned int i = 0U; i < 3; i++) {
 		/* Calculate the expected granule address */
-		expected_granule = get_granule_struct_base() + granule_indexes[i];
+		expected_granule = realm_test_util_granule_struct_base() +
+							granule_indexes[i];
 		/* Calculated the expected PA for the granule */
 		addr = (granule_indexes[i] * GRANULE_SIZE) +
 						host_util_get_granule_base();
@@ -253,7 +245,8 @@
 	 * granules in between.
 	 ******************************************************************/
 	for (unsigned int i = 0U; i < 3U; i++) {
-		granule = get_granule_struct_base() + granule_indexes[i];
+		granule = realm_test_util_granule_struct_base() +
+							granule_indexes[i];
 		expected_address = (granule_indexes[i] * GRANULE_SIZE) +
 						host_util_get_granule_base();
 		addr = granule_addr(granule);
@@ -294,7 +287,7 @@
 	 ******************************************************************/
 
 	idx += test_helpers_get_rand_in_range(1, 10);
-	granule = get_granule_struct_base() + idx;
+	granule = realm_test_util_granule_struct_base() + idx;
 	test_helpers_expect_assert_fail(true);
 	(void)granule_addr(granule);
 	test_helpers_fail_if_no_assert_failed();
@@ -311,7 +304,7 @@
 	 * granule[0];
 	 ******************************************************************/
 
-	granule = get_granule_struct_base() - 1U;
+	granule = realm_test_util_granule_struct_base() - 1U;
 	test_helpers_expect_assert_fail(true);
 	(void)granule_addr(granule);
 	test_helpers_fail_if_no_assert_failed();
@@ -329,7 +322,7 @@
 	 * not properly aligned.
 	 ******************************************************************/
 
-	granule = (uintptr_t)get_granule_struct_base();
+	granule = (uintptr_t)realm_test_util_granule_struct_base();
 	granule += test_helpers_get_rand_in_range(1,
 					sizeof(struct granule) - 1U);
 	test_helpers_expect_assert_fail(true);
@@ -342,7 +335,8 @@
 {
 	struct granule *granule;
 	unsigned long addr = get_rand_granule_addr();
-	unsigned long val = (unsigned long)test_helpers_get_rand_in_range(10, INT_MAX);
+	unsigned long val =
+		(unsigned long)test_helpers_get_rand_in_range(10, INT_MAX);
 	unsigned long read_val;
 
 	/******************************************************************
@@ -374,7 +368,8 @@
 {
 	struct granule *granule;
 	unsigned long addr = get_rand_granule_addr();
-	unsigned long val = (unsigned long)test_helpers_get_rand_in_range(10, 10000);
+	unsigned long val =
+		(unsigned long)test_helpers_get_rand_in_range(10, 10000);
 	unsigned long read_val;
 
 	/******************************************************************
@@ -422,7 +417,8 @@
 	 ******************************************************************/
 
 	for (unsigned int i = 0U; i < 3U; i++) {
-		expected_granule = get_granule_struct_base() + granule_indexes[i];
+		expected_granule = realm_test_util_granule_struct_base() + \
+							granule_indexes[i];
 		address = (granule_indexes[i] * GRANULE_SIZE) +
 						host_util_get_granule_base();
 		granule = find_granule(address);
@@ -501,8 +497,8 @@
 	} while (g1_index == g2_index);
 
 	/* Get the expected address for the granules */
-	exp_g1 = get_granule_struct_base() + g1_index;
-	exp_g2 = get_granule_struct_base() + g2_index;
+	exp_g1 = realm_test_util_granule_struct_base() + g1_index;
+	exp_g2 = realm_test_util_granule_struct_base() + g2_index;
 
 	/* Get the expected PA for the corresponding granules */
 	addr1 = (g1_index * GRANULE_SIZE) + host_util_get_granule_base();
@@ -1310,7 +1306,8 @@
 {
 	unsigned long address = get_rand_granule_addr();
 	struct granule *granule = find_granule(address);
-	unsigned long val = (unsigned long)test_helpers_get_rand_in_range(1, INT_MAX);
+	unsigned long val =
+		(unsigned long)test_helpers_get_rand_in_range(1, INT_MAX);
 
 	/******************************************************************
 	 * TEST CASE 1:
@@ -1336,7 +1333,8 @@
 {
 	unsigned long address = get_rand_granule_addr();
 	struct granule *granule = find_granule(address);
-	unsigned long val = (unsigned long)test_helpers_get_rand_in_range(10, INT_MAX);
+	unsigned long val =
+		(unsigned long)test_helpers_get_rand_in_range(10, INT_MAX);
 
 	/******************************************************************
 	 * TEST CASE 1:
@@ -1361,7 +1359,8 @@
 {
 	unsigned long address = get_rand_granule_addr();
 	struct granule *granule = find_granule(address);
-	unsigned long val = (unsigned long)test_helpers_get_rand_in_range(10, INT_MAX);
+	unsigned long val =
+		(unsigned long)test_helpers_get_rand_in_range(10, INT_MAX);
 
 	/******************************************************************
 	 * TEST CASE 2:
@@ -1392,7 +1391,8 @@
 {
 	unsigned long address = get_rand_granule_addr();
 	struct granule *granule = find_granule(address);
-	unsigned long val = (unsigned long)test_helpers_get_rand_in_range(10, INT_MAX - 1);
+	unsigned long val =
+		(unsigned long)test_helpers_get_rand_in_range(10, INT_MAX - 1);
 
 	/******************************************************************
 	 * TEST CASE 3:
diff --git a/lib/realm/tests/realm_test_utils.c b/lib/realm/tests/realm_test_utils.c
index 9bc3bde..9f00b97 100644
--- a/lib/realm/tests/realm_test_utils.c
+++ b/lib/realm/tests/realm_test_utils.c
@@ -5,6 +5,8 @@
 
 #include <buffer.h>
 #include <buffer_private.h>
+#include <granule.h>
+#include <host_utils.h>
 #include <xlat_tables.h>
 
 /*
@@ -45,3 +47,12 @@
 	/* No buffer slot found */
 	return (uintptr_t)NULL;
 }
+
+/*
+ * Function to return the base pointer to granule structure.
+ * This function relies on addr_to_granule().
+ */
+struct granule *realm_test_util_granule_struct_base(void)
+{
+	return addr_to_granule(host_util_get_granule_base());
+}
diff --git a/lib/realm/tests/realm_test_utils.h b/lib/realm/tests/realm_test_utils.h
index 9dcc56c..1787907 100644
--- a/lib/realm/tests/realm_test_utils.h
+++ b/lib/realm/tests/realm_test_utils.h
@@ -10,5 +10,6 @@
 
 uintptr_t realm_test_util_slot_to_pa(enum buffer_slot slot);
 uintptr_t realm_test_util_slot_va_from_pa(uintptr_t pa);
+struct granule *realm_test_util_granule_struct_base(void);
 
 #endif /* REALM_TEST_UTILS_H */