From b9a6970bfd04b8fbe9f78bc849b5bddc776202eb Mon Sep 17 00:00:00 2001 From: William Blew Date: Thu, 16 Mar 2023 19:36:22 -0700 Subject: [PATCH 1/2] Fix addKey to handle substrings of existing keys Fix addKey to handle the case where a key is being added, and that key is the leading substring of an already existing key within that file. For example: add "server=192.168.1.1", when "server=192.168.1.178" already exists within the /etc/dnsmasq.d/01-pihole.conf file. Check pihole docker with PIHOLE_DNS="192.168.1.178;192.168.1.1". Its /etc/dnsmasq/01-pihole.conf will be missing its second server= entry. Add the test_key_addition_substr, to test addKey when its adding a substring key of an existing key in the file. Signed-off-by: William Blew --- advanced/Scripts/utils.sh | 2 +- test/test_any_utils.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/advanced/Scripts/utils.sh b/advanced/Scripts/utils.sh index 37516472..9e714606 100755 --- a/advanced/Scripts/utils.sh +++ b/advanced/Scripts/utils.sh @@ -57,7 +57,7 @@ addKey(){ # touch file to prevent grep error if file does not exist yet touch "${file}" - if ! grep -q "^${key}" "${file}"; then + if ! grep -q "^${key}$" "${file}"; then # Key does not exist, add it. echo "${key}" >> "${file}" fi diff --git a/test/test_any_utils.py b/test/test_any_utils.py index 5b4075d9..b3fabe6c 100644 --- a/test/test_any_utils.py +++ b/test/test_any_utils.py @@ -40,6 +40,26 @@ def test_key_addition_works(host): assert expected_stdout == output.stdout +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 + + def test_key_removal_works(host): """Confirms removeKey removes a key or key/value pair""" host.run( From 3c91b6558dcc947736e1df1631a9a7dfc7d32f9b Mon Sep 17 00:00:00 2001 From: William Blew Date: Fri, 17 Mar 2023 11:47:26 -0700 Subject: [PATCH 2/2] restore the addKey comment, reworded for anchors Per @dschaper, restore the addKey clarifying comment. It has been reworded to describe the use of anchors where before it referenced using grep's 'match only an entire line' argument. Signed-off-by: William Blew --- advanced/Scripts/utils.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/advanced/Scripts/utils.sh b/advanced/Scripts/utils.sh index 9e714606..f655e56c 100755 --- a/advanced/Scripts/utils.sh +++ b/advanced/Scripts/utils.sh @@ -57,6 +57,10 @@ addKey(){ # touch file to prevent grep error if file does not exist yet touch "${file}" + # Match key against entire line, using both anchors. We assume + # that the file's keys never have bounding whitespace. Anchors + # are necessary to ensure the key is considered absent when it + # is a substring of another key present in the file. if ! grep -q "^${key}$" "${file}"; then # Key does not exist, add it. echo "${key}" >> "${file}"