Skip to content

Commit a882427

Browse files
committed
assert->assert_equal + rules file checks in separate func + tests
1 parent 83bcd03 commit a882427

File tree

4 files changed

+101
-16
lines changed

4 files changed

+101
-16
lines changed

common_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ print_success() {
2525
}
2626

2727
# Assert function to check if actual and expected values are the same
28-
assert() {
28+
assert_equals() {
2929
local actual="$1"
3030
local expected="$2"
3131
local message="$3"

rules.sh

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ source validators.sh
1010

1111
declare -A RULES
1212

13+
# This function checks if the rules file exists, is readable, and has non-zero size
14+
# It logs ERROR errors for any failed checks and returns a non-zero status
15+
# Return values:
16+
# 0 - on success
17+
# 1 - if any of the checks fail (file not found, no read permissions, or empty file).
18+
check_rules_file() {
19+
logger "DEBUG" "Starting check_rules_file function"
20+
21+
if [ ! -f "$RULES_FILE" ]; then
22+
logger "ERROR" "Rules file \"$RULES_FILE\" not found"
23+
return 1
24+
fi
25+
26+
if [ ! -r "$RULES_FILE" ]; then
27+
logger "ERROR" "Current user does not have read permissions on rules file \"$RULES_FILE\""
28+
return 1
29+
fi
30+
31+
if [ ! -s "$RULES_FILE" ]; then
32+
logger "ERROR" "Rules file \"$RULES_FILE\" is empty"
33+
return 1
34+
fi
35+
36+
logger "DEBUG" "check_rules_file function finished"
37+
}
38+
1339
# This function loads the rules from the rules file into an associative array called RULES.
1440
# The format of the rules file is:
1541
# <username>@<hostname/IP address>:<port>
@@ -21,13 +47,8 @@ declare -A RULES
2147
load_rules() {
2248
logger "DEBUG" "Starting load_rules function"
2349

24-
# Check if the rules file exists and has content
25-
if [ ! -f "$RULES_FILE" ]; then
26-
logger "FATAL" "Rules file \"$RULES_FILE\" not found"
27-
fi
28-
29-
if [ ! -s "$RULES_FILE" ]; then
30-
logger "FATAL" "Rules file \"$RULES_FILE\" is empty"
50+
if ! check_rules_file; then
51+
logger "FATAL" "Rules file checks failed"
3152
fi
3253

3354
# Variable to keep track of the current host being processed

rules_test.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC1091
3+
# Source the common test functions
4+
source common_test.sh
5+
# Source the bash file containing the functions to be tested
6+
source rules.sh
7+
8+
# Test function for check_rules_file
9+
test_check_rules_file() {
10+
# Test with existing and readable rules file
11+
test_check_rules_file_with_existing_readable_file() {
12+
local temp_file
13+
temp_file=$(mktemp /tmp/rules.XXXXXX)
14+
touch "$temp_file"
15+
chmod u+r "$temp_file"
16+
assert_no_error "$(check_rules_file "$temp_file")" "${FUNCNAME[0]}: test with existing and readable file"
17+
rm "$temp_file"
18+
}
19+
test_check_rules_file_with_existing_readable_file
20+
21+
# Test with non-existent file
22+
test_check_rules_file_with_nonexistent_file() {
23+
local temp_file
24+
temp_file=$(mktemp /tmp/rules.XXXXXX)
25+
rm "$temp_file"
26+
local expected_return_code=1
27+
assert_is_error "$(
28+
check_rules_file "$temp_file"
29+
echo $?
30+
)" "${expected_return_code}" "${FUNCNAME[0]}: test with non-existent file"
31+
}
32+
test_check_rules_file_with_nonexistent_file
33+
34+
# Test with non-readable file
35+
test_check_rules_file_with_nonreadable_file() {
36+
local temp_file
37+
temp_file=$(mktemp /tmp/rules.XXXXXX)
38+
touch "$temp_file"
39+
chmod u-r "$temp_file"
40+
local expected_return_code=1
41+
assert_is_error "$(
42+
check_rules_file "$temp_file"
43+
echo $?
44+
)" "${expected_return_code}" "${FUNCNAME[0]}: test with non-readable file"
45+
rm "$temp_file"
46+
}
47+
test_check_rules_file_with_nonreadable_file
48+
49+
# Test with empty file
50+
test_check_rules_file_with_empty_file() {
51+
local temp_file
52+
temp_file=$(mktemp /tmp/rules.XXXXXX)
53+
local expected_return_code=1
54+
assert_is_error "$(
55+
check_rules_file "$temp_file"
56+
echo $?
57+
)" "${expected_return_code}" "${FUNCNAME[0]}: test with empty file"
58+
rm "$temp_file"
59+
}
60+
test_check_rules_file_with_empty_file
61+
}
62+
63+
# Run all test functions
64+
test_check_rules_file

