TestOptionalManager.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import hashlib
  2. import os
  3. import copy
  4. import pytest
  5. from OptionalManager import OptionalManagerPlugin
  6. from util import helper
  7. @pytest.mark.usefixtures("resetSettings")
  8. class TestOptionalManager:
  9. def testDbFill(self, site):
  10. contents = site.content_manager.contents
  11. assert len(site.content_manager.hashfield) > 0
  12. assert contents.db.execute("SELECT COUNT(*) FROM file_optional WHERE is_downloaded = 1").fetchone()[0] == len(site.content_manager.hashfield)
  13. def testSetContent(self, site):
  14. contents = site.content_manager.contents
  15. # Add new file
  16. new_content = copy.deepcopy(contents["content.json"])
  17. new_content["files_optional"]["testfile"] = {
  18. "size": 1234,
  19. "sha512": "aaaabbbbcccc"
  20. }
  21. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  22. contents["content.json"] = new_content
  23. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] > num_optional_files_before
  24. # Remove file
  25. new_content = copy.deepcopy(contents["content.json"])
  26. del new_content["files_optional"]["testfile"]
  27. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  28. contents["content.json"] = new_content
  29. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] < num_optional_files_before
  30. def testDeleteContent(self, site):
  31. contents = site.content_manager.contents
  32. num_optional_files_before = contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0]
  33. del contents["content.json"]
  34. assert contents.db.execute("SELECT COUNT(*) FROM file_optional").fetchone()[0] < num_optional_files_before
  35. def testVerifyFiles(self, site):
  36. contents = site.content_manager.contents
  37. # Add new file
  38. new_content = copy.deepcopy(contents["content.json"])
  39. new_content["files_optional"]["testfile"] = {
  40. "size": 1234,
  41. "sha512": "aaaabbbbcccc"
  42. }
  43. contents["content.json"] = new_content
  44. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  45. assert not file_row["is_downloaded"]
  46. # Write file from outside of ZeroNet
  47. site.storage.open("testfile", "wb").write("A" * 1234) # For quick check hash does not matter only file size
  48. hashfield_len_before = len(site.content_manager.hashfield)
  49. site.storage.verifyFiles(quick_check=True)
  50. assert len(site.content_manager.hashfield) == hashfield_len_before + 1
  51. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  52. assert file_row["is_downloaded"]
  53. # Delete file outside of ZeroNet
  54. site.storage.delete("testfile")
  55. site.storage.verifyFiles(quick_check=True)
  56. file_row = contents.db.execute("SELECT * FROM file_optional WHERE inner_path = 'testfile'").fetchone()
  57. assert not file_row["is_downloaded"]
  58. def testVerifyFilesSameHashId(self, site):
  59. contents = site.content_manager.contents
  60. new_content = copy.deepcopy(contents["content.json"])
  61. # Add two files with same hashid (first 4 character)
  62. new_content["files_optional"]["testfile1"] = {
  63. "size": 1234,
  64. "sha512": "aaaabbbbcccc"
  65. }
  66. new_content["files_optional"]["testfile2"] = {
  67. "size": 2345,
  68. "sha512": "aaaabbbbdddd"
  69. }
  70. contents["content.json"] = new_content
  71. assert site.content_manager.hashfield.getHashId("aaaabbbbcccc") == site.content_manager.hashfield.getHashId("aaaabbbbdddd")
  72. # Write files from outside of ZeroNet (For quick check hash does not matter only file size)
  73. site.storage.open("testfile1", "wb").write("A" * 1234)
  74. site.storage.open("testfile2", "wb").write("B" * 2345)
  75. site.storage.verifyFiles(quick_check=True)
  76. # Make sure that both is downloaded
  77. assert site.content_manager.isDownloaded("testfile1")
  78. assert site.content_manager.isDownloaded("testfile2")
  79. assert site.content_manager.hashfield.getHashId("aaaabbbbcccc") in site.content_manager.hashfield
  80. # Delete one of the files
  81. site.storage.delete("testfile1")
  82. site.storage.verifyFiles(quick_check=True)
  83. assert not site.content_manager.isDownloaded("testfile1")
  84. assert site.content_manager.isDownloaded("testfile2")
  85. assert site.content_manager.hashfield.getHashId("aaaabbbbdddd") in site.content_manager.hashfield