summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBlue Swirl <blauwirbel@gmail.com>2013-01-19 15:43:53 +0000
committerBlue Swirl <blauwirbel@gmail.com>2013-01-26 13:32:29 +0000
commit3f0f31a0f1c9a89314e9f9ed423371f4d925d5a6 (patch)
treef6c843abe98a62180205f23d05eb24b414e56cfa /tests
parent0c3c89d649d029b29bf245f739c82339fcf2e699 (diff)
downloadhqemu-3f0f31a0f1c9a89314e9f9ed423371f4d925d5a6.zip
hqemu-3f0f31a0f1c9a89314e9f9ed423371f4d925d5a6.tar.gz
tests: add fuzzing to visitor tests
Perform input tests on random data. Improvement to code coverage for qapi/string-input-visitor.c is about 3 percentage points. Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/test-string-input-visitor.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-visitor.c
index 899feda..f6b0093 100644
--- a/tests/test-string-input-visitor.c
+++ b/tests/test-string-input-visitor.c
@@ -165,6 +165,53 @@ static void test_visitor_in_enum(TestInputVisitorData *data,
data->siv = NULL;
}
+/* Try to crash the visitors */
+static void test_visitor_in_fuzz(TestInputVisitorData *data,
+ const void *unused)
+{
+ int64_t ires;
+ bool bres;
+ double nres;
+ char *sres;
+ EnumOne eres;
+ Error *errp = NULL;
+ Visitor *v;
+ unsigned int i;
+ char buf[10000];
+
+ for (i = 0; i < 100; i++) {
+ unsigned int j;
+
+ j = g_test_rand_int_range(0, sizeof(buf) - 1);
+
+ buf[j] = '\0';
+
+ if (j != 0) {
+ for (j--; j != 0; j--) {
+ buf[j - 1] = (char)g_test_rand_int_range(0, 256);
+ }
+ }
+
+ v = visitor_input_test_init(data, buf);
+ visit_type_int(v, &ires, NULL, &errp);
+
+ v = visitor_input_test_init(data, buf);
+ visit_type_bool(v, &bres, NULL, &errp);
+ visitor_input_teardown(data, NULL);
+
+ v = visitor_input_test_init(data, buf);
+ visit_type_number(v, &nres, NULL, &errp);
+
+ v = visitor_input_test_init(data, buf);
+ visit_type_str(v, &sres, NULL, &errp);
+ g_free(sres);
+
+ v = visitor_input_test_init(data, buf);
+ visit_type_EnumOne(v, &eres, NULL, &errp);
+ visitor_input_teardown(data, NULL);
+ }
+}
+
static void input_visitor_test_add(const char *testpath,
TestInputVisitorData *data,
void (*test_func)(TestInputVisitorData *data, const void *user_data))
@@ -189,6 +236,8 @@ int main(int argc, char **argv)
&in_visitor_data, test_visitor_in_string);
input_visitor_test_add("/string-visitor/input/enum",
&in_visitor_data, test_visitor_in_enum);
+ input_visitor_test_add("/string-visitor/input/fuzz",
+ &in_visitor_data, test_visitor_in_fuzz);
g_test_run();
OpenPOWER on IntegriCloud