confserver: Send an error response if JSON request is malformatted

This commit is contained in:
Angus Gratton 2019-03-15 12:17:14 +11:00 committed by Angus Gratton
parent ec4c75b692
commit 2f2f0fbcbd
2 changed files with 25 additions and 1 deletions

View file

@ -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)

View file

@ -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()