File size: 3,268 Bytes
870b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import hashlib
import json
from pathlib import Path
import sqlite3
import tempfile
import unittest

from engine.native_contract import load_pools, check_candidate


class NativeContractTests(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.TemporaryDirectory()
        self.addCleanup(self.temp.cleanup)
        self.root = Path(self.temp.name)
        asset = self.root / 'fixture.asset'
        asset.write_text('synthetic source bytes, not a real asset')
        sha = hashlib.sha256(asset.read_bytes()).hexdigest()
        self.candidate = dict(asset_id='new', dimensions_m=[1, 2, 3],
             asset_sha256=sha, runtime_sha256=sha, canonical_path=str(asset),
             runtime_path=str(asset), canonical_transform=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])
        self.actual = dict(self.candidate, source_dimensions_m=[1,2,3], uniform_scale=1)
        self.actual['entity_id'] = 'table'

    def pool(self):
        path = self.root / 'pool.sqlite3'
        with sqlite3.connect(path) as db:
            db.execute('CREATE TABLE candidates (candidate_json TEXT)')
            db.execute('INSERT INTO candidates VALUES (?)', (json.dumps(self.candidate),))
        path.with_suffix('.receipt.json').write_text(json.dumps(dict(
            schema='rhg.prepared_asset_pool.v2', sha256=hashlib.sha256(path.read_bytes()).hexdigest())))
        return path

    def test_read_only_pool_and_native_record_check(self):
        candidates, hashes = load_pools([self.pool()], {'new'})
        errors, sources = check_candidate(self.actual, candidates['new'])
        self.assertEqual(errors, [])
        self.assertTrue(sources)
        self.assertEqual(len(hashes), 2)

    def test_size_or_transform_mismatch_rejected(self):
        for field, value in [('dimensions_m', [.1,.1,.1]), ('uniform_scale', .1),
                             ('canonical_transform', [.5]*16), ('runtime_sha256', '0'*64)]:
            actual = dict(self.actual, **{field: value})
            errors, _ = check_candidate(actual, [self.candidate])
            self.assertTrue(errors, field)

    def test_stale_source_bytes_rejected(self):
        Path(self.candidate['runtime_path']).write_text('mutated source')
        self.assertTrue(check_candidate(self.actual, [self.candidate])[0])

    def test_tampered_pool_receipt_rejected(self):
        path = self.pool()
        path.with_suffix('.receipt.json').write_text('{"schema":"rhg.prepared_asset_pool.v2","sha256":"bad"}')
        with self.assertRaises(ValueError):
            load_pools([path], {'new'})

    def test_active_wal_is_not_part_of_a_sealed_database(self):
        path = self.pool()
        with sqlite3.connect(path) as db:
            db.execute('PRAGMA journal_mode=WAL')
            db.execute('PRAGMA wal_checkpoint(TRUNCATE)')
            path.with_suffix('.receipt.json').write_text(json.dumps(dict(
                schema='rhg.prepared_asset_pool.v2', sha256=hashlib.sha256(path.read_bytes()).hexdigest())))
            db.execute('UPDATE candidates SET candidate_json=?', (json.dumps(dict(self.candidate, dimensions_m=[.01]*3)),))
            db.commit()
            with self.assertRaises(ValueError):
                load_pools([path], {'new'})


if __name__ == '__main__':
    unittest.main()