1 /*
2  * Copyright (c) 2017-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 recursive downloads with partial content
21  *
22  */
23 
24 #include <config.h>
25 
26 #include <stdlib.h> // exit()
27 #include <string.h> // strlen()
28 #include "libtest.h"
29 
30 static const char *mainpage = "\
31 <html>\n\
32 <head>\n\
33   <title>Main Page</title>\n\
34 </head>\n\
35 <body>\n\
36   <p>\n\
37     <a href=\"http://localhost:{{port}}/secondpage.html\">second page</a>.\n\
38     <a href=\"http://localhost:{{port}}/thirdpage.html\">third page</a>.\n\
39   </p>\n\
40 </body>\n\
41 </html>\n";
42 
43 static const char *subpage = "\
44 <html>\n\
45 <head>\n\
46   <title>Main Page</title>\n\
47 </head>\n\
48 <body>\n\
49   <p>\n\
50     Some text\n\
51   </p>\n\
52 </body>\n\
53 </html>\n";
54 
55 static const char *mainpage_partial = "\
56 <html>\n\
57 <head>\n\
58   <title>Main Page</title>\n\
59 </head>\n\
60 <body>\n\
61   <p>\n\
62     <a href=\"http://localhost:{{port}}/secondpage.html\">second page</a>.\n";
63 
main(void)64 int main(void)
65 {
66 	wget_test_url_t urls[]={
67 		{	.name = "/index.html",
68 			.code = "200 Dontcare",
69 			.body = mainpage,
70 			.headers = {
71 				"Content-Type: text/html",
72 			}
73 		},
74 		{	.name = "/secondpage.html",
75 			.code = "200 Dontcare",
76 			.body = subpage,
77 			.headers = {
78 				"Content-Type: text/html",
79 			}
80 		},
81 		{	.name = "/thirdpage.html",
82 			.code = "200 Dontcare",
83 			.body = subpage,
84 			.headers = {
85 				"Content-Type: text/html",
86 			}
87 		},
88 		{	.name = "/index_partial.html",
89 			.code = "200 Dontcare",
90 			.body = mainpage_partial,
91 			.headers = {
92 				"Content-Type: text/html",
93 			}
94 		}
95 	};
96 
97 	wget_test_start_server(
98 			WGET_TEST_RESPONSE_URLS, &urls, countof(urls),
99 			WGET_TEST_FEATURE_MHD,
100 			0);
101 
102 	// test-c-r with no existing files
103 	wget_test(
104 		WGET_TEST_OPTIONS, "--continue --recursive --no-host-directories",
105 		WGET_TEST_REQUEST_URL, "index.html",
106 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
107 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
108 			{ "index.html", urls[0].body },
109 			{ "secondpage.html", urls[1].body },
110 			{ "thirdpage.html", urls[2].body },
111 			{	NULL } },
112 		0);
113 
114 	// test-c-r with existing file
115 	wget_test(
116 		WGET_TEST_OPTIONS, "--continue --recursive --no-host-directories",
117 		WGET_TEST_REQUEST_URL, "index.html",
118 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
119 		WGET_TEST_EXISTING_FILES, &(wget_test_file_t []) {
120 			{ "index.html", urls[0].body },
121 			{	NULL } },
122 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
123 			{ "index.html", urls[0].body },
124 			{ "secondpage.html", urls[1].body },
125 			{ "thirdpage.html", urls[2].body },
126 			{	NULL } },
127 		0);
128 
129 	// test-c-r with existing partial file
130 	wget_test(
131 		WGET_TEST_OPTIONS, "--continue --recursive --no-host-directories",
132 		WGET_TEST_REQUEST_URL, "index.html",
133 		WGET_TEST_EXPECTED_ERROR_CODE, 0,
134 		WGET_TEST_EXISTING_FILES, &(wget_test_file_t []) {
135 			{ "index.html", urls[3].body },
136 			{	NULL } },
137 		WGET_TEST_EXPECTED_FILES, &(wget_test_file_t []) {
138 			{ "index.html", urls[0].body },
139 			{ "secondpage.html", urls[1].body },
140 			{ "thirdpage.html", urls[2].body },
141 			{	NULL } },
142 		0);
143 
144 	exit(EXIT_SUCCESS);
145 }
146