diff options
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/fastcgi.cpp | 81 | ||||
-rw-r--r-- | src/tests/nidstool.cpp | 241 | ||||
-rw-r--r-- | src/tests/url.cpp | 45 |
3 files changed, 105 insertions, 262 deletions
diff --git a/src/tests/fastcgi.cpp b/src/tests/fastcgi.cpp new file mode 100644 index 0000000..53dd68a --- /dev/null +++ b/src/tests/fastcgi.cpp | |||
@@ -0,0 +1,81 @@ | |||
1 | #include "bu/fastcgi.h" | ||
2 | |||
3 | class Cgi : public Bu::FastCgi | ||
4 | { | ||
5 | public: | ||
6 | Cgi() : | ||
7 | Bu::FastCgi::FastCgi() | ||
8 | { | ||
9 | } | ||
10 | |||
11 | Cgi( int iPort ) : | ||
12 | Bu::FastCgi::FastCgi( iPort ) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | virtual ~Cgi() | ||
17 | { | ||
18 | } | ||
19 | |||
20 | virtual int request( const StrHash &hParams, | ||
21 | const Bu::FString &sStdIn, Bu::Stream &sStdOut, | ||
22 | Bu::Stream &sStdErr ) | ||
23 | { | ||
24 | Bu::FString sOut("Content-Type: text/html\r\n\r\n"); | ||
25 | sOut += "<html><body><h1>Environment:</h1><ul>"; | ||
26 | for( StrHash::const_iterator i = hParams.begin(); i; i++ ) | ||
27 | { | ||
28 | sOut += "<li>" + i.getKey() + " = " + | ||
29 | i.getValue() + "</li>"; | ||
30 | } | ||
31 | sOut += "</ul>"; | ||
32 | char buf[2048]; | ||
33 | sOut += "<h1>Cwd:</h1><ul><li>"; | ||
34 | sOut += getcwd( buf, 2048 ); | ||
35 | sOut += "</li></ul>"; | ||
36 | sOut += "<h1>Stdin:</h1>"; | ||
37 | sOut.formatAppend("%d bytes<pre>", sStdIn.getSize() ); | ||
38 | Bu::FString sL, sR; | ||
39 | for( Bu::FString::const_iterator i = sStdIn.begin(); | ||
40 | i; i++ ) | ||
41 | { | ||
42 | sL.formatAppend("%02X ", | ||
43 | (unsigned int)((unsigned char)*i) ); | ||
44 | if( *i < 27 ) | ||
45 | sR += ". "; | ||
46 | else | ||
47 | sR.formatAppend("&#%d; ", | ||
48 | (unsigned int)((unsigned char)*i) ); | ||
49 | if( sL.getSize()/3 == 8 ) | ||
50 | { | ||
51 | sOut += sL + " | " + sR + "\n"; | ||
52 | sL = sR = ""; | ||
53 | } | ||
54 | } | ||
55 | if( sL != "" ) | ||
56 | { | ||
57 | while( sL.getSize()/3 < 8 ) | ||
58 | sL += " "; | ||
59 | sOut += sL + " | " + sR + "\n"; | ||
60 | } | ||
61 | sOut += "</pre><hr/><pre>"; | ||
62 | sOut += sStdIn; | ||
63 | sOut += "</pre>"; | ||
64 | sOut += "<form method=\"post\" enctype=\"multipart/form-data\"><textarea name=\"bob\"></textarea><br /><input type=\"file\" name=\"somefile\" /><br /><input type=\"submit\" name=\"yeah\" value=\"try it\" /></form>"; | ||
65 | sOut += "</body></html>"; | ||
66 | |||
67 | sStdOut.write( sOut ); | ||
68 | |||
69 | return 0; | ||
70 | } | ||
71 | }; | ||
72 | |||
73 | int main() | ||
74 | { | ||
75 | Cgi c( 8789 ); | ||
76 | |||
77 | c.run(); | ||
78 | |||
79 | return 0; | ||
80 | } | ||
81 | |||
diff --git a/src/tests/nidstool.cpp b/src/tests/nidstool.cpp deleted file mode 100644 index d1465ce..0000000 --- a/src/tests/nidstool.cpp +++ /dev/null | |||
@@ -1,241 +0,0 @@ | |||
1 | #include "bu/file.h" | ||
2 | #include "bu/nids.h" | ||
3 | #include "bu/nidsstream.h" | ||
4 | #include "bu/paramproc.h" | ||
5 | |||
6 | #include <stdlib.h> | ||
7 | |||
8 | typedef struct Block | ||
9 | { | ||
10 | uint32_t uFirstBlock; | ||
11 | uint32_t uNextBlock; | ||
12 | uint32_t uBytesUsed; | ||
13 | } Block; | ||
14 | |||
15 | class Param : public Bu::ParamProc | ||
16 | { | ||
17 | public: | ||
18 | Param( int argc, char *argv[] ) | ||
19 | { | ||
20 | addHelpBanner("nidstool - Do stuff with nids files.\n\n"); | ||
21 | addParam("info", 'i', mkproc(Param::procInfo), | ||
22 | "Print some info about the file."); | ||
23 | addParam("dump", 'd', mkproc(Param::procDump), | ||
24 | "Dump a stream to a file."); | ||
25 | addParam("analyze", 'a', mkproc(Param::procAnalyze), | ||
26 | "Analyze a nids file."); | ||
27 | addParam("copy", 'c', mkproc(Param::procCopy), | ||
28 | "Copy a nids file, changing settings."); | ||
29 | addParam("help", 'h', mkproc(Bu::ParamProc::help), "This help."); | ||
30 | process( argc, argv ); | ||
31 | } | ||
32 | |||
33 | virtual ~Param() | ||
34 | { | ||
35 | } | ||
36 | |||
37 | void printInfo( Bu::Nids &n ) | ||
38 | { | ||
39 | printf("File info:\n"); | ||
40 | printf(" Header overhead: %db\n", n.getBlockStart() ); | ||
41 | printf(" Block size: %db\n", n.getBlockSize() ); | ||
42 | printf(" Block count: %d\n", n.getNumBlocks() ); | ||
43 | printf(" Blocks used: %d (%d%%)\n", n.getNumUsedBlocks(), | ||
44 | n.getNumUsedBlocks()*100/n.getNumBlocks() ); | ||
45 | printf(" Block overhead: %db\n", n.getBlockOverhead() ); | ||
46 | printf(" Block storage: %db (%d%%)\n", | ||
47 | n.getBlockSize()-n.getBlockOverhead(), | ||
48 | (n.getBlockSize()-n.getBlockOverhead())*100/n.getBlockSize() ); | ||
49 | } | ||
50 | |||
51 | int procInfo( int argc, char *argv[] ) | ||
52 | { | ||
53 | if( argc < 1 ) | ||
54 | { | ||
55 | printf("You must provide a file name.\n"); | ||
56 | exit( 1 ); | ||
57 | } | ||
58 | |||
59 | Bu::File fIn( argv[0], Bu::File::Read ); | ||
60 | Bu::Nids n( fIn ); | ||
61 | n.initialize(); | ||
62 | |||
63 | printInfo( n ); | ||
64 | |||
65 | if( argc >= 2 ) | ||
66 | { | ||
67 | uint32_t uStream = strtoul( argv[1], NULL, 0 ); | ||
68 | uint32_t uBlock = uStream; | ||
69 | |||
70 | Block b; | ||
71 | |||
72 | for(;;) | ||
73 | { | ||
74 | fIn.setPos( n.getBlockStart()+n.getBlockSize()*uBlock ); | ||
75 | fIn.read( &b, sizeof(Block) ); | ||
76 | printf("Stream %u: block %u, next %u, %ub used.\n", | ||
77 | uStream, uBlock, b.uNextBlock, b.uBytesUsed | ||
78 | ); | ||
79 | if( b.uNextBlock == 0xFFFFFFFFUL ) | ||
80 | break; | ||
81 | uBlock = b.uNextBlock; | ||
82 | } | ||
83 | printf("Stream End.\n"); | ||
84 | |||
85 | return 2; | ||
86 | } | ||
87 | |||
88 | return 1; | ||
89 | } | ||
90 | |||
91 | int procDump( int argc, char *argv[] ) | ||
92 | { | ||
93 | if( argc < 3 ) | ||
94 | { | ||
95 | printf("You must provide a nids file, a stream id, and an output " | ||
96 | "file.\n"); | ||
97 | exit( 1 ); | ||
98 | } | ||
99 | |||
100 | Bu::File fIn( argv[0], Bu::File::Read ); | ||
101 | Bu::Nids n( fIn ); | ||
102 | n.initialize(); | ||
103 | |||
104 | int iStream = strtol( argv[1], NULL, 0 ); | ||
105 | Bu::NidsStream sIn = n.openStream( iStream ); | ||
106 | |||
107 | Bu::File fOut( argv[2], Bu::File::Write|Bu::File::Create ); | ||
108 | int iTotal = 0; | ||
109 | char buf[100]; | ||
110 | for(;;) | ||
111 | { | ||
112 | int iRead = sIn.read( buf, 100 ); | ||
113 | iTotal += fOut.write( buf, iRead ); | ||
114 | if( iRead < 100 ) | ||
115 | break; | ||
116 | } | ||
117 | |||
118 | printf("Wrote %db from stream %d in %s to %s.\n", | ||
119 | iTotal, iStream, argv[0], argv[2] ); | ||
120 | return 3; | ||
121 | } | ||
122 | |||
123 | int procAnalyze( int argc, char *argv[] ) | ||
124 | { | ||
125 | if( argc < 1 ) | ||
126 | { | ||
127 | printf("You must provide a file name.\n"); | ||
128 | exit( 1 ); | ||
129 | } | ||
130 | |||
131 | Bu::File fIn( argv[0], Bu::File::Read ); | ||
132 | Bu::Nids n( fIn ); | ||
133 | n.initialize(); | ||
134 | |||
135 | printInfo( n ); | ||
136 | |||
137 | int iStreamCnt = 0; | ||
138 | int iStreamTotal = 0; | ||
139 | int iOneBlock = 0; | ||
140 | uint32_t iLargest = 0; | ||
141 | uint32_t iSmallest = 0; | ||
142 | int iWaste = 0; | ||
143 | int iUsable = n.getBlockSize()-n.getBlockOverhead(); | ||
144 | Block b; | ||
145 | for( int j = 0; j < n.getNumBlocks(); j++ ) | ||
146 | { | ||
147 | fIn.setPos( n.getBlockStart()+n.getBlockSize()*j ); | ||
148 | fIn.read( &b, sizeof(Block) ); | ||
149 | if( b.uFirstBlock != (uint32_t)j ) | ||
150 | continue; | ||
151 | |||
152 | iStreamCnt++; | ||
153 | iStreamTotal += b.uBytesUsed; | ||
154 | |||
155 | if( b.uNextBlock == 0xFFFFFFFFUL ) | ||
156 | { | ||
157 | iOneBlock++; | ||
158 | iWaste += iUsable - b.uBytesUsed; | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | iWaste += iUsable - (b.uBytesUsed%iUsable); | ||
163 | } | ||
164 | |||
165 | if( j == 0 ) | ||
166 | { | ||
167 | iSmallest = iLargest = b.uBytesUsed; | ||
168 | } | ||
169 | else | ||
170 | { | ||
171 | if( iLargest < b.uBytesUsed ) | ||
172 | iLargest = b.uBytesUsed; | ||
173 | if( iSmallest > b.uBytesUsed ) | ||
174 | iSmallest = b.uBytesUsed; | ||
175 | } | ||
176 | } | ||
177 | printf("Steam analysis:\n"); | ||
178 | printf(" Stream count: %d\n", iStreamCnt ); | ||
179 | printf(" Stream size: %db/%db/%db (min/avr/max)\n", | ||
180 | iSmallest, iStreamTotal/iStreamCnt, iLargest ); | ||
181 | printf(" One-block streams: %d (%d%%)\n", | ||
182 | iOneBlock, iOneBlock*100/iStreamCnt ); | ||
183 | printf(" Total wasted space: %db (%d%%)\n", | ||
184 | iWaste, iWaste*100/iStreamTotal ); | ||
185 | printf(" Avr blocks-per-stream: %f%%\n", | ||
186 | (float)n.getNumBlocks()/(float)iStreamCnt ); | ||
187 | |||
188 | return 1; | ||
189 | } | ||
190 | |||
191 | int procCopy( int argc, char *argv[] ) | ||
192 | { | ||
193 | if( argc < 3 ) | ||
194 | { | ||
195 | printf("You must provide source stream, blocksize, destination.\n"); | ||
196 | exit( 1 ); | ||
197 | } | ||
198 | |||
199 | Bu::File fIn( argv[0], Bu::File::Read ); | ||
200 | Bu::Nids nIn( fIn ); | ||
201 | nIn.initialize(); | ||
202 | |||
203 | Bu::File fOut( argv[2], Bu::File::Read|Bu::File::Write|Bu::File::Create| | ||
204 | Bu::File::Truncate ); | ||
205 | Bu::Nids nOut( fOut ); | ||
206 | nOut.initialize( strtol( argv[1], 0, NULL ) ); | ||
207 | |||
208 | Block b; | ||
209 | for( int j = 0; j < nIn.getNumBlocks(); j++ ) | ||
210 | { | ||
211 | fIn.setPos( nIn.getBlockStart()+nIn.getBlockSize()*j ); | ||
212 | fIn.read( &b, sizeof(Block) ); | ||
213 | if( b.uFirstBlock != (uint32_t)j ) | ||
214 | continue; | ||
215 | |||
216 | Bu::NidsStream sIn = nIn.openStream( j ); | ||
217 | int iNew = nOut.createStream(); | ||
218 | Bu::NidsStream sOut = nOut.openStream( iNew ); | ||
219 | |||
220 | char buf[1024]; | ||
221 | for(;;) | ||
222 | { | ||
223 | int iRead = sIn.read( buf, 1024 ); | ||
224 | sOut.write( buf, iRead ); | ||
225 | if( iRead < 1024 ) | ||
226 | break; | ||
227 | } | ||
228 | } | ||
229 | |||
230 | return 3; | ||
231 | } | ||
232 | }; | ||
233 | |||
234 | |||
235 | int main( int argc, char *argv[] ) | ||
236 | { | ||
237 | Param p( argc, argv ); | ||
238 | |||
239 | return 0; | ||
240 | } | ||
241 | |||
diff --git a/src/tests/url.cpp b/src/tests/url.cpp index c9af676..4dc8c46 100644 --- a/src/tests/url.cpp +++ b/src/tests/url.cpp | |||
@@ -4,28 +4,31 @@ | |||
4 | 4 | ||
5 | int main( int argc, char *argv[] ) | 5 | int main( int argc, char *argv[] ) |
6 | { | 6 | { |
7 | printf("encodede: %s\n", Bu::Url::encode( argv[1] ).getStr() ); | 7 | for( argc--, argv++; argc >= 0; argc--, argv++ ) |
8 | printf("decodede: %s\n", Bu::Url::decode( argv[1] ).getStr() ); | ||
9 | Bu::Url u( argv[1] ); | ||
10 | |||
11 | printf("Protocol: %s\n", u.getProtocol().getStr() ); | ||
12 | printf("User: %s\n", u.getUser().getStr() ); | ||
13 | printf("Pass: %s\n", u.getPass().getStr() ); | ||
14 | printf("Host: %s\n", u.getHost().getStr() ); | ||
15 | printf("Path: %s\n", u.getPath().getStr() ); | ||
16 | try | ||
17 | { | ||
18 | printf("Port: %d\n", u.getPort() ); | ||
19 | } catch( Bu::ExceptionBase &e ) | ||
20 | { | 8 | { |
21 | printf("Port: not set.\n"); | 9 | printf("encodede: %s\n", Bu::Url::encode( *argv ).getStr() ); |
22 | } | 10 | printf("decodede: %s\n", Bu::Url::decode( *argv ).getStr() ); |
23 | printf("Parameters:\n"); | 11 | Bu::Url u( *argv ); |
24 | for( Bu::Url::ParamList::const_iterator i = u.getParamBegin(); i; i++ ) | 12 | |
25 | { | 13 | printf("Protocol: %s\n", u.getProtocol().getStr() ); |
26 | printf(" \"%s\" = \"%s\"\n", | 14 | printf("User: %s\n", u.getUser().getStr() ); |
27 | (*i).sName.getStr(), (*i).sValue.getStr() | 15 | printf("Pass: %s\n", u.getPass().getStr() ); |
28 | ); | 16 | printf("Host: %s\n", u.getHost().getStr() ); |
17 | printf("Path: %s\n", u.getPath().getStr() ); | ||
18 | try | ||
19 | { | ||
20 | printf("Port: %d\n", u.getPort() ); | ||
21 | } catch( Bu::ExceptionBase &e ) | ||
22 | { | ||
23 | printf("Port: not set.\n"); | ||
24 | } | ||
25 | printf("Parameters:\n"); | ||
26 | for( Bu::Url::ParamList::const_iterator i = u.getParamBegin(); i; i++ ) | ||
27 | { | ||
28 | printf(" \"%s\" = \"%s\"\n", | ||
29 | (*i).sName.getStr(), (*i).sValue.getStr() | ||
30 | ); | ||
31 | } | ||
29 | } | 32 | } |
30 | 33 | ||
31 | return 0; | 34 | return 0; |