plugins/tools/test_plugin_pack_properties.py

29 lines
2.4 KiB
Python

import json
from pathlib import Path
import sys
import tempfile
import unittest
sys.path.insert(0,str(Path(__file__).resolve().parent))
import plugin_pack_properties as props
class PropertiesTests(unittest.TestCase):
def setUp(self):
self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup);self.root=Path(self.tmp.name);self.path=self.root/'server.properties';self.path.write_bytes(b'# old timestamp\nmotd=Keep this\nresource-pack=https\\://old.example/world.zip\nresource-pack-sha1=old\nrequire-resource-pack=true\nrcon.password=never-return-this\n');self.before=self.path.read_bytes()
self.data={'unified':True,'sourceCommit':'a'*40,'sha1':'b'*40,'packId':'c24c7cf5-a292-539d-971f-a96d8b838162'};self.plan=props.plan(self.root,self.data);self.backup=self.root/'backup';self.backup.mkdir();props.backup(self.root,self.backup,self.plan)
def test_only_four_pack_fields_change_and_plan_does_not_expose_other_values(self):
self.assertNotIn('never-return-this',json.dumps(self.plan));props.install(self.root,self.plan)
before=props.fields(self.before);after=props.fields(self.path.read_bytes())
self.assertEqual({k:v for k,v in before.items() if k not in props.KEYS},{k:v for k,v in after.items() if k not in props.KEYS})
self.assertEqual(after['resource-pack-id'],self.data['packId'])
def test_runtime_timestamp_rewrite_allows_verified_receipt_rollback(self):
props.install(self.root,self.plan);self.path.write_bytes(b'# new Paper timestamp\n'+self.path.read_bytes())
self.assertFalse(props.verify_rollback(self.root,self.backup,self.plan));props.restore(self.root,self.backup,self.plan);self.assertEqual(self.path.read_bytes(),self.before)
def test_unrelated_setting_drift_and_corrupt_backup_block_rollback(self):
props.install(self.root,self.plan);self.path.write_bytes(self.path.read_bytes().replace(b'Keep this',b'Later owner change'))
with self.assertRaises(ValueError):props.verify_rollback(self.root,self.backup,self.plan)
(self.backup/'server.properties').write_bytes(b'corrupt')
with self.assertRaises(ValueError):props.verify_rollback(self.root,self.backup,self.plan)
def test_changed_after_plan_refused_and_legacy_overlay_does_not_touch_properties(self):
self.assertIsNone(props.plan(self.root,{'unified':False}));self.path.write_bytes(self.before+b'new=value\n')
with self.assertRaises(ValueError):props.install(self.root,self.plan)
if __name__=='__main__':unittest.main()