test_zblacklist.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import unittest
  2. import subprocess
  3. import os
  4. import sys
  5. executable_path = None
  6. class ZBlacklistTest(unittest.TestCase):
  7. BLACKLIST = [
  8. "10.0.0.0/8 # private subnet",
  9. "192.168.0.0/16 # private subnet",
  10. "128.255.0.0/16 # university of iowa",
  11. "141.212.120.0/24 # halderman lab"
  12. ]
  13. WHITELIST = [
  14. "141.212.0.0/16 # university of michigan",
  15. ]
  16. IPS = [
  17. "61.193.80.24",
  18. "195.19.1.6",
  19. "114.34.253.25",
  20. "180.69.174.9",
  21. "38.134.130.203",
  22. "192.168.1.50",
  23. "98.125.221.180",
  24. "197.160.60.150",
  25. "47.139.63.128",
  26. "95.224.78.221",
  27. "170.114.52.252",
  28. "10.0.0.5",
  29. "128.255.134.1",
  30. "141.212.120.10",
  31. "141.212.12.6"
  32. ]
  33. IPS_MINUS_BL = [
  34. "61.193.80.24",
  35. "195.19.1.6",
  36. "114.34.253.25",
  37. "180.69.174.9",
  38. "38.134.130.203",
  39. "98.125.221.180",
  40. "197.160.60.150",
  41. "47.139.63.128",
  42. "95.224.78.221",
  43. "170.114.52.252",
  44. "141.212.12.6"
  45. ]
  46. WL_IPS = [
  47. "141.212.120.10",
  48. "141.212.12.6"
  49. ]
  50. WL_IPS_MINUS_BL = [
  51. "141.212.12.6"
  52. ]
  53. COMMENT_STRS = [
  54. "# some comment here",
  55. " # some comment here",
  56. ",google.com,data",
  57. "\t#some comment here"
  58. ]
  59. def setUp(self):
  60. global executable_path
  61. self.path = executable_path
  62. with open("/tmp/blacklist", "w") as fd:
  63. for line in self.BLACKLIST:
  64. fd.write("%s\n" % line)
  65. with open("/tmp/whitelist", "w") as fd:
  66. for line in self.WHITELIST:
  67. fd.write("%s\n" % line)
  68. with open("/tmp/ips", "w") as fd:
  69. for line in self.IPS:
  70. fd.write("%s\n" % line)
  71. with open("/tmp/ips-commented", "w") as fd:
  72. for line in self.IPS:
  73. for comment in self.COMMENT_STRS:
  74. fd.write("%s%s\n" % (line, comment))
  75. def tearDown(self):
  76. if os.path.exists("/tmp/blacklist"):
  77. os.remove("/tmp/blacklist")
  78. if os.path.exists("/tmp/whitelist"):
  79. os.remove("/tmp/whitelist")
  80. if os.path.exists("/tmp/ips"):
  81. os.remove("/tmp/ips")
  82. if os.path.exists("/tmp/ips-commented"):
  83. os.remove("/tmp/ips-commented")
  84. def execute(self, whitelist, blacklist, ipsfile="/tmp/ips", numtimestocat=1):
  85. cmd = "cat"
  86. for _ in range(0, numtimestocat):
  87. cmd += " %s" % ipsfile
  88. cmd += " | %s" % self.path
  89. if whitelist:
  90. cmd = cmd + " -w %s" % whitelist
  91. if blacklist:
  92. cmd = cmd + " -b %s" % blacklist
  93. results = subprocess.check_output(cmd, shell=True)
  94. ips = results.rstrip().split("\n")
  95. return ips
  96. def testValidBlacklist(self):
  97. res = self.execute(None, "/tmp/blacklist")
  98. self.assertEqual(set(res), set(self.IPS_MINUS_BL))
  99. def testValidWhitelist(self):
  100. res = self.execute("/tmp/whitelist", None)
  101. self.assertEqual(set(res), set(self.WL_IPS))
  102. def testValidWhiteAndBlackList(self):
  103. res = self.execute("/tmp/whitelist", "/tmp/blacklist")
  104. self.assertEqual(set(res), set(self.WL_IPS_MINUS_BL))
  105. def testDuplicateChecking(self):
  106. res = self.execute(None, "/tmp/blacklist", numtimestocat=5)
  107. self.assertEqual(len(res), len(self.IPS_MINUS_BL))
  108. self.assertEqual(set(res), set(self.IPS_MINUS_BL))
  109. def testCommentCharacters(self):
  110. res = self.execute(None, "/tmp/blacklist", ipsfile="/tmp/ips-commented")
  111. self.assertEqual(set(res), set(self.IPS_MINUS_BL))
  112. if __name__ == "__main__":
  113. if len(sys.argv) != 2:
  114. print "USAGE: %s zblacklist" % sys.argv[0]
  115. sys.exit(1)
  116. executable_path = sys.argv[1]
  117. assert(os.path.exists(executable_path))
  118. unittest.main(argv=sys.argv[:1])