• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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#     https://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
15
16"""Test class for Convertors."""
17
18import unittest
19
20from private_join_and_compute.py.crypto_util import converters
21
22
23class ConvertorsTest(unittest.TestCase):
24
25  def testLongToBytes(self):
26    bytes_n = converters.LongToBytes(5)
27    self.assertEqual(b'\005', bytes_n)
28
29  def testZeroToBytes(self):
30    bytes_n = converters.LongToBytes(0)
31    self.assertEqual(b'\000', bytes_n)
32
33  def testLongToBytesForBigNum(self):
34    bytes_n = converters.LongToBytes(2**72 - 1)
35    self.assertEqual(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff', bytes_n)
36
37  def testBytesToLong(self):
38    number = converters.BytesToLong(b'\005')
39    self.assertEqual(5, number)
40
41  def testBytesToLongForBigNum(self):
42    number = converters.BytesToLong(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff')
43    self.assertEqual(2**72 - 1, number)
44
45  def testLongToBytesCompatibleWithBytesToLong(self):
46    long_num = 4239423984023840823047823975923401283971204812394723040127401238
47    self.assertEqual(
48        long_num, converters.BytesToLong(converters.LongToBytes(long_num))
49    )
50
51  def testLongToBytesWithPadding(self):
52    bytes_n = converters.LongToBytes(5, 6)
53    self.assertEqual(b'\000\000\000\000\000\005', bytes_n)
54
55  def testBytesToLongWithPadding(self):
56    number = converters.BytesToLong(b'\000\000\000\000\000\005')
57    self.assertEqual(5, number)
58
59  def testLongToBytesCompatibleWithBytesToLongWithPadding(self):
60    long_num = 4239423984023840823047823975923401283971204812394723040127401238
61    self.assertEqual(
62        long_num, converters.BytesToLong(converters.LongToBytes(long_num, 51))
63    )
64
65  def testLongToBytesRaisesValueErrorForNegativeNumbers(self):
66    self.assertRaises(ValueError, converters.LongToBytes, -1)
67
68
69if __name__ == '__main__':
70  unittest.main()
71