[13/21] initscript fkt: ignore blank lines in readhash

Message ID 20240520090611.10406-14-jonatan.schlag@ipfire.org
State New
Headers
Series [01/21] test: Add bash lib for colors |

Commit Message

Jonatan Schlag May 20, 2024, 9:06 a.m. UTC
  Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 src/initscripts/system/functions                            | 6 ++++++
 tests/src/initscripts/system/functions/data/1_output_stderr | 0
 tests/src/initscripts/system/functions/data/1_output_stdout | 0
 tests/src/initscripts/system/functions/test.sh              | 3 +++
 4 files changed, 9 insertions(+)
 create mode 100644 tests/src/initscripts/system/functions/data/1_output_stderr
 create mode 100644 tests/src/initscripts/system/functions/data/1_output_stdout
  

Comments

Michael Tremer May 31, 2024, 9:55 a.m. UTC | #1
> On 20 May 2024, at 10:06, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote:
> 
> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> ---
> src/initscripts/system/functions                            | 6 ++++++
> tests/src/initscripts/system/functions/data/1_output_stderr | 0
> tests/src/initscripts/system/functions/data/1_output_stdout | 0
> tests/src/initscripts/system/functions/test.sh              | 3 +++
> 4 files changed, 9 insertions(+)
> create mode 100644 tests/src/initscripts/system/functions/data/1_output_stderr
> create mode 100644 tests/src/initscripts/system/functions/data/1_output_stdout
> 
> diff --git a/src/initscripts/system/functions b/src/initscripts/system/functions
> index 44ce999d3..3f01be9e0 100644
> --- a/src/initscripts/system/functions
> +++ b/src/initscripts/system/functions
> @@ -900,6 +900,12 @@ readhash() {
> 
> local line
> while read -r line; do
> +
> + # Skip Blank Lines
> + if [[ ${line} =~ ^[[:space:]]*$ ]]; then
> + continue
> + fi
> +
> local key="${line%=*}"
> local val="${line#*=}"

I don’t think this is quite sufficient.

You might have other lines that are invalid. For example “=VALUE”. I think we should check that key isn’t empty, and we should limit what characters can be used in keys and values as it used to be before in the script.

https://git.ipfire.org/?p=ipfire-2.x.git;a=blob;f=src/scripts/readhash;h=bffc93fbd00c31a76e9c96fc17dabf0a87c3c9ef;hb=HEAD

> 
> diff --git a/tests/src/initscripts/system/functions/data/1_output_stderr b/tests/src/initscripts/system/functions/data/1_output_stderr
> new file mode 100644
> index 000000000..e69de29bb
> diff --git a/tests/src/initscripts/system/functions/data/1_output_stdout b/tests/src/initscripts/system/functions/data/1_output_stdout
> new file mode 100644
> index 000000000..e69de29bb
> diff --git a/tests/src/initscripts/system/functions/test.sh b/tests/src/initscripts/system/functions/test.sh
> index ec502e199..8d644b8cd 100755
> --- a/tests/src/initscripts/system/functions/test.sh
> +++ b/tests/src/initscripts/system/functions/test.sh
> @@ -14,3 +14,6 @@ readhash "CONFIG" "${SCRIPT_PATH}/data/1"
> # test if we read the correct data
> test_that_key_in_arry_has_value "CONFIG" "RED_DHCP_HOSTNAME" "ipfire"
> test_that_key_in_arry_has_value "CONFIG" "BLUE_MACADDR" "bc:30:7d:58:6b:e3"
> +
> +test_that_output_is "${SCRIPT_PATH}/data/1_output_stdout" "1" readhash "CONFIG" "${SCRIPT_PATH}/data/1"
> +test_that_output_is "${SCRIPT_PATH}/data/1_output_stderr" "2" readhash "CONFIG" "${SCRIPT_PATH}/data/1"
> -- 
> 2.39.2
>
  
Jonatan Schlag June 2, 2024, 6:07 p.m. UTC | #2
Hi,


Am Freitag, dem 31.05.2024 um 10:55 +0100 schrieb Michael Tremer:
> 
> 
> > On 20 May 2024, at 10:06, Jonatan Schlag
> > <jonatan.schlag@ipfire.org> wrote:
> > 
> > Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> > ---
> > src/initscripts/system/functions                            | 6
> > ++++++
> > tests/src/initscripts/system/functions/data/1_output_stderr | 0
> > tests/src/initscripts/system/functions/data/1_output_stdout | 0
> > tests/src/initscripts/system/functions/test.sh              | 3 +++
> > 4 files changed, 9 insertions(+)
> > create mode 100644
> > tests/src/initscripts/system/functions/data/1_output_stderr
> > create mode 100644
> > tests/src/initscripts/system/functions/data/1_output_stdout
> > 
> > diff --git a/src/initscripts/system/functions
> > b/src/initscripts/system/functions
> > index 44ce999d3..3f01be9e0 100644
> > --- a/src/initscripts/system/functions
> > +++ b/src/initscripts/system/functions
> > @@ -900,6 +900,12 @@ readhash() {
> > 
> > local line
> > while read -r line; do
> > +
> > + # Skip Blank Lines
> > + if [[ ${line} =~ ^[[:space:]]*$ ]]; then
> > + continue
> > + fi
> > +
> > local key="${line%=*}"
> > local val="${line#*=}"
> 
> I don’t think this is quite sufficient.
> 
> You might have other lines that are invalid. For example “=VALUE”. I
> think we should check that key isn’t empty, and we should limit what
> characters can be used in keys and values as it used to be before in
> the script.
> 
> https://git.ipfire.org/?p=ipfire-2.x.git;a=blob;f=src/scripts/readhash;h=bffc93fbd00c31a76e9c96fc17dabf0a87c3c9ef;hb=HEAD
> 
Hi,

I will merge all commits for checks in the next series. There are more
checks to come. In a later commit, I check for correct values and keys.

Jonatan
> > 
> > diff --git
> > a/tests/src/initscripts/system/functions/data/1_output_stderr
> > b/tests/src/initscripts/system/functions/data/1_output_stderr
> > new file mode 100644
> > index 000000000..e69de29bb
> > diff --git
> > a/tests/src/initscripts/system/functions/data/1_output_stdout
> > b/tests/src/initscripts/system/functions/data/1_output_stdout
> > new file mode 100644
> > index 000000000..e69de29bb
> > diff --git a/tests/src/initscripts/system/functions/test.sh
> > b/tests/src/initscripts/system/functions/test.sh
> > index ec502e199..8d644b8cd 100755
> > --- a/tests/src/initscripts/system/functions/test.sh
> > +++ b/tests/src/initscripts/system/functions/test.sh
> > @@ -14,3 +14,6 @@ readhash "CONFIG" "${SCRIPT_PATH}/data/1"
> > # test if we read the correct data
> > test_that_key_in_arry_has_value "CONFIG" "RED_DHCP_HOSTNAME"
> > "ipfire"
> > test_that_key_in_arry_has_value "CONFIG" "BLUE_MACADDR"
> > "bc:30:7d:58:6b:e3"
> > +
> > +test_that_output_is "${SCRIPT_PATH}/data/1_output_stdout" "1"
> > readhash "CONFIG" "${SCRIPT_PATH}/data/1"
> > +test_that_output_is "${SCRIPT_PATH}/data/1_output_stderr" "2"
> > readhash "CONFIG" "${SCRIPT_PATH}/data/1"
> > -- 
> > 2.39.2
> > 
>
  
Michael Tremer June 3, 2024, 9:21 a.m. UTC | #3
Your tests I didn’t understand at all. You need to structure those in a more simple way.

> On 2 Jun 2024, at 19:07, Jonatan Schlag <jonatan.schlag@ipfire.org> wrote:
> 
> Hi,
> 
> 
> Am Freitag, dem 31.05.2024 um 10:55 +0100 schrieb Michael Tremer:
>> 
>> 
>>> On 20 May 2024, at 10:06, Jonatan Schlag
>>> <jonatan.schlag@ipfire.org> wrote:
>>> 
>>> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
>>> ---
>>> src/initscripts/system/functions                            | 6
>>> ++++++
>>> tests/src/initscripts/system/functions/data/1_output_stderr | 0
>>> tests/src/initscripts/system/functions/data/1_output_stdout | 0
>>> tests/src/initscripts/system/functions/test.sh              | 3 +++
>>> 4 files changed, 9 insertions(+)
>>> create mode 100644
>>> tests/src/initscripts/system/functions/data/1_output_stderr
>>> create mode 100644
>>> tests/src/initscripts/system/functions/data/1_output_stdout
>>> 
>>> diff --git a/src/initscripts/system/functions
>>> b/src/initscripts/system/functions
>>> index 44ce999d3..3f01be9e0 100644
>>> --- a/src/initscripts/system/functions
>>> +++ b/src/initscripts/system/functions
>>> @@ -900,6 +900,12 @@ readhash() {
>>> 
>>> local line
>>> while read -r line; do
>>> +
>>> + # Skip Blank Lines
>>> + if [[ ${line} =~ ^[[:space:]]*$ ]]; then
>>> + continue
>>> + fi
>>> +
>>> local key="${line%=*}"
>>> local val="${line#*=}"
>> 
>> I don’t think this is quite sufficient.
>> 
>> You might have other lines that are invalid. For example “=VALUE”. I
>> think we should check that key isn’t empty, and we should limit what
>> characters can be used in keys and values as it used to be before in
>> the script.
>> 
>> https://git.ipfire.org/?p=ipfire-2.x.git;a=blob;f=src/scripts/readhash;h=bffc93fbd00c31a76e9c96fc17dabf0a87c3c9ef;hb=HEAD
>> 
> Hi,
> 
> I will merge all commits for checks in the next series. There are more
> checks to come. In a later commit, I check for correct values and keys.
> 
> Jonatan
>>> 
>>> diff --git
>>> a/tests/src/initscripts/system/functions/data/1_output_stderr
>>> b/tests/src/initscripts/system/functions/data/1_output_stderr
>>> new file mode 100644
>>> index 000000000..e69de29bb
>>> diff --git
>>> a/tests/src/initscripts/system/functions/data/1_output_stdout
>>> b/tests/src/initscripts/system/functions/data/1_output_stdout
>>> new file mode 100644
>>> index 000000000..e69de29bb
>>> diff --git a/tests/src/initscripts/system/functions/test.sh
>>> b/tests/src/initscripts/system/functions/test.sh
>>> index ec502e199..8d644b8cd 100755
>>> --- a/tests/src/initscripts/system/functions/test.sh
>>> +++ b/tests/src/initscripts/system/functions/test.sh
>>> @@ -14,3 +14,6 @@ readhash "CONFIG" "${SCRIPT_PATH}/data/1"
>>> # test if we read the correct data
>>> test_that_key_in_arry_has_value "CONFIG" "RED_DHCP_HOSTNAME"
>>> "ipfire"
>>> test_that_key_in_arry_has_value "CONFIG" "BLUE_MACADDR"
>>> "bc:30:7d:58:6b:e3"
>>> +
>>> +test_that_output_is "${SCRIPT_PATH}/data/1_output_stdout" "1"
>>> readhash "CONFIG" "${SCRIPT_PATH}/data/1"
>>> +test_that_output_is "${SCRIPT_PATH}/data/1_output_stderr" "2"
>>> readhash "CONFIG" "${SCRIPT_PATH}/data/1"
>>> -- 
>>> 2.39.2
  

Patch

diff --git a/src/initscripts/system/functions b/src/initscripts/system/functions
index 44ce999d3..3f01be9e0 100644
--- a/src/initscripts/system/functions
+++ b/src/initscripts/system/functions
@@ -900,6 +900,12 @@  readhash() {
 
 	local line
 	while read -r line; do
+
+		# Skip Blank Lines
+		if [[ ${line} =~ ^[[:space:]]*$ ]]; then
+			continue
+		fi
+
 		local key="${line%=*}"
 		local val="${line#*=}"
 
diff --git a/tests/src/initscripts/system/functions/data/1_output_stderr b/tests/src/initscripts/system/functions/data/1_output_stderr
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/src/initscripts/system/functions/data/1_output_stdout b/tests/src/initscripts/system/functions/data/1_output_stdout
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/src/initscripts/system/functions/test.sh b/tests/src/initscripts/system/functions/test.sh
index ec502e199..8d644b8cd 100755
--- a/tests/src/initscripts/system/functions/test.sh
+++ b/tests/src/initscripts/system/functions/test.sh
@@ -14,3 +14,6 @@  readhash "CONFIG" "${SCRIPT_PATH}/data/1"
 # test if we read the correct data
 test_that_key_in_arry_has_value "CONFIG" "RED_DHCP_HOSTNAME" "ipfire"
 test_that_key_in_arry_has_value "CONFIG" "BLUE_MACADDR" "bc:30:7d:58:6b:e3"
+
+test_that_output_is "${SCRIPT_PATH}/data/1_output_stdout" "1" readhash "CONFIG" "${SCRIPT_PATH}/data/1"
+test_that_output_is "${SCRIPT_PATH}/data/1_output_stderr" "2" readhash "CONFIG" "${SCRIPT_PATH}/data/1"