65 lines
2.7 KiB
Python
65 lines
2.7 KiB
Python
"""Exercise paid item state round trips and inventory migration boundaries."""
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
spec = importlib.util.spec_from_file_location(
|
|
'extended_items', ROOT/'tools/ava2-itemshop/migrate_extended_items.py')
|
|
migration = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(migration)
|
|
|
|
|
|
class ExtendedItems(unittest.TestCase):
|
|
def test_quickslot_boundaries(self):
|
|
positions = [0, 224, 225, 256, 257, 268, 269, 286, 287, 302, 303, 65535]
|
|
source = b''.join(bytes([1])+p.to_bytes(2, 'little') for p in positions)
|
|
# Skill and emotion quickslots may contain the same numeric positions.
|
|
source += bytes([2, 1, 1, 3, 31, 1]) + bytes(108-len(source)-6)
|
|
result = migration.quickslots(source)
|
|
expected = [0, 224, 225, 256, 265, 276, 269, 286, 295, 310, 303, 65535]
|
|
self.assertEqual([int.from_bytes(result[i+1:i+3], 'little')
|
|
for i in range(0, 36, 3)], expected)
|
|
self.assertEqual(result[36:], source[36:])
|
|
self.assertEqual(migration.quickslots(b''), b'')
|
|
with self.assertRaises(ValueError):
|
|
migration.quickslots(bytes(107))
|
|
|
|
def test_extra_round_trip_and_corruption(self):
|
|
source = r'''
|
|
#include "src/server/common/port_item_extra.h"
|
|
#include <cassert>
|
|
#include <limits>
|
|
int main() {
|
|
PortItemExtra source={},decoded={};
|
|
source.sockets[0]=std::numeric_limits<int32_t>::min();
|
|
source.sockets[4]=std::numeric_limits<int32_t>::max();
|
|
source.types[0]=99;source.types[7]=255;
|
|
source.values[0]=-32768;source.values[7]=32767;
|
|
std::memcpy(source.name,"abcdefghijklmnopqrstuvwx",24);
|
|
auto hex=PortItemExtraHex(source);
|
|
assert(hex.size()==138);
|
|
assert(PortItemExtraRead(hex.c_str(),decoded));
|
|
assert(!std::memcmp(&source,&decoded,sizeof(source)));
|
|
// Empty DB values are pre-migration items and must load as empty state.
|
|
assert(PortItemExtraRead("",decoded));
|
|
PortItemExtra zero={};assert(!std::memcmp(&zero,&decoded,sizeof(zero)));
|
|
assert(!PortItemExtraRead("00",decoded));
|
|
hex[137]='g';assert(!PortItemExtraRead(hex.c_str(),decoded));
|
|
assert(!std::memcmp(&zero,&decoded,sizeof(zero)));
|
|
hex=std::string(138,'f');assert(!PortItemExtraRead(hex.c_str(),decoded));
|
|
assert(decoded.name[24]==0);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
cpp, exe = Path(tmp)/'extra.cpp', Path(tmp)/'extra'
|
|
cpp.write_text(source)
|
|
subprocess.run(['c++', '-std=c++17', '-Werror=implicit-int-conversion', '-I'+str(ROOT), str(cpp), '-o', str(exe)], check=True)
|
|
subprocess.run([str(exe)], check=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|