TestOptionalManager.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import hashlib
  2. import os
  3. import copy
  4. import json
  5. from cStringIO import StringIO
  6. import pytest
  7. from OptionalManager import OptionalManagerPlugin
  8. from util import helper
  9. from Crypt import CryptBitcoin
  10. @pytest.mark.usefixtures("resetSettings")
  11. class TestOptionalManager:
  12. def testDbFill(self, site):
  13. contents = site.content_manager.contents
  14. assert len(site.content_manager.hashfield) > 0
  15. assert contents.db.execute("SELECT COUNT(*) FROM file_optional WHERE is_downloaded = 1").fetchone()[0] == len(site.content_manager.hashfield)
  16. def testSetContent(self, site):
  17. contents = site.content_manager.contents
  18. # Add new file
  19. new_content = copy.deepcopy(contents["content.json"])
  20. new_content["files_optional"]["testfile"] = {
  21. "size": 1234,
  22. "sha512": "aaaabbbbcccc"
  23. }
  24. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  25. contents["content.json"] = new_content
  26. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] > num_optional_files_before
  27. # Remove file
  28. new_content = copy.deepcopy(contents["content.json"])
  29. del new_content["files_optional"]["testfile"]
  30. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  31. contents["content.json"] = new_content
  32. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] < num_optional_files_before
  33. def testDeleteContent(self, site):
  34. contents = site.content_manager.contents
  35. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  36. del contents["content.json"]
  37. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] < num_optional_files_before
  38. def testVerifyFiles(self, site):
  39. contents = site.content_manager.contents
  40. # Add new file
  41. new_content = copy.deepcopy(contents["content.json"])
  42. new_content["files_optional"]["testfile"] = {
  43. "size": 1234,
  44. "sha512": "aaaabbbbcccc"
  45. }
  46. contents["content.json"] = new_content
  47. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  48. assert not file_row["is_downloaded"]
  49. # Write file from outside of ZeroNet
  50. site.storage.open("testfile", "wb").write("A" * 1234) # For quick check hash does not matter only file size
  51. hashfield_len_before = len(site.content_manager.hashfield)
  52. site.storage.verifyFiles(quick_check=True)
  53. assert len(site.content_manager.hashfield) == hashfield_len_before + 1
  54. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  55. assert file_row["is_downloaded"]
  56. # Delete file outside of ZeroNet
  57. site.storage.delete("testfile")
  58. site.storage.verifyFiles(quick_check=True)
  59. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  60. assert not file_row["is_downloaded"]
  61. def testVerifyFilesSameHashId(self, site):
  62. contents = site.content_manager.contents
  63. new_content = copy.deepcopy(contents["content.json"])
  64. # Add two files with same hashid (first 4 character)
  65. new_content["files_optional"]["testfile1"] = {
  66. "size": 1234,
  67. "sha512": "aaaabbbbcccc"
  68. }
  69. new_content["files_optional"]["testfile2"] = {
  70. "size": 2345,
  71. "sha512": "aaaabbbbdddd"
  72. }
  73. contents["content.json"] = new_content
  74. assert site.content_manager.hashfield.getHashId("aaaabbbbcccc") == site.content_manager.hashfield.getHashId("aaaabbbbdddd")
  75. # Write files from outside of ZeroNet (For quick check hash does not matter only file size)
  76. site.storage.open("testfile1", "wb").write("A" * 1234)
  77. site.storage.open("testfile2", "wb").write("B" * 2345)
  78. site.storage.verifyFiles(quick_check=True)
  79. # Make sure that both is downloaded
  80. assert site.content_manager.isDownloaded("testfile1")
  81. assert site.content_manager.isDownloaded("testfile2")
  82. assert site.content_manager.hashfield.getHashId("aaaabbbbcccc") in site.content_manager.hashfield
  83. # Delete one of the files
  84. site.storage.delete("testfile1")
  85. site.storage.verifyFiles(quick_check=True)
  86. assert not site.content_manager.isDownloaded("testfile1")
  87. assert site.content_manager.isDownloaded("testfile2")
  88. assert site.content_manager.hashfield.getHashId("aaaabbbbdddd") in site.content_manager.hashfield
  89. def testIsPinned(self, site):
  90. assert not site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  91. site.content_manager.setPin("data/img/zerotalk-upvote.png", True)
  92. assert site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  93. assert len(site.content_manager.cache_is_pinned) == 1
  94. site.content_manager.cache_is_pinned = {}
  95. assert site.content_manager.isPinned("data/img/zerotalk-upvote.png")
  96. def testBigfilePieceReset(self, site):
  97. site.bad_files = {
  98. "data/fake_bigfile.mp4|0-1024": 10,
  99. "data/fake_bigfile.mp4|1024-2048": 10,
  100. "data/fake_bigfile.mp4|2048-3064": 10
  101. }
  102. site.onFileDone("data/fake_bigfile.mp4|0-1024")
  103. assert site.bad_files["data/fake_bigfile.mp4|1024-2048"] == 1
  104. assert site.bad_files["data/fake_bigfile.mp4|2048-3064"] == 1
  105. def testOptionalDelete(self, site):
  106. privatekey = "5KUh3PvNm5HUWoCfSUfcYvfQ2g3PrRNJWr6Q9eqdBGu23mtMntv"
  107. contents = site.content_manager.contents
  108. site.content_manager.setPin("data/img/zerotalk-upvote.png", True)
  109. site.content_manager.setPin("data/img/zeroid.png", False)
  110. new_content = copy.deepcopy(contents["content.json"])
  111. del new_content["files_optional"]["data/img/zerotalk-upvote.png"]
  112. del new_content["files_optional"]["data/img/zeroid.png"]
  113. assert site.storage.isFile("data/img/zerotalk-upvote.png")
  114. assert site.storage.isFile("data/img/zeroid.png")
  115. site.storage.writeJson("content.json", new_content)
  116. site.content_manager.loadContent("content.json", force=True)
  117. assert not site.storage.isFile("data/img/zeroid.png")
  118. assert site.storage.isFile("data/img/zerotalk-upvote.png")