2022-01-30 23:05:28 +00:00
|
|
|
def test_key_val_replacement_works(host):
|
2022-09-19 12:44:10 +00:00
|
|
|
"""Confirms addOrEditKeyValPair either adds or replaces a key value pair in a given file"""
|
|
|
|
host.run(
|
|
|
|
"""
|
2022-01-30 23:05:28 +00:00
|
|
|
source /opt/pihole/utils.sh
|
2022-03-16 20:46:15 +00:00
|
|
|
addOrEditKeyValPair "./testoutput" "KEY_ONE" "value1"
|
|
|
|
addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2"
|
|
|
|
addOrEditKeyValPair "./testoutput" "KEY_ONE" "value3"
|
|
|
|
addOrEditKeyValPair "./testoutput" "KEY_FOUR" "value4"
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-01-30 23:05:28 +00:00
|
|
|
cat ./testoutput
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\n"
|
2022-03-16 17:42:01 +00:00
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
2022-03-16 20:46:15 +00:00
|
|
|
|
2022-04-19 17:35:00 +00:00
|
|
|
def test_key_addition_works(host):
|
2022-09-19 12:44:10 +00:00
|
|
|
"""Confirms addKey adds a key (no value) to a file without duplicating it"""
|
|
|
|
host.run(
|
|
|
|
"""
|
2022-04-19 17:35:00 +00:00
|
|
|
source /opt/pihole/utils.sh
|
|
|
|
addKey "./testoutput" "KEY_ONE"
|
|
|
|
addKey "./testoutput" "KEY_ONE"
|
|
|
|
addKey "./testoutput" "KEY_TWO"
|
|
|
|
addKey "./testoutput" "KEY_TWO"
|
|
|
|
addKey "./testoutput" "KEY_THREE"
|
|
|
|
addKey "./testoutput" "KEY_THREE"
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-04-19 17:35:00 +00:00
|
|
|
cat ./testoutput
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "KEY_ONE\nKEY_TWO\nKEY_THREE\n"
|
2022-04-19 17:35:00 +00:00
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
|
|
|
|
2023-03-17 02:36:22 +00:00
|
|
|
def test_key_addition_substr(host):
|
|
|
|
"""Confirms addKey adds substring keys (no value) to a file"""
|
|
|
|
host.run(
|
|
|
|
"""
|
|
|
|
source /opt/pihole/utils.sh
|
|
|
|
addKey "./testoutput" "KEY_ONE"
|
|
|
|
addKey "./testoutput" "KEY_O"
|
|
|
|
addKey "./testoutput" "KEY_TWO"
|
|
|
|
addKey "./testoutput" "Y_TWO"
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
output = host.run(
|
|
|
|
"""
|
|
|
|
cat ./testoutput
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "KEY_ONE\nKEY_O\nKEY_TWO\nY_TWO\n"
|
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
|
|
|
|
2022-04-19 17:35:00 +00:00
|
|
|
def test_key_removal_works(host):
|
2022-09-19 12:44:10 +00:00
|
|
|
"""Confirms removeKey removes a key or key/value pair"""
|
|
|
|
host.run(
|
|
|
|
"""
|
2022-03-16 17:42:01 +00:00
|
|
|
source /opt/pihole/utils.sh
|
2022-03-16 20:46:15 +00:00
|
|
|
addOrEditKeyValPair "./testoutput" "KEY_ONE" "value1"
|
|
|
|
addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2"
|
|
|
|
addOrEditKeyValPair "./testoutput" "KEY_THREE" "value3"
|
2022-04-19 17:35:00 +00:00
|
|
|
addKey "./testoutput" "KEY_FOUR"
|
2022-04-04 21:02:26 +00:00
|
|
|
removeKey "./testoutput" "KEY_TWO"
|
2022-04-19 17:35:00 +00:00
|
|
|
removeKey "./testoutput" "KEY_FOUR"
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-03-16 17:42:01 +00:00
|
|
|
cat ./testoutput
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "KEY_ONE=value1\nKEY_THREE=value3\n"
|
2022-01-30 23:05:28 +00:00
|
|
|
assert expected_stdout == output.stdout
|
2022-02-20 21:24:17 +00:00
|
|
|
|
2022-07-26 17:33:38 +00:00
|
|
|
|
2023-02-16 14:21:18 +00:00
|
|
|
def test_get_value_works(host):
|
|
|
|
"""Confirms getVal returns the correct value for a given key"""
|
|
|
|
output = host.run(
|
|
|
|
"""
|
|
|
|
source /opt/pihole/utils.sh
|
|
|
|
echo "Somekey=xxx" >> /tmp/testfile
|
|
|
|
echo "#Testkey=1234" >> /tmp/testfile
|
|
|
|
echo "Testkey=5678" >> /tmp/testfile
|
|
|
|
echo "Testkey=abcd" >> /tmp/testfile
|
|
|
|
getVal "/tmp/testfile" "Testkey"
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "5678"
|
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
|
|
|
|
2022-09-21 07:24:44 +00:00
|
|
|
def test_getFTLAPIPort_default(host):
|
|
|
|
"""Confirms getFTLAPIPort returns the default API port"""
|
2022-09-19 12:44:10 +00:00
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-07-26 14:57:06 +00:00
|
|
|
source /opt/pihole/utils.sh
|
2022-09-21 07:24:44 +00:00
|
|
|
getFTLAPIPort
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
2022-09-21 07:24:44 +00:00
|
|
|
expected_stdout = "4711\n"
|
2022-07-26 14:57:06 +00:00
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
2022-02-20 21:24:17 +00:00
|
|
|
|
2022-09-21 07:24:44 +00:00
|
|
|
def test_getFTLAPIPort_custom(host):
|
|
|
|
"""Confirms getFTLAPIPort returns a custom API port"""
|
|
|
|
host.run(
|
|
|
|
"""
|
|
|
|
echo "FTLPORT=1234" > /etc/pihole/pihole-FTL.conf
|
|
|
|
"""
|
|
|
|
)
|
2022-09-19 12:44:10 +00:00
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-02-20 21:24:17 +00:00
|
|
|
source /opt/pihole/utils.sh
|
2022-09-21 07:24:44 +00:00
|
|
|
getFTLAPIPort
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
2022-09-21 07:24:44 +00:00
|
|
|
expected_stdout = "1234\n"
|
2022-02-20 21:24:17 +00:00
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
|
|
|
|
2022-09-21 07:24:44 +00:00
|
|
|
def test_getFTLAPIPort_malicious(host):
|
2022-09-26 21:40:09 +00:00
|
|
|
"""Confirms getFTLAPIPort returns 4711 if the setting in pihole-FTL.conf contains non-digits"""
|
2022-09-19 12:44:10 +00:00
|
|
|
host.run(
|
|
|
|
"""
|
2022-09-21 07:24:44 +00:00
|
|
|
echo "FTLPORT=*$ssdfsd" > /etc/pihole/pihole-FTL.conf
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-02-20 21:24:17 +00:00
|
|
|
source /opt/pihole/utils.sh
|
2022-09-21 07:24:44 +00:00
|
|
|
getFTLAPIPort
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
2022-09-26 21:40:09 +00:00
|
|
|
expected_stdout = "4711\n"
|
2022-02-20 21:24:17 +00:00
|
|
|
assert expected_stdout == output.stdout
|
2022-07-26 14:57:06 +00:00
|
|
|
|
2022-07-26 17:33:38 +00:00
|
|
|
|
2022-07-26 14:57:06 +00:00
|
|
|
def test_getFTLPIDFile_default(host):
|
2022-09-19 12:44:10 +00:00
|
|
|
"""Confirms getFTLPIDFile returns the default PID file path"""
|
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-07-26 14:57:06 +00:00
|
|
|
source /opt/pihole/utils.sh
|
|
|
|
getFTLPIDFile
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "/run/pihole-FTL.pid\n"
|
2022-07-26 14:57:06 +00:00
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
2022-07-26 17:33:38 +00:00
|
|
|
|
2022-07-26 15:34:42 +00:00
|
|
|
def test_getFTLPID_default(host):
|
2022-09-19 12:44:10 +00:00
|
|
|
"""Confirms getFTLPID returns the default value if FTL is not running"""
|
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-07-26 15:34:42 +00:00
|
|
|
source /opt/pihole/utils.sh
|
|
|
|
getFTLPID
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "-1\n"
|
2022-07-26 15:34:42 +00:00
|
|
|
assert expected_stdout == output.stdout
|
|
|
|
|
2022-07-26 17:33:38 +00:00
|
|
|
|
2022-07-26 15:34:42 +00:00
|
|
|
def test_getFTLPIDFile_and_getFTLPID_custom(host):
|
2022-09-19 12:44:10 +00:00
|
|
|
"""Confirms getFTLPIDFile returns a custom PID file path"""
|
|
|
|
host.run(
|
|
|
|
"""
|
2022-07-26 17:33:38 +00:00
|
|
|
tmpfile=$(mktemp)
|
|
|
|
echo "PIDFILE=${tmpfile}" > /etc/pihole/pihole-FTL.conf
|
|
|
|
echo "1234" > ${tmpfile}
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
output = host.run(
|
|
|
|
"""
|
2022-07-26 14:57:06 +00:00
|
|
|
source /opt/pihole/utils.sh
|
2022-07-26 15:34:42 +00:00
|
|
|
FTL_PID_FILE=$(getFTLPIDFile)
|
|
|
|
getFTLPID "${FTL_PID_FILE}"
|
2022-09-19 12:44:10 +00:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
expected_stdout = "1234\n"
|
2022-07-26 14:57:06 +00:00
|
|
|
assert expected_stdout == output.stdout
|