Signed-off-by: Barret Rhoden <[email protected]>
---
kern/drivers/dev/Kconfig | 9 ++++
kern/drivers/dev/vars.c | 46 +++++++++++++++++
user/utest/devvars.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 180 insertions(+)
create mode 100644 user/utest/devvars.c
diff --git a/kern/drivers/dev/Kconfig b/kern/drivers/dev/Kconfig
index d01c6dd563f6..6a1ee1f39f89 100644
--- a/kern/drivers/dev/Kconfig
+++ b/kern/drivers/dev/Kconfig
@@ -10,3 +10,12 @@ config DEVVARS
default y
help
The #vars device exports read access to select kernel variables.
+
+config DEVVARS_TEST
+ bool "#vars test files"
+ depends on DEVVARS
+ default n
+ help
+ Have #vars include a collection of test files that devvars
utest uses.
+ Say 'y' if you plan to use the utest, at the expense of having a
+ cluttered #vars.
diff --git a/kern/drivers/dev/vars.c b/kern/drivers/dev/vars.c
index bf9d68553212..a295629ed1c0 100644
--- a/kern/drivers/dev/vars.c
+++ b/kern/drivers/dev/vars.c
@@ -439,3 +439,49 @@ struct dev vars_devtab __devtab = {
.chaninfo = devchaninfo,
.tapfd = 0,
};
+
+/* The utest needs these variables exported */
+#ifdef CONFIG_DEVVARS_TEST
+
+static char *s = "string";
+static char c = 'x';
+static uint8_t u8 = 8;
+static uint16_t u16 = 16;
+static uint32_t u32 = 32;
+static uint64_t u64 = 64;
+static uint8_t d8 = -8;
+static uint16_t d16 = -16;
+static uint32_t d32 = -32;
+static uint64_t d64 = -64;
+static uint8_t x8 = 0x8;
+static uint16_t x16 = 0x16;
+static uint32_t x32 = 0x32;
+static uint64_t x64 = 0x64;
+static uint8_t o8 = 01;
+static uint16_t o16 = 016;
+static uint32_t o32 = 032;
+static uint64_t o64 = 064;
+
+/* For testing creation. There is no ENTRY for this. */
+char *devvars_foobar = "foobar";
+
+DEVVARS_ENTRY(s, "s");
+DEVVARS_ENTRY(c, "c");
+DEVVARS_ENTRY(u8, "ub");
+DEVVARS_ENTRY(u16, "uh");
+DEVVARS_ENTRY(u32, "uw");
+DEVVARS_ENTRY(u64, "ug");
+DEVVARS_ENTRY(d8, "db");
+DEVVARS_ENTRY(d16, "dh");
+DEVVARS_ENTRY(d32, "dw");
+DEVVARS_ENTRY(d64, "dg");
+DEVVARS_ENTRY(x8, "xb");
+DEVVARS_ENTRY(x16, "xh");
+DEVVARS_ENTRY(x32, "xw");
+DEVVARS_ENTRY(x64, "xg");
+DEVVARS_ENTRY(o8, "ob");
+DEVVARS_ENTRY(o16, "oh");
+DEVVARS_ENTRY(o32, "ow");
+DEVVARS_ENTRY(o64, "og");
+
+#endif /* CONFIG_DEVVARS_TEST */
diff --git a/user/utest/devvars.c b/user/utest/devvars.c
new file mode 100644
index 000000000000..1806454a6eab
--- /dev/null
+++ b/user/utest/devvars.c
@@ -0,0 +1,125 @@
+/* Copyright (c) 2015 Google Inc
+ * Barret Rhoden <[email protected]>
+ * See LICENSE for details. */
+
+#include <utest/utest.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <parlib/net.h>
+
+TEST_SUITE("DEVVARS");
+
+static bool test_read_var(const char *name, const char *val)
+{
+ char buf[128];
+ int fd, ret;
+
+ ret = snprintf(buf, sizeof(buf), "#vars/%s", name);
+ if (snprintf_overflow(ret, buf, sizeof(buf)))
+ UT_ASSERT_FMT("snprintf failed!", FALSE);
+ fd = open(buf, O_READ);
+ UT_ASSERT_FMT("Could not open vars file %s, check CONFIG_DEVVARS_TEST",
+ fd >= 0, buf);
+ ret = read(fd, buf, sizeof(buf));
+ UT_ASSERT_FMT("Could not read vars file %s", ret > 0, buf);
+ UT_ASSERT_FMT("Value differs, got %s, wanted %s", strcmp(buf, val) == 0,
+ buf, val);
+ return TRUE;
+}
+
+bool test_devvars_fmt(void)
+{
+ if (!test_read_var("s!s", "string"))
+ return FALSE;
+ if (!test_read_var("c!c", "x"))
+ return FALSE;
+ if (!test_read_var("u8!ub", "8"))
+ return FALSE;
+ if (!test_read_var("u16!uh", "16"))
+ return FALSE;
+ if (!test_read_var("u32!uw", "32"))
+ return FALSE;
+ if (!test_read_var("u64!ug", "64"))
+ return FALSE;
+ if (!test_read_var("d8!db", "-8"))
+ return FALSE;
+ if (!test_read_var("d16!dh", "-16"))
+ return FALSE;
+ if (!test_read_var("d32!dw", "-32"))
+ return FALSE;
+ if (!test_read_var("d64!dg", "-64"))
+ return FALSE;
+ if (!test_read_var("x8!xb", "0x8"))
+ return FALSE;
+ if (!test_read_var("x16!xh", "0x16"))
+ return FALSE;
+ if (!test_read_var("x32!xw", "0x32"))
+ return FALSE;
+ if (!test_read_var("x64!xg", "0x64"))
+ return FALSE;
+ if (!test_read_var("o8!ob", "01"))
+ return FALSE;
+ if (!test_read_var("o16!oh", "016"))
+ return FALSE;
+ if (!test_read_var("o32!ow", "032"))
+ return FALSE;
+ if (!test_read_var("o64!og", "064"))
+ return FALSE;
+ return TRUE;
+}
+
+static bool test_new_var(const char *name, const char *val)
+{
+ char buf[128];
+ char path[128];
+ int fd, ret;
+
+ ret = snprintf(path, sizeof(path), "#vars/%s", name);
+ if (snprintf_overflow(ret, path, sizeof(path)))
+ UT_ASSERT_FMT("snprintf failed!", FALSE);
+ fd = open(path, O_READ | O_CREATE, S_IRUSR);
+ UT_ASSERT_FMT("Could not open vars file %s, check CONFIG_DEVVARS_TEST",
+ fd >= 0, path);
+ ret = read(fd, buf, sizeof(buf));
+ UT_ASSERT_FMT("Could not read vars file %s", ret > 0, path);
+ UT_ASSERT_FMT("Value differs, got %s, wanted %s", strcmp(buf, val) == 0,
+ buf, val);
+ ret = unlink(path);
+ UT_ASSERT_FMT("Could not remove %s", ret == 0, path);
+ return TRUE;
+}
+
+bool test_devvars_newfile(void)
+{
+ if (!test_new_var("devvars_foobar!s", "foobar"))
+ return FALSE;
+ return TRUE;
+}
+
+/* Make sure test_read_var() knows how to fail */
+bool test_devvars_test(void)
+{
+ UT_ASSERT_FMT("Opened when it shouldn't have",
+ !test_read_var("NO_SUCH_FILE!xw", "0x32"));
+ UT_ASSERT_FMT("Got the wrong value but thought it was fine",
+ !test_read_var("x32!xw", "0xdeadbeef"));
+ return TRUE;
+}
+
+struct utest utests[] = {
+ UTEST_REG(devvars_fmt),
+ UTEST_REG(devvars_newfile),
+ UTEST_REG(devvars_test),
+};
+int num_utests = sizeof(utests) / sizeof(struct utest);
+
+int main(int argc, char *argv[])
+{
+ char **whitelist = &argv[1];
+ int whitelist_len = argc - 1;
+
+ RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);
+}
--
2.6.0.rc2.230.g3dd15c0
--
You received this message because you are subscribed to the Google Groups
"Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.