1 /*
2  * Copyright (c) 2018-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 
21 #include <config.h>
22 
23 #include <stdlib.h> // exit()
24 #include "libtest.h"
25 
main(void)26 int main(void)
27 {
28 	wget_test_url_t urls[]={
29 		{	.name = "/index.html",
30 			.code = "200 Dontcare",
31 			.body =
32 				"<html><head><title>Main Page</title></head><body><p>A link to a" \
33 				" <A hreF=\"http://localhost:{{port}}/secondpage.html\">second page</a>." \
34 				" <a href=\"picture_a.jpeg\">Picture a</a>." \
35 				" <a href=\"picture_aa.jpeg\">Picture aa</a>." \
36 				"</p></body></html>",
37 			.headers = {
38 				"Content-Type: text/html",
39 			}
40 		},
41 		{	.name = "/secondpage.html",
42 			.code = "200 Dontcare",
43 			.body =
44 				"<html><head><title>Second Page</title></head><body><p>A link to a" \
45 				" <a href=\"picture_b.jpeg\">Picture b</a>." \
46 				" <a href=\"picture_bb.JpeG\">Picture bb</a>." \
47 				" <a href=\"picture_c.png\">Picture c</a>." \
48 				"</p></body></html>",
49 			.headers = {
50 				"Content-Type: text/html",
51 			}
52 		},
53 		{	.name = "/picture_a.jpeg",
54 			.code = "200 Dontcare",
55 			.body = "don't care",
56 			.headers = { "Content-Type: image/jpeg" }
57 		},
58 		{	.name = "/picture_aa.jpeg",
59 			.code = "200 Dontcare",
60 			.body = "don't care",
61 			.headers = { "Content-Type: image/jpeg" }
62 		},
63 		{	.name = "/picture_b.jpeg",
64 			.code = "200 Dontcare",
65 			.body = "don't care",
66 			.headers = { "Content-Type: image/jpeg" }
67 		},
68 		{	.name = "/picture_bb.JpeG",
69 			.code = "200 Dontcare",
70 			.body = "don't care",
71 			.headers = { "Content-Type: image/jpeg" }
72 		},
73 		{	.name = "/picture_c.png",
74 			.code = "200 Dontcare",
75 			.body = "don't care",
76 			.headers = { "Content-Type: image/png" }
77 		},
78                 {       .name = "/dummy.txt",
79                         .code = "200 Dontcare",
80                         .body = "What ever"
81                 }
82 	};
83 
84 	// functions won't come back if an error occurs
85 	wget_test_start_server(
86 		WGET_TEST_RESPONSE_URLS, &urls, countof(urls),
87 		WGET_TEST_FEATURE_MHD,
88 		0);
89 
90 	// only want image/png
91 	wget_test(
92 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"image/png\"",
93 		WGET_TEST_REQUEST_URL, "index.html",
94 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
95 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
96 			{ urls[6].name + 1, urls[6].body },
97 			{	NULL } },
98 		0);
99 
100 	// don't want image/png
101 	wget_test(
102 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"*,!image/png\"",
103 		WGET_TEST_REQUEST_URL, "index.html",
104 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
105 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
106 			{ urls[0].name + 1, urls[0].body },
107 			{ urls[1].name + 1, urls[1].body },
108 			{ urls[2].name + 1, urls[2].body },
109 			{ urls[3].name + 1, urls[3].body },
110 			{ urls[4].name + 1, urls[4].body },
111 			{ urls[5].name + 1, urls[5].body },
112 			{	NULL } },
113 		0);
114 
115 	// only want png using wildcards
116 	wget_test(
117 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"*/png\"",
118 		WGET_TEST_REQUEST_URL, "index.html",
119 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
120 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
121 			{ urls[6].name + 1, urls[6].body },
122 			{	NULL } },
123 		0);
124 
125 	// don't want png using wildcards
126 	wget_test(
127 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"*,!*/png\"",
128 		WGET_TEST_REQUEST_URL, "index.html",
129 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
130 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
131 			{ urls[0].name + 1, urls[0].body },
132 			{ urls[1].name + 1, urls[1].body },
133 			{ urls[2].name + 1, urls[2].body },
134 			{ urls[3].name + 1, urls[3].body },
135 			{ urls[4].name + 1, urls[4].body },
136 			{ urls[5].name + 1, urls[5].body },
137 			{	NULL } },
138 		0);
139 
140 	// only want png and jpeg using wildcards
141 	wget_test(
142 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"*/png,*/jpeg\"",
143 		WGET_TEST_REQUEST_URL, "index.html",
144 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
145 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
146 			{ urls[2].name + 1, urls[2].body },
147 			{ urls[3].name + 1, urls[3].body },
148 			{ urls[4].name + 1, urls[4].body },
149 			{ urls[5].name + 1, urls[5].body },
150 			{ urls[6].name + 1, urls[6].body },
151 			{	NULL } },
152 		0);
153 
154 	// don't want png but want jpeg using wildcards
155 	wget_test(
156 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"!*/png,*/jpeg\"",
157 		WGET_TEST_REQUEST_URL, "index.html",
158 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
159 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
160 			{ urls[2].name + 1, urls[2].body },
161 			{ urls[3].name + 1, urls[3].body },
162 			{ urls[4].name + 1, urls[4].body },
163 			{ urls[5].name + 1, urls[5].body },
164 			{	NULL } },
165 		0);
166 
167 	// only want png using wildcards
168 	wget_test(
169 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"image/*,!*/jpeg\"",
170 		WGET_TEST_REQUEST_URL, "index.html",
171 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
172 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
173 			{ urls[6].name + 1, urls[6].body },
174 			{	NULL } },
175 		0);
176 
177 	// simple --filter-mime-type tests
178 	wget_test(
179 		WGET_TEST_OPTIONS, "--filter-mime-type text/html",
180 		WGET_TEST_REQUEST_URL, "index.html",
181 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
182 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
183 			{ urls[0].name + 1, urls[0].body },
184 			{	NULL } },
185 		0);
186 
187 	wget_test(
188 		WGET_TEST_OPTIONS, "--filter-mime-type \"*,!text/html\"",
189 		WGET_TEST_REQUEST_URL, "index.html",
190 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
191 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
192 			{	NULL } },
193 		0);
194 
195 	// As dummy.txt hasn't MIME type is considered to be 'application/octet-stream' (RFC 7231, sec. 3.1.1.5)
196 	wget_test(
197 		WGET_TEST_OPTIONS, "--filter-mime-type \"*,!text/plain\"",
198 		WGET_TEST_REQUEST_URL, "dummy.txt",
199 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
200 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
201 			{ urls[7].name + 1, urls[7].body },
202 			{	NULL } },
203 		0);
204 
205 	wget_test(
206 		WGET_TEST_OPTIONS, "--filter-mime-type \"text/plain\"",
207 		WGET_TEST_REQUEST_URL, "dummy.txt",
208 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
209 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
210 			{	NULL } },
211 		0);
212 
213 	// tests with -N --no-if-modified-since
214 	int n_urls = countof(urls);
215 	for (int i = 0; i < n_urls; i++) {
216 		urls[i].headers[1] = "Last-Modified: Sat, 09 Oct 2004 08:30:00 GMT";
217 	}
218 	wget_test(
219 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"image/*,!*/jpeg\" -N --no-if-modified-since",
220 		WGET_TEST_REQUEST_URL, "index.html",
221 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
222 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
223 			{ urls[6].name + 1, urls[6].body, 1097310600 },
224 			{	NULL } },
225 		0);
226 
227 	wget_test(
228 		WGET_TEST_OPTIONS, "-r -nH --filter-mime-type \"image/*,!*/jpeg\" -N --no-if-modified-since",
229 		WGET_TEST_REQUEST_URL, "index.html",
230 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
231 		WGET_TEST_EXISTING_FILES, &(wget_test_file_t []) {
232 			{ urls[0].name + 1, urls[0].body, 1097310900 },
233 			{	NULL } },
234 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
235 			{ urls[0].name + 1, urls[0].body, 1097310900 },
236 			{ urls[6].name + 1, urls[6].body, 1097310600 },
237 			{	NULL } },
238 		0);
239 
240 	exit(EXIT_SUCCESS);
241 }
242