1 /*
2 * Copyright (c) 2020-2021 Free Software Foundation, Inc.
3 *
4 * This file is part of Wget
5 *
6 * Wget is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Wget 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 Wget If not, see <https://www.gnu.org/licenses/>.
18 *
19 *
20 * Testing the HTML5 download tag and --download-attr
21 */
22
23 #include <config.h>
24
25 #include <stdlib.h> // exit()
26 #include <string.h> // strlen()
27 #include "libtest.h"
28
main(void)29 int main(void)
30 {
31 wget_test_url_t urls[]={
32 { .name = "/page1.html",
33 .code = "200 Dontcare",
34 .body =
35 "<html><head></head>\n"\
36 "<body>\n"\
37 " <a href=\"image1\" download>image 1</a>." \
38 " <a href=\"image2\" download=\"/tmp/sleepingcat.png\">image 2</a>." \
39 " <a href=\"image3\">image 3</a>." \
40 " <img href=\"image4\" download>image 4</img>." \
41 " <img href=\"image5\" download=\"sleepingcat2.png\">image 5</img>." \
42 " <img href=\"image6\">image 6</img>." \
43 " <a href=\"subdir/image1\" download>image 1</a>." \
44 " <a href=\"subdir/image2\" download=\"sleepingcat.png\">image 2</a>." \
45 " <a href=\"subdir/image3\">image 3</a>." \
46 "</body>\n"\
47 "</html>\n",
48 .headers = {
49 "Content-Type: text/html",
50 }
51 },
52 { .name = "/image1",
53 .code = "200 Dontcare",
54 .body = "Image 1",
55 .headers = {
56 "Content-Type: image/png",
57 }
58 },
59 { .name = "/image2",
60 .code = "200 Dontcare",
61 .body = "Image 2",
62 .headers = {
63 "Content-Type: image/png",
64 }
65 },
66 { .name = "/image3",
67 .code = "200 Dontcare",
68 .body = "Image 3",
69 .headers = {
70 "Content-Type: image/png",
71 }
72 },
73 { .name = "/image4",
74 .code = "200 Dontcare",
75 .body = "Image 4",
76 .headers = {
77 "Content-Type: image/png",
78 }
79 },
80 { .name = "/image5",
81 .code = "200 Dontcare",
82 .body = "Image 5",
83 .headers = {
84 "Content-Type: image/png",
85 }
86 },
87 { .name = "/image6",
88 .code = "200 Dontcare",
89 .body = "Image 6",
90 .headers = {
91 "Content-Type: image/png",
92 }
93 },
94 { .name = "/subdir/image1",
95 .code = "200 Dontcare",
96 .body = "Image 7",
97 .headers = {
98 "Content-Type: image/png",
99 }
100 },
101 { .name = "/subdir/image2",
102 .code = "200 Dontcare",
103 .body = "Image 8",
104 .headers = {
105 "Content-Type: image/png",
106 }
107 },
108 { .name = "/subdir/image3",
109 .code = "200 Dontcare",
110 .body = "Image 9",
111 .headers = {
112 "Content-Type: image/png",
113 }
114 },
115 };
116
117 // functions won't come back if an error occurs
118 wget_test_start_server(
119 WGET_TEST_RESPONSE_URLS, &urls, countof(urls),
120 WGET_TEST_FEATURE_MHD,
121 0);
122
123 // Download everything as named in href attribute
124 wget_test(
125 WGET_TEST_OPTIONS, "-nH --no-robots -r",
126 WGET_TEST_REQUEST_URL, "page1.html",
127 WGET_TEST_EXPECTED_ERROR_CODE, 0,
128 WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
129 { urls[0].name + 1, urls[0].body },
130 { urls[1].name + 1, urls[1].body },
131 { urls[2].name + 1, urls[2].body },
132 { urls[3].name + 1, urls[3].body },
133 { urls[4].name + 1, urls[4].body },
134 { urls[5].name + 1, urls[5].body },
135 { urls[6].name + 1, urls[6].body },
136 { urls[7].name + 1, urls[7].body },
137 { urls[8].name + 1, urls[8].body },
138 { urls[9].name + 1, urls[9].body },
139 { NULL } },
140 0);
141
142 // --download-attr: File names amended by download attribute (only in <a> and <area> tags)
143 // check if default is working
144 wget_test(
145 WGET_TEST_OPTIONS, "-nH --no-robots -r --download-attr",
146 WGET_TEST_REQUEST_URL, "page1.html",
147 WGET_TEST_EXPECTED_ERROR_CODE, 0,
148 WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
149 { urls[0].name + 1, urls[0].body },
150 { urls[1].name + 1, urls[1].body },
151 { "sleepingcat.png", urls[2].body },
152 { urls[3].name + 1, urls[3].body },
153 { urls[4].name + 1, urls[4].body },
154 { urls[5].name + 1, urls[5].body },
155 { urls[6].name + 1, urls[6].body },
156 { urls[7].name + 1, urls[7].body },
157 { "subdir/sleepingcat.png", urls[8].body },
158 { urls[9].name + 1, urls[9].body },
159 { NULL } },
160 0);
161
162 // --download-attr=strippath: File names amended by download attribute (only in <a> and <area> tags)
163 // check if 'strippath' default is working
164 wget_test(
165 WGET_TEST_OPTIONS, "-nH --no-robots -r --download-attr=strippath",
166 WGET_TEST_REQUEST_URL, "page1.html",
167 WGET_TEST_EXPECTED_ERROR_CODE, 0,
168 WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
169 { urls[0].name + 1, urls[0].body },
170 { urls[1].name + 1, urls[1].body },
171 { "sleepingcat.png", urls[2].body },
172 { urls[3].name + 1, urls[3].body },
173 { urls[4].name + 1, urls[4].body },
174 { urls[5].name + 1, urls[5].body },
175 { urls[6].name + 1, urls[6].body },
176 { urls[7].name + 1, urls[7].body },
177 { "subdir/sleepingcat.png", urls[8].body },
178 { urls[9].name + 1, urls[9].body },
179 { NULL } },
180 0);
181
182 // --download-attr=strippath: File names amended by download attribute (only in <a> and <area> tags)
183 // check if 'usepath' default is working with paths
184 wget_test(
185 WGET_TEST_OPTIONS, "-nH --no-robots -r --download-attr=usepath",
186 WGET_TEST_REQUEST_URL, "page1.html",
187 WGET_TEST_EXPECTED_ERROR_CODE, 0,
188 WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
189 { urls[0].name + 1, urls[0].body },
190 { urls[1].name + 1, urls[1].body },
191 { "/tmp/sleepingcat.png", urls[2].body },
192 { urls[3].name + 1, urls[3].body },
193 { urls[4].name + 1, urls[4].body },
194 { urls[5].name + 1, urls[5].body },
195 { urls[6].name + 1, urls[6].body },
196 { urls[7].name + 1, urls[7].body },
197 { "subdir/sleepingcat.png", urls[8].body },
198 { urls[9].name + 1, urls[9].body },
199 { NULL } },
200 0);
201
202 exit(EXIT_SUCCESS);
203 }
204