blob: 0fd2c02163ba4a2d954e2d48f0712c5c7c539a07 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3#
4# BitBake Tests for the Fetcher (fetch2/)
5#
6# Copyright (C) 2012 Richard Purdie
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License version 2 as
10# published by the Free Software Foundation.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21
22import unittest
23import tempfile
24import subprocess
Patrick Williamsc0f7c042017-02-23 20:41:17 -060025import collections
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026import os
27from bb.fetch2 import URI
28from bb.fetch2 import FetchMethod
29import bb
30
31class URITest(unittest.TestCase):
32 test_uris = {
33 "http://www.google.com/index.html" : {
34 'uri': 'http://www.google.com/index.html',
35 'scheme': 'http',
36 'hostname': 'www.google.com',
37 'port': None,
38 'hostport': 'www.google.com',
39 'path': '/index.html',
40 'userinfo': '',
41 'username': '',
42 'password': '',
43 'params': {},
44 'query': {},
45 'relative': False
46 },
47 "http://www.google.com/index.html;param1=value1" : {
48 'uri': 'http://www.google.com/index.html;param1=value1',
49 'scheme': 'http',
50 'hostname': 'www.google.com',
51 'port': None,
52 'hostport': 'www.google.com',
53 'path': '/index.html',
54 'userinfo': '',
55 'username': '',
56 'password': '',
57 'params': {
58 'param1': 'value1'
59 },
60 'query': {},
61 'relative': False
62 },
63 "http://www.example.org/index.html?param1=value1" : {
64 'uri': 'http://www.example.org/index.html?param1=value1',
65 'scheme': 'http',
66 'hostname': 'www.example.org',
67 'port': None,
68 'hostport': 'www.example.org',
69 'path': '/index.html',
70 'userinfo': '',
71 'username': '',
72 'password': '',
73 'params': {},
74 'query': {
75 'param1': 'value1'
76 },
77 'relative': False
78 },
79 "http://www.example.org/index.html?qparam1=qvalue1;param2=value2" : {
80 'uri': 'http://www.example.org/index.html?qparam1=qvalue1;param2=value2',
81 'scheme': 'http',
82 'hostname': 'www.example.org',
83 'port': None,
84 'hostport': 'www.example.org',
85 'path': '/index.html',
86 'userinfo': '',
87 'username': '',
88 'password': '',
89 'params': {
90 'param2': 'value2'
91 },
92 'query': {
93 'qparam1': 'qvalue1'
94 },
95 'relative': False
96 },
97 "http://www.example.com:8080/index.html" : {
98 'uri': 'http://www.example.com:8080/index.html',
99 'scheme': 'http',
100 'hostname': 'www.example.com',
101 'port': 8080,
102 'hostport': 'www.example.com:8080',
103 'path': '/index.html',
104 'userinfo': '',
105 'username': '',
106 'password': '',
107 'params': {},
108 'query': {},
109 'relative': False
110 },
111 "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : {
112 'uri': 'cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg',
113 'scheme': 'cvs',
114 'hostname': 'cvs.handhelds.org',
115 'port': None,
116 'hostport': 'cvs.handhelds.org',
117 'path': '/cvs',
118 'userinfo': 'anoncvs',
119 'username': 'anoncvs',
120 'password': '',
121 'params': {
122 'module': 'familiar/dist/ipkg'
123 },
124 'query': {},
125 'relative': False
126 },
127 "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg": {
128 'uri': 'cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg',
129 'scheme': 'cvs',
130 'hostname': 'cvs.handhelds.org',
131 'port': None,
132 'hostport': 'cvs.handhelds.org',
133 'path': '/cvs',
134 'userinfo': 'anoncvs:anonymous',
135 'username': 'anoncvs',
136 'password': 'anonymous',
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600137 'params': collections.OrderedDict([
138 ('tag', 'V0-99-81'),
139 ('module', 'familiar/dist/ipkg')
140 ]),
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 'query': {},
142 'relative': False
143 },
144 "file://example.diff": { # NOTE: Not RFC compliant!
145 'uri': 'file:example.diff',
146 'scheme': 'file',
147 'hostname': '',
148 'port': None,
149 'hostport': '',
150 'path': 'example.diff',
151 'userinfo': '',
152 'username': '',
153 'password': '',
154 'params': {},
155 'query': {},
156 'relative': True
157 },
158 "file:example.diff": { # NOTE: RFC compliant version of the former
159 'uri': 'file:example.diff',
160 'scheme': 'file',
161 'hostname': '',
162 'port': None,
163 'hostport': '',
164 'path': 'example.diff',
165 'userinfo': '',
166 'userinfo': '',
167 'username': '',
168 'password': '',
169 'params': {},
170 'query': {},
171 'relative': True
172 },
173 "file:///tmp/example.diff": {
174 'uri': 'file:///tmp/example.diff',
175 'scheme': 'file',
176 'hostname': '',
177 'port': None,
178 'hostport': '',
179 'path': '/tmp/example.diff',
180 'userinfo': '',
181 'userinfo': '',
182 'username': '',
183 'password': '',
184 'params': {},
185 'query': {},
186 'relative': False
187 },
188 "git:///path/example.git": {
189 'uri': 'git:///path/example.git',
190 'scheme': 'git',
191 'hostname': '',
192 'port': None,
193 'hostport': '',
194 'path': '/path/example.git',
195 'userinfo': '',
196 'userinfo': '',
197 'username': '',
198 'password': '',
199 'params': {},
200 'query': {},
201 'relative': False
202 },
203 "git:path/example.git": {
204 'uri': 'git:path/example.git',
205 'scheme': 'git',
206 'hostname': '',
207 'port': None,
208 'hostport': '',
209 'path': 'path/example.git',
210 'userinfo': '',
211 'userinfo': '',
212 'username': '',
213 'password': '',
214 'params': {},
215 'query': {},
216 'relative': True
217 },
218 "git://example.net/path/example.git": {
219 'uri': 'git://example.net/path/example.git',
220 'scheme': 'git',
221 'hostname': 'example.net',
222 'port': None,
223 'hostport': 'example.net',
224 'path': '/path/example.git',
225 'userinfo': '',
226 'userinfo': '',
227 'username': '',
228 'password': '',
229 'params': {},
230 'query': {},
231 'relative': False
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500232 },
233 "http://somesite.net;someparam=1": {
234 'uri': 'http://somesite.net;someparam=1',
235 'scheme': 'http',
236 'hostname': 'somesite.net',
237 'port': None,
238 'hostport': 'somesite.net',
239 'path': '',
240 'userinfo': '',
241 'userinfo': '',
242 'username': '',
243 'password': '',
244 'params': {"someparam" : "1"},
245 'query': {},
246 'relative': False
247 },
248 "file://somelocation;someparam=1": {
249 'uri': 'file:somelocation;someparam=1',
250 'scheme': 'file',
251 'hostname': '',
252 'port': None,
253 'hostport': '',
254 'path': 'somelocation',
255 'userinfo': '',
256 'userinfo': '',
257 'username': '',
258 'password': '',
259 'params': {"someparam" : "1"},
260 'query': {},
261 'relative': True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500263
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264 }
265
266 def test_uri(self):
267 for test_uri, ref in self.test_uris.items():
268 uri = URI(test_uri)
269
270 self.assertEqual(str(uri), ref['uri'])
271
272 # expected attributes
273 self.assertEqual(uri.scheme, ref['scheme'])
274
275 self.assertEqual(uri.userinfo, ref['userinfo'])
276 self.assertEqual(uri.username, ref['username'])
277 self.assertEqual(uri.password, ref['password'])
278
279 self.assertEqual(uri.hostname, ref['hostname'])
280 self.assertEqual(uri.port, ref['port'])
281 self.assertEqual(uri.hostport, ref['hostport'])
282
283 self.assertEqual(uri.path, ref['path'])
284 self.assertEqual(uri.params, ref['params'])
285
286 self.assertEqual(uri.relative, ref['relative'])
287
288 def test_dict(self):
289 for test in self.test_uris.values():
290 uri = URI()
291
292 self.assertEqual(uri.scheme, '')
293 self.assertEqual(uri.userinfo, '')
294 self.assertEqual(uri.username, '')
295 self.assertEqual(uri.password, '')
296 self.assertEqual(uri.hostname, '')
297 self.assertEqual(uri.port, None)
298 self.assertEqual(uri.path, '')
299 self.assertEqual(uri.params, {})
300
301
302 uri.scheme = test['scheme']
303 self.assertEqual(uri.scheme, test['scheme'])
304
305 uri.userinfo = test['userinfo']
306 self.assertEqual(uri.userinfo, test['userinfo'])
307 self.assertEqual(uri.username, test['username'])
308 self.assertEqual(uri.password, test['password'])
309
310 # make sure changing the values doesn't do anything unexpected
311 uri.username = 'changeme'
312 self.assertEqual(uri.username, 'changeme')
313 self.assertEqual(uri.password, test['password'])
314 uri.password = 'insecure'
315 self.assertEqual(uri.username, 'changeme')
316 self.assertEqual(uri.password, 'insecure')
317
318 # reset back after our trickery
319 uri.userinfo = test['userinfo']
320 self.assertEqual(uri.userinfo, test['userinfo'])
321 self.assertEqual(uri.username, test['username'])
322 self.assertEqual(uri.password, test['password'])
323
324 uri.hostname = test['hostname']
325 self.assertEqual(uri.hostname, test['hostname'])
326 self.assertEqual(uri.hostport, test['hostname'])
327
328 uri.port = test['port']
329 self.assertEqual(uri.port, test['port'])
330 self.assertEqual(uri.hostport, test['hostport'])
331
332 uri.path = test['path']
333 self.assertEqual(uri.path, test['path'])
334
335 uri.params = test['params']
336 self.assertEqual(uri.params, test['params'])
337
338 uri.query = test['query']
339 self.assertEqual(uri.query, test['query'])
340
341 self.assertEqual(str(uri), test['uri'])
342
343 uri.params = {}
344 self.assertEqual(uri.params, {})
345 self.assertEqual(str(uri), (str(uri).split(";"))[0])
346
347class FetcherTest(unittest.TestCase):
348
349 def setUp(self):
350 self.origdir = os.getcwd()
351 self.d = bb.data.init()
352 self.tempdir = tempfile.mkdtemp()
353 self.dldir = os.path.join(self.tempdir, "download")
354 os.mkdir(self.dldir)
355 self.d.setVar("DL_DIR", self.dldir)
356 self.unpackdir = os.path.join(self.tempdir, "unpacked")
357 os.mkdir(self.unpackdir)
358 persistdir = os.path.join(self.tempdir, "persistdata")
359 self.d.setVar("PERSISTENT_DIR", persistdir)
360
361 def tearDown(self):
362 os.chdir(self.origdir)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600363 if os.environ.get("BB_TMPDIR_NOCLEAN") == "yes":
364 print("Not cleaning up %s. Please remove manually." % self.tempdir)
365 else:
366 bb.utils.prunedir(self.tempdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500367
368class MirrorUriTest(FetcherTest):
369
370 replaceuris = {
371 ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "http://somewhere.org/somedir/")
372 : "http://somewhere.org/somedir/git2_git.invalid.infradead.org.mtd-utils.git.tar.gz",
373 ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/somedir/\\2;protocol=http")
374 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
375 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/somedir/\\2;protocol=http")
376 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
377 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/([^/]+/)*([^/]*)", "git://somewhere.org/\\2;protocol=http")
378 : "git://somewhere.org/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
379 ("git://someserver.org/bitbake;tag=1234567890123456789012345678901234567890", "git://someserver.org/bitbake", "git://git.openembedded.org/bitbake")
380 : "git://git.openembedded.org/bitbake;tag=1234567890123456789012345678901234567890",
381 ("file://sstate-xyz.tgz", "file://.*", "file:///somewhere/1234/sstate-cache")
382 : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
383 ("file://sstate-xyz.tgz", "file://.*", "file:///somewhere/1234/sstate-cache/")
384 : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
385 ("http://somewhere.org/somedir1/somedir2/somefile_1.2.3.tar.gz", "http://.*/.*", "http://somewhere2.org/somedir3")
386 : "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz",
387 ("http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz")
388 : "http://somewhere2.org/somedir3/somefile_1.2.3.tar.gz",
389 ("http://www.apache.org/dist/subversion/subversion-1.7.1.tar.bz2", "http://www.apache.org/dist", "http://archive.apache.org/dist")
390 : "http://archive.apache.org/dist/subversion/subversion-1.7.1.tar.bz2",
391 ("http://www.apache.org/dist/subversion/subversion-1.7.1.tar.bz2", "http://.*/.*", "file:///somepath/downloads/")
392 : "file:///somepath/downloads/subversion-1.7.1.tar.bz2",
393 ("git://git.invalid.infradead.org/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/BASENAME;protocol=http")
394 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
395 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/BASENAME;protocol=http")
396 : "git://somewhere.org/somedir/mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
397 ("git://git.invalid.infradead.org/foo/mtd-utils.git;tag=1234567890123456789012345678901234567890", "git://.*/.*", "git://somewhere.org/somedir/MIRRORNAME;protocol=http")
398 : "git://somewhere.org/somedir/git.invalid.infradead.org.foo.mtd-utils.git;tag=1234567890123456789012345678901234567890;protocol=http",
399
400 #Renaming files doesn't work
401 #("http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere.org/somedir1/somefile_1.2.3.tar.gz", "http://somewhere2.org/somedir3/somefile_2.3.4.tar.gz") : "http://somewhere2.org/somedir3/somefile_2.3.4.tar.gz"
402 #("file://sstate-xyz.tgz", "file://.*/.*", "file:///somewhere/1234/sstate-cache") : "file:///somewhere/1234/sstate-cache/sstate-xyz.tgz",
403 }
404
405 mirrorvar = "http://.*/.* file:///somepath/downloads/ \n" \
406 "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n" \
407 "https://.*/.* file:///someotherpath/downloads/ \n" \
408 "http://.*/.* file:///someotherpath/downloads/ \n"
409
410 def test_urireplace(self):
411 for k, v in self.replaceuris.items():
412 ud = bb.fetch.FetchData(k[0], self.d)
413 ud.setup_localpath(self.d)
414 mirrors = bb.fetch2.mirror_from_string("%s %s" % (k[1], k[2]))
415 newuris, uds = bb.fetch2.build_mirroruris(ud, mirrors, self.d)
416 self.assertEqual([v], newuris)
417
418 def test_urilist1(self):
419 fetcher = bb.fetch.FetchData("http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
420 mirrors = bb.fetch2.mirror_from_string(self.mirrorvar)
421 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
422 self.assertEqual(uris, ['file:///somepath/downloads/bitbake-1.0.tar.gz', 'file:///someotherpath/downloads/bitbake-1.0.tar.gz'])
423
424 def test_urilist2(self):
425 # Catch https:// -> files:// bug
426 fetcher = bb.fetch.FetchData("https://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
427 mirrors = bb.fetch2.mirror_from_string(self.mirrorvar)
428 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
429 self.assertEqual(uris, ['file:///someotherpath/downloads/bitbake-1.0.tar.gz'])
430
431 def test_mirror_of_mirror(self):
432 # Test if mirror of a mirror works
433 mirrorvar = self.mirrorvar + " http://.*/.* http://otherdownloads.yoctoproject.org/downloads/ \n"
434 mirrorvar = mirrorvar + " http://otherdownloads.yoctoproject.org/.* http://downloads2.yoctoproject.org/downloads/ \n"
435 fetcher = bb.fetch.FetchData("http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
436 mirrors = bb.fetch2.mirror_from_string(mirrorvar)
437 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
438 self.assertEqual(uris, ['file:///somepath/downloads/bitbake-1.0.tar.gz',
439 'file:///someotherpath/downloads/bitbake-1.0.tar.gz',
440 'http://otherdownloads.yoctoproject.org/downloads/bitbake-1.0.tar.gz',
441 'http://downloads2.yoctoproject.org/downloads/bitbake-1.0.tar.gz'])
442
Patrick Williamsd7e96312015-09-22 08:09:05 -0500443 recmirrorvar = "https://.*/[^/]* http://AAAA/A/A/A/ \n" \
444 "https://.*/[^/]* https://BBBB/B/B/B/ \n"
445
446 def test_recursive(self):
447 fetcher = bb.fetch.FetchData("https://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", self.d)
448 mirrors = bb.fetch2.mirror_from_string(self.recmirrorvar)
449 uris, uds = bb.fetch2.build_mirroruris(fetcher, mirrors, self.d)
450 self.assertEqual(uris, ['http://AAAA/A/A/A/bitbake/bitbake-1.0.tar.gz',
451 'https://BBBB/B/B/B/bitbake/bitbake-1.0.tar.gz',
452 'http://AAAA/A/A/A/B/B/bitbake/bitbake-1.0.tar.gz'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500453
454class FetcherLocalTest(FetcherTest):
455 def setUp(self):
456 def touch(fn):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600457 with open(fn, 'a'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500458 os.utime(fn, None)
459
460 super(FetcherLocalTest, self).setUp()
461 self.localsrcdir = os.path.join(self.tempdir, 'localsrc')
462 os.makedirs(self.localsrcdir)
463 touch(os.path.join(self.localsrcdir, 'a'))
464 touch(os.path.join(self.localsrcdir, 'b'))
465 os.makedirs(os.path.join(self.localsrcdir, 'dir'))
466 touch(os.path.join(self.localsrcdir, 'dir', 'c'))
467 touch(os.path.join(self.localsrcdir, 'dir', 'd'))
468 os.makedirs(os.path.join(self.localsrcdir, 'dir', 'subdir'))
469 touch(os.path.join(self.localsrcdir, 'dir', 'subdir', 'e'))
470 self.d.setVar("FILESPATH", self.localsrcdir)
471
472 def fetchUnpack(self, uris):
473 fetcher = bb.fetch.Fetch(uris, self.d)
474 fetcher.download()
475 fetcher.unpack(self.unpackdir)
476 flst = []
477 for root, dirs, files in os.walk(self.unpackdir):
478 for f in files:
479 flst.append(os.path.relpath(os.path.join(root, f), self.unpackdir))
480 flst.sort()
481 return flst
482
483 def test_local(self):
484 tree = self.fetchUnpack(['file://a', 'file://dir/c'])
485 self.assertEqual(tree, ['a', 'dir/c'])
486
487 def test_local_wildcard(self):
488 tree = self.fetchUnpack(['file://a', 'file://dir/*'])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500489 self.assertEqual(tree, ['a', 'dir/c', 'dir/d', 'dir/subdir/e'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500490
491 def test_local_dir(self):
492 tree = self.fetchUnpack(['file://a', 'file://dir'])
493 self.assertEqual(tree, ['a', 'dir/c', 'dir/d', 'dir/subdir/e'])
494
495 def test_local_subdir(self):
496 tree = self.fetchUnpack(['file://dir/subdir'])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500497 self.assertEqual(tree, ['dir/subdir/e'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500498
499 def test_local_subdir_file(self):
500 tree = self.fetchUnpack(['file://dir/subdir/e'])
501 self.assertEqual(tree, ['dir/subdir/e'])
502
503 def test_local_subdirparam(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500504 tree = self.fetchUnpack(['file://a;subdir=bar', 'file://dir;subdir=foo/moo'])
505 self.assertEqual(tree, ['bar/a', 'foo/moo/dir/c', 'foo/moo/dir/d', 'foo/moo/dir/subdir/e'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500506
507 def test_local_deepsubdirparam(self):
508 tree = self.fetchUnpack(['file://dir/subdir/e;subdir=bar'])
509 self.assertEqual(tree, ['bar/dir/subdir/e'])
510
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600511 def test_local_absolutedir(self):
512 # Unpacking to an absolute path that is a subdirectory of the root
513 # should work
514 tree = self.fetchUnpack(['file://a;subdir=%s' % os.path.join(self.unpackdir, 'bar')])
515
516 # Unpacking to an absolute path outside of the root should fail
517 with self.assertRaises(bb.fetch2.UnpackError):
518 self.fetchUnpack(['file://a;subdir=/bin/sh'])
519
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500520class FetcherNetworkTest(FetcherTest):
521
522 if os.environ.get("BB_SKIP_NETTESTS") == "yes":
523 print("Unset BB_SKIP_NETTESTS to run network tests")
524 else:
525 def test_fetch(self):
526 fetcher = bb.fetch.Fetch(["http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", "http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.1.tar.gz"], self.d)
527 fetcher.download()
528 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
529 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.1.tar.gz"), 57892)
530 self.d.setVar("BB_NO_NETWORK", "1")
531 fetcher = bb.fetch.Fetch(["http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz", "http://downloads.yoctoproject.org/releases/bitbake/bitbake-1.1.tar.gz"], self.d)
532 fetcher.download()
533 fetcher.unpack(self.unpackdir)
534 self.assertEqual(len(os.listdir(self.unpackdir + "/bitbake-1.0/")), 9)
535 self.assertEqual(len(os.listdir(self.unpackdir + "/bitbake-1.1/")), 9)
536
537 def test_fetch_mirror(self):
538 self.d.setVar("MIRRORS", "http://.*/.* http://downloads.yoctoproject.org/releases/bitbake")
539 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
540 fetcher.download()
541 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
542
543 def test_fetch_mirror_of_mirror(self):
544 self.d.setVar("MIRRORS", "http://.*/.* http://invalid2.yoctoproject.org/ \n http://invalid2.yoctoproject.org/.* http://downloads.yoctoproject.org/releases/bitbake")
545 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
546 fetcher.download()
547 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
548
549 def test_fetch_file_mirror_of_mirror(self):
550 self.d.setVar("MIRRORS", "http://.*/.* file:///some1where/ \n file:///some1where/.* file://some2where/ \n file://some2where/.* http://downloads.yoctoproject.org/releases/bitbake")
551 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
552 os.mkdir(self.dldir + "/some2where")
553 fetcher.download()
554 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
555
556 def test_fetch_premirror(self):
557 self.d.setVar("PREMIRRORS", "http://.*/.* http://downloads.yoctoproject.org/releases/bitbake")
558 fetcher = bb.fetch.Fetch(["http://invalid.yoctoproject.org/releases/bitbake/bitbake-1.0.tar.gz"], self.d)
559 fetcher.download()
560 self.assertEqual(os.path.getsize(self.dldir + "/bitbake-1.0.tar.gz"), 57749)
561
562 def gitfetcher(self, url1, url2):
563 def checkrevision(self, fetcher):
564 fetcher.unpack(self.unpackdir)
565 revision = bb.process.run("git rev-parse HEAD", shell=True, cwd=self.unpackdir + "/git")[0].strip()
566 self.assertEqual(revision, "270a05b0b4ba0959fe0624d2a4885d7b70426da5")
567
568 self.d.setVar("BB_GENERATE_MIRROR_TARBALLS", "1")
569 self.d.setVar("SRCREV", "270a05b0b4ba0959fe0624d2a4885d7b70426da5")
570 fetcher = bb.fetch.Fetch([url1], self.d)
571 fetcher.download()
572 checkrevision(self, fetcher)
573 # Wipe out the dldir clone and the unpacked source, turn off the network and check mirror tarball works
574 bb.utils.prunedir(self.dldir + "/git2/")
575 bb.utils.prunedir(self.unpackdir)
576 self.d.setVar("BB_NO_NETWORK", "1")
577 fetcher = bb.fetch.Fetch([url2], self.d)
578 fetcher.download()
579 checkrevision(self, fetcher)
580
581 def test_gitfetch(self):
582 url1 = url2 = "git://git.openembedded.org/bitbake"
583 self.gitfetcher(url1, url2)
584
585 def test_gitfetch_goodsrcrev(self):
586 # SRCREV is set but matches rev= parameter
587 url1 = url2 = "git://git.openembedded.org/bitbake;rev=270a05b0b4ba0959fe0624d2a4885d7b70426da5"
588 self.gitfetcher(url1, url2)
589
590 def test_gitfetch_badsrcrev(self):
591 # SRCREV is set but does not match rev= parameter
592 url1 = url2 = "git://git.openembedded.org/bitbake;rev=dead05b0b4ba0959fe0624d2a4885d7b70426da5"
593 self.assertRaises(bb.fetch.FetchError, self.gitfetcher, url1, url2)
594
595 def test_gitfetch_tagandrev(self):
596 # SRCREV is set but does not match rev= parameter
597 url1 = url2 = "git://git.openembedded.org/bitbake;rev=270a05b0b4ba0959fe0624d2a4885d7b70426da5;tag=270a05b0b4ba0959fe0624d2a4885d7b70426da5"
598 self.assertRaises(bb.fetch.FetchError, self.gitfetcher, url1, url2)
599
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600600 def test_gitfetch_localusehead(self):
601 # Create dummy local Git repo
602 src_dir = tempfile.mkdtemp(dir=self.tempdir,
603 prefix='gitfetch_localusehead_')
604 src_dir = os.path.abspath(src_dir)
605 bb.process.run("git init", cwd=src_dir)
606 bb.process.run("git commit --allow-empty -m'Dummy commit'",
607 cwd=src_dir)
608 # Use other branch than master
609 bb.process.run("git checkout -b my-devel", cwd=src_dir)
610 bb.process.run("git commit --allow-empty -m'Dummy commit 2'",
611 cwd=src_dir)
612 stdout = bb.process.run("git rev-parse HEAD", cwd=src_dir)
613 orig_rev = stdout[0].strip()
614
615 # Fetch and check revision
616 self.d.setVar("SRCREV", "AUTOINC")
617 url = "git://" + src_dir + ";protocol=file;usehead=1"
618 fetcher = bb.fetch.Fetch([url], self.d)
619 fetcher.download()
620 fetcher.unpack(self.unpackdir)
621 stdout = bb.process.run("git rev-parse HEAD",
622 cwd=os.path.join(self.unpackdir, 'git'))
623 unpack_rev = stdout[0].strip()
624 self.assertEqual(orig_rev, unpack_rev)
625
626 def test_gitfetch_remoteusehead(self):
627 url = "git://git.openembedded.org/bitbake;usehead=1"
628 self.assertRaises(bb.fetch.ParameterError, self.gitfetcher, url, url)
629
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500630 def test_gitfetch_premirror(self):
631 url1 = "git://git.openembedded.org/bitbake"
632 url2 = "git://someserver.org/bitbake"
633 self.d.setVar("PREMIRRORS", "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n")
634 self.gitfetcher(url1, url2)
635
636 def test_gitfetch_premirror2(self):
637 url1 = url2 = "git://someserver.org/bitbake"
638 self.d.setVar("PREMIRRORS", "git://someserver.org/bitbake git://git.openembedded.org/bitbake \n")
639 self.gitfetcher(url1, url2)
640
641 def test_gitfetch_premirror3(self):
642 realurl = "git://git.openembedded.org/bitbake"
643 dummyurl = "git://someserver.org/bitbake"
644 self.sourcedir = self.unpackdir.replace("unpacked", "sourcemirror.git")
645 os.chdir(self.tempdir)
646 bb.process.run("git clone %s %s 2> /dev/null" % (realurl, self.sourcedir), shell=True)
647 self.d.setVar("PREMIRRORS", "%s git://%s;protocol=file \n" % (dummyurl, self.sourcedir))
648 self.gitfetcher(dummyurl, dummyurl)
649
650 def test_git_submodule(self):
651 fetcher = bb.fetch.Fetch(["gitsm://git.yoctoproject.org/git-submodule-test;rev=f12e57f2edf0aa534cf1616fa983d165a92b0842"], self.d)
652 fetcher.download()
653 # Previous cwd has been deleted
654 os.chdir(os.path.dirname(self.unpackdir))
655 fetcher.unpack(self.unpackdir)
656
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500657
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500658class TrustedNetworksTest(FetcherTest):
659 def test_trusted_network(self):
660 # Ensure trusted_network returns False when the host IS in the list.
661 url = "git://Someserver.org/foo;rev=1"
662 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org someserver.org server2.org server3.org")
663 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500664
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500665 def test_wild_trusted_network(self):
666 # Ensure trusted_network returns true when the *.host IS in the list.
667 url = "git://Someserver.org/foo;rev=1"
668 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
669 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500670
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500671 def test_prefix_wild_trusted_network(self):
672 # Ensure trusted_network returns true when the prefix matches *.host.
673 url = "git://git.Someserver.org/foo;rev=1"
674 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
675 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500676
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500677 def test_two_prefix_wild_trusted_network(self):
678 # Ensure trusted_network returns true when the prefix matches *.host.
679 url = "git://something.git.Someserver.org/foo;rev=1"
680 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org *.someserver.org server2.org server3.org")
681 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500682
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500683 def test_port_trusted_network(self):
684 # Ensure trusted_network returns True, even if the url specifies a port.
685 url = "git://someserver.org:8080/foo;rev=1"
686 self.d.setVar("BB_ALLOWED_NETWORKS", "someserver.org")
687 self.assertTrue(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500688
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500689 def test_untrusted_network(self):
690 # Ensure trusted_network returns False when the host is NOT in the list.
691 url = "git://someserver.org/foo;rev=1"
692 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org server2.org server3.org")
693 self.assertFalse(bb.fetch.trusted_network(self.d, url))
694
695 def test_wild_untrusted_network(self):
696 # Ensure trusted_network returns False when the host is NOT in the list.
697 url = "git://*.someserver.org/foo;rev=1"
698 self.d.setVar("BB_ALLOWED_NETWORKS", "server1.org server2.org server3.org")
699 self.assertFalse(bb.fetch.trusted_network(self.d, url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500700
701class URLHandle(unittest.TestCase):
702
703 datatable = {
704 "http://www.google.com/index.html" : ('http', 'www.google.com', '/index.html', '', '', {}),
705 "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : ('cvs', 'cvs.handhelds.org', '/cvs', 'anoncvs', '', {'module': 'familiar/dist/ipkg'}),
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600706 "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg" : ('cvs', 'cvs.handhelds.org', '/cvs', 'anoncvs', 'anonymous', collections.OrderedDict([('tag', 'V0-99-81'), ('module', 'familiar/dist/ipkg')])),
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500707 "git://git.openembedded.org/bitbake;branch=@foo" : ('git', 'git.openembedded.org', '/bitbake', '', '', {'branch': '@foo'}),
708 "file://somelocation;someparam=1": ('file', '', 'somelocation', '', '', {'someparam': '1'}),
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500709 }
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500710 # we require a pathname to encodeurl but users can still pass such urls to
711 # decodeurl and we need to handle them
712 decodedata = datatable.copy()
713 decodedata.update({
714 "http://somesite.net;someparam=1": ('http', 'somesite.net', '', '', '', {'someparam': '1'}),
715 })
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500716
717 def test_decodeurl(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500718 for k, v in self.decodedata.items():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500719 result = bb.fetch.decodeurl(k)
720 self.assertEqual(result, v)
721
722 def test_encodeurl(self):
723 for k, v in self.datatable.items():
724 result = bb.fetch.encodeurl(v)
725 self.assertEqual(result, k)
726
727class FetchLatestVersionTest(FetcherTest):
728
729 test_git_uris = {
730 # version pattern "X.Y.Z"
731 ("mx-1.0", "git://github.com/clutter-project/mx.git;branch=mx-1.4", "9b1db6b8060bd00b121a692f942404a24ae2960f", "")
732 : "1.99.4",
733 # version pattern "vX.Y"
734 ("mtd-utils", "git://git.infradead.org/mtd-utils.git", "ca39eb1d98e736109c64ff9c1aa2a6ecca222d8f", "")
735 : "1.5.0",
736 # version pattern "pkg_name-X.Y"
737 ("presentproto", "git://anongit.freedesktop.org/git/xorg/proto/presentproto", "24f3a56e541b0a9e6c6ee76081f441221a120ef9", "")
738 : "1.0",
739 # version pattern "pkg_name-vX.Y.Z"
740 ("dtc", "git://git.qemu.org/dtc.git", "65cc4d2748a2c2e6f27f1cf39e07a5dbabd80ebf", "")
741 : "1.4.0",
742 # combination version pattern
743 ("sysprof", "git://git.gnome.org/sysprof", "cd44ee6644c3641507fb53b8a2a69137f2971219", "")
744 : "1.2.0",
745 ("u-boot-mkimage", "git://git.denx.de/u-boot.git;branch=master;protocol=git", "62c175fbb8a0f9a926c88294ea9f7e88eb898f6c", "")
746 : "2014.01",
747 # version pattern "yyyymmdd"
748 ("mobile-broadband-provider-info", "git://git.gnome.org/mobile-broadband-provider-info", "4ed19e11c2975105b71b956440acdb25d46a347d", "")
749 : "20120614",
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500750 # packages with a valid UPSTREAM_CHECK_GITTAGREGEX
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500751 ("xf86-video-omap", "git://anongit.freedesktop.org/xorg/driver/xf86-video-omap", "ae0394e687f1a77e966cf72f895da91840dffb8f", "(?P<pver>(\d+\.(\d\.?)*))")
752 : "0.4.3",
753 ("build-appliance-image", "git://git.yoctoproject.org/poky", "b37dd451a52622d5b570183a81583cc34c2ff555", "(?P<pver>(([0-9][\.|_]?)+[0-9]))")
754 : "11.0.0",
755 ("chkconfig-alternatives-native", "git://github.com/kergoth/chkconfig;branch=sysroot", "cd437ecbd8986c894442f8fce1e0061e20f04dee", "chkconfig\-(?P<pver>((\d+[\.\-_]*)+))")
756 : "1.3.59",
757 ("remake", "git://github.com/rocky/remake.git", "f05508e521987c8494c92d9c2871aec46307d51d", "(?P<pver>(\d+\.(\d+\.)*\d*(\+dbg\d+(\.\d+)*)*))")
758 : "3.82+dbg0.9",
759 }
760
761 test_wget_uris = {
762 # packages with versions inside directory name
763 ("util-linux", "http://kernel.org/pub/linux/utils/util-linux/v2.23/util-linux-2.24.2.tar.bz2", "", "")
764 : "2.24.2",
765 ("enchant", "http://www.abisource.com/downloads/enchant/1.6.0/enchant-1.6.0.tar.gz", "", "")
766 : "1.6.0",
767 ("cmake", "http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz", "", "")
768 : "2.8.12.1",
769 # packages with versions only in current directory
770 ("eglic", "http://downloads.yoctoproject.org/releases/eglibc/eglibc-2.18-svnr23787.tar.bz2", "", "")
771 : "2.19",
772 ("gnu-config", "http://downloads.yoctoproject.org/releases/gnu-config/gnu-config-20120814.tar.bz2", "", "")
773 : "20120814",
774 # packages with "99" in the name of possible version
775 ("pulseaudio", "http://freedesktop.org/software/pulseaudio/releases/pulseaudio-4.0.tar.xz", "", "")
776 : "5.0",
777 ("xserver-xorg", "http://xorg.freedesktop.org/releases/individual/xserver/xorg-server-1.15.1.tar.bz2", "", "")
778 : "1.15.1",
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500779 # packages with valid UPSTREAM_CHECK_URI and UPSTREAM_CHECK_REGEX
780 ("cups", "http://www.cups.org/software/1.7.2/cups-1.7.2-source.tar.bz2", "https://github.com/apple/cups/releases", "(?P<name>cups\-)(?P<pver>((\d+[\.\-_]*)+))\-source\.tar\.gz")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500781 : "2.0.0",
782 ("db", "http://download.oracle.com/berkeley-db/db-5.3.21.tar.gz", "http://www.oracle.com/technetwork/products/berkeleydb/downloads/index-082944.html", "http://download.oracle.com/otn/berkeley-db/(?P<name>db-)(?P<pver>((\d+[\.\-_]*)+))\.tar\.gz")
783 : "6.1.19",
784 }
785 if os.environ.get("BB_SKIP_NETTESTS") == "yes":
786 print("Unset BB_SKIP_NETTESTS to run network tests")
787 else:
788 def test_git_latest_versionstring(self):
789 for k, v in self.test_git_uris.items():
790 self.d.setVar("PN", k[0])
791 self.d.setVar("SRCREV", k[2])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500792 self.d.setVar("UPSTREAM_CHECK_GITTAGREGEX", k[3])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500793 ud = bb.fetch2.FetchData(k[1], self.d)
794 pupver= ud.method.latest_versionstring(ud, self.d)
795 verstring = pupver[0]
796 r = bb.utils.vercmp_string(v, verstring)
797 self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
798
799 def test_wget_latest_versionstring(self):
800 for k, v in self.test_wget_uris.items():
801 self.d.setVar("PN", k[0])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500802 self.d.setVar("UPSTREAM_CHECK_URI", k[2])
803 self.d.setVar("UPSTREAM_CHECK_REGEX", k[3])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500804 ud = bb.fetch2.FetchData(k[1], self.d)
805 pupver = ud.method.latest_versionstring(ud, self.d)
806 verstring = pupver[0]
807 r = bb.utils.vercmp_string(v, verstring)
808 self.assertTrue(r == -1 or r == 0, msg="Package %s, version: %s <= %s" % (k[0], v, verstring))
809
810
811class FetchCheckStatusTest(FetcherTest):
812 test_wget_uris = ["http://www.cups.org/software/1.7.2/cups-1.7.2-source.tar.bz2",
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500813 "http://www.cups.org/",
814 "http://downloads.yoctoproject.org/releases/sato/sato-engine-0.1.tar.gz",
815 "http://downloads.yoctoproject.org/releases/sato/sato-engine-0.2.tar.gz",
816 "http://downloads.yoctoproject.org/releases/sato/sato-engine-0.3.tar.gz",
817 "https://yoctoproject.org/",
818 "https://yoctoproject.org/documentation",
819 "http://downloads.yoctoproject.org/releases/opkg/opkg-0.1.7.tar.gz",
820 "http://downloads.yoctoproject.org/releases/opkg/opkg-0.3.0.tar.gz",
821 "ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.60.tar.gz",
822 "ftp://ftp.gnu.org/gnu/chess/gnuchess-5.08.tar.gz",
823 "ftp://ftp.gnu.org/gnu/gmp/gmp-4.0.tar.gz",
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500824 # GitHub releases are hosted on Amazon S3, which doesn't support HEAD
825 "https://github.com/kergoth/tslib/releases/download/1.1/tslib-1.1.tar.xz"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500826 ]
827
828 if os.environ.get("BB_SKIP_NETTESTS") == "yes":
829 print("Unset BB_SKIP_NETTESTS to run network tests")
830 else:
831
832 def test_wget_checkstatus(self):
833 fetch = bb.fetch2.Fetch(self.test_wget_uris, self.d)
834 for u in self.test_wget_uris:
835 ud = fetch.ud[u]
836 m = ud.method
837 ret = m.checkstatus(fetch, ud, self.d)
838 self.assertTrue(ret, msg="URI %s, can't check status" % (u))
839
840
841 def test_wget_checkstatus_connection_cache(self):
842 from bb.fetch2 import FetchConnectionCache
843
844 connection_cache = FetchConnectionCache()
845 fetch = bb.fetch2.Fetch(self.test_wget_uris, self.d,
846 connection_cache = connection_cache)
847
848 for u in self.test_wget_uris:
849 ud = fetch.ud[u]
850 m = ud.method
851 ret = m.checkstatus(fetch, ud, self.d)
852 self.assertTrue(ret, msg="URI %s, can't check status" % (u))
853
854 connection_cache.close_connections()