summaryrefslogtreecommitdiff
path: root/runner/runner_json_tests.c
blob: 7c9540d1c277e671f8e6e61fb0c59734ac0b65fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <json.h>

#include "igt.h"
#include "resultgen.h"

static char testdatadir[] = JSON_TESTS_DIRECTORY;

static struct json_object *read_json(int fd)
{
	struct json_object *obj;
	struct json_tokener *tok = json_tokener_new();
	enum json_tokener_error err;
	char buf[512];
	ssize_t s;

	do {
		s = read(fd, buf, sizeof(buf));
		obj = json_tokener_parse_ex(tok, buf, s);
	} while ((err = json_tokener_get_error(tok)) == json_tokener_continue);

	igt_assert_eq(err, json_tokener_success);

	json_tokener_free(tok);
	return obj;
}

static void compare(struct json_object *one,
		    struct json_object *two);

static void compare_objects(struct json_object *one,
			    struct json_object *two)
{
	json_object_iter iter;
	struct json_object *subobj;

	json_object_object_foreachC(one, iter) {
		igt_debug("Key %s\n", iter.key);

		igt_assert(json_object_object_get_ex(two, iter.key, &subobj));

		compare(iter.val, subobj);
	}
}

static void compare_arrays(struct json_object *one,
			   struct json_object *two)
{
	size_t i;

	for (i = 0; i < json_object_array_length(one); i++) {
		igt_debug("Array index %zd\n", i);
		compare(json_object_array_get_idx(one, i),
			json_object_array_get_idx(two, i));
	}
}

static bool compatible_types(struct json_object *one,
			     struct json_object *two)
{
	/*
	 * A double of value 0.0 gets written as "0", which gets read
	 * as an int.
	 */
	json_type onetype = json_object_get_type(one);
	json_type twotype = json_object_get_type(two);

	switch (onetype) {
	case json_type_boolean:
	case json_type_string:
	case json_type_object:
	case json_type_array:
	case json_type_null:
		return onetype == twotype;
		break;
	case json_type_double:
	case json_type_int:
		return twotype == json_type_double || twotype == json_type_int;
		break;
	}

	igt_assert(!"Cannot be reached");
	return false;
}

static void compare(struct json_object *one,
		    struct json_object *two)
{
	igt_assert(compatible_types(one, two));

	switch (json_object_get_type(one)) {
	case json_type_boolean:
		igt_assert_eq(json_object_get_boolean(one), json_object_get_boolean(two));
		break;
	case json_type_double:
	case json_type_int:
		/*
		 * A double of value 0.0 gets written as "0", which
		 * gets read as an int. Both yield 0.0 with
		 * json_object_get_double(). Comparing doubles with ==
		 * considered crazy but it's good enough.
		 */
		igt_assert(json_object_get_double(one) == json_object_get_double(two));
		break;
	case json_type_string:
		igt_assert(!strcmp(json_object_get_string(one), json_object_get_string(two)));
		break;
	case json_type_object:
		igt_assert_eq(json_object_object_length(one), json_object_object_length(two));
		compare_objects(one, two);
		break;
	case json_type_array:
		igt_assert_eq(json_object_array_length(one), json_object_array_length(two));
		compare_arrays(one, two);
		break;
	case json_type_null:
		break;
	default:
		igt_assert(!"Cannot be reached");
	}
}

static void run_results_and_compare(int dirfd, const char *dirname)
{
	int testdirfd = openat(dirfd, dirname, O_RDONLY | O_DIRECTORY);
	int reference;
	struct json_object *resultsobj, *referenceobj;

	igt_assert_fd(testdirfd);

	igt_assert((resultsobj = generate_results_json(testdirfd)) != NULL);

	reference = openat(testdirfd, "reference.json", O_RDONLY);
	close(testdirfd);

	igt_assert_fd(reference);
	referenceobj = read_json(reference);
	close(reference);
	igt_assert(referenceobj != NULL);

	igt_debug("Root object\n");
	compare(resultsobj, referenceobj);
	igt_assert_eq(json_object_put(resultsobj), 1);
	igt_assert_eq(json_object_put(referenceobj), 1);
}

static const char *dirnames[] = {
	"normal-run",
	"warnings",
	"warnings-with-dmesg-warns",
	"piglit-style-dmesg",
	"incomplete-before-any-subtests",
	"dmesg-results",
	"aborted-on-boot",
	"aborted-after-a-test",
	"dmesg-escapes",
	"notrun-results",
	"notrun-results-multiple-mode",
	"dmesg-warn-level",
	"dmesg-warn-level-piglit-style",
	"dmesg-warn-level-one-piglit-style"
};

igt_main
{
	int dirfd = open(testdatadir, O_RDONLY | O_DIRECTORY);
	size_t i;

	igt_assert_fd(dirfd);

	for (i = 0; i < ARRAY_SIZE(dirnames); i++) {
		igt_subtest(dirnames[i]) {
			run_results_and_compare(dirfd, dirnames[i]);
		}
	}
}