From 2f2f0fbcbd5acb60af36220b413731a5a8e91c1d Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 15 Mar 2019 12:17:14 +1100 Subject: [PATCH] confserver: Send an error response if JSON request is malformatted --- tools/kconfig_new/confserver.py | 8 +++++++- tools/kconfig_new/test/test_confserver.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tools/kconfig_new/confserver.py b/tools/kconfig_new/confserver.py index 3373a9d0c..fcb33e63d 100755 --- a/tools/kconfig_new/confserver.py +++ b/tools/kconfig_new/confserver.py @@ -79,7 +79,13 @@ def run_server(kconfig, sdkconfig, default_version=MAX_PROTOCOL_VERSION): line = sys.stdin.readline() if not line: break - req = json.loads(line) + try: + req = json.loads(line) + except ValueError as e: # json module throws JSONDecodeError (sublcass of ValueError) on Py3 but ValueError on Py2 + response = {"version": default_version, "error": ["JSON formatting error: %s" % e]} + json.dump(response, sys.stdout) + print("\n") + continue before = confgen.get_json_values(config) before_ranges = get_ranges(config) before_visible = get_visible(config) diff --git a/tools/kconfig_new/test/test_confserver.py b/tools/kconfig_new/test/test_confserver.py index 53cad5b7c..d5da31913 100755 --- a/tools/kconfig_new/test/test_confserver.py +++ b/tools/kconfig_new/test/test_confserver.py @@ -64,6 +64,8 @@ def main(): test_load_save(p, temp_sdkconfig_path) + test_invalid_json(p) + print("Done. All passed.") finally: @@ -143,5 +145,21 @@ def test_load_save(p, temp_sdkconfig_path): assert len(load_result["ranges"]) > 0 +def test_invalid_json(p): + print("Testing invalid JSON formatting...") + + bad_escaping = r'{ "version" : 2, "load" : "c:\some\path\not\escaped\as\json" }' + p.send("%s\n" % bad_escaping) + readback = expect_json(p) + print(readback) + assert "json" in readback["error"][0].lower() + + not_really_json = 'Hello world!!' + p.send("%s\n" % not_really_json) + readback = expect_json(p) + print(readback) + assert "json" in readback["error"][0].lower() + + if __name__ == "__main__": main()