1# Copyright 2021 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tests for big_integer_util.""" 15 16from absl.testing import absltest 17from absl.testing import parameterized 18 19from tink.internal import big_integer_util 20 21 22class BigIntegerUtilTest(parameterized.TestCase): 23 24 def test_bytes_to_num(self): 25 for i in range(100000): 26 big_int_bytes = big_integer_util.num_to_bytes(i) 27 self.assertEqual(int.from_bytes(big_int_bytes, byteorder='big'), i) 28 29 @parameterized.named_parameters( 30 ('0', 0, b'\x00'), ('255', 255, b'\xff'), ('256', 256, b'\x01\x00'), 31 ('65535', 65535, b'\xff\xff'), ('65536', 65536, b'\x01\x00\x00'), 32 ('65537', 65537, b'\x01\x00\x01'), ('65538', 65538, b'\x01\x00\x02'), 33 ('16909060', 16909060, b'\x01\x02\x03\x04')) 34 def test_num_to_bytes(self, number, expected): 35 self.assertEqual(big_integer_util.num_to_bytes(number), expected) 36 37 def test_num_to_bytes_minus_one_overflow(self): 38 with self.assertRaises(OverflowError): 39 big_integer_util.num_to_bytes(-1) 40 41 42if __name__ == '__main__': 43 absltest.main() 44