splitters_test.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test_split_user_host_port() {
1111
test_split_user_host_port_with_hostname() {
1212
local input="john@example.com:22"
1313
local expected_output=("john@example.com" "22")
14-
assert "$(split_user_host_port $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with hostname"
14+
assert_equals "$(split_user_host_port $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with hostname"
1515
assert_no_error $? "${FUNCNAME[0]}"
1616
}
1717
test_split_user_host_port_with_hostname
@@ -20,7 +20,7 @@ test_split_user_host_port() {
2020
test_split_user_host_port_with_ip() {
2121
local input="alice@192.168.1.10:8080"
2222
local expected_output=("alice@192.168.1.10" "8080")
23-
assert "$(split_user_host_port $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with IP address"
23+
assert_equals "$(split_user_host_port $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with IP address"
2424
assert_no_error $? "${FUNCNAME[0]}"
2525
}
2626
test_split_user_host_port_with_ip
@@ -29,7 +29,7 @@ test_split_user_host_port() {
2929
test_split_user_host_port_with_localhost() {
3030
local input="root@localhost:3306"
3131
local expected_output=("root@localhost" "3306")
32-
assert "$(split_user_host_port $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with localhost"
32+
assert_equals "$(split_user_host_port $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with localhost"
3333
assert_no_error $? "${FUNCNAME[0]}"
3434
}
3535
test_split_user_host_port_with_localhost
@@ -65,7 +65,7 @@ test_split_user_host() {
6565
test_split_user_host_with_hostname() {
6666
local input="john@example.com"
6767
local expected_output=("john" "example.com")
68-
assert "$(split_user_host $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with hostname"
68+
assert_equals "$(split_user_host $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with hostname"
6969
assert_no_error $? "${FUNCNAME[0]}"
7070
}
7171
test_split_user_host_with_hostname
@@ -74,7 +74,7 @@ test_split_user_host() {
7474
test_split_user_host_with_ip() {
7575
local input="alice@192.168.1.10"
7676
local expected_output=("alice" "192.168.1.10")
77-
assert "$(split_user_host $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with IP address"
77+
assert_equals "$(split_user_host $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with IP address"
7878
assert_no_error $? "${FUNCNAME[0]}"
7979
}
8080
test_split_user_host_with_ip
@@ -83,7 +83,7 @@ test_split_user_host() {
8383
test_split_user_host_with_localhost() {
8484
local input="root@localhost"
8585
local expected_output=("root" "localhost")
86-
assert "$(split_user_host $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with localhost"
86+
assert_equals "$(split_user_host $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with localhost"
8787
assert_no_error $? "${FUNCNAME[0]}"
8888
}
8989
test_split_user_host_with_localhost
@@ -119,7 +119,7 @@ test_split_interface_ports() {
119119
test_split_interface_ports_ips() {
120120
local input="192.168.1.2:8080:192.168.1.100:80"
121121
local expected_output=("192.168.1.2" "8080" "192.168.1.100" "80")
122-
assert "$(split_interface_ports $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with IP addresses"
122+
assert_equals "$(split_interface_ports $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with IP addresses"
123123
assert_no_error $? "${FUNCNAME[0]}"
124124
}
125125
test_split_interface_ports_ips
@@ -128,7 +128,7 @@ test_split_interface_ports() {
128128
test_split_interface_ports_mixed_hostnames() {
129129
local input="localhost:80:test.com.test:2048"
130130
local expected_output=("localhost" "80" "test.com.test" "2048")
131-
assert "$(split_interface_ports $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with mixed hostnames"
131+
assert_equals "$(split_interface_ports $input)" "${expected_output[*]}" "${FUNCNAME[0]}: test with mixed hostnames"
132132
assert_no_error $? "${FUNCNAME[0]}"
133133
}
134134
test_split_interface_ports_mixed_hostnames

0 commit comments

Comments
 (0)