1import unittest
2from util import *
3
4class InternalTests(unittest.TestCase):
5
6    def test_secp_context(self):
7        """Tests for secp context functions"""
8        # Allocate and free a secp context
9        ctx = wally_get_new_secp_context()
10        wally_secp_context_free(ctx)
11
12        # Freeing a NULL context is a no-op
13        wally_secp_context_free(None)
14
15    def test_operations(self):
16        """Tests for overriding the libraries default operations"""
17        # get_operations
18        # NULL output
19        self.assertEqual(wally_get_operations(None), WALLY_EINVAL)
20        # Incorrect struct size
21        ops = wally_operations()
22        ops.struct_size = 0
23        self.assertEqual(wally_get_operations(byref(ops)), WALLY_EINVAL)
24        # Correct struct size succeeds
25        ops.struct_size = sizeof(wally_operations)
26        self.assertEqual(wally_get_operations(byref(ops)), WALLY_OK)
27
28        # set_operations
29        # NULL input
30        self.assertEqual(wally_set_operations(None), WALLY_EINVAL)
31        # Incorrect struct size
32        ops.struct_size = 0
33        self.assertEqual(wally_set_operations(byref(ops)), WALLY_EINVAL)
34        # Correct struct size succeeds
35        ops.struct_size = sizeof(wally_operations)
36        # Set a secp context function that returns NULL
37        def null_secp_context():
38            return None
39        secp_context_fn_t = CFUNCTYPE(c_void_p)
40        ops.secp_context_fn = secp_context_fn_t(null_secp_context)
41        self.assertEqual(wally_set_operations(byref(ops)), WALLY_OK)
42
43        # Verify that the function was set correctly
44        self.assertEqual(wally_secp_randomize(urandom(32), 32), WALLY_ENOMEM)
45
46        # Verify that NULL members are unchanged when setting
47        # TODO: OSX function casting results in a non-null pointer on OSX
48        #ops.secp_context_fn = cast(0, secp_context_fn_t)
49        #self.assertEqual(wally_set_operations(byref(ops)), WALLY_OK)
50        #self.assertEqual(wally_secp_randomize(urandom(32), 32), WALLY_ENOMEM)
51
52
53if __name__ == '__main__':
54    unittest.main()
55