Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_any_utils.py « test - github.com/pi-hole/pi-hole.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b4075d9b992ef270a09746f3f0cda6a00a36bf6 (plain)
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
144
145
146
147
148
149
150
151
152
def test_key_val_replacement_works(host):
    """Confirms addOrEditKeyValPair either adds or replaces a key value pair in a given file"""
    host.run(
        """
    source /opt/pihole/utils.sh
    addOrEditKeyValPair "./testoutput" "KEY_ONE" "value1"
    addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2"
    addOrEditKeyValPair "./testoutput" "KEY_ONE" "value3"
    addOrEditKeyValPair "./testoutput" "KEY_FOUR" "value4"
    """
    )
    output = host.run(
        """
    cat ./testoutput
    """
    )
    expected_stdout = "KEY_ONE=value3\nKEY_TWO=value2\nKEY_FOUR=value4\n"
    assert expected_stdout == output.stdout


def test_key_addition_works(host):
    """Confirms addKey adds a key (no value) to a file without duplicating it"""
    host.run(
        """
    source /opt/pihole/utils.sh
    addKey "./testoutput" "KEY_ONE"
    addKey "./testoutput" "KEY_ONE"
    addKey "./testoutput" "KEY_TWO"
    addKey "./testoutput" "KEY_TWO"
    addKey "./testoutput" "KEY_THREE"
    addKey "./testoutput" "KEY_THREE"
    """
    )
    output = host.run(
        """
    cat ./testoutput
    """
    )
    expected_stdout = "KEY_ONE\nKEY_TWO\nKEY_THREE\n"
    assert expected_stdout == output.stdout


def test_key_removal_works(host):
    """Confirms removeKey removes a key or key/value pair"""
    host.run(
        """
    source /opt/pihole/utils.sh
    addOrEditKeyValPair "./testoutput" "KEY_ONE" "value1"
    addOrEditKeyValPair "./testoutput" "KEY_TWO" "value2"
    addOrEditKeyValPair "./testoutput" "KEY_THREE" "value3"
    addKey "./testoutput" "KEY_FOUR"
    removeKey "./testoutput" "KEY_TWO"
    removeKey "./testoutput" "KEY_FOUR"
    """
    )
    output = host.run(
        """
    cat ./testoutput
    """
    )
    expected_stdout = "KEY_ONE=value1\nKEY_THREE=value3\n"
    assert expected_stdout == output.stdout


def test_getFTLAPIPort_default(host):
    """Confirms getFTLAPIPort returns the default API port"""
    output = host.run(
        """
    source /opt/pihole/utils.sh
    getFTLAPIPort
    """
    )
    expected_stdout = "4711\n"
    assert expected_stdout == output.stdout


def test_getFTLAPIPort_custom(host):
    """Confirms getFTLAPIPort returns a custom API port"""
    host.run(
        """
    echo "FTLPORT=1234" > /etc/pihole/pihole-FTL.conf
    """
    )
    output = host.run(
        """
    source /opt/pihole/utils.sh
    getFTLAPIPort
    """
    )
    expected_stdout = "1234\n"
    assert expected_stdout == output.stdout


def test_getFTLAPIPort_malicious(host):
    """Confirms getFTLAPIPort returns 4711 if the setting in pihole-FTL.conf contains non-digits"""
    host.run(
        """
    echo "FTLPORT=*$ssdfsd" > /etc/pihole/pihole-FTL.conf
    """
    )
    output = host.run(
        """
    source /opt/pihole/utils.sh
    getFTLAPIPort
    """
    )
    expected_stdout = "4711\n"
    assert expected_stdout == output.stdout


def test_getFTLPIDFile_default(host):
    """Confirms getFTLPIDFile returns the default PID file path"""
    output = host.run(
        """
    source /opt/pihole/utils.sh
    getFTLPIDFile
    """
    )
    expected_stdout = "/run/pihole-FTL.pid\n"
    assert expected_stdout == output.stdout


def test_getFTLPID_default(host):
    """Confirms getFTLPID returns the default value if FTL is not running"""
    output = host.run(
        """
    source /opt/pihole/utils.sh
    getFTLPID
    """
    )
    expected_stdout = "-1\n"
    assert expected_stdout == output.stdout


def test_getFTLPIDFile_and_getFTLPID_custom(host):
    """Confirms getFTLPIDFile returns a custom PID file path"""
    host.run(
        """
    tmpfile=$(mktemp)
    echo "PIDFILE=${tmpfile}" > /etc/pihole/pihole-FTL.conf
    echo "1234" > ${tmpfile}
    """
    )
    output = host.run(
        """
    source /opt/pihole/utils.sh
    FTL_PID_FILE=$(getFTLPIDFile)
    getFTLPID "${FTL_PID_FILE}"
    """
    )
    expected_stdout = "1234\n"
    assert expected_stdout == output.stdout