[Piglit] [PATCH 1/3] Test Reporting: Use JSON as interchange format for piglit tests

Dylan Baker baker.dylan.c at gmail.com
Wed Jun 11 16:32:31 PDT 2014


It just so happens that the plain text representation of a python
dictionary is a superset of a JSON dictionary. The only real difference
is that python allows ' and " to be used interchangeably while JSON only
accepts ". It also just so happens that JSON is a really nice format for
sending data between two programs, and that python has a very fast
implementation of JSON available, which piglit already supports
(simplejson) as well as a native (but slower) version.

This patch removes the use of eval(), which is considered slow, and
allows for arbitrary code execution. By moving to JSON as our
interchange format between compiled tests and python we gain
standardization, easier maintenance, and less python code required.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/exectest.py            |  9 +++++++--
 framework/tests/exectest_test.py | 17 +++++++++++++++++
 tests/util/piglit-util.c         |  6 +++---
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/framework/exectest.py b/framework/exectest.py
index a833066..d2c550d 100644
--- a/framework/exectest.py
+++ b/framework/exectest.py
@@ -27,6 +27,10 @@ import shlex
 import time
 import sys
 import traceback
+try:
+    import simplejson as json
+except ImportError:
+    import json
 
 from .core import TestResult, Environment
 
@@ -263,8 +267,9 @@ class PiglitTest(Test):
             if piglit.startswith('subtest'):
                 if not 'subtest' in self.result:
                     self.result['subtest'] = {}
-                self.result['subtest'].update(eval(piglit[7:]))
+                self.result['subtest'].update(
+                    json.loads(piglit[7:]))
             else:
-                self.result.update(eval(piglit))
+                self.result.update(json.loads(piglit))
         self.result['out'] = '\n'.join(
             s for s in outlines if not s.startswith('PIGLIT:'))
diff --git a/framework/tests/exectest_test.py b/framework/tests/exectest_test.py
index 2f0569f..dbc748a 100644
--- a/framework/tests/exectest_test.py
+++ b/framework/tests/exectest_test.py
@@ -31,3 +31,20 @@ def test_initialize_test():
 def test_initialize_piglittest():
     """ Test that PiglitTest initializes correctly """
     PiglitTest('/bin/true')
+
+
+def test_piglittest_interpret_result():
+    """ PiglitTest.interpret_result() works no subtests """
+    test = PiglitTest('foo')
+    test.result['out'] = 'PIGLIT: {"result": "pass"}\n'
+    test.interpret_result()
+    assert test.result['result'] == 'pass'
+
+
+def test_piglittest_interpret_result_subtest():
+    """ PiglitTest.interpret_result() works with subtests """
+    test = PiglitTest('foo')
+    test.result['out'] = ('PIGLIT: {"result": "pass"}\n'
+                          'PIGLIT:subtest {"subtest": "pass"}\n')
+    test.interpret_result()
+    assert test.result['subtest']['subtest'] == 'pass'
diff --git a/tests/util/piglit-util.c b/tests/util/piglit-util.c
index c9b72ca..a6da456 100644
--- a/tests/util/piglit-util.c
+++ b/tests/util/piglit-util.c
@@ -220,7 +220,7 @@ piglit_report_result(enum piglit_result result)
 
 	fflush(stderr);
 
-	printf("PIGLIT: {'result': '%s' }\n", result_str);
+	printf("PIGLIT: {\"result\": \"%s\" }\n", result_str);
 	fflush(stdout);
 
 	switch(result) {
@@ -241,9 +241,9 @@ piglit_report_subtest_result(enum piglit_result result, const char *format, ...)
 
 	va_start(ap, format);
 
-	printf("PIGLIT:subtest {'");
+	printf("PIGLIT:subtest {\"");
 	vprintf(format, ap);
-	printf("' : '%s'}\n", result_str);
+	printf("\" : \"%s\"}\n", result_str);
 	fflush(stdout);
 
 	va_end(ap);
-- 
2.0.0



More information about the Piglit mailing list