Debian Codegraph @

testenv/Test-metalink-xml-urlbreak.py

raw
1 #!/usr/bin/env python3
2 from sys import exit
3 from test.http_test import HTTPTest
4 from misc.wget_file import WgetFile
5 import hashlib
6
7 """
8 This is to test Metalink/XML white spaces in url resources.
9
10 With --trust-server-names, trust the metalink:file names.
11
12 Without --trust-server-names, don't trust the metalink:file names:
13 use the basename of --input-metalink, and add a sequential number
14 (e.g. .#1, .#2, etc.).
15
16 Strip the directory from unsafe paths.
17 """
18 ############# File Definitions ###############################################
19 bad = "Ouch!"
20
21 File1 = "Would you like some Tea?"
22 File1_lowPref = "Do not take this"
23 File1_sha256 = hashlib.sha256 (File1.encode ('UTF-8')).hexdigest ()
24
25 File2 = "This is gonna be good"
26 File2_lowPref = "Not this one too"
27 File2_sha256 = hashlib.sha256 (File2.encode ('UTF-8')).hexdigest ()
28
29 File3 = "A little more, please"
30 File3_lowPref = "That's just too much"
31 File3_sha256 = hashlib.sha256 (File3.encode ('UTF-8')).hexdigest ()
32
33 File4 = "Maybe a biscuit?"
34 File4_lowPref = "No, thanks"
35 File4_sha256 = hashlib.sha256 (File4.encode ('UTF-8')).hexdigest ()
36
37 File5 = "More Tea...?"
38 File5_lowPref = "I have to go..."
39 File5_sha256 = hashlib.sha256 (File5.encode ('UTF-8')).hexdigest ()
40
41 MetaXml = \
42 """<?xml version="1.0" encoding="utf-8"?>
43 <metalink version="3.0" xmlns="http://www.metalinker.org/">
44 <publisher>
45 <name>GNU Wget</name>
46 </publisher>
47 <license>
48 <name>GNU GPL</name>
49 <url>http://www.gnu.org/licenses/gpl.html</url>
50 </license>
51 <identity>Wget Test Files</identity>
52 <version>1.2.3</version>
53 <description>Wget Test Files description</description>
54 <files>
55 <file name="File1">
56 <verification>
57 <hash type="sha256">{{FILE1_HASH}}</hash>
58 </verification>
59 <resources>
60 <url type="http" preference="35">
61 http://{{SRV_HOST}}:{{SRV_PORT}}/wrong_file
62 </url>
63 <url type="http" preference="40">
64 http://{{SRV_HOST}}:{{SRV_PORT}}/404
65 </url>
66 <url type="http" preference="25">
67 http://{{SRV_HOST}}:{{SRV_PORT}}/File1_lowPref
68 </url>
69 <url type="http" preference="30">
70 http://{{SRV_HOST}}:{{SRV_PORT}}/File1
71 </url>
72 </resources>
73 </file>
74 <file name="File2">
75 <verification>
76 <hash type="sha256">{{FILE2_HASH}}</hash>
77 </verification>
78 <resources>
79 <url type="http" preference="35">
80 http://{{SRV_HOST}}:{{SRV_PORT}}/wrong_file
81 </url>
82 <url type="http" preference="40">
83 http://{{SRV_HOST}}:{{SRV_PORT}}/404
84 </url>
85 <url type="http" preference="25">
86 http://{{SRV_HOST}}:{{SRV_PORT}}/File2_lowPref
87 </url>
88 <url type="http" preference="30">
89 http://{{SRV_HOST}}:{{SRV_PORT}}/File2
90 </url>
91 </resources>
92 </file>
93 <file name="File3">
94 <verification>
95 <hash type="sha256">{{FILE3_HASH}}</hash>
96 </verification>
97 <resources>
98 <url type="http" preference="35">
99 http://{{SRV_HOST}}:{{SRV_PORT}}/wrong_file
100 </url>
101 <url type="http" preference="40">
102 http://{{SRV_HOST}}:{{SRV_PORT}}/404
103 </url>
104 <url type="http" preference="25">
105 http://{{SRV_HOST}}:{{SRV_PORT}}/File3_lowPref
106 </url>
107 <url type="http" preference="30">
108 http://{{SRV_HOST}}:{{SRV_PORT}}/File3
109 </url>
110 </resources>
111 </file>
112 <file name="File4">
113 <verification>
114 <hash type="sha256">{{FILE4_HASH}}</hash>
115 </verification>
116 <resources>
117 <url type="http" preference="35">
118 http://{{SRV_HOST}}:{{SRV_PORT}}/wrong_file
119 </url>
120 <url type="http" preference="40">
121 http://{{SRV_HOST}}:{{SRV_PORT}}/404
122 </url>
123 <url type="http" preference="25">
124 http://{{SRV_HOST}}:{{SRV_PORT}}/File4_lowPref
125 </url>
126 <url type="http" preference="30">
127 http://{{SRV_HOST}}:{{SRV_PORT}}/File4
128 </url>
129 </resources>
130 </file>
131 <file name="File5">
132 <verification>
133 <hash type="sha256">{{FILE5_HASH}}</hash>
134 </verification>
135 <resources>
136 <url type="http" preference="35">
137 http://{{SRV_HOST}}:{{SRV_PORT}}/wrong_file
138 </url>
139 <url type="http" preference="40">
140 http://{{SRV_HOST}}:{{SRV_PORT}}/404
141 </url>
142 <url type="http" preference="25">
143 http://{{SRV_HOST}}:{{SRV_PORT}}/File5_lowPref
144 </url>
145 <url type="http" preference="30">
146 http://{{SRV_HOST}}:{{SRV_PORT}}/File5
147 </url>
148 </resources>
149 </file>
150 </files>
151 </metalink>
152 """
153
154 wrong_file = WgetFile ("wrong_file", bad)
155
156 File1_orig = WgetFile ("File1", File1)
157 File1_down = WgetFile ("test.metalink.#1", File1)
158 File1_nono = WgetFile ("File1_lowPref", File1_lowPref)
159
160 File2_orig = WgetFile ("File2", File2)
161 File2_down = WgetFile ("test.metalink.#2", File2)
162 File2_nono = WgetFile ("File2_lowPref", File2_lowPref)
163
164 File3_orig = WgetFile ("File3", File3)
165 File3_down = WgetFile ("test.metalink.#3", File3)
166 File3_nono = WgetFile ("File3_lowPref", File3_lowPref)
167
168 File4_orig = WgetFile ("File4", File4)
169 File4_down = WgetFile ("test.metalink.#4", File4)
170 File4_nono = WgetFile ("File4_lowPref", File4_lowPref)
171
172 File5_orig = WgetFile ("File5", File5)
173 File5_down = WgetFile ("test.metalink.#5", File5)
174 File5_nono = WgetFile ("File5_lowPref", File5_lowPref)
175
176 MetaFile = WgetFile ("test.metalink", MetaXml)
177
178 WGET_OPTIONS = "--input-metalink test.metalink"
179 WGET_URLS = [[]]
180
181 Files = [[
182 wrong_file,
183 File1_orig, File1_nono,
184 File2_orig, File2_nono,
185 File3_orig, File3_nono,
186 File4_orig, File4_nono,
187 File5_orig, File5_nono
188 ]]
189 Existing_Files = [MetaFile]
190
191 ExpectedReturnCode = 0
192 ExpectedDownloadedFiles = [
193 File1_down,
194 File2_down,
195 File3_down,
196 File4_down,
197 File5_down,
198 MetaFile
199 ]
200
201 ################ Pre and Post Test Hooks #####################################
202 pre_test = {
203 "ServerFiles" : Files,
204 "LocalFiles" : Existing_Files
205 }
206 test_options = {
207 "WgetCommands" : WGET_OPTIONS,
208 "Urls" : WGET_URLS
209 }
210 post_test = {
211 "ExpectedFiles" : ExpectedDownloadedFiles,
212 "ExpectedRetcode" : ExpectedReturnCode
213 }
214
215 http_test = HTTPTest (
216 pre_hook=pre_test,
217 test_params=test_options,
218 post_hook=post_test,
219 )
220
221 http_test.server_setup()
222 ### Get and use dynamic server sockname
223 srv_host, srv_port = http_test.servers[0].server_inst.socket.getsockname ()
224
225 MetaXml = MetaXml.replace('{{FILE1_HASH}}', File1_sha256)
226 MetaXml = MetaXml.replace('{{FILE2_HASH}}', File2_sha256)
227 MetaXml = MetaXml.replace('{{FILE3_HASH}}', File3_sha256)
228 MetaXml = MetaXml.replace('{{FILE4_HASH}}', File4_sha256)
229 MetaXml = MetaXml.replace('{{FILE5_HASH}}', File5_sha256)
230 MetaXml = MetaXml.replace('{{SRV_HOST}}', srv_host)
231 MetaXml = MetaXml.replace('{{SRV_PORT}}', str (srv_port))
232 MetaFile.content = MetaXml
233
234 err = http_test.begin ()
235
236 exit (err)