fix(unittest): fix implementation of test_helpers_get_rand_in_range()

Current implementaton of test_helpers_get_rand_in_range() returns a 32
bit integer which is not enough for certain tests which expect a 64
bit value.

This patch fixes the implementation of such function as well as it
updates the callers to accomodate the new declaration.

Signed-off-by: Javier Almansa Sobrino <javier.almansasobrino@arm.com>
Change-Id: I9d54d4d608a541992f50cb2a125707283df4b8a2
diff --git a/lib/realm/tests/buffer.cpp b/lib/realm/tests/buffer.cpp
index 4be126e..444c61c 100644
--- a/lib/realm/tests/buffer.cpp
+++ b/lib/realm/tests/buffer.cpp
@@ -36,8 +36,8 @@
 {
 	uintptr_t addr;
 
-	int random_granule = test_helpers_get_rand_in_range(0,
-					test_helpers_get_nr_granules() - 1);
+	unsigned long random_granule = test_helpers_get_rand_in_range(0UL,
+				test_helpers_get_nr_granules() - 1);
 
 	addr = (uintptr_t)(random_granule * GRANULE_SIZE)
 					+ host_util_get_granule_base();
@@ -285,7 +285,8 @@
 
 	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);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								(MAX_CPUS - 1));
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -314,8 +315,10 @@
 	(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);
+						(unsigned long)(SLOT_NS + 1U),
+						(unsigned long)NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+						(unsigned long)(MAX_CPUS - 1));
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -347,8 +350,10 @@
 
 	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);
+						(unsigned long)(SLOT_NS + 1U),
+						(unsigned long)NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+						(unsigned long)(MAX_CPUS - 1));
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -380,8 +385,10 @@
 	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);
+						(unsigned long)(SLOT_NS + 1U),
+						(unsigned long)NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -413,12 +420,14 @@
 
 	granule_addr = get_rand_granule_addr();
 	test_granule = (uintptr_t)addr_to_granule(granule_addr);
-	test_granule += test_helpers_get_rand_in_range(1,
+	test_granule += test_helpers_get_rand_in_range(1UL,
 						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);
+						(unsigned long)(SLOT_NS + 1U),
+						(unsigned long)NR_CPU_SLOTS);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -583,8 +592,10 @@
 
 	/* Get two random CPUs where to run the tests. */
 	do {
-		cpu[0] = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
-		cpu[1] = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+		cpu[0] = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
+		cpu[1] = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	} while (cpu[0] == cpu[1]);
 
 	/* Get two different patterns of data to copy. */
@@ -641,9 +652,11 @@
 
 	/* 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);
+						(unsigned long)(SLOT_NS + 1U),
+						(unsigned long)NR_CPU_SLOTS);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+					(unsigned long)(MAX_CPUS - 1U));
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -674,7 +687,8 @@
 
 	granule_addr = get_rand_granule_addr();
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -705,7 +719,8 @@
 
 	granule_addr = get_rand_granule_addr();
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -739,9 +754,10 @@
 	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);
+	size = (size_t)test_helpers_get_rand_in_range(1UL, 7UL);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -775,9 +791,10 @@
 	get_rand_granule_array(granule_addrs, 2U);
 
 	/* Get a random offset between 1 and 7 */
-	offset = test_helpers_get_rand_in_range(1, 7);
+	offset = (unsigned int)test_helpers_get_rand_in_range(1UL, 7UL);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -814,9 +831,11 @@
 	 * 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);
+	granule_addrs[1] += (unsigned int)test_helpers_get_rand_in_range(1UL,
+									 7UL);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -857,7 +876,8 @@
 	offset = GRANULE_SIZE >> 1U;
 	size = (size_t)GRANULE_SIZE;
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -1022,8 +1042,10 @@
 
 	/* Get two random CPUs where to run the tests. */
 	do {
-		cpu[0] = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
-		cpu[1] = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+		cpu[0] = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
+		cpu[1] = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	} while (cpu[0] == cpu[1]);
 
 	/* Store random data at the beginning of each granule */
@@ -1078,9 +1100,11 @@
 
 	/* 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);
+						(unsigned long)(SLOT_NS + 1U),
+						(unsigned long)NR_CPU_SLOTS);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -1111,7 +1135,8 @@
 
 	granule_addr = get_rand_granule_addr();
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -1142,7 +1167,8 @@
 
 	granule_addr = get_rand_granule_addr();
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+					(unsigned long)(MAX_CPUS - 1U));
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -1176,9 +1202,10 @@
 	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);
+	size = (size_t)test_helpers_get_rand_in_range(1UL, 7UL);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -1212,9 +1239,10 @@
 	get_rand_granule_array(granule_addrs, 2U);
 
 	/* Get a random offset between 1 and 7 */
-	offset = test_helpers_get_rand_in_range(1, 7);
+	offset = (unsigned int)test_helpers_get_rand_in_range(1UL, 7UL);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+					(unsigned long)(MAX_CPUS - 1U));
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -1251,9 +1279,10 @@
 	 * 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);
