This file is indexed.

/usr/lib/python3/dist-packages/policyd_rate_limit/tests/test_daemon.py is in policyd-rate-limit 0.7.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
# more details.
#
# You should have received a copy of the GNU General Public License version 3
# along with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# (c) 2016 Valentin Samir
import os
import tempfile
from unittest import TestCase

from policyd_rate_limit.tests import utils as test_utils


class DaemonTestCase(TestCase):

    def setUp(self):
        self.base_config = dict(
            SOCKET=tempfile.mktemp('.sock'),
            sqlite_config={"database": tempfile.mktemp('.sqlite3')},
            pidfile=tempfile.mktemp('.pid'),
            limit_by_sasl=True,
            limit_by_ip=True,
            limited_networks=["192.168.0.0/16", "ffee::/64"],
            debug=True,
            report=True,
            report_limits=[60, 86400],
            user="root",
            group="root",
        )

    def tearDown(self):
        if os.path.isfile(self.base_config["sqlite_config"]["database"]):
            os.remove(self.base_config["sqlite_config"]["database"])

    def test_main_unix_socket(self):
        with test_utils.lauch(self.base_config) as cfg:
            self.base_test(cfg)

    def test_main_afinet_socket(self):
        self.base_config["SOCKET"] = ("127.0.0.1", 27184)
        with test_utils.lauch(self.base_config) as cfg:
            self.base_test(cfg)

    def test_main_afinet6_socket(self):
        self.base_config["SOCKET"] = ("::1", 27184)
        with test_utils.lauch(self.base_config) as cfg:
            self.base_test(cfg)

    def test_no_debug_no_report(self):
        self.base_config["debug"] = False
        self.base_config["report"] = False
        with test_utils.lauch(self.base_config) as cfg:
            self.base_test(cfg)

    def test_slow_connection(self):
        with test_utils.lauch(self.base_config) as cfg:
            with test_utils.sock(cfg["SOCKET"]) as s:
                msg = test_utils.postfix_request(sasl_username="test")
                i = 0
                s.send(msg[i:10])
                i += 10
                # run another request before the previous one is ended
                data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
                self.assertEqual(data.strip(), b"action=dunno")
                s.send(msg[i:1])
                i += 1
                data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
                self.assertEqual(data.strip(), b"action=dunno")
                s.send(msg[i:])
                datal = []
                datal.append(s.recv(2))
                # run another request before the previous one is ended
                data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
                self.assertEqual(data.strip(), b"action=dunno")
                datal.append(s.recv(1024))
                data = b"".join(datal)
                self.assertEqual(data.strip(), b"action=dunno")

    def test_database_unavailable(self):
        # create the database
        with open(self.base_config["sqlite_config"]["database"], 'a'):
            pass
        # make it unavailable
        os.chmod(self.base_config["sqlite_config"]["database"], 0)
        # lauch policyd-rate-limit with the database navailable
        with test_utils.lauch(self.base_config) as cfg:
            # as long as the database is unavailable, all response should be dunno
            for i in range(20):
                data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
                self.assertEqual(data.strip(), b"action=dunno")
            # make the database available, it should be initialized upon the next request
            os.chmod(self.base_config["sqlite_config"]["database"], 0o644)
            # these requests should be counted
            for i in range(10):
                data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
                self.assertEqual(data.strip(), b"action=dunno")
            # the eleventh counted requests should fail
            data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
            self.assertEqual(data.strip(), b"action=defer_if_permit Rate limit reach, retry later")

    def test_bad_config(self):
        self.base_config["backend"] = 1000
        with test_utils.lauch(self.base_config, get_process=True) as p:
            self.assertEqual(p.wait(), 5)

    def test_get_config(self):
        with test_utils.lauch(
            self.base_config,
            get_process=True,
            options=["--get-config", "pidfile"]
        ) as p:
            self.assertEqual(p.wait(), 0)
            self.assertEqual(p.stdout.read(), self.base_config["pidfile"].encode())
        with test_utils.lauch(
            self.base_config,
            get_process=True,
            options=["--get-config", "sqlite_config.database"]
        ) as p:
            self.assertEqual(p.wait(), 0)
            self.assertEqual(
                p.stdout.read(),
                self.base_config["sqlite_config"]["database"].encode()
            )
        with test_utils.lauch(
            self.base_config,
            get_process=True,
            options=["--get-config", "foo"]
        ) as p:
            self.assertEqual(p.wait(), 1)
        with test_utils.lauch(
            None,
            get_process=True,
            options=["--get-config", "pidfile"]
        ) as p:
            self.assertEqual(p.wait(), 0)
            self.assertEqual(
                p.stdout.read(),
                b'/var/run/policyd-rate-limit/policyd-rate-limit.pid'
            )

    def test_no_config_file_found(self):
        with test_utils.lauch(None, get_process=True) as p:
            self.assertEqual(p.wait(), 5)

    def test_already_running(self):
        with test_utils.lauch(self.base_config, no_coverage=True, get_process=True) as p1:
            pid = p1.pid
            with test_utils.lauch(self.base_config, get_process=True) as p2:
                self.assertEqual(p2.wait(), 3)
        with open(self.base_config["pidfile"], 'w') as f:
            f.write("%s" % pid)
        try:
            with test_utils.lauch(self.base_config, get_process=True) as p:
                pass
            self.assertEqual(p.wait(), 0)
            with open(self.base_config["pidfile"], 'w') as f:
                f.write("foo")
            with test_utils.lauch(self.base_config, get_process=True) as p:
                pass
            self.assertEqual(p.wait(), 0)
            with open(self.base_config["pidfile"], 'w') as f:
                f.write("")
            os.chmod(self.base_config["pidfile"], 0)
            with test_utils.lauch(self.base_config, get_process=True) as p:
                self.assertEqual(p.wait(), 6)
        finally:
            try:
                os.remove(self.base_config["pidfile"])
            except OSError:
                pass

    def test_bad_socket_bind_address(self):
        self.base_config["SOCKET"] = ("toto", 1234)
        with test_utils.lauch(self.base_config, get_process=True, no_wait=True) as p:
            self.assertEqual(p.wait(), 4)
        self.base_config["SOCKET"] = ("192.168::1", 1234)
        with test_utils.lauch(self.base_config, get_process=True, no_wait=True) as p:
            self.assertEqual(p.wait(), 6)

    def test_clean(self):
        self.base_config["report_to"] = "foo@example.com"
        with test_utils.lauch(self.base_config, options=["--clean"], get_process=True) as p:
            self.assertEqual(p.wait(), 0)
        self.base_config["report_only_if_needed"] = False
        self.base_config["smtp_server"] = "localhost"
        with test_utils.lauch(self.base_config, options=["--clean"], get_process=True) as p:
            self.assertEqual(p.wait(), 8)

    def test_limits_by_id(self):
        self.base_config["limits_by_id"] = {'foo': [[2, 60]], 'bar': []}
        with test_utils.lauch(self.base_config) as cfg:
            self.base_test(cfg)
            for i in range(20):
                data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="bar")
                self.assertEqual(data.strip(), b"action=dunno")
            for i in range(2):
                data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="foo")
                self.assertEqual(data.strip(), b"action=dunno")
            data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="foo")
            self.assertEqual(data.strip(), b"action=defer_if_permit Rate limit reach, retry later")

    def base_test(self, cfg):
        # test limit by sasl username
        for i in range(10):
            data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
            self.assertEqual(data.strip(), b"action=dunno")
        data = test_utils.send_policyd_request(cfg["SOCKET"], sasl_username="test")
        self.assertEqual(data.strip(), b"action=defer_if_permit Rate limit reach, retry later")
        # test limit by ip
        for i in range(10):
            data = test_utils.send_policyd_request(cfg["SOCKET"], client_address="192.168.0.1")
            self.assertEqual(data.strip(), b"action=dunno")
        data = test_utils.send_policyd_request(cfg["SOCKET"], client_address="192.168.0.1")
        self.assertEqual(data.strip(), b"action=defer_if_permit Rate limit reach, retry later")
        # test limit by ip in ipv6
        for i in range(10):
            data = test_utils.send_policyd_request(cfg["SOCKET"], client_address="ffee::1")
            self.assertEqual(data.strip(), b"action=dunno")
        data = test_utils.send_policyd_request(cfg["SOCKET"], client_address="ffee::1")
        self.assertEqual(data.strip(), b"action=defer_if_permit Rate limit reach, retry later")
        # test limit by ip not limited
        for i in range(10):
            data = test_utils.send_policyd_request(cfg["SOCKET"], client_address="10.0.0.1")
            self.assertEqual(data.strip(), b"action=dunno")
        data = test_utils.send_policyd_request(cfg["SOCKET"], client_address="10.0.0.1")
        self.assertEqual(data.strip(), b"action=dunno")
        # test with bad protocol state
        for i in range(10):
            data = test_utils.send_policyd_request(
                cfg["SOCKET"],
                sasl_username="test",
                protocol_state="VRFY"
            )
            self.assertEqual(data.strip(), b"action=dunno")
        data = test_utils.send_policyd_request(
            cfg["SOCKET"],
            sasl_username="test",
            protocol_state="VRFY"
        )
        self.assertEqual(data.strip(), b"action=dunno")