Files
WoW-Hybrid-gateway/tests/editor_spawn_undo_test.py

83 lines
4.3 KiB
Python

import copy
from pathlib import Path
import sqlite3
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'ops/editor'))
import spawn_undo as undo
from editor_spawn_export_test import Export, state
def payload(table='creature'):
columns = state()['tables'][table]['columns']
values = ['77', '100', '-12.345678901234567', 'NULL']
# Match WoW SavedVariables formatting, including bracket keys and comments.
strings = lambda xs: '{' + ','.join('"' + s + '"' for s in xs) + ',}'
return ('OctoEditorUndo = { ["format"] = 1, ["rows"] = {\n'
'[1] = { ["table"] = "' + table + '", ["columns"] = ' + strings(columns) + ',\n'
'["values"] = ' + strings(values) + ', }, -- [1]\n}, }\n')
class Undo(unittest.TestCase):
def test_restore_complete_creature_and_gameobject(self):
for name in ('creature', 'gameobject'):
sql = undo.restore(undo.read_saved_variables(payload(name)), state(), 1)
self.assertIn('-12.345678901234567', sql)
self.assertNotIn('REPLACE', sql)
db = sqlite3.connect(':memory:')
columns = state()['tables'][name]['columns']
db.execute('CREATE TABLE '+name+' ('+','.join(c+' NUMERIC'+(' PRIMARY KEY' if c=='guid' else '') for c in columns)+')')
db.executescript(sql.replace('USE `tw_world`;', ''))
row = db.execute('SELECT * FROM '+name).fetchone()
self.assertEqual(row[:2], (77, 100)); self.assertIsNone(row[-1])
# Applying twice must fail, never silently replace existing state.
with self.assertRaises(sqlite3.IntegrityError):db.executescript(sql.replace('USE `tw_world`;', ''))
def test_guid_collision_and_new_guid(self):
saved = undo.read_saved_variables(payload())
sql = undo.restore(saved, state(), 1, '90')
self.assertIn('VALUES (90,100,', sql)
self.assertEqual(saved['rows'][1]['values'][1], '77')
for guid in ('1', '0', '-1', '4294967296', '1;DELETE', '01'):
with self.assertRaises(ValueError):undo.restore(saved, state(), 1, guid)
current = state(); current['tables']['creature']['rows']['77'] = ['77','100','0','0']
with self.assertRaises(ValueError):undo.restore(saved, current, 1, '90')
def test_refuses_partial_schema_bad_values_and_queue(self):
saved = undo.read_saved_variables(payload())
for field, replacement in (('columns', {1:'guid'}), ('values', {1:'77'}), ('table', 'account')):
bad=copy.deepcopy(saved);bad['rows'][1][field]=replacement
with self.assertRaises(ValueError):undo.restore(bad, state(), 1)
for badvalue in ('NaN', '1);DROP TABLE creature;', 1):
bad=copy.deepcopy(saved);bad['rows'][1]['values'][3]=badvalue
with self.assertRaises(ValueError):undo.restore(bad, state(), 1)
for index in (0,2):
with self.assertRaises(ValueError):undo.restore(saved, state(), index)
saved['rows']={i:saved['rows'][1] for i in range(1,22)}
with self.assertRaises(ValueError):undo.restore(saved,state(),1)
def test_never_executes_lua_and_rejects_ambiguous_input(self):
for text in (payload()+' os.execute("echo unsafe")', 'OctoEditorUndo = loadstring("x")()',
'OctoEditorUndo = {[1]=1,[1]=2}', payload()[:-3],
'OctoEditorUndo = "bad\\123"', 'OctoEditorUndo = '+('{'*10)+('}'*10)):
with self.assertRaises(ValueError):undo.read_saved_variables(text)
with self.assertRaises(ValueError):undo.array({2:'sparse'})
def test_cli_offline_and_no_overwrite(self):
import json
with tempfile.TemporaryDirectory() as folder:
p=Path(folder);(p/'undo.lua').write_text(payload());(p/'current.json').write_text(json.dumps(state()))
cmd=[sys.executable,str(ROOT/'ops/editor/spawn_undo.py'),str(p/'undo.lua'),str(p/'restore.sql'),'--entry','1','--current',str(p/'current.json')]
a=subprocess.run(cmd,capture_output=True,text=True)
self.assertEqual(a.returncode,0,a.stderr)
before=(p/'restore.sql').read_bytes()
self.assertNotEqual(subprocess.run(cmd,capture_output=True).returncode,0)
self.assertEqual((p/'restore.sql').read_bytes(),before)
if __name__ == '__main__':unittest.main()