File size: 4,343 Bytes
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
"""Check independent calculator probe oracles and containment; does not run Java on the host."""
import ast
import base64
from decimal import Decimal, localcontext, ROUND_HALF_UP
import unittest

from src.modules.execution.scenarios.calculator import (
    ACCEPTANCE_MAP, CONTRACT, DEMO_CASES, REQUIREMENT_ITEMS, acceptance_cases, calculation_path, scenario,
)


def arithmetic_oracle(expression):
    """Independent Python AST oracle limited to decimal constants and four operators."""
    text = expression.strip()
    def evaluate(node):
        if isinstance(node, ast.Constant) and type(node.value) in (int, float):
            return Decimal(ast.get_source_segment(text, node))
        if isinstance(node, ast.UnaryOp) and isinstance(node.op, (ast.UAdd, ast.USub)):
            value = evaluate(node.operand)
            return -value if isinstance(node.op, ast.USub) else value
        if isinstance(node, ast.BinOp):
            left, right = evaluate(node.left), evaluate(node.right)
            if isinstance(node.op, ast.Add): return left + right
            if isinstance(node.op, ast.Sub): return left - right
            if isinstance(node.op, ast.Mult): return left * right
            if isinstance(node.op, ast.Div): return left / right
        raise ValueError('Oracle expression is outside the four-operation grammar')
    with localcontext() as context:
        context.prec = 34
        value = evaluate(ast.parse(text, mode='eval').body)
    with localcontext() as context:
        context.prec = 64
        value = value.quantize(Decimal('0.000000000001'), rounding=ROUND_HALF_UP)
        result = format(value, 'f').rstrip('0').rstrip('.')
    return '0' if value == 0 else result


class CalculatorContractTests(unittest.TestCase):
    def test_success_oracles_match_independent_decimal_arithmetic(self):
        successful = [case['http'] for case in acceptance_cases() if case['http'].status == 200 and case['id'] != 'health']
        self.assertEqual(len(successful), 15)
        for case in successful:
            expression = case.expected_json['expression']
            with self.subTest(expression=expression):
                self.assertEqual(case.expected_json['result'], arithmetic_oracle(expression))
        for example in DEMO_CASES:
            self.assertEqual(example['result'], arithmetic_oracle(example['expression']))

    def test_expression_transport_preserves_operators_and_stays_in_probe_policy(self):
        config = scenario('test-java@sha256:' + 'a' * 64)
        self.assertLessEqual(len(CONTRACT), 4000)
        self.assertEqual(len(config.tests), 39)
        self.assertTrue(all('%' not in case.path and '..' not in case.path and len(case.path) <= 241 for case in config.tests))
        for expression in ['-(2+3)*-2', ' 1 / (2 + 3) ', '1+' * 64 + '1']:
            path = calculation_path(expression)
            token = path.split('=', 1)[1]
            decoded = base64.urlsafe_b64decode(token + '=' * (-len(token) % 4)).decode('utf-8')
            self.assertEqual(decoded, expression)
            self.assertNotIn('=', token)

    def test_rejection_gates_and_requirements_cannot_disappear(self):
        cases = {case['id']: case['http'] for case in acceptance_cases()}
        self.assertEqual(cases['zero-divisor'].expected_json, {'error': 'DIVISION_BY_ZERO'})
        for name in ['length-limit', 'depth-limit', 'operator-limit', 'digit-limit', 'value-limit']:
            self.assertEqual((cases[name].status, cases[name].expected_json), (422, {'error': 'LIMIT_EXCEEDED'}))
        for name in ['missing-argument', 'duplicate-argument', 'extra-argument', 'invalid-base64', 'padded-base64', 'invalid-utf8']:
            self.assertEqual((cases[name].status, cases[name].expected_json), (400, {'error': 'INVALID_INPUT'}))
        self.assertEqual(cases['unauthorized'].headers, {})
        self.assertEqual(cases['unauthorized'].status, 401)
        self.assertEqual(cases['wrong-method'].headers, {})
        self.assertEqual((cases['wrong-method'].method, cases['wrong-method'].status), ('POST', 405))
        self.assertEqual(cases['unknown-path'].status, 404)
        self.assertEqual(set(ACCEPTANCE_MAP), set(REQUIREMENT_ITEMS))
        self.assertTrue(all(ACCEPTANCE_MAP[requirement] for requirement in REQUIREMENT_ITEMS))


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