pyruse/tests/action_nftBan.py

148 lines
5.1 KiB
Python
Raw Normal View History

# pyruse is intended as a replacement to both fail2ban and epylog
2018-01-31 08:28:05 +01:00
# Copyright © 20172018 Y. Gablin
# Full licensing information in the LICENSE file, or gnu.org/licences/gpl-3.0.txt if the file is missing.
2017-12-15 19:36:50 +01:00
import json
import os
import time
from pyruse.actions.action_nftBan import Action
nftBanCmd = "nftBan.cmd"
nftBanState = "action_nftBan.py.json"
def _clean():
if os.path.exists(nftBanCmd):
os.remove(nftBanCmd)
if os.path.exists(nftBanState):
os.remove(nftBanState)
def whenBanIPv4ThenAddToIPv4Set():
_clean()
2018-03-12 18:18:28 +01:00
Action({"IP": "ip", "nftSetIPv4": "ip I4 ban", "nftSetIPv6": "ip6 I6 ban"}).act({"ip": "10.0.0.1"})
2017-12-15 19:36:50 +01:00
assert os.path.exists(nftBanCmd)
assert os.path.exists(nftBanState)
nbLines = 0
with open(nftBanCmd, "rt") as c:
for line in c:
2018-03-12 18:18:28 +01:00
assert line == "add element ip I4 ban {10.0.0.1}\n", line
2017-12-15 19:36:50 +01:00
nbLines += 1
assert nbLines == 1, nbLines
nbBans = 0
with open(nftBanState) as s:
for ban in json.load(s):
2018-03-12 18:18:28 +01:00
assert ban["IP"] == "10.0.0.1" and ban["nftSet"] == "ip I4 ban", str(ban)
2017-12-15 19:36:50 +01:00
nbBans += 1
assert nbBans == 1, nbBans
_clean()
def whenBanIPv6ThenAddToIPv6Set():
_clean()
2018-03-12 18:18:28 +01:00
Action({"IP": "ip", "nftSetIPv4": "ip I4 ban", "nftSetIPv6": "ip6 I6 ban"}).act({"ip": "::1"})
2017-12-15 19:36:50 +01:00
assert os.path.exists(nftBanCmd)
assert os.path.exists(nftBanState)
nbLines = 0
with open(nftBanCmd, "rt") as c:
for line in c:
2018-03-12 18:18:28 +01:00
assert line == "add element ip6 I6 ban {::1}\n", line
2017-12-15 19:36:50 +01:00
nbLines += 1
assert nbLines == 1, nbLines
nbBans = 0
with open(nftBanState) as s:
for ban in json.load(s):
2018-03-12 18:18:28 +01:00
assert ban["IP"] == "::1" and ban["nftSet"] == "ip6 I6 ban", str(ban)
2017-12-15 19:36:50 +01:00
nbBans += 1
assert nbBans == 1, nbBans
_clean()
def whenBanTwoIPThenTwoLinesInState():
_clean()
2018-03-12 18:18:28 +01:00
action = Action({"IP": "ip", "nftSetIPv4": "ip I4 ban", "nftSetIPv6": "ip6 I6 ban"})
2017-12-15 19:36:50 +01:00
action.act({"ip": "10.0.0.1"})
action.act({"ip": "::1"})
action.act({"ip": "10.0.0.1"})
assert os.path.exists(nftBanState)
nbBans = 0
with open(nftBanState) as s:
for ban in json.load(s):
if ban["IP"] == "10.0.0.1":
2018-03-12 18:18:28 +01:00
assert ban["nftSet"] == "ip I4 ban", str(ban)
2017-12-15 19:36:50 +01:00
elif ban["IP"] == "::1":
2018-03-12 18:18:28 +01:00
assert ban["nftSet"] == "ip6 I6 ban", str(ban)
2017-12-15 19:36:50 +01:00
else:
assert false, str(ban)
nbBans += 1
assert nbBans == 2, nbBans
_clean()
def whenBanAnewThenNoDuplicate():
_clean()
2018-03-12 18:18:28 +01:00
action = Action({"IP": "ip", "nftSetIPv4": "ip I4 ban", "nftSetIPv6": "ip6 I6 ban"})
2017-12-15 19:36:50 +01:00
action.act({"ip": "10.0.0.1"})
action.act({"ip": "10.0.0.1"})
assert os.path.exists(nftBanCmd)
assert os.path.exists(nftBanState)
lineCount = 0
with open(nftBanCmd, "rt") as c:
for line in c:
lineCount += 1
if lineCount == 1:
2018-03-12 18:18:28 +01:00
assert line == "add element ip I4 ban {10.0.0.1}\n", line
2017-12-15 19:36:50 +01:00
elif lineCount == 2:
2018-03-12 18:18:28 +01:00
assert line == "delete element ip I4 ban {10.0.0.1}\n", line
2017-12-15 19:36:50 +01:00
elif lineCount == 3:
2018-03-12 18:18:28 +01:00
assert line == "add element ip I4 ban {10.0.0.1}\n", line
2017-12-15 19:36:50 +01:00
assert lineCount == 3, lineCount
nbBans = 0
with open(nftBanState) as s:
for ban in json.load(s):
if ban["IP"] == "10.0.0.1":
2018-03-12 18:18:28 +01:00
assert ban["nftSet"] == "ip I4 ban", str(ban)
2017-12-15 19:36:50 +01:00
nbBans += 1
assert nbBans == 1, nbBans
_clean()
def whenFinishedBanThenAsIfNotThere():
_clean()
2018-03-12 18:18:28 +01:00
action = Action({"IP": "ip", "nftSetIPv4": "ip I4 ban", "nftSetIPv6": "ip6 I6 ban", "banSeconds": 1})
2017-12-15 19:36:50 +01:00
action.act({"ip": "10.0.0.1"})
time.sleep(1)
action.act({"ip": "10.0.0.1"})
assert os.path.exists(nftBanCmd)
lineCount = 0
with open(nftBanCmd, "rt") as c:
for line in c:
lineCount += 1
if lineCount == 1:
2018-03-12 18:18:28 +01:00
assert line == "add element ip I4 ban {10.0.0.1 timeout 1s}\n", line
2017-12-15 19:36:50 +01:00
elif lineCount == 2:
2018-03-12 18:18:28 +01:00
assert line == "add element ip I4 ban {10.0.0.1 timeout 1s}\n", line
2017-12-15 19:36:50 +01:00
assert lineCount == 2, lineCount
_clean()
def whenUnfinishedBanThenTimeoutReset():
_clean()
2018-03-12 18:18:28 +01:00
action = Action({"IP": "ip", "nftSetIPv4": "ip I4 ban", "nftSetIPv6": "ip6 I6 ban", "banSeconds": 2})
2017-12-15 19:36:50 +01:00
action.act({"ip": "10.0.0.1"})
time.sleep(1)
action.act({"ip": "10.0.0.1"})
assert os.path.exists(nftBanCmd)
lineCount = 0
with open(nftBanCmd, "rt") as c:
for line in c:
lineCount += 1
if lineCount == 1:
2018-03-12 18:18:28 +01:00
assert line == "add element ip I4 ban {10.0.0.1 timeout 2s}\n", line
2017-12-15 19:36:50 +01:00
elif lineCount == 2:
2018-03-12 18:18:28 +01:00
assert line == "delete element ip I4 ban {10.0.0.1}\n", line
2017-12-15 19:36:50 +01:00
elif lineCount == 3:
2018-03-12 18:18:28 +01:00
assert line == "add element ip I4 ban {10.0.0.1 timeout 2s}\n", line
2017-12-15 19:36:50 +01:00
assert lineCount == 3, lineCount
_clean()
2018-01-31 08:28:05 +01:00
def unitTests():
whenBanIPv4ThenAddToIPv4Set()
whenBanIPv6ThenAddToIPv6Set()
whenBanTwoIPThenTwoLinesInState()
whenBanAnewThenNoDuplicate()
whenFinishedBanThenAsIfNotThere()
whenUnfinishedBanThenTimeoutReset()