PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/bencode_tests.py

https://gitlab.com/lukaf/python-bittorrent
Python | 464 lines | 220 code | 152 blank | 92 comment | 0 complexity | 00d27159ee9c84125e2de39468edd12b MD5 | raw file
  1. #!/usr/bin/env python
  2. # bencode_tests.py -- testing the bencoding module
  3. import unittest
  4. import bencode
  5. class Walk(unittest.TestCase):
  6. """ Check the function walk() works correctly. """
  7. def test_simple_list(self):
  8. """ Test that simple lists are correctly seperated. """
  9. self.exp = "li1eei1e"
  10. self.n = bencode.walk(self.exp, 1)
  11. self.assertEqual(self.exp[:self.n], "li1ee")
  12. def test_longer_list(self):
  13. """ Test that longer lists are correctly seperated. """
  14. self.exp = "li1ei2eei1e"
  15. self.n = bencode.walk(self.exp, 1)
  16. self.assertEqual(self.exp[:self.n], "li1ei2ee")
  17. def test_list_with_string(self):
  18. """ Test that simple list with a string is seperated. """
  19. self.exp = "l4:teste3:end"
  20. self.n = bencode.walk(self.exp, 1)
  21. self.assertEqual(self.exp[:self.n], "l4:teste")
  22. def test_list_with_long_string(self):
  23. """ Test a list with a long string is seperated correctly. """
  24. self.exp = "l10:eggsandhame3:end"
  25. self.n = bencode.walk(self.exp, 1)
  26. self.assertEqual(self.exp[:self.n], "l10:eggsandhame")
  27. def test_nested_list(self):
  28. """ Test a nested list is seperated correctly. """
  29. self.exp = "li1eli2eei3eeli1ee"
  30. self.n = bencode.walk(self.exp, 1)
  31. self.assertEqual(self.exp[:self.n], "li1eli2eei3ee")
  32. def test_simple_dict(self):
  33. """ Test that simple dict is correctly seperated. """
  34. self.exp = "d3:key5:valueei1e"
  35. self.n = bencode.walk(self.exp, 1)
  36. self.assertEqual(self.exp[:self.n], "d3:key5:valuee")
  37. def test_longer_dict(self):
  38. """ Test that a longer dict is correctly seperated. """
  39. self.exp = "d5:key_17:value_15:key_27:value_2ei1e"
  40. self.n = bencode.walk(self.exp, 1)
  41. self.assertEqual(self.exp[:self.n], "d5:key_17:value_15:key_27:value_2e")
  42. def test_nested_dict(self):
  43. """ Test that a nested dict is correctly seperated. """
  44. self.exp = "d3:subd3:key5:valueeei1e"
  45. self.n = bencode.walk(self.exp, 1)
  46. self.assertEqual(self.exp[:self.n], "d3:subd3:key5:valueee")
  47. class Inflate(unittest.TestCase):
  48. """ Check the inflate() function works correctly. """
  49. def test_simple(self):
  50. """ Test that a simple expression is inflated correctly. """
  51. self.n = bencode.inflate("i1e")
  52. self.assertEqual(self.n, ["i1e"])
  53. def test_longer(self):
  54. """ Test that a longer expression is inflated correctly. """
  55. self.n = bencode.inflate("i1ei2ei3e")
  56. self.assertEqual(self.n, ["i1e", "i2e", "i3e"])
  57. def test_long_string(self):
  58. """ Test that an expression containing a long string is
  59. inflated correctly. """
  60. self.n = bencode.inflate("i1e15:averylongstringi2e")
  61. self.assertEqual(self.n, ["i1e", "15:averylongstring", "i2e"])
  62. def test_mixed_simple(self):
  63. """ Test that a mixed simple expression is inflated correctly. """
  64. self.n = bencode.inflate("3:onei1e3:twoi2e")
  65. self.assertEqual(self.n, ["3:one", "i1e", "3:two", "i2e"])
  66. def test_mixed_complex(self):
  67. """ Test that a mixed complex expression is inflated correctly. """
  68. self.n = bencode.inflate("li1ei2eed3:key5:valuee")
  69. self.assertEqual(self.n, ["li1ei2ee", "d3:key5:valuee"])
  70. class Ben_Type(unittest.TestCase):
  71. """ Check the function ben_type() works correctly. """
  72. def test_integers(self):
  73. """ Test that integers are correctly identified. """
  74. self.n = bencode.ben_type("i1e")
  75. self.assertEqual(self.n, int)
  76. def test_string(self):
  77. """ Test that strings are correctly identified. """
  78. self.n = bencode.ben_type("4:test")
  79. self.assertEqual(self.n, str)
  80. def test_list(self):
  81. """ Test that lists are correctly identified. """
  82. self.n = bencode.ben_type("l4:teste")
  83. self.assertEqual(self.n, list)
  84. def test_dict(self):
  85. """ Test that dictionaries are correctly identified. """
  86. self.n = bencode.ben_type("d3:key5:valuee")
  87. self.assertEqual(self.n, dict)
  88. class Encode_Int(unittest.TestCase):
  89. """ Check the function encode_int() works correctly. """
  90. def test_simple_integers(self):
  91. """ Test that simple integers are encoded correctly. """
  92. self.n = bencode.encode_int(1)
  93. self.assertEqual(self.n, "i1e")
  94. def test_zero(self):
  95. """ Test that zero is encoded correctly. """
  96. self.n = bencode.encode_int(0)
  97. self.assertEqual(self.n, "i0e")
  98. def test_longer_integers(self):
  99. """ Test that longer numbers are correctly encoded. """
  100. self.n = bencode.encode_int(12345)
  101. self.assertEqual(self.n, "i12345e")
  102. def test_minus_integers(self):
  103. """ Test that minus numbers are correctly encoded. """
  104. self.n = bencode.encode_int(-1)
  105. self.assertEqual(self.n, "i-1e")
  106. def test_leading_zeros(self):
  107. """ Test that leading zeros are correctly removed. """
  108. self.n = bencode.encode_int(01)
  109. self.assertEqual(self.n, "i1e")
  110. def test_exception_on_string(self):
  111. """ Test an exception is raised when encoding a string. """
  112. self.assertRaises(bencode.BencodeError, bencode.encode_int, "test")
  113. class Decode_Int(unittest.TestCase):
  114. """ Check the function decode_int() works correctly. """
  115. def test_simple_integers(self):
  116. """ Test that simple integers are decoded correctly. """
  117. self.n = bencode.decode_int("i1e")
  118. self.assertEqual(self.n, 1)
  119. def test_zero(self):
  120. """ Test that zero is decoded correctly. """
  121. self.n = bencode.decode_int("i0e")
  122. self.assertEqual(self.n, 0)
  123. def test_longer_integers(self):
  124. """ Test that longer numbers are correctly decoded. """
  125. self.n = bencode.decode_int("i12345e")
  126. self.assertEqual(self.n, 12345)
  127. def test_minus_integers(self):
  128. """ Test that minus numbers are correctly decoded. """
  129. self.n = bencode.decode_int("i-1e")
  130. self.assertEqual(self.n, -1)
  131. def test_exception_on_leading_zeros(self):
  132. """ Test that an exception is raised when decoding an expression which
  133. has leading zeros. """
  134. self.assertRaises(bencode.BencodeError, bencode.decode_int, "i01e")
  135. def test_exception_on_missing_start_constant(self):
  136. """ Test that an exception is raised when trying to decode an expression
  137. which is missing the start constant. """
  138. self.assertRaises(bencode.BencodeError, bencode.decode_int, "1e")
  139. def test_exception_on_missing_end_constant(self):
  140. """ Test that an exception is raised when trying to decode an expression
  141. which is missing the end constant. """
  142. self.assertRaises(bencode.BencodeError, bencode.decode_int, "i1")
  143. class Encode_Str(unittest.TestCase):
  144. """ Check the function encode_str() works correctly. """
  145. def test_character(self):
  146. """ Test that a single character is encoded correctly. """
  147. self.n = bencode.encode_str("a")
  148. self.assertEqual(self.n, "1:a")
  149. def test_string(self):
  150. """ Test that a string is encoded correctly. """
  151. self.n = bencode.encode_str("test")
  152. self.assertEqual(self.n, "4:test")
  153. def test_long_string(self):
  154. """ Test that a long string is encoded correctly. """
  155. self.n = bencode.encode_str("averylongstring")
  156. self.assertEqual(self.n, "15:averylongstring")
  157. def test_exception_on_int(self):
  158. """ Test that an exception is raised when trying to encode an integer. """
  159. self.assertRaises(bencode.BencodeError, bencode.encode_str, 1)
  160. class Decode_Str(unittest.TestCase):
  161. """ Check the function decode_str() works correctly. """
  162. def test_character(self):
  163. """ Test that a single character is decoded correctly """
  164. self.n = bencode.decode_str("1:a")
  165. self.assertEqual(self.n, "a")
  166. def test_string(self):
  167. """ Test that a string is decoded correctly. """
  168. self.n = bencode.decode_str("4:test")
  169. self.assertEqual(self.n, "test")
  170. def test_long_string(self):
  171. """ Test that a long string is decoded correctly. """
  172. self.n = bencode.decode_str("15:averylongstring")
  173. self.assertEqual(self.n, "averylongstring")
  174. def test_string_length(self):
  175. """ Test that string length is respected. """
  176. self.n = bencode.decode_str("1:abc")
  177. self.assertEqual(self.n, "a")
  178. def test_exception_on_no_number(self):
  179. """ Test that an exception is raised when no number is prefixed. """
  180. self.assertRaises(bencode.BencodeError, bencode.decode_str, "abc")
  181. class Encode_List(unittest.TestCase):
  182. """ Check the function encode_list() works correctly. """
  183. def test_simple_list(self):
  184. """ Test that a one item list is encoded correctly. """
  185. self.n = bencode.encode_list([1])
  186. self.assertEquals(self.n, "li1ee")
  187. def test_longer_list(self):
  188. """ Test that a longer list is encoded correctly. """
  189. self.n = bencode.encode_list([1, 2, 3])
  190. self.assertEquals(self.n, "li1ei2ei3ee")
  191. def test_mixed_list(self):
  192. """ Test that a mixed list is encoded correctly. """
  193. self.n = bencode.encode_list([1, "one"])
  194. self.assertEquals(self.n, "li1e3:onee")
  195. def test_nested_list(self):
  196. """ Test that a nested list is encoded correctly. """
  197. self.n = bencode.encode_list([[1, 2], [3, 4]])
  198. self.assertEquals(self.n, "lli1ei2eeli3ei4eee")
  199. def test_empty_list(self):
  200. """ Test that an empty list is encoded correctly. """
  201. self.n = bencode.encode_list([])
  202. self.assertEquals(self.n, "le")
  203. def test_exception_on_string(self):
  204. """ Test that an exception is raised when given a string. """
  205. self.assertRaises(bencode.BencodeError, bencode.encode_list, "test")
  206. class Decode_List(unittest.TestCase):
  207. """ Check the function decode_list() works correctly. """
  208. def test_simple_list(self):
  209. """ Test that a one item list is decoded correctly. """
  210. self.n = bencode.decode_list("li1ee")
  211. self.assertEquals(self.n, [1])
  212. def test_longer_list(self):
  213. """ Test that a longer list is decoded correctly. """
  214. self.n = bencode.decode_list("li1ei2ei3ee")
  215. self.assertEquals(self.n, [1, 2, 3])
  216. def test_mixed_list(self):
  217. """ Test that a mixed list is decoded correctly. """
  218. self.n = bencode.decode_list("li1e3:onee")
  219. self.assertEquals(self.n, [1, "one"])
  220. def test_nested_list(self):
  221. """ Test that a nested list is decoded correctly. """
  222. self.n = bencode.decode_list("lli1ei2eeli3ei4eee")
  223. self.assertEquals(self.n, [[1, 2], [3, 4]])
  224. def test_empty_list(self):
  225. """ Test that an empty list is decoded correctly. """
  226. self.n = bencode.decode_list("le")
  227. self.assertEquals(self.n, [])
  228. def test_exception_on_string(self):
  229. """ Test that an exception is raised when given a string. """
  230. self.assertRaises(bencode.BencodeError, bencode.decode_list, "test")
  231. class Encode_Dict(unittest.TestCase):
  232. """ Check the function encode_dict() works correctly. """
  233. def test_simple_dict(self):
  234. """ Test that a one key dict is encoded correctly. """
  235. self.n = bencode.encode_dict({"key":"value"})
  236. self.assertEquals(self.n, "d3:key5:valuee")
  237. def test_longer_dict(self):
  238. """ Test that a longer dict is encoded correctly. """
  239. self.n = bencode.encode_dict({"key_1":"value_1", "key_2":"value_2"})
  240. self.assertEquals(self.n, "d5:key_17:value_15:key_27:value_2e")
  241. def test_mixed_dict(self):
  242. """ Test that a dict with a list value is encoded correctly. """
  243. self.n = bencode.encode_dict({'key': ['a', 'b']})
  244. self.assertEquals(self.n, "d3:keyl1:a1:bee")
  245. def test_nested_dict(self):
  246. """ Test that a nested dict is encoded correctly. """
  247. self.n = bencode.encode_dict({"key":{"key":"value"}})
  248. self.assertEquals(self.n, "d3:keyd3:key5:valueee")
  249. def test_exception_on_string(self):
  250. """ Test that an exception is raised when given a string. """
  251. self.assertRaises(bencode.BencodeError, bencode.encode_dict, "test")
  252. class Decode_Dict(unittest.TestCase):
  253. """ Check the function decode_dict() works correctly. """
  254. def test_simple_dict(self):
  255. """ Test that a one key dict is decoded correctly. """
  256. self.n = bencode.decode_dict("d3:key5:valuee")
  257. self.assertEquals(self.n, {"key":"value"})
  258. def test_longer_dict(self):
  259. """ Test that a longer dict is decoded correctly. """
  260. self.n = bencode.decode_dict("d5:key_17:value_15:key_27:value_2e")
  261. self.assertEquals(self.n, {"key_1":"value_1", "key_2":"value_2"})
  262. def test_mixed_dict(self):
  263. """ Test that a dict with a list value is decoded correctly. """
  264. self.n = bencode.decode_dict("d3:keyl1:a1:bee")
  265. self.assertEquals(self.n, {'key': ['a', 'b']})
  266. def test_nested_dict(self):
  267. """ Test that a nested dict is decoded correctly. """
  268. self.n = bencode.decode_dict("d3:keyd3:key5:valueee")
  269. self.assertEquals(self.n, {"key":{"key":"value"}})
  270. def test_exception_on_string(self):
  271. """ Test that an exception is raised when given a string. """
  272. self.assertRaises(bencode.BencodeError, bencode.decode_dict, "test")
  273. class Encode(unittest.TestCase):
  274. """ Check the encode() function works. As this dispatches to the other
  275. encode functions, we only have to check the dispatching, not the other
  276. functions, as we have already checked those. """
  277. def test_integers(self):
  278. """ Test integers are encoded correctly. """
  279. self.n = bencode.encode(123)
  280. self.assertEqual(self.n, "i123e")
  281. def test_strings(self):
  282. """ Test strings are encoded correctly. """
  283. self.n = bencode.encode("test")
  284. self.assertEqual(self.n, "4:test")
  285. def test_lists(self):
  286. """ Test lists are encoded correctly. """
  287. self.n = bencode.encode([1, 2, 3])
  288. self.assertEquals(self.n, "li1ei2ei3ee")
  289. def test_dicts(self):
  290. """ Test dicts are encoded correctly. """
  291. self.n = bencode.encode({"key":"value"})
  292. self.assertEquals(self.n, "d3:key5:valuee")
  293. class Decode(unittest.TestCase):
  294. """ Check the decode() function works. As this dispatches to the other
  295. decode functions, we only have to check the dispatching, not the other
  296. functions, as we have already checked those. """
  297. def test_integers(self):
  298. """ Test integers are decoded correctly. """
  299. self.n = bencode.decode("i123e")
  300. self.assertEqual(self.n, 123)
  301. def test_strings(self):
  302. """ Test strings are decoded correctly. """
  303. self.n = bencode.decode("4:test")
  304. self.assertEqual(self.n, "test")
  305. def test_lists(self):
  306. """ Test lists are decoded correctly. """
  307. self.n = bencode.decode("li1ee")
  308. self.assertEqual(self.n, [1])
  309. def test_dicts(self):
  310. """ Test dictionaries are decoded correctly. """
  311. self.n = bencode.decode("d3:key5:valuee")
  312. self.assertEqual(self.n, {"key":"value"})