test_octets.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  5. # License: https://pyasn1.readthedocs.io/en/latest/license.html
  6. #
  7. import sys
  8. import unittest
  9. from __tests__.base import BaseTestCase
  10. from pyasn1.compat import octets
  11. class OctetsTestCase(BaseTestCase):
  12. if sys.version_info[0] > 2:
  13. def test_ints2octs(self):
  14. assert [1, 2, 3] == list(octets.ints2octs([1, 2, 3]))
  15. def test_ints2octs_empty(self):
  16. assert not octets.ints2octs([])
  17. def test_int2oct(self):
  18. assert [12] == list(octets.int2oct(12))
  19. def test_octs2ints(self):
  20. assert [1, 2, 3] == list(octets.octs2ints(bytes([1, 2, 3])))
  21. def test_octs2ints_empty(self):
  22. assert not octets.octs2ints(bytes([]))
  23. def test_oct2int(self):
  24. assert 12 == octets.oct2int(bytes([12]))[0]
  25. def test_str2octs(self):
  26. assert bytes([1, 2, 3]) == octets.str2octs('\x01\x02\x03')
  27. def test_str2octs_empty(self):
  28. assert not octets.str2octs('')
  29. def test_octs2str(self):
  30. assert '\x01\x02\x03' == octets.octs2str(bytes([1, 2, 3]))
  31. def test_octs2str_empty(self):
  32. assert not octets.octs2str(bytes([]))
  33. def test_isOctetsType(self):
  34. assert octets.isOctetsType('abc') == False
  35. assert octets.isOctetsType(123) == False
  36. assert octets.isOctetsType(bytes()) == True
  37. def test_isStringType(self):
  38. assert octets.isStringType('abc') == True
  39. assert octets.isStringType(123) == False
  40. assert octets.isStringType(bytes()) == False
  41. def test_ensureString(self):
  42. assert 'abc'.encode() == octets.ensureString('abc'.encode())
  43. assert bytes([1, 2, 3]) == octets.ensureString([1, 2, 3])
  44. else:
  45. def test_ints2octs(self):
  46. assert '\x01\x02\x03' == octets.ints2octs([1, 2, 3])
  47. def test_ints2octs_empty(self):
  48. assert not octets.ints2octs([])
  49. def test_int2oct(self):
  50. assert '\x0c' == octets.int2oct(12)
  51. def test_octs2ints(self):
  52. assert [1, 2, 3] == octets.octs2ints('\x01\x02\x03')
  53. def test_octs2ints_empty(self):
  54. assert not octets.octs2ints('')
  55. def test_oct2int(self):
  56. assert 12 == octets.oct2int('\x0c')
  57. def test_str2octs(self):
  58. assert '\x01\x02\x03' == octets.str2octs('\x01\x02\x03')
  59. def test_str2octs_empty(self):
  60. assert not octets.str2octs('')
  61. def test_octs2str(self):
  62. assert '\x01\x02\x03' == octets.octs2str('\x01\x02\x03')
  63. def test_octs2str_empty(self):
  64. assert not octets.octs2str('')
  65. def test_isOctetsType(self):
  66. assert octets.isOctetsType('abc') == True
  67. assert octets.isOctetsType(123) == False
  68. assert octets.isOctetsType(unicode('abc')) == False
  69. def test_isStringType(self):
  70. assert octets.isStringType('abc') == True
  71. assert octets.isStringType(123) == False
  72. assert octets.isStringType(unicode('abc')) == True
  73. def test_ensureString(self):
  74. assert 'abc' == octets.ensureString('abc')
  75. assert '123' == octets.ensureString(123)
  76. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  77. if __name__ == '__main__':
  78. unittest.TextTestRunner(verbosity=2).run(suite)