@@ -116,42 +116,85 @@ std::string UnitTest::print() const {
116116}
117117
118118
119- std::unique_ptr<UnitTest> UnitTest::from_yajl_node (const yajl_val &node) {
120- size_t num_tests = node->u .object .len ;
121- auto u = std::make_unique<UnitTest>();
122-
123- u->skipped = false ;
124- u->capture = 0 ;
125-
126- for (int i = 0 ; i < num_tests; i++) {
127- const char *key = node->u .object .keys [ i ];
128- yajl_val val = node->u .object .values [ i ];
129-
130- if (strcmp (key, " param" ) == 0 ) {
131- u->param = YAJL_GET_STRING (val);
132- } else if (strcmp (key, " input" ) == 0 ) {
133- u->input = YAJL_GET_STRING (val);
134- json2bin (&u->input );
135- } else if (strcmp (key, " resource" ) == 0 ) {
136- u->resource = YAJL_GET_STRING (val);
137- } else if (strcmp (key, " name" ) == 0 ) {
138- u->name = YAJL_GET_STRING (val);
139- } else if (strcmp (key, " type" ) == 0 ) {
140- u->type = YAJL_GET_STRING (val);
141- } else if (strcmp (key, " ret" ) == 0 ) {
142- u->ret = YAJL_GET_INTEGER (val);
143- } else if (strcmp (key, " capture" ) == 0 ) {
144- u->capture = YAJL_GET_INTEGER (val);
145- } else if (strcmp (key, " libinjection_override" ) == 0 ) {
146- u->libinjection_override = YAJL_GET_STRING (val);
147- } else if (strcmp (key, " output" ) == 0 ) {
148- u->output = std::string (YAJL_GET_STRING (val));
149- json2bin (&u->output );
150- /*
151- * Converting \\u0000 to \0 due to the following gcc bug:
152- * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53690
153- *
154- */
119+ std::unique_ptr<UnitTest> UnitTest::from_json_document (
120+ const modsecurity_test::json::JsonDocument *document) {
121+ modsecurity_test::json::JsonValue root;
122+
123+ if (modsecurity_test::json::get (document->get_value (), &root) == false ) {
124+ return make_empty_unit_test ();
125+ }
126+
127+ modsecurity_test::json::JsonType type;
128+ if (modsecurity_test::json::get (root.type (), &type) == false ) {
129+ return make_empty_unit_test ();
130+ }
131+
132+ if (type == modsecurity_test::json::JsonType::Array) {
133+ modsecurity_test::json::JsonArray tests;
134+ if (modsecurity_test::json::get (root.get_array (), &tests) == false ) {
135+ return make_empty_unit_test ();
136+ }
137+
138+ for (auto test_result : tests) {
139+ modsecurity_test::json::JsonValue test_value;
140+ if (modsecurity_test::json::get (std::move (test_result),
141+ &test_value) == false ) {
142+ continue ;
143+ }
144+
145+ return from_json_value (test_value);
146+ }
147+
148+ return make_empty_unit_test ();
149+ }
150+
151+ return from_json_value (root);
152+ }
153+
154+ std::unique_ptr<UnitTest> UnitTest::from_json_value (
155+ modsecurity_test::json::JsonValue value) {
156+ modsecurity_test::json::JsonObject object;
157+ auto u = make_empty_unit_test ();
158+
159+ if (modsecurity_test::json::get (value.get_object (), &object) == false ) {
160+ return u;
161+ }
162+
163+ for (auto field_result : object) {
164+ modsecurity_test::json::JsonField field;
165+ std::string_view key;
166+ modsecurity_test::json::JsonValue child;
167+
168+ if (modsecurity_test::json::get (field_result, &field)
169+ == false ) {
170+ continue ;
171+ }
172+ if (modsecurity_test::json::get (field.unescaped_key (), &key) == false ) {
173+ continue ;
174+ }
175+ child = field.value ();
176+
177+ if (key == " param" ) {
178+ u->param = modsecurity_test::json::get_string (child);
179+ } else if (key == " input" ) {
180+ u->input = modsecurity_test::json::get_string (child);
181+ json2bin (&u->input );
182+ } else if (key == " resource" ) {
183+ u->resource = modsecurity_test::json::get_string (child);
184+ } else if (key == " name" ) {
185+ u->name = modsecurity_test::json::get_string (child);
186+ } else if (key == " type" ) {
187+ u->type = modsecurity_test::json::get_string (child);
188+ } else if (key == " ret" ) {
189+ u->ret = static_cast <int >(modsecurity_test::json::get_integer (child));
190+ } else if (key == " output" ) {
191+ u->output = modsecurity_test::json::get_string (child);
192+ json2bin (&u->output );
193+ /*
194+ * Converting \\u0000 to \0 due to the following gcc bug:
195+ * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53690
196+ *
197+ */
155198 }
156199 }
157200
0 commit comments