File: test_bash_ensure_ini_config.bats.jinja

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (143 lines) | stat: -rw-r--r-- 4,713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash

set -pu

function call_bash_ensure_ini_config {
    {{{ bash_ensure_ini_config("$1", "$2", "$3", "$4") | indent(4) }}}
}

is_old_bats=0

setup() {
    if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
        BATS_TEST_TMPDIR="$(mktemp -d)"  # 1.4.0
        # shellcheck disable=SC2034
        BATS_TEARDOWN_STARTED=  # 1.3.0
        is_old_bats=1
    else
        is_old_bats=0
    fi
    pushd "${BATS_TEST_TMPDIR}" || exit 1
    mkdir -p sssd_test
}

teardown() {
    if (( is_old_bats )); then
        if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        local tmppath xpwd
        tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")"
        if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        xpwd="$(readlink -f -- .)"
        if [[ "${tmppath}" != "${xpwd}" ]]; then
            >&2 echo "INTERNAL ERROR"
            exit 3
        fi
        popd || exit 1
        rm -rf -- "${tmppath}"
        BATS_TEST_TMPDIR=""
    fi
}

@test "bash_ensure_ini_config - Basic value remediation" {
    printf "[pam]\npam_cert_auth = false\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Value remediation in multiple files" {
    printf "[pam]\npam_cert_auth = false\n" > sssd_test/sssd.conf
    printf "[pam]\npam_cert_auth = false\n" > pam_cert_auth.conf
    expected_output="[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf pam_cert_auth.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]

    run diff "pam_cert_auth.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - No remediation happened" {
    printf "[pam]\npam_cert_auth = true\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append section with option to empty file" {
    printf "" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Create file with section and option" {
    expected_output="[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append option to section" {
    printf "[pam]\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append option to section when section is substring of option" {
    printf "[pam]\n" > sssd_test/sssd.conf
    expected_output="[pam]\npam_verbosity=1\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"
    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_verbosity" "1"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append option to section in multiple files" {
    printf "[pam]\n" > sssd_test/sssd.conf
    printf "[pam]\n" > pam_cert_auth.conf
    expected_output="[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "pam_cert_auth.conf sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]

     run diff "pam_cert_auth.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}

@test "bash_ensure_ini_config - Append section with option to non-empty file" {
    printf "[section]\nkey = value\n" > sssd_test/sssd.conf
    expected_output="[section]\nkey = value\n[pam]\npam_cert_auth=true\n"

    call_bash_ensure_ini_config "sssd_test/sssd.conf" "pam" "pam_cert_auth" "true"

    run diff "sssd_test/sssd.conf" <(printf "$expected_output")
    [ "$status" -eq 0 ]
}