다마고치 연구소 한국어판 소스 공개
This commit is contained in:
commit
db23855630
661 files changed
+503597
No files matched your search
@@ -0,0 +1,133 @@
|
||||
import importlib.util
|
||||
import json
|
||||
import struct
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
from pathlib import Path
|
||||
|
||||
spec = importlib.util.spec_from_file_location('archive', Path(__file__).parents[1] / 'server/app.py')
|
||||
app = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(app)
|
||||
|
||||
|
||||
def fixture(edition, marker=0):
|
||||
data = bytearray(app.SIZE)
|
||||
data[:8] = b'SONIXDEV'
|
||||
data[100] = marker
|
||||
off = 0xD29000 if edition >= 5 else 0xD49000
|
||||
struct.pack_into('<I', data, off, edition)
|
||||
k = (app.CONSTANTS[edition] - edition) & 0xFFFFFFFF
|
||||
struct.pack_into('<II', data, off + 0xFF8, k, ~k & 0xFFFFFFFF)
|
||||
return bytes(data)
|
||||
|
||||
|
||||
class ArchiveTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
app.DATA = Path(self.temp.name)
|
||||
self.cfg = dict(app.DEFAULTS, username='test-admin', passwordHash=app.password_hash('test-only-password'))
|
||||
app.atomic_json(app.DATA / 'config.json', self.cfg)
|
||||
app.SESSIONS.clear()
|
||||
app.ATTEMPTS.clear()
|
||||
self.server = app.ThreadingHTTPServer(('127.0.0.1', 0), app.Handler)
|
||||
self.thread = threading.Thread(target=self.server.serve_forever, daemon=True)
|
||||
self.thread.start()
|
||||
self.base = 'http://127.0.0.1:' + str(self.server.server_port)
|
||||
|
||||
def tearDown(self):
|
||||
self.server.shutdown()
|
||||
self.server.server_close()
|
||||
self.thread.join()
|
||||
self.temp.cleanup()
|
||||
|
||||
def request(self, path, body=None, cookie=None, origin=True, binary=False):
|
||||
headers = {}
|
||||
if cookie:
|
||||
headers['Cookie'] = cookie
|
||||
if body is not None:
|
||||
headers['Content-Type'] = 'application/octet-stream' if binary else 'application/json'
|
||||
if origin:
|
||||
headers['Origin'] = app.ORIGIN
|
||||
if binary:
|
||||
headers['X-Backup-Notice'] = 'shown'
|
||||
else:
|
||||
body = json.dumps(body).encode()
|
||||
req = urllib.request.Request(self.base + path, data=body, headers=headers)
|
||||
try:
|
||||
response = urllib.request.urlopen(req, timeout=15)
|
||||
except urllib.error.HTTPError as error:
|
||||
response = error
|
||||
content = response.read()
|
||||
value = json.loads(content) if 'json' in response.headers.get('Content-Type', '') else content
|
||||
return response.status, value, response.headers
|
||||
|
||||
def login(self):
|
||||
status, _, headers = self.request('/api/login', {'username': 'test-admin', 'password': 'test-only-password'})
|
||||
self.assertEqual(status, 200)
|
||||
self.assertIn('HttpOnly; Secure; SameSite=Strict', headers['Set-Cookie'])
|
||||
return headers['Set-Cookie'].split(';')[0]
|
||||
|
||||
def test_classification_and_first_per_edition(self):
|
||||
for edition in range(1, 7):
|
||||
data = fixture(edition)
|
||||
self.assertEqual(app.detect(data), edition)
|
||||
first = app.store_image(data)
|
||||
self.assertEqual(first['status'], 'stored')
|
||||
second = app.store_image(fixture(edition, 3))
|
||||
self.assertEqual(second['status'], 'already-held')
|
||||
self.assertEqual(len(app.records()), 6)
|
||||
self.assertTrue(all(not r['published'] for r in app.records()))
|
||||
with self.assertRaises(ValueError):
|
||||
app.detect(bytes(app.SIZE))
|
||||
|
||||
def test_concurrent_upload_does_not_overwrite(self):
|
||||
threads = [threading.Thread(target=app.store_image, args=(fixture(4, x),)) for x in range(3)]
|
||||
for t in threads: t.start()
|
||||
for t in threads: t.join()
|
||||
self.assertEqual(len(app.records()), 1)
|
||||
self.assertEqual(len(list(app.DATA.glob('*/*.bin'))), 1)
|
||||
|
||||
def test_private_approval_and_revoke(self):
|
||||
status, result, _ = self.request('/api/backups', fixture(2), binary=True)
|
||||
self.assertEqual(status, 200)
|
||||
sha = result['sha256']
|
||||
self.assertEqual(self.request('/api/file/' + sha)[0], 404)
|
||||
self.assertEqual(self.request('/api/admin/file/' + sha)[0], 401)
|
||||
self.assertEqual(self.request('/api/catalog')[1]['items'], [])
|
||||
cookie = self.login()
|
||||
self.assertEqual(self.request('/api/admin/file/' + sha, cookie=cookie)[1], fixture(2))
|
||||
change = {'sha256': sha, 'label': 'Water verified file', 'published': True}
|
||||
self.assertEqual(self.request('/api/admin/record', change, cookie=cookie)[0], 200)
|
||||
self.assertEqual(self.request('/api/file/' + sha)[1], fixture(2))
|
||||
change['published'] = False
|
||||
self.request('/api/admin/record', change, cookie=cookie)
|
||||
self.assertEqual(self.request('/api/file/' + sha)[0], 404)
|
||||
|
||||
def test_auth_csrf_settings_and_original_protection(self):
|
||||
sha = app.store_image(fixture(4), protected=True, published=True)['sha256']
|
||||
cookie = self.login()
|
||||
self.assertEqual(self.request('/api/admin/settings', self.cfg)[0], 401)
|
||||
self.assertEqual(self.request('/api/admin/settings', self.cfg, cookie=cookie, origin=False)[0], 403)
|
||||
self.assertEqual(self.request('/api/admin/record', {'sha256': sha, 'action': 'delete'}, cookie=cookie)[0], 400)
|
||||
values = dict(app.DEFAULTS, title='Changed title', autoBackup=False)
|
||||
self.assertEqual(self.request('/api/admin/settings', values, cookie=cookie)[0], 200)
|
||||
self.assertEqual(app.store_image(fixture(1))['status'], 'disabled')
|
||||
self.assertEqual(self.request('/api/catalog')[1]['title'], 'Changed title')
|
||||
self.assertEqual(self.request('/api/admin/password', {'currentPassword':'test-only-password', 'newPassword':'replacement-password'}, cookie=cookie)[0], 200)
|
||||
self.assertEqual(self.request('/api/admin/state', cookie=cookie)[0], 401)
|
||||
self.assertTrue(app.password_ok('replacement-password', app.config()['passwordHash']))
|
||||
|
||||
def test_bad_image_and_quota(self):
|
||||
self.assertEqual(self.request('/api/backups', b'invalid', binary=True)[0], 400)
|
||||
self.cfg['maxStorageMB'] = 16
|
||||
app.atomic_json(app.DATA / 'config.json', self.cfg)
|
||||
app.store_image(fixture(1))
|
||||
self.assertEqual(self.request('/api/backups', fixture(2), binary=True)[0], 400)
|
||||
self.assertEqual(len(app.records()), 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main(verbosity=2)
|
||||
Reference in new issue
Block a user