~yolanda.robla/+junk/squid3_fresh_branch

« back to all changes in this revision

Viewing changes to debian/tests/test-squid.py

  • Committer: yolanda.robla at canonical
  • Date: 2013-05-27 10:52:43 UTC
  • Revision ID: yolanda.robla@canonical.com-20130527105243-mcd0nscika17sl4w
first branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
#    test-squid.py quality assurance test script
 
4
#    Copyright (C) 2008-2012 Canonical Ltd.
 
5
#    Author: Jamie Strandboge <jamie@canonical.com>
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License version 2,
 
9
#    as published by the Free Software Foundation.
 
10
#
 
11
#    This program is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    GNU General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the GNU General Public License
 
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
'''
 
21
  *** IMPORTANT ***
 
22
  DO NOT RUN ON A PRODUCTION SERVER.
 
23
  *** IMPORTANT ***
 
24
 
 
25
  How to run:
 
26
    $ sudo apt-get remove --purge squid
 
27
    $ sudo apt-get -y install squid squidclient python-unit elinks netcat
 
28
    $ sudo ./test-squid.py -v
 
29
 
 
30
  NOTE:
 
31
    The host running this script needs to have access to the internet
 
32
 
 
33
  TODO:
 
34
    acls
 
35
    ident
 
36
    purge (via squidclient)
 
37
    ...
 
38
    squidguard:
 
39
      - test with:
 
40
        $ echo "http://blocked.com 1.2.3.4/- - GET -" | squidGuard -c /etc/squid/squidGuard.conf -d
 
41
        if using a 'redirect', then the redirect URL is displayed, otherwise
 
42
        nothing
 
43
      - test block with the following in default acl in squidGuard.conf:
 
44
        pass     local none
 
45
        redirect http://www.example.com/redirected.html
 
46
      - test pass with the following in default acl in squidGuard.conf:
 
47
        pass     local all
 
48
        redirect http://www.example.com/redirected.html
 
49
      - test domains and urls with something like the following acl:
 
50
        dest bad {
 
51
            domainlist      test/domains
 
52
            urllist         test/urls
 
53
        }
 
54
        acl {
 
55
            default {
 
56
                pass !bad all
 
57
                redirect http://www.example.com/redirected.html
 
58
            }
 
59
        }
 
60
 
 
61
        then create /var/lib/squidguard/db/test/domains with:
 
62
        blocked.com
 
63
 
 
64
        Test with:
 
65
        $ echo "http://ok.com 1.2.3.4/- - GET -" | squidGuard -c /etc/squid/squidGuard.conf -d
 
66
        $ echo "http://blocked.com 1.2.3.4/- - GET -" | squidGuard -c /etc/squid/squidGuard.conf -d
 
67
'''
 
68
 
 
69
# QRT-Packages: squid squidclient python-unit elinks netcat pygopherd apparmor-utils
 
70
# QRT-Depends: testlib_httpd.py private/qrt/squid.py
 
71
 
 
72
import unittest, subprocess
 
73
import os
 
74
import sys
 
75
import testlib
 
76
import testlib_httpd
 
77
import time
 
78
 
 
79
try:
 
80
    from private.qrt.squid import PrivateSquidTest
 
81
except ImportError:
 
82
    class PrivateSquidTest(object):
 
83
        '''Empty class'''
 
84
    print >>sys.stdout, "Skipping private tests"
 
85
 
 
86
class BasicTest(testlib_httpd.HttpdCommon, PrivateSquidTest):
 
87
    '''Test basic functionality'''
 
88
    def setUp(self):
 
89
        '''Setup mechanisms'''
 
90
 
 
91
        # for some reason, squid on maverick is missing the init.d
 
92
        # upstart compatibility symlink
 
93
        if self.lsb_release['Release'] == 10.10 and not os.path.exists("/etc/init.d/squid"):
 
94
            os.symlink("/lib/init/upstart-job", "/etc/init.d/squid")
 
95
 
 
96
        self._set_initscript("/etc/init.d/squid")
 
97
        if self.lsb_release['Release'] >= 12.04:
 
98
            self._set_initscript("/etc/init.d/squid3")
 
99
 
 
100
        testlib_httpd.HttpdCommon._setUp(self)
 
101
 
 
102
        self.gophermap = "/var/gopher/gophermap"
 
103
 
 
104
        self.aa_profile = "usr.sbin.squid3"
 
105
        self.aa_abs_profile = "/etc/apparmor.d/%s" % self.aa_profile
 
106
        self.version_with_apparmor = 12.10
 
107
        # This hack is only used until we have tests run both confined and
 
108
        # unconfined
 
109
        self.aa_unload_at_teardown = False
 
110
 
 
111
    def tearDown(self):
 
112
        '''Shutdown methods'''
 
113
        testlib_httpd.HttpdCommon._tearDown(self)
 
114
        testlib.config_restore(self.gophermap)
 
115
 
 
116
    def test_daemons(self):
 
117
        '''Test daemon'''
 
118
        pidfile = "/run/squid3.pid"
 
119
        exe = "squid3"
 
