1#!/usr/bin/env python3
2
3from sys import exit
4from misc.metalinkv3_xml import Metalinkv3_XML
5
6"""
7    This is to test if Metalink/XML forbids the home path and names
8    beginning with the ~ (tilde) character.
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
19############# File Definitions ###############################################
20wrong_file = "Ouch!"
21
22File1 = "Would you like some Tea?"
23File1_lowPref = "Do not take this"
24
25File2 = "This is gonna be good"
26File2_lowPref = "Not this one too"
27
28File3 = "A little more, please"
29File3_lowPref = "That's just too much"
30
31File4 = "Maybe a biscuit?"
32File4_lowPref = "No, thanks"
33
34File5 = "More Tea...?"
35File5_lowPref = "I have to go..."
36
37############# Metalink/XML ###################################################
38Meta = Metalinkv3_XML()
39
40# file_name: metalink:file "name" field
41# save_name: metalink:file save name, if None the file is rejected
42# content  : metalink:file content
43#
44# size:
45#   True     auto-compute size
46#   None     no <size></size>
47#    any     use this size
48#
49# hash_sha256:
50#   False    no <verification></verification>
51#   True     auto-compute sha256
52#   None     no <hash></hash>
53#    any     use this hash
54#
55# srv_file   : metalink:url server file
56# srv_content: metalink:url server file content, if None the file doesn't exist
57# utype      : metalink:url type
58# location   : metalink:url location (default 'no location field')
59# preference : metalink:url preference (default 999999)
60
61XmlName = "test.metalink"
62
63Meta.xml (
64    # Metalink/XML file name
65    XmlName,
66    # file_name, save_name, content, size, hash_sha256
67    ["~File1", None, File1, None, True,
68     # srv_file, srv_content, utype, location, preference
69     ["wrong_file", wrong_file, "http", None, 35],
70     ["404", None, "http", None, 40],
71     ["File1_lowPref", File1_lowPref, "http", None, 25],
72     ["File1", File1, "http", None, 30]],
73    ["~/File2", None, File2, None, True,
74     ["wrong_file", wrong_file, "http", None, 35],
75     ["404", None, "http", None, 40],
76     ["File2_lowPref", File2_lowPref, "http", None, 25],
77     ["File2", File2, "http", None, 30]],
78    ["dir/~File3", None, File3, None, True,
79     ["wrong_file", wrong_file, "http", None, 35],
80     ["404", None, "http", None, 40],
81     ["File3_lowPref", File3_lowPref, "http", None, 25],
82     ["File3", File3, "http", None, 30]],
83    ["dir/File4~", "dir/File4~", File4, None, True,
84     ["wrong_file", wrong_file, "http", None, 35],
85     ["404", None, "http", None, 40],
86     ["File4_lowPref", File4_lowPref, "http", None, 25],
87     ["File4", File4, "http", None, 30]],
88    ["dir/~/File5", "dir/~/File5", File5, None, True,
89     ["wrong_file", wrong_file, "http", None, 35],
90     ["404", None, "http", None, 40],
91     ["File5_lowPref", File5_lowPref, "http", None, 25],
92     ["File5", File5, "http", None, 30]],
93)
94
95Meta.print_meta ()
96
97err = Meta.http_test (
98    "--trust-server-names " + \
99    "--input-metalink " + XmlName, 0
100)
101
102exit (err)
103