Kaynağa Gözat

Add a couple of tests for the URI parsing and resolving code.

tags/v0.9.3
Michael Brown 17 yıl önce
ebeveyn
işleme
b7ccdb7948
1 değiştirilmiş dosya ile 145 ekleme ve 0 silme
  1. 145
    0
      src/tests/uri_test.c

+ 145
- 0
src/tests/uri_test.c Dosyayı Görüntüle

@@ -0,0 +1,145 @@
1
+#include <stdint.h>
2
+#include <stddef.h>
3
+#include <stdio.h>
4
+#include <string.h>
5
+#include <errno.h>
6
+#include <gpxe/uri.h>
7
+
8
+#define URI_MAX_LEN 1024
9
+
10
+struct uri_test {
11
+	const char *base_uri_string;
12
+	const char *relative_uri_string;
13
+	const char *resolved_uri_string;
14
+};
15
+
16
+static struct uri_test uri_tests[] = {
17
+	{ "http://www.fensystems.co.uk", "",
18
+	  "http://www.fensystems.co.uk/" },
19
+	{ "http://etherboot.org/wiki/page1", "page2",
20
+	  "http://etherboot.org/wiki/page2" },
21
+	{ "http://etherboot.org/wiki/page1", "../page3",
22
+	  "http://etherboot.org/page3" },
23
+	{ "tftp://192.168.0.1/", "/tftpboot/vmlinuz",
24
+	  "tftp://192.168.0.1/tftpboot/vmlinuz" },
25
+#if 0
26
+	"http://www.etherboot.org/wiki",
27
+	"mailto:bob@nowhere.com",
28
+	"ftp://joe:secret@insecure.org:8081/hidden/path/to?what=is#this",
29
+#endif
30
+};
31
+
32
+static int test_parse_unparse ( const char *uri_string ) {
33
+	char buf[URI_MAX_LEN];
34
+	size_t len;
35
+	struct uri *uri = NULL;
36
+	int rc;
37
+
38
+	/* Parse and unparse URI */
39
+	uri = parse_uri ( uri_string );
40
+	if ( ! uri ) {
41
+		rc = -ENOMEM;
42
+		goto done;
43
+	}
44
+	len = unparse_uri ( buf, sizeof ( buf ), uri );
45
+
46
+	/* Compare result */
47
+	if ( strcmp ( buf, uri_string ) != 0 ) {
48
+		printf ( "Unparse of \"%s\" produced \"%s\"\n",
49
+			 uri_string, buf );
50
+		rc = -EINVAL;
51
+		goto done;
52
+	}
53
+
54
+	rc = 0;
55
+
56
+ done:
57
+	uri_put ( uri );
58
+	if ( rc ) {
59
+		printf ( "URI parse-unparse of \"%s\" failed: %s\n",
60
+			 uri_string, strerror ( rc ) );
61
+	}
62
+	return rc;
63
+}
64
+
65
+static int test_resolve ( const char *base_uri_string,
66
+			  const char *relative_uri_string,
67
+			  const char *resolved_uri_string ) {
68
+	struct uri *base_uri = NULL;
69
+	struct uri *relative_uri = NULL;
70
+	struct uri *resolved_uri = NULL;
71
+	char buf[URI_MAX_LEN];
72
+	size_t len;
73
+	int rc;
74
+
75
+	/* Parse URIs */
76
+	base_uri = parse_uri ( base_uri_string );
77
+	if ( ! base_uri ) {
78
+		rc = -ENOMEM;
79
+		goto done;
80
+	}
81
+	relative_uri = parse_uri ( relative_uri_string );
82
+	if ( ! relative_uri ) {
83
+		rc = -ENOMEM;
84
+		goto done;
85
+	}
86
+
87
+	/* Resolve URI */
88
+	resolved_uri = resolve_uri ( base_uri, relative_uri );
89
+	if ( ! resolved_uri ) {
90
+		rc = -ENOMEM;
91
+		goto done;
92
+	}
93
+
94
+	/* Compare result */
95
+	len = unparse_uri ( buf, sizeof ( buf ), resolved_uri );
96
+	if ( strcmp ( buf, resolved_uri_string ) != 0 ) {
97
+		printf ( "Resolution of \"%s\"+\"%s\" produced \"%s\"\n",
98
+			 base_uri_string, relative_uri_string, buf );
99
+		rc = -EINVAL;
100
+		goto done;
101
+	}
102
+
103
+	rc = 0;
104
+
105
+ done:
106
+	uri_put ( base_uri );
107
+	uri_put ( relative_uri );
108
+	uri_put ( resolved_uri );
109
+	if ( rc ) {
110
+		printf ( "URI resolution of \"%s\"+\"%s\" failed: %s\n",
111
+			 base_uri_string, relative_uri_string,
112
+			 strerror ( rc ) );
113
+	}
114
+	return rc;
115
+}
116
+
117
+int uri_test ( void ) {
118
+	unsigned int i;
119
+	struct uri_test *uri_test;
120
+	int rc;
121
+	int overall_rc = 0;
122
+
123
+	for ( i = 0 ; i < ( sizeof ( uri_tests ) /
124
+			    sizeof ( uri_tests[0] ) ) ; i++ ) {
125
+		uri_test = &uri_tests[i];
126
+		rc = test_parse_unparse ( uri_test->base_uri_string );
127
+		if ( rc != 0 )
128
+			overall_rc = rc;
129
+		rc = test_parse_unparse ( uri_test->relative_uri_string );
130
+		if ( rc != 0 )
131
+			overall_rc = rc;
132
+		rc = test_parse_unparse ( uri_test->resolved_uri_string );
133
+		if ( rc != 0 )
134
+			overall_rc = rc;
135
+		rc = test_resolve ( uri_test->base_uri_string,
136
+				    uri_test->relative_uri_string,
137
+				    uri_test->resolved_uri_string );
138
+		if ( rc != 0 )
139
+			overall_rc = rc;
140
+	}
141
+
142
+	if ( overall_rc )
143
+		printf ( "URI tests failed: %s\n", strerror ( overall_rc ) );
144
+	return overall_rc;
145
+}

Loading…
İptal
Kaydet