+	granule_addrs[1] += test_helpers_get_rand_in_range(1UL, 7UL);
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+								MAX_CPUS - 1U);
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
@@ -1294,7 +1323,8 @@
 	offset = GRANULE_SIZE >> 1U;
 	size = (size_t)GRANULE_SIZE;
 
-	cpuid = test_helpers_get_rand_in_range(0, MAX_CPUS - 1U);
+	cpuid = (unsigned int)test_helpers_get_rand_in_range(0UL,
+					(unsigned long)(MAX_CPUS - 1U));
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
diff --git a/lib/realm/tests/granule.cpp b/lib/realm/tests/granule.cpp
index fa751b5..fd8edf4 100644
--- a/lib/realm/tests/granule.cpp
+++ b/lib/realm/tests/granule.cpp
@@ -26,7 +26,7 @@
 /* Function to get a random granule index in the range [1, NR_GRANULES - 2] */
 static inline unsigned int get_rand_granule_idx(void)
 {
-	return (unsigned int)test_helpers_get_rand_in_range(1,
+	return (unsigned int)test_helpers_get_rand_in_range(1UL,
 					test_helpers_get_nr_granules() - 2U);
 
 }
@@ -65,9 +65,9 @@
 	}
 
 	if (higher_range == true) {
-		*addr = (unsigned long)(test_helpers_get_rand_in_range(
+		*addr = (test_helpers_get_rand_in_range(
 					test_helpers_get_nr_granules(),
-					test_helpers_get_nr_granules() + 10) *
+					(test_helpers_get_nr_granules() + 10)) *
 								GRANULE_SIZE);
 		*addr += host_util_get_granule_base();
 	} else {
@@ -83,7 +83,7 @@
 		*addr = host_util_get_granule_base();
 		*addr -= (granules_below == 1U ?
 			    GRANULE_SIZE :
-			    GRANULE_SIZE * test_helpers_get_rand_in_range(1,
+			    GRANULE_SIZE * test_helpers_get_rand_in_range(1UL,
 							granules_below - 1U));
 	}
 
@@ -99,7 +99,8 @@
 static inline unsigned int set_rand_non_zero_lock_value(struct granule *granule)
 {
 	unsigned int lock =
-		(unsigned int)test_helpers_get_rand_in_range(1, INT_MAX);
+		(unsigned int)test_helpers_get_rand_in_range(1UL, INT_MAX);
+
 	granule->lock.val = lock;
 	return lock;
 }
@@ -192,7 +193,8 @@
 	 * Verify that addr_to_granule() asserts with an unaligned address
 	 ******************************************************************/
 
-	addr += test_helpers_get_rand_in_range(1, GRANULE_SIZE - 2);
+	addr += test_helpers_get_rand_in_range(1UL, GRANULE_SIZE - 2U);
+
 	test_helpers_expect_assert_fail(true);
 	(void)addr_to_granule(addr);
 	test_helpers_fail_if_no_assert_failed();
@@ -293,7 +295,7 @@
 	 * NR_GRANULES
 	 ******************************************************************/
 
-	idx += test_helpers_get_rand_in_range(1, 10);
+	idx += (unsigned long)test_helpers_get_rand_in_range(1UL, 10UL);
 	granule = realm_test_util_granule_struct_base() + idx;
 	test_helpers_expect_assert_fail(true);
 	(void)granule_addr(granule);
@@ -330,7 +332,7 @@
 	 ******************************************************************/
 
 	granule = (uintptr_t)realm_test_util_granule_struct_base();
-	granule += test_helpers_get_rand_in_range(1,
+	granule += test_helpers_get_rand_in_range(1UL,
 					sizeof(struct granule) - 1U);
 	test_helpers_expect_assert_fail(true);
 	(void)granule_addr((struct granule *)granule);
@@ -343,7 +345,7 @@
 	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)test_helpers_get_rand_in_range(10UL, INT_MAX);
 	unsigned long read_val;
 
 	/******************************************************************
@@ -376,7 +378,7 @@
 	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)test_helpers_get_rand_in_range(10UL, 10000UL);
 	unsigned long read_val;
 
 	/******************************************************************
@@ -450,7 +452,8 @@
 	 * Try to get a granule for an unaligned address.
 	 ***************************************************************/
 	address = get_rand_granule_addr();
-	address += test_helpers_get_rand_in_range(1, GRANULE_SIZE - 1);
+	address += test_helpers_get_rand_in_range(1UL, GRANULE_SIZE - 1U);
+
 	granule = find_granule(address);
 	POINTERS_EQUAL(NULL, granule);
 }
@@ -480,7 +483,7 @@
 
 TEST(granule, find_lock_two_granules_TC1)
 {
-	int g1_index, g2_index;
+	unsigned long g1_index, g2_index;
 	struct granule *exp_g1, *exp_g2;
 	struct granule *g1, *g2;
 	unsigned long addr1, addr2;
@@ -495,7 +498,7 @@
 
 	/* Get random indexes for the granules */
 	do {
-		g1_index = test_helpers_get_rand_in_range(1,
+		g1_index = test_helpers_get_rand_in_range(1UL,
 					test_helpers_get_nr_granules() - 1);
 		g2_index = test_helpers_get_rand_in_range(1,
 					test_helpers_get_nr_granules() - 1);
@@ -580,7 +583,7 @@
 	g2 = NULL;
 
 	/* Get a misaligned address */
-	tmp_addr = addr2 + test_helpers_get_rand_in_range(1, GRANULE_SIZE - 1);
+	tmp_addr = addr2 + test_helpers_get_rand_in_range(1UL, GRANULE_SIZE - 1);
 
 	retval = find_lock_two_granules(tmp_addr, GRANULE_STATE_NS, &g1,
 					addr1, GRANULE_STATE_NS, &g2);
@@ -841,7 +844,7 @@
 	 * to all possible states.
 	 ***************************************************************/
 	addr = get_rand_granule_addr();
-	addr += test_helpers_get_rand_in_range(1, GRANULE_SIZE - 1);
+	addr += test_helpers_get_rand_in_range(1UL, GRANULE_SIZE - 1);
 	for (unsigned int state = GRANULE_STATE_NS;
 	     state <= GRANULE_STATE_LAST; state++) {
 		granule = find_lock_granule(addr,
@@ -943,10 +946,12 @@
 
 	granule = addr_to_granule(addr);
 	do {
-		state = test_helpers_get_rand_in_range((int)GRANULE_STATE_NS,
-					  (int)GRANULE_STATE_LAST);
-		expected = test_helpers_get_rand_in_range((int)GRANULE_STATE_NS,
-					     (int)GRANULE_STATE_LAST);
+		state = (unsigned int)test_helpers_get_rand_in_range(
+					(unsigned long)GRANULE_STATE_NS,
+					(unsigned long)GRANULE_STATE_LAST);
+		expected = (unsigned int)test_helpers_get_rand_in_range(
+					(unsigned long)GRANULE_STATE_NS,
+					(unsigned long)GRANULE_STATE_LAST);
 	} while (state == expected);
 
 	/* Ensure the granule is unlocked */
@@ -1289,7 +1294,7 @@
 	 *
 	 * The refcount before the test starts is expected to be 0.
 	 ******************************************************************/
-	get_count = (unsigned int)test_helpers_get_rand_in_range(10, 1000);
+	get_count = (unsigned int)test_helpers_get_rand_in_range(10UL, 1000UL);
 	for (unsigned int i = 0; i < get_count; i++) {
 		__granule_get(granule);
 	}
@@ -1311,8 +1316,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 = test_helpers_get_rand_in_range(1UL, INT_MAX);
+
 	unsigned int lock = set_rand_non_zero_lock_value(granule);
 
 	/******************************************************************
@@ -1339,8 +1344,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 = test_helpers_get_rand_in_range(10UL, INT_MAX);
+
 	unsigned int lock = set_rand_non_zero_lock_value(granule);
 
 	/******************************************************************
@@ -1366,8 +1371,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 = test_helpers_get_rand_in_range(10UL, INT_MAX);
+
 	unsigned int lock = set_rand_non_zero_lock_value(granule);
 
 	/******************************************************************
@@ -1399,8 +1404,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 = test_helpers_get_rand_in_range(10UL, INT_MAX - 1U);
+
 	set_rand_non_zero_lock_value(granule);
 
 	/******************************************************************
@@ -1480,7 +1485,7 @@
 	 *
 	 * The refcount before the test starts is expected to be 0.
 	 ******************************************************************/
-	get_count = (unsigned int)test_helpers_get_rand_in_range(10, 1000);
+	get_count = (unsigned int)test_helpers_get_rand_in_range(10UL, 1000UL);
 	for (unsigned int i = 0; i < get_count; i++) {
 		atomic_granule_get(granule);
 	}
@@ -1536,7 +1541,7 @@
 	 *
 	 * The refcount before the test starts is expected to be 0.
 	 ******************************************************************/
-	get_count = (unsigned int)test_helpers_get_rand_in_range(10, 1000);
+	get_count = (unsigned int)test_helpers_get_rand_in_range(10UL, 1000UL);
 	for (unsigned int i = 0; i < get_count; i++) {
 		atomic_granule_get(granule);
 	}
@@ -1705,7 +1710,7 @@
 	 * Try to find and lock a granule for a misaligned address.
 	 ***************************************************************/
 	addr = get_rand_granule_addr();
-	addr += test_helpers_get_rand_in_range(1, GRANULE_SIZE - 1);
+	addr += test_helpers_get_rand_in_range(1UL, GRANULE_SIZE - 1);
 	ret = find_lock_unused_granule(addr, GRANULE_STATE_NS, &granule);
 
 	CHECK_TRUE(ret == -EINVAL);
@@ -1787,8 +1792,8 @@
 
 				/* Initialize the granule with random data */
 				memset((void *)addrs[i],
-					test_helpers_get_rand_in_range(1, INT_MAX),
-								GRANULE_SIZE);
+					(int)test_helpers_get_rand_in_range(1UL, INT_MAX),
+					GRANULE_SIZE);
 				granule_memzero(granule, (enum buffer_slot)k);
 
 				for (unsigned int l = 0;