3 #include <stdio.h>
4 #include <limits.h> // realpath
5 #include <stdlib.h> // mkdtemp, realpath
6 #include <unistd.h> // chdir
7 #include <string.h> // strlen, strncpy, strrchr
9 #include "inkscapestream.h"
10 #include "base64stream.h"
11 #include "gzipstream.h"
12 #include "stringstream.h"
13 #include "uristream.h"
14 #include "xsltstream.h"
16 // quick way to pass the name of the executable into the test
17 char myself[PATH_MAX];
19 // names and path storage for other tests
20 char *xmlname = "crystalegg.xml";
21 char xmlpath[PATH_MAX];
22 char *xslname = "doc2html.xsl";
23 char xslpath[PATH_MAX];
25 bool testUriStream()
26 {
27 printf("######### UriStream copy ############\n");
28 Inkscape::URI inUri(myself);
29 Inkscape::IO::UriInputStream ins(inUri);
30 Inkscape::URI outUri("streamtest.copy");
31 Inkscape::IO::UriOutputStream outs(outUri);
33 pipeStream(ins, outs);
35 ins.close();
36 outs.close();
38 return true;
39 }
41 bool testWriter()
42 {
43 printf("######### OutputStreamWriter ############\n");
44 Inkscape::IO::StdOutputStream outs;
45 Inkscape::IO::OutputStreamWriter writer(outs);
47 writer << "Hello, world! " << 123.45 << " times\n";
49 writer.printf("There are %f quick brown foxes in %d states\n", 123.45, 88);
51 return true;
52 }
54 bool testStdWriter()
55 {
56 printf("######### StdWriter ############\n");
57 Inkscape::IO::StdWriter writer;
59 writer << "Hello, world! " << 123.45 << " times\n";
61 writer.printf("There are %f quick brown foxes in %d states\n", 123.45, 88);
63 return true;
64 }
66 bool testBase64()
67 {
68 printf("######### Base64 Out ############\n");
69 Inkscape::URI plainInUri(xmlpath);
70 Inkscape::IO::UriInputStream ins1(plainInUri);
72 Inkscape::URI b64OutUri("crystalegg.xml.b64");
73 Inkscape::IO::UriOutputStream outs1(b64OutUri);
74 Inkscape::IO::Base64OutputStream b64Outs(outs1);
76 pipeStream(ins1, b64Outs);
78 ins1.close();
79 b64Outs.close();
81 printf("######### Base64 In ############\n");
82 Inkscape::URI b64InUri("crystalegg.xml.b64");
83 Inkscape::IO::UriInputStream ins2(b64InUri);
84 Inkscape::IO::Base64InputStream b64Ins(ins2);
86 Inkscape::URI plainOutUri("crystalegg.xml.b64dec");
87 Inkscape::IO::UriOutputStream outs2(plainOutUri);
89 pipeStream(b64Ins, outs2);
91 outs2.close();
92 b64Ins.close();
94 return true;
95 }
97 bool testXslt()
98 {
99 printf("######### XSLT Sheet ############\n");
100 Inkscape::URI xsltSheetUri(xslpath);
101 Inkscape::IO::UriInputStream xsltSheetIns(xsltSheetUri);
102 Inkscape::IO::XsltStyleSheet stylesheet(xsltSheetIns);
103 xsltSheetIns.close();
105 Inkscape::URI sourceUri(xmlpath);
106 Inkscape::IO::UriInputStream xmlIns(sourceUri);
108 printf("######### XSLT Input ############\n");
109 Inkscape::URI destUri("test.html");
110 Inkscape::IO::UriOutputStream xmlOuts(destUri);
112 Inkscape::IO::XsltInputStream xsltIns(xmlIns, stylesheet);
113 pipeStream(xsltIns, xmlOuts);
114 xsltIns.close();
115 xmlOuts.close();
118 printf("######### XSLT Output ############\n");
120 Inkscape::IO::UriInputStream xmlIns2(sourceUri);
122 Inkscape::URI destUri2("test2.html");
123 Inkscape::IO::UriOutputStream xmlOuts2(destUri2);
125 Inkscape::IO::XsltOutputStream xsltOuts(xmlOuts2, stylesheet);
126 pipeStream(xmlIns2, xsltOuts);
127 xmlIns2.close();
128 xsltOuts.close();
130 return true;
131 }
133 bool testGzip()
134 {
136 printf("######### Gzip Output ############\n");
137 Inkscape::URI gzUri("test.gz");
138 Inkscape::URI sourceUri(xmlpath);
139 Inkscape::IO::UriInputStream sourceIns(sourceUri);
140 Inkscape::IO::UriOutputStream gzOuts(gzUri);
142 Inkscape::IO::GzipOutputStream gzipOuts(gzOuts);
143 pipeStream(sourceIns, gzipOuts);
144 sourceIns.close();
145 gzipOuts.close();
147 printf("######### Gzip Input ############\n");
149 Inkscape::IO::UriInputStream gzIns(gzUri);
150 Inkscape::URI destUri("crystalegg2.xml");
151 Inkscape::IO::UriOutputStream destOuts(destUri);
153 Inkscape::IO::GzipInputStream gzipIns(gzIns);
154 pipeStream(gzipIns, destOuts);
155 gzipIns.close();
156 destOuts.close();
158 return true;
159 }
161 bool doTest()
162 {
163 if (!testUriStream())
164 {
165 return false;
166 }
167 if (!testWriter())
168 {
169 return false;
170 }
171 if (!testStdWriter())
172 {
173 return false;
174 }
175 if (!testBase64())
176 {
177 return false;
178 }
179 if (!testXslt())
180 {
181 return false;
182 }
183 if (!testGzip())
184 {
185 return false;
186 }
187 return true;
188 }
190 void path_init(char *path, char *name)
191 {
192 if (strlen(name)>PATH_MAX-strlen(myself))
193 {
194 printf("merging paths would be too long\n");
195 exit(1);
196 }
197 strncpy(path,myself,PATH_MAX);
198 char * ptr = strrchr(path,'/');
199 if (!ptr)
200 {
201 printf("path '%s' is missing any slashes\n",path);
202 exit(1);
203 }
204 strncpy(ptr+1,name,strlen(name)+1);
205 printf("'%s'\n",path);
206 }
209 int main(int argc, char **argv)
210 {
211 if (!realpath(argv[0],myself))
212 {
213 perror("realpath");
214 return 1;
215 }
216 path_init(xmlpath,xmlname);
217 path_init(xslpath,xslname);
219 // create temp files somewhere else instead of current dir
220 // TODO: clean them up too
221 char * testpath = strdup("/tmp/streamtest-XXXXXX");
222 testpath = mkdtemp(testpath);
223 if (!testpath)
224 {
225 perror("mkdtemp");
226 return 1;
227 }
228 if (chdir(testpath))
229 {
230 perror("chdir");
231 return 1;
232 }
234 if (!doTest())
235 {
236 printf("#### Test failed\n");
237 return 1;
238 }
239 else
240 {
241 printf("##### Test succeeded\n");
242 }
243 return 0;
244 }