File: test-parse-html.c

package info (click to toggle)
wget2 2.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,468 kB
  • sloc: ansic: 121,166; sh: 11,559; makefile: 878; xml: 182; sed: 16
file content (210 lines) | stat: -rw-r--r-- 5,405 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright (c) 2012 Tim Ruehsen
 * Copyright (c) 2015-2024 Free Software Foundation, Inc.
 *
 * This file is part of Wget.
 *
 * Wget is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Wget is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Wget.  If not, see <https://www.gnu.org/licenses/>.
 *
 *
 * test routines
 *
 * Changelog
 * 16.08.2013  Tim Ruehsen  created
 *
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <c-ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif

#include <wget.h>
#include "../libwget/private.h"
#include "../src/wget_options.h"

static int
	ok,
	failed;

static const char *test_data[] ={
	"<!doctype html>\n"\
	"<!--[if lt IE 7 ]> <html class=\"no-js ie6\" lang=\"en\"> <![endif]-->\n"\
	"<!--[if (gte IE 9)|!(IE)]><!--> <html class=\"no-js\"> <!--<![endif]-->\n"\
	"<head>\n"\
   "  <meta charset=\"utf-8\">\n"\
	"  <script>window.jQuery || document.write(\"<script src='/common/inc/js/jquery-1.5.1.min.js'><\\/script>\")</script>\n"\
	"  <script src=\"/common/inc/js/core.js?v=1\"></script>\n"\
	"</head>\n"\
	"<body>\n"\
	"  <ul>\n"\
	"    <li><a href=\"/solutions/platform.html#server\">Server</a></li>\n"\
	"    <li><a href=\"/solutions/platform.html#desktop\">Desktop</a></li>\n"\
	"  </ul>\n"\
	"</body>\n"\
	"</html>\n"
};

static void html_dump(void *user_ctx, int flags, const char *dir WGET_GCC_UNUSED, const char *attr, const char *val, size_t len, size_t pos)
{
//	info_printf("\n%02X %s %s '%.*s' %zd %zd\n", flags, dir, attr, (int) len, val, len, pos);
	if ((flags & XML_FLG_ATTRIBUTE) && val) {
		// info_printf("%02X %s %s '%.*s' %zd %zd\n", flags, dir, attr, (int) len, val, len, pos);

		// very simplified
		// see http://stackoverflow.com/questions/2725156/complete-list-of-html-tag-attributes-which-have-a-url-value
		switch (c_tolower(*attr)) {
		case 'h':
			if (wget_strcasecmp_ascii(attr, "href"))
				return;
			break;
		case 's':
			if (wget_strcasecmp_ascii(attr, "src"))
				return;
			break;
		default:
			return;
		}

		// check if len and pos matches
		const char *doc = (const char *)user_ctx;

		if (memcmp(doc + pos, val, len)) {
			failed++;
			error_printf_exit("Not found: '%.*s' expected at pos %zu with length %zu\n", (int) len, val, pos, len);
		} else
			ok++;
	}

/*
	if (flags & XML_FLG_BEGIN) {
		const char *p = *dir == '/' ? strrchr(dir, '/') : dir;
		if (p) {
			if (*dir == '/') p++;
			if (flags == (XML_FLG_BEGIN | XML_FLG_END)) {
				info_printf("<%s/>", p);
				return;
			}
			info_printf("<%s", p);
		}
	}
	if (flags & XML_FLG_ATTRIBUTE) {
		if (val)
			info_printf(" %s=\"%.*s\"", attr, (int) len, val);
		else
			info_printf(" %s", attr); // HTML bareword attribute
	}
	if (flags & XML_FLG_CLOSE) {
		info_printf(">");
	}
	if (flags & XML_FLG_CONTENT) {
		info_printf("%.*s", (int) len, val);
	}
	if (flags & XML_FLG_END) {
		const char *p = *dir == '/' ? strrchr(dir, '/') : dir;
		if (p) {
			if (*dir == '/') p++;
			info_printf("</%s>", p);
		}
	}

	if (flags == XML_FLG_COMMENT)
		info_printf("<!--%.*s-->", (int) len, val);
	else if (flags == XML_FLG_PROCESSING)
		info_printf("<?%.*s?>", (int) len, val);
	else if (flags == XML_FLG_SPECIAL)
		info_printf("<!%.*s>", (int) len, val);
*/
}

static void test_parse_buffer(void)
{
	unsigned it;

	for (it = 0; it < countof(test_data); it++) {
		wget_html_parse_buffer(test_data[it], html_dump, (void *)test_data[it], 0);
	}
}

static void test_parse_files(void)
{
	DIR *dirp;
	struct dirent *dp;
	const char *ext;
	int xml = 0, html = 0, css = 0, type;

	// test the XML / HTML parser, you should start the test with valgrind
	// to detect memory faults
	if ((dirp = opendir(SRCDIR "/files")) != NULL) {
		while ((dp = readdir(dirp)) != NULL) {
			if (*dp->d_name == '.') continue;
			if ((ext = strrchr(dp->d_name, '.'))) {
				if (!wget_strcasecmp_ascii(ext, ".xml"))
					type = 1;
				else if (!wget_strcasecmp_ascii(ext, ".html"))
					type = 2;
				else
					continue;

				char *fname = wget_aprintf("%s/files/%s", SRCDIR, dp->d_name);
				info_printf("parsing %s\n", fname);

				char *data;

				if ((data = wget_read_file(fname, NULL))) {
					if (type == 1) {
						wget_xml_parse_buffer(data, html_dump, data, 0);
						xml++;
					} else {
						wget_html_parse_buffer(data, html_dump, data, 0);
						html++;
					}

					xfree(data);
				}

				xfree(fname);
			}
		}
		closedir(dirp);
	}

	info_printf("%d XML, %d HTML and %d CSS files parsed\n", xml, html, css);
}

int main(int argc, const char **argv)
{
	if (init(argc, argv) < 0) // allows us to test with options (e.g. with --debug)
		return -1;
	test_parse_buffer();
	test_parse_files();

	deinit(); // free resources allocated by init()

	if (failed) {
		info_printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
		return 1;
	}

	info_printf("Summary: All %d tests passed\n", ok + failed);
	return 0;
}