[Skiboot] [PATCH v2 7/7] core/test: add tests for nvram_query()

Oliver O'Halloran oohall at gmail.com
Wed Aug 17 15:32:54 AEST 2016


Adds some basic functionality tests for nvram_query() to the core test
suite.

Signed-off-by: Oliver O'Halloran <oohall at gmail.com>
---
 core/test/run-nvram-format.c | 46 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/core/test/run-nvram-format.c b/core/test/run-nvram-format.c
index b8ba69867c77..4bf3ae07f0eb 100644
--- a/core/test/run-nvram-format.c
+++ b/core/test/run-nvram-format.c
@@ -18,11 +18,25 @@
 
 #include "../nvram-format.c"
 
+static char *nvram_reset(void *nvram_image, int size)
+{
+	struct chrp_nvram_hdr *h = nvram_image;
+
+	/* entire partition used by one key */
+	assert(nvram_format(nvram_image, size) == 0);
+	memset((char *) h + sizeof(*h), 0, NVRAM_SIZE_FW_PRIV - sizeof(*h));
+	assert(nvram_check(nvram_image, size) == 0);
+
+	return (char *) h + sizeof(*h);
+}
+
 int main(void)
 {
 	char *nvram_image;
 	size_t sz;
 	struct chrp_nvram_hdr *h;
+	char *data;
+	const char *result;
 
 	/* 1024 bytes is too small for our NVRAM */
 	nvram_image = malloc(1024);
@@ -111,6 +125,38 @@ int main(void)
 	h->cksum = chrp_nv_cksum(h);
 	assert(nvram_check(nvram_image,128*1024) != 0);
 
+	/* test nvram_query() */
+
+	/* does an empty partition break us? */
+	data = nvram_reset(nvram_image, 128*1024);
+	assert(nvram_query("test") == NULL);
+
+	/* does a zero length key break us? */
+	data = nvram_reset(nvram_image, 128*1024);
+	data[0] = '=';
+	assert(nvram_query("test") == NULL);
+
+	/* does a missing = break us? */
+	data = nvram_reset(nvram_image, 128*1024);
+	data[0] = 'a';
+	assert(nvram_query("test") == NULL);
+
+	/* does an empty value break us? */
+	data = nvram_reset(nvram_image, 128*1024);
+	data[0] = 'a';
+	data[1] = '=';
+	result = nvram_query("a");
+	assert(result);
+	assert(strlen(result) == 0);
+
+	/* do we trip over malformed keys? */
+	data = nvram_reset(nvram_image, 128*1024);
+#define TEST_1 "a\0a=\0test=test\0"
+	memcpy(data, TEST_1, sizeof(TEST_1));
+	result = nvram_query("test");
+	assert(result);
+	assert(strcmp(result, "test") == 0);
+
 	free(nvram_image);
 
 	return 0;
-- 
2.5.5



More information about the Skiboot mailing list