Files
metin2-server/tests/features/test_db_config_runtime.py
T

59 lines
3.8 KiB
Python

"""Run the actual DB config parser with bounded buffers and atomic rejection."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT=Path(__file__).resolve().parents[2]
class ConfigRuntime(unittest.TestCase):
def test_tokens_lines_and_malformed_reload_preserve_active_config(self):
source=(ROOT/'src/server/db/src/Config.cpp').read_text(encoding='latin1')
source='\n'.join(line for line in source.splitlines() if not line.startswith('#include'))
stl=(ROOT/'src/server/common/stl.h').read_text(encoding='latin1')
lower=stl[stl.index('inline void stl_lowers'):stl.index('struct stringhash')]
code=r'''
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <string>
using DWORD=uint32_t;using WORD=uint16_t;using BYTE=uint8_t;
#include <map>
#include <cctype>
#define itertype(v) decltype((v).begin())
// Singleton lifetime is outside the parser boundary exercised here.
template<class T> struct singleton {};
#include "src/server/common/utils.h"
#include "src/server/db/src/Config.h"
#define sys_log(...) ((void)0)
'''+lower+source+r'''
FILE* input(const std::string& s){FILE* f=tmpfile();assert(f);assert(fwrite(s.data(),1,s.size(),f)==s.size());rewind(f);return f;}
void file(const char* path,const std::string& text){std::ofstream out(path);out<<text;}
int main(int argc,char** argv){
const char* path=argv[1];CConfig config;
struct {char before='A';char text[4];char after='Z';} buffer;
auto f=input("abc");assert(config.GetWord(f,buffer.text,sizeof(buffer.text))&&!strcmp(buffer.text,"abc"));fclose(f);
f=input("abcd");assert(!config.GetWord(f,buffer.text,sizeof(buffer.text)));assert(buffer.before=='A'&&buffer.after=='Z');fclose(f);
// LoadFile resets the previous parser error, commits only a complete snapshot.
file(path,"NAME = \"dragon shop\"\nVALUE = 42\nITEM_ID_RANGE = 10 99\n");assert(config.LoadFile(path));
char name[32];DWORD value=0,a=0,b=0;assert(config.GetValue("NAME",name,sizeof(name))&&!strcmp(name,"dragon shop"));assert(config.GetValue("VALUE",&value)&&value==42);assert(config.GetTwoValue("ITEM_ID_RANGE",&a,&b)&&a==10&&b==99);
file(path,"BAD_RANGE = \"1 no\"\nMISSING_RANGE = 1\nHUGE_RANGE = \"1 4294967296\"\n");assert(config.LoadFile(path));
for(const char* key:{"BAD_RANGE","MISSING_RANGE","HUGE_RANGE"}){a=777;b=888;assert(!config.GetTwoValue(key,&a,&b)&&a==777&&b==888);}
// A line must be NUL-terminated, including when EOF ends the last line.
file(path,"ITEM_ID_RANGE = 1 2");assert(config.LoadFile(path));
f=input("xy\n");assert(config.GetLine(f,buffer.text,sizeof(buffer.text))&&!strcmp(buffer.text,"xy"));fclose(f);
f=input("xyz");assert(config.GetLine(f,buffer.text,sizeof(buffer.text))&&!strcmp(buffer.text,"xyz"));fclose(f);
for(const auto& bad:{std::string("NEW = safe\nBIG = ")+std::string(256,'x'),std::string("NEW = safe\nBIG = \"")+std::string(256,'x')+"\"",std::string("NEW = safe\nITEM_ID_RANGE = ")+std::string(256,'1'),std::string("NEW = safe\nBAD = \"unterminated"),std::string("NEW = safe\nBROKEN ="),std::string("NEW = safe\nBAD = a\0b",19)}){
file(path,bad);assert(!config.LoadFile(path));assert(!config.GetValue("NEW",name,sizeof(name)));assert(config.GetValue("VALUE",&value)&&value==42);
}
std::string unicode="DRAGON \xe9\xbe\x99";stl_lowers(unicode);assert(unicode=="dragon \xe9\xbe\x99");
}
'''
with tempfile.TemporaryDirectory() as tmp:
src=Path(tmp)/'probe.cpp';exe=src.with_suffix('');src.write_text(code)
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined','-fno-sanitize-recover=all','-Werror=implicit-int-conversion','-I'+str(ROOT),str(src),'-o',str(exe)],check=True)
subprocess.run([str(exe),str(Path(tmp)/'config')],check=True)
if __name__=='__main__':unittest.main()