aboutsummaryrefslogtreecommitdiff
path: root/tests/test_click_through_license.py
blob: 4a0520d771c5c07c9d79433f7604dc5767881a67 (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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python

import re
import os
import shutil
import shlex
import subprocess
import socket

from testtools import TestCase
from testtools.matchers import Mismatch
from license_protected_file_downloader import LicenseProtectedFileFetcher

fetcher = LicenseProtectedFileFetcher()
cwd = os.getcwd()
docroot = cwd
srvroot = os.path.abspath(os.path.join(*([cwd] + ['tests'])))
local_rewrite = 'RewriteCond %{REMOTE_ADDR} 127.0.0.1 [OR]'

host = 'http://127.0.0.1'
port = '0'  # 0 == Pick a free port.
samsung_license_path = '/licenses/license.php'
ste_license_path = '/licenses/license.php'
linaro_license_path = '/licenses/license.php'
samsung_test_file = '/android/~linaro-android/staging-origen/test.txt'
ste_test_file = ('/android/~linaro-android/staging-snowball'
                 '/173/target/product/snowball/test.txt')
ste_open_test_file = '/android/~linaro-android/staging-snowball/173/test.txt'
never_available = '/android/~linaro-android/staging-imx53/test.txt'
linaro_test_file = '/android/~linaro-android/staging-panda/test.txt'
not_protected_test_file = ('/android/~linaro-android/staging-vexpress-a9'
                           '/test.txt')
not_found_test_file = ('/android/~linaro-android/staging-vexpress-a9'
                       '/notfound.txt')
per_file_samsung_test_file = '/android/images/origen-blob.txt'
per_file_ste_test_file = '/android/images/snowball-blob.txt'
per_file_not_protected_test_file = '/android/images/MANIFEST'
dirs_only_dir = '/android/~linaro-android/'
build_info_samsung_test_file = '/android/build-info/origen-blob.txt'
build_info_ste_test_file = '/android/build-info/snowball-blob.txt'
build_info_not_protected_test_file = '/android/build-info/panda-open.txt'
build_info_openid_test_file = '/android/build-info/openid.txt'


class Contains(object):
    '''Match if a string contains substring'''
    def __init__(self, substr):
        self.substr = substr

    def __str__(self):
        return 'Contains(%s)' % (self.substr,)

    def match(self, actual):
        for line in actual.splitlines():
            res = re.search(self.substr, line)
            if res:
                return None
        return Mismatch("Initial string doesn't contain substring (%s)" %
                self.substr)


class CommandNotFoundException(Exception):
    ''' Unable to find command '''


class NonZeroReturnValueException(Exception):
    ''' Command exited with nonzero return value '''


class TestLicense(TestCase):
    '''Tests for accessing files and directories with license protection'''

    @classmethod
    def setUpClass(cls):
        global host
        global port
        if port == '0':
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind(('127.0.0.1', 0))
            port = str(s.getsockname()[1])
            s.close()
            host = host + ':' + port
        shutil.copy("%s/apache2.conf.tmpl" % srvroot, "%s/apache2.conf" %
                srvroot)
        shutil.copy("%s/.htaccess" % docroot, "%s/dothtaccess" % docroot)
        subprocess.Popen(['sed', '-i', 's/ServerRoot \"\"/ServerRoot \"%s\"/'
            % srvroot.replace('/', '\/'), '%s/apache2.conf' % srvroot],
            stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT).wait()
        subprocess.Popen(['sed', '-i', 's/DocumentRoot \"\"/DocumentRoot '
            '\"%s\"/' % docroot.replace('/', '\/'), '%s/apache2.conf'
            % srvroot], stdout=open('/dev/null', 'w'),
            stderr=subprocess.STDOUT).wait()
        subprocess.Popen(['sed', '-i', 's/Directory \"\"/Directory \"%s\"/'
            % docroot.replace('/', '\/'), '%s/apache2.conf' % srvroot],
            stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT).wait()
        subprocess.Popen(['sed', '-i', 's/Listen/Listen %s/' % port,
            '%s/apache2.conf' % srvroot], stdout=open('/dev/null', 'w'),
            stderr=subprocess.STDOUT).wait()
        if subprocess.Popen(['which', 'apache2'],
                stdout=open('/dev/null', 'w'),
                stderr=subprocess.STDOUT).wait():
            raise CommandNotFoundException("apache2 command not found. Please "
                "install apache2 web server and rerun tests.")
        args = shlex.split('apache2 -d %s -f apache2.conf -k start' % srvroot)
        rc = subprocess.Popen(args, stdout=open('/dev/null', 'w'),
                stderr=subprocess.STDOUT).wait()
        if rc:
            raise NonZeroReturnValueException("apache2 server exited with "
                "error %s" % rc)

    @classmethod
    def tearDownClass(cls):
        if os.path.exists("%s/cookies.txt" % docroot):
            os.unlink("%s/cookies.txt" % docroot)
        args = shlex.split('apache2 -d %s -f apache2.conf -k stop' % srvroot)
        subprocess.Popen(args, stdout=open('/dev/null', 'w'),
                stderr=subprocess.STDOUT).wait()
        if os.path.exists("%s/apache2.conf" % srvroot):
            os.unlink("%s/apache2.conf" % srvroot)
        if os.path.exists("%s/click_through_license_access.log" % srvroot):
            os.unlink("%s/click_through_license_access.log" % srvroot)
        if os.path.exists("%s/click_through_license_error.log" % srvroot):
            os.unlink("%s/click_through_license_error.log" % srvroot)
        if os.path.exists("%s/rewrite.log" % srvroot):
            os.unlink("%s/rewrite.log" % srvroot)
        os.rename("%s/dothtaccess" % docroot, "%s/.htaccess" % docroot)

    def setUp(self):
        super(TestLicense, self).setUp()
        global fetcher
        fetcher = LicenseProtectedFileFetcher()

    def tearDown(self):
        super(TestLicense, self).tearDown()
        if isinstance(fetcher, LicenseProtectedFileFetcher):
            fetcher.close()
        if os.path.exists("%s/cookies.txt" % docroot):
            os.unlink("%s/cookies.txt" % docroot)

    def test_licensefile_directly_samsung(self):
        search = "Index of /"
        testfile = fetcher.get(host + samsung_license_path)
        self.assertThat(testfile, Contains(search))

    def test_licensefile_directly_ste(self):
        search = "Index of /"
        testfile = fetcher.get(host + ste_license_path)
        self.assertThat(testfile, Contains(search))

    def test_licensefile_directly_linaro(self):
        search = "Index of /"
        testfile = fetcher.get(host + linaro_license_path)
        self.assertThat(testfile, Contains(search))

    def test_redirect_to_license_samsung(self):
        search = "PLEASE READ THE FOLLOWING AGREEMENT CAREFULLY"
        testfile = fetcher.get_or_return_license(host + samsung_test_file)
        self.assertThat(testfile[0], Contains(search))

    def test_redirect_to_license_ste(self):
        search = "PLEASE READ THE FOLLOWING AGREEMENT CAREFULLY"
        testfile = fetcher.get_or_return_license(host + ste_test_file)
        self.assertThat(testfile[0], Contains(search))

    def test_redirect_to_license_linaro(self):
        search = "Linaro license."
        testfile = fetcher.get_or_return_license(host + linaro_test_file)
        self.assertThat(testfile[0], Contains(search))

    def test_decline_license_samsung(self):
        search = "License has not been accepted"
        testfile = fetcher.get(host + samsung_test_file, accept_license=False)
        self.assertThat(testfile, Contains(search))

    def test_decline_license_ste(self):
        search = "License has not been accepted"
        testfile = fetcher.get(host + ste_test_file, accept_license=False)
        self.assertThat(testfile, Contains(search))

    def test_decline_license_linaro(self):
        search = "License has not been accepted"
        testfile = fetcher.get(host + linaro_test_file, accept_license=False)
        self.assertThat(testfile, Contains(search))

    def test_non_protected_dirs(self):
        search = "This is always available."
        testfile = fetcher.get(host + not_protected_test_file)
        self.assertThat(testfile, Contains(search))

    def test_never_available_dirs(self):
        search = "Forbidden"
        testfile = fetcher.get(host + never_available)
        self.assertThat(testfile, Contains(search))

    def test_accept_license_samsung_file(self):
        search = "This is protected with click-through Samsung license."
        testfile = fetcher.get(host + samsung_test_file)
        fetcher.close()
        if os.path.exists("%s/cookies.txt" % docroot):
            os.rename("%s/cookies.txt" % docroot,
                    "%s/cookies.samsung" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_accept_license_samsung_dir(self):
        search = "Index of /android/~linaro-android/staging-origen"
        testfile = fetcher.get(host + os.path.dirname(samsung_test_file))
        self.assertThat(testfile, Contains(search))

    def test_accept_license_ste_file(self):
        search = "This is protected with click-through ST-E license."
        testfile = fetcher.get(host + ste_test_file)
        fetcher.close()
        if os.path.exists("%s/cookies.txt" % docroot):
            os.rename("%s/cookies.txt" % docroot, "%s/cookies.ste" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_accept_license_ste_dir(self):
        search = "Index of /android/~linaro-android/staging-snowball"
        testfile = fetcher.get(host + os.path.dirname(ste_test_file))
        self.assertThat(testfile, Contains(search))

    def test_license_accepted_samsung(self):
        search = "This is protected with click-through Samsung license."
        os.rename("%s/cookies.samsung" % docroot, "%s/cookies.txt" % docroot)
        testfile = fetcher.get(host + samsung_test_file)
        self.assertThat(testfile, Contains(search))

    def test_license_accepted_ste(self):
        search = "This is protected with click-through ST-E license."
        os.rename("%s/cookies.ste" % docroot, "%s/cookies.txt" % docroot)
        testfile = fetcher.get(host + ste_test_file)
        self.assertThat(testfile, Contains(search))

    def test_internal_host_samsung(self):
        search = "This is protected with click-through Samsung license."
        subprocess.Popen(['sed', '-i', '/## Let internal hosts through '
            'always./ a %s' % local_rewrite, '%s/.htaccess' % docroot],
            stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT).wait()
        testfile = fetcher.get(host + samsung_test_file, ignore_license=True)
        shutil.copy("%s/dothtaccess" % docroot, "%s/.htaccess" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_internal_host_ste(self):
        search = "This is protected with click-through ST-E license."
        subprocess.Popen(['sed', '-i', '/## Let internal hosts through '
            'always./ a %s' % local_rewrite, '%s/.htaccess' % docroot],
            stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT).wait()
        testfile = fetcher.get(host + ste_test_file, ignore_license=True)
        shutil.copy("%s/dothtaccess" % docroot, "%s/.htaccess" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_ste_open_file(self):
        search = "This is always available."
        testfile = fetcher.get(host + ste_open_test_file)
        self.assertThat(testfile, Contains(search))

    def test_per_file_accept_license_samsung_file(self):
        search = "This is protected with click-through Samsung license."
        testfile = fetcher.get(host + per_file_samsung_test_file)
        fetcher.close()
        if os.path.exists("%s/cookies.txt" % docroot):
            os.rename("%s/cookies.txt" % docroot,
                    "%s/cookies.samsung" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_per_file_accept_license_ste_file(self):
        search = "This is protected with click-through ST-E license."
        testfile = fetcher.get(host + per_file_ste_test_file)
        fetcher.close()
        if os.path.exists("%s/cookies.txt" % docroot):
            os.rename("%s/cookies.txt" % docroot, "%s/cookies.ste" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_per_file_license_accepted_samsung(self):
        search = "This is protected with click-through Samsung license."
        os.rename("%s/cookies.samsung" % docroot, "%s/cookies.txt" % docroot)
        testfile = fetcher.get(host + per_file_samsung_test_file,
                               ignore_license=True)
        self.assertThat(testfile, Contains(search))

    def test_per_file_license_accepted_ste(self):
        search = "This is protected with click-through ST-E license."
        os.rename("%s/cookies.ste" % docroot, "%s/cookies.txt" % docroot)
        testfile = fetcher.get(host + per_file_ste_test_file,
                               ignore_license=True)
        self.assertThat(testfile, Contains(search))

    def test_per_file_non_protected_dirs(self):
        search = "MANIFEST"
        testfile = fetcher.get(host + per_file_not_protected_test_file)
        self.assertThat(testfile, Contains(search))

    def test_dir_containing_only_dirs(self):
        search = "Index of /android/~linaro-android"
        testfile = fetcher.get(host + dirs_only_dir)
        self.assertThat(testfile, Contains(search))

    def test_not_found_file(self):
        search = "Not Found"
        testfile = fetcher.get(host + not_found_test_file)
        self.assertThat(testfile, Contains(search))

    def test_build_info_non_protected_file(self):
        search = "This is always available."
        testfile = fetcher.get(host + build_info_not_protected_test_file)
        self.assertThat(testfile, Contains(search))

    def test_build_info_accept_license_samsung_file(self):
        search = "This is protected with click-through Samsung license."
        testfile = fetcher.get(host + build_info_samsung_test_file)
        fetcher.close()
        if os.path.exists("%s/cookies.txt" % docroot):
            os.rename("%s/cookies.txt" % docroot,
                    "%s/cookies.samsung" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_build_info_accept_license_ste_file(self):
        search = "This is protected with click-through ST-E license."
        testfile = fetcher.get(host + build_info_ste_test_file)
        fetcher.close()
        if os.path.exists("%s/cookies.txt" % docroot):
            os.rename("%s/cookies.txt" % docroot, "%s/cookies.ste" % docroot)
        self.assertThat(testfile, Contains(search))

    def test_build_info_openid_protection(self):
        search = "This is protected with OpenID."
        testfile = fetcher.get(host + build_info_openid_test_file)
        fetcher.close()
        self.assertThat(testfile, Contains(search))