120
 
 
121
        if self.lsb_release['Release'] < 12.04:
 
122
            pidfile = "/var/run/squid.pid"
 
123
            exe = "squid"
 
124
 
 
125
        self.assertTrue(testlib.check_pidfile(exe, pidfile))
 
126
 
 
127
    def test_http_proxy(self):
 
128
        '''Test http'''
 
129
        self._test_url_proxy("http://www.ubuntu.com/", "", "http://localhost:3128/")
 
130
 
 
131
    def test_https_proxy(self):
 
132
        '''Test https'''
 
133
        self._test_url_proxy("https://wiki.ubuntu.com/", "", "http://localhost:3128/")
 
134
 
 
135
    def test_ftp_proxy(self):
 
136
        '''Test ftp'''
 
137
        self._test_url_proxy("ftp://ftp.ubuntu.com/", "", "http://localhost:3128/")
 
138
 
 
139
    def test_squidclient(self):
 
140
        '''Test squidclient'''
 
141
        urls = ['http://www.ubuntu.com/', 'https://wiki.ubuntu.com/', \
 
142
                'ftp://ftp.ubuntu.com/', 'gopher://127.0.0.1']
 
143
        for url in urls:
 
144
            rc, report = testlib.cmd(['squidclient', '-h', '127.0.0.1', '-p', '3128', '-r', url])
 
145
            expected = 0
 
146
            result = 'Got exit code %d, expected %d\n' % (rc, expected)
 
147
            self.assertEquals(expected, rc, result + report)
 
148
 
 
149
    def test_CVE_2011_3205(self):
 
150
        '''Test parsing lines > 4096 in length (CVE-2011-3205)'''
 
151
 
 
152
        longline = "ABCDEF" * 4096
 
153
 
 
154
        testlib.config_replace(self.gophermap, """Welcome to Pygopherd!  You can place your documents
 
155
in /var/gopher for future use.  You can remove the gophermap
 
156
file there to get rid of this message, or you can edit it to
 
157
use other things.  (You'll need to do at least one of these
 
158
two things in order to get your own data to show up!)
 
159
 
 
160
%s
 
161
 
 
162
Some links to get you started:
 
163
 
 
164
1Pygopherd Home /devel/gopher/pygopherd gopher.quux.org 70
 
165
1Quux.Org Mega Server   /   gopher.quux.org 70
 
166
1The Gopher Project /Software/Gopher    gopher.quux.org 70
 
167
1Traditional UMN Home Gopher    /   gopher.tc.umn.edu   70
 
168
 
 
169
Welcome to the world of Gopher and enjoy!
 
170
""" %(longline), append=False)
 
171
 
 
172
        rc, report = testlib.cmd(['squidclient', '-h', '127.0.0.1', '-p', '3128', '-r', "gopher://127.0.0.1"])
 
173
        expected = 0
 
174
        result = 'Got exit code %d, expected %d\n' % (rc, expected)
 
175
        self.assertEquals(expected, rc, result + report)
 
176
 
 
177
    # Run this last so if we enable the profile then we don't unload it
 
178
    def test_zz_apparmor(self):
 
179
        '''Test apparmor'''
 
180
        if self.lsb_release['Release'] < 12.10:
 
181
            self._skipped("No profile in 12.04 and under")
 
182
 
 
183
        self.aa_unload_at_teardown = True
 
184
 
 
185
        # Currently while we have a profile, it is shipped disabled by default.
 
186
        # Verify that.
 
187
        rc, report = testlib.check_apparmor(self.aa_abs_profile, 12.10, is_running=False)
 
188
        expected = 1
 
189
        result = 'Got exit code %d, expected %d\n' % (rc, expected)
 
190
        self.assertEquals(rc, expected, result + report)
 
191
 
 
192
        # Verify it is syntactically correct
 
193
        rc, report = testlib.cmd(['apparmor_parser', '-p', self.aa_abs_profile])
 
194
        expected = 0
 
195
        result = 'Got exit code %d, expected %d\n' % (rc, expected)
 
196
        self.assertEquals(rc, expected, result + report)
 
197
 
 
198
        # Verify it loads ok
 
199
        rc, report = testlib.cmd(['aa-enforce', self.aa_abs_profile])
 
200
        expected = 0
 
201
        result = 'Got exit code %d, expected %d\n' % (rc, expected)
 
202
        self.assertEquals(rc, expected, result + report)
 
203
 
 
204
        self._stop()
 
205
        self._start()
 
206
 
 
207
        rc, report = testlib.check_apparmor(self.aa_abs_profile, 12.10, is_running=True)
 
208
        expected = 1
 
209
        result = 'Got exit code %d, expected %d\n' % (rc, expected)
 
210
        self.assertEquals(rc, expected, result + report)
 
211
 
 
212
 
 
213
if __name__ == '__main__':
 
214
    suite = unittest.TestSuite()
 
215
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(BasicTest))
 
216
 
 
217
    rc = unittest.TextTestRunner(verbosity=2).run(suite)
 
218
    if not rc.wasSuccessful():
 
219
        sys.exit(1)