forked from ComplianceAsCode/content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bash_replace_or_append.bats.jinja
127 lines (88 loc) · 3.71 KB
/
test_bash_replace_or_append.bats.jinja
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
#!/bin/bash
function call_bash_replace_or_append {
{{{ bash_replace_or_append("$1", "$2", "$3") | indent(4) }}}
}
function call_bash_replace_or_append_w_format {
{{{ bash_replace_or_append("$1", "$2", "$3", "$4") | indent(4) }}}
}
@test "bash_replace_or_append - Basic value remediation" {
tmp_file="$(mktemp)"
printf "%s\n" "kernel.randomize_va_space = 5" > "$tmp_file"
expected_output="kernel.randomize_va_space = 2\n"
call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - No remediation happened" {
tmp_file="$(mktemp)"
printf "%s\n" "kernel.randomize_va_space = 2" > "$tmp_file"
expected_output="kernel.randomize_va_space = 2\n"
call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - Append key to empty file" {
tmp_file="$(mktemp)"
printf "" > "$tmp_file"
expected_output="kernel.randomize_va_space = 2\n"
call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - Append key to non-empty file" {
tmp_file="$(mktemp)"
printf "%s\n" "kernel.randomize_va_fake = 5" > "$tmp_file"
expected_output="kernel.randomize_va_fake = 5\nkernel.randomize_va_space = 2\n"
call_bash_replace_or_append "$tmp_file" '^kernel.randomize_va_space' '2'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - Remediation with format" {
tmp_file="$(mktemp)"
printf "%s\n" "kernel.randomize_va_space=5" > "$tmp_file"
expected_output="kernel.randomize_va_space=2\n"
call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - No remediation with format" {
tmp_file="$(mktemp)"
printf "%s\n" "kernel.randomize_va_space=2" > "$tmp_file"
expected_output="kernel.randomize_va_space=2\n"
call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - Append key with format to empty file" {
tmp_file="$(mktemp)"
printf "" > "$tmp_file"
expected_output="kernel.randomize_va_space=2\n"
call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - Append key with format to non-empty file" {
tmp_file="$(mktemp)"
printf "%s\n" "kernel.randomize_va_fake=5" > "$tmp_file"
expected_output="kernel.randomize_va_fake=5\nkernel.randomize_va_space=2\n"
call_bash_replace_or_append_w_format "$tmp_file" '^kernel.randomize_va_space' '2' '%s=%s'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}
@test "bash_replace_or_append - Remediate value containing forward slash" {
tmp_file="$(mktemp)"
printf "%s\n" "kernel.core_pattern=|/bin/true" > "$tmp_file"
expected_output="kernel.core_pattern=|/bin/false\n"
call_bash_replace_or_append_w_format "$tmp_file" '^kernel.core_pattern' '|/bin/false' '%s=%s'
run diff "$tmp_file" <(printf "$expected_output")
[ "$status" -eq 0 ]
rm "$tmp_file"
}