]> err.no Git - sope/blob - sope-core/samples/testsock.m
Drop apache 1 build-dependency
[sope] / sope-core / samples / testsock.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "common.h"
23 #include <NGStreams/NGStreams.h>
24 #include <NGStreams/NGNet.h>
25
26 @interface TestSock : NSObject
27 - (void)runSMTPTest:(NSString *)sockClassName:(NSString *)host:(int)port;
28 - (void)runHTTPTest:(NSString *)sockClassName:(NSString *)host:(int)port;
29 - (void)runIMAP4Test:(NSString *)sockClassName:(NSString *)host:(int)port;
30 @end
31
32 @interface SockTest : NSObject
33 {
34   id    address;
35   Class socketClass;
36
37   NGActiveSocket *socket;
38   NGCTextStream  *txt;
39 }
40
41 + (id)test:(NSString *)sockClassName:(NSString *)host:(int)port;
42
43 @end
44
45 @implementation SockTest
46
47 - (id)init:(NSString *)sockClassName:(NSString *)host:(int)port {
48   if ((self = [super init])) {
49     self->address = 
50       [[NGInternetSocketAddress addressWithPort:port onHost:host] retain];
51     NSLog(@"addr: %@", self->address);
52     
53     if ((self->socketClass = NSClassFromString(sockClassName)) == Nil) {
54       [self logWithFormat:@"did not find socket class %@", sockClassName];
55       [self release];
56     }
57   }
58   return self;
59 }
60
61 + (id)test:(NSString *)_s:(NSString *)host:(int)port {
62   return [[[self alloc] init:_s:host:port] autorelease];
63 }
64
65 - (void)dealloc {
66   [self->address release];
67   [super dealloc];
68 }
69
70 /* tests */
71
72 - (void)runSMTPTest {
73   [self->txt writeString:@"HELO imap\r\n"];
74   NSLog(@"read: %@", [self->txt readLineAsString]);
75 }
76
77 - (void)_readHTTP {
78   NSString *s;
79   BOOL isRespLine = YES;
80   BOOL isHeader   = NO;
81   BOOL hasContent = YES;
82
83   while ((s = [txt readLineAsString])) {
84     if (isRespLine) {
85       isRespLine = NO;
86       isHeader   = YES;
87       NSLog(@"SR: %@", s);
88     }
89     else if (isHeader) {
90       if ([s length] == 0) {
91         isHeader = NO;
92         if (!hasContent) break;
93       }
94       else {
95         NSLog(@"SH: %@", s);
96         
97         s = [s lowercaseString];
98         if ([s hasPrefix:@"content-length:"]) {
99           s = [s substringFromIndex:[@"content-length:" length]];
100           s = [s stringByTrimmingSpaces];
101           //NSLog(@"content-length: %i", [s intValue]);
102           hasContent = [s intValue] != 0;
103         }
104       }
105     }
106     else {
107       NSLog(@"SB: %@ (len=%u)", s, [s length]);
108     }
109   }
110 }
111
112 - (void)runHTTPTest {
113   NSString *s;
114   
115   if ((s = [[NSUserDefaults standardUserDefaults] stringForKey:@"url"])==nil)
116     s = @"/";
117   
118   NSLog(@"C: GET %@ HTTP/1.0", s);
119   [txt writeFormat:@"GET %@ HTTP/1.0\r\n\r\n", s];
120   
121   [self _readHTTP];
122 }
123
124 - (void)runXmlRpcTest {
125   NSString *s;
126   
127   if ((s = [[NSUserDefaults standardUserDefaults] stringForKey:@"url"])==nil)
128     s = @"/RPC2";
129   
130   NSLog(@"C: GET %@ HTTP/1.0", s);
131   [txt writeFormat:@"POST %@ HTTP/1.0\r\n", s];
132   [txt writeString:@"content-type: text/xml\r\n"];
133   [txt writeString:@"\r\n"];
134   [txt writeString:@"<?xml version=\"1.0\"?>\n"];
135   [txt writeString:@"<methodCall>\n"];
136   [txt writeString:@"<methodName>system.listMethods</methodName>\n"];
137   [txt writeString:@"<params>\n"];
138   [txt writeString:@"</params>\n"];
139   [txt writeString:@"</methodCall>\n"];
140   
141   [self _readHTTP];
142 }
143
144 - (void)runIMAP4Test {
145   NSString *s;
146   
147   NSLog(@"reading IMAP server hello ...");
148   s = [self->txt readLineAsString];
149   NSLog(@"S: %@", s);
150 }
151
152 /* common stuff */
153
154 - (void)setUp {
155   self->socket = 
156     [[self->socketClass socketConnectedToAddress:self->address] retain];
157   self->txt = 
158     [[NGCTextStream textStreamWithSource:self->socket] retain];
159 }
160 - (void)tearDown {
161   [self->txt    close];
162   [self->txt    release];
163   [self->socket release];
164 }
165
166 - (void)handleException:(NSException *)_e {
167   [self logWithFormat:@"FAIL: %@", _e];
168 }
169
170 - (void)runTest:(NSString *)_name {
171   NSAutoreleasePool *pool;
172   SEL s;
173   
174   pool = [[NSAutoreleasePool alloc] init];
175
176   NSLog(@"-------------------- RUN: %@", _name);
177   
178   s = NSSelectorFromString([NSString stringWithFormat:@"run%@Test", _name]);
179   
180   [self setUp];
181   
182   NS_DURING
183     [self performSelector:s];
184   NS_HANDLER
185     [self handleException:localException];
186   NS_ENDHANDLER;
187
188   NS_DURING
189     [self tearDown];
190   NS_HANDLER
191     ;
192   NS_ENDHANDLER;
193   
194   NSLog(@"-------------------- DONE: %@\n", _name);
195   [pool release];
196 }
197
198 @end /* SockTest */
199
200
201 @implementation TestSock
202
203 - (void)runSMTPTest:(NSString *)sockClassName:(NSString *)host:(int)port {
204   [[SockTest test:sockClassName:host:port] runTest:@"SMTP"];
205 }
206
207 - (void)runHTTPTest:(NSString *)sockClassName:(NSString *)host:(int)port {
208   [[SockTest test:sockClassName:host:port] runTest:@"HTTP"];
209 }
210
211 - (void)runIMAP4Test:(NSString *)sockClassName:(NSString *)host:(int)port {
212   [[SockTest test:sockClassName:host:port] runTest:@"IMAP4"];
213 }
214
215 @end /* TestSock */
216
217 int main(int argc, char **argv, char **env) {
218   NSAutoreleasePool *pool;
219   TestSock *sock;
220   
221   pool = [[NSAutoreleasePool alloc] init];
222
223 #if LIB_FOUNDATION_LIBRARY
224   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
225 #endif
226   
227   sock = [[TestSock alloc] init];
228   
229 #if 0  
230   [sock runSMTPTest:@"NGActiveSocket":@"imap.mdlink.de":25];
231   [sock runSMTPTest:@"NGActiveSocket":@"skyrix.in.skyrix.com":25];
232
233   [sock runHTTPTest:@"NGActiveSocket":@"www.skyrix.de":80];
234   [sock runHTTPTest:@"NGActiveSSLSocket":@"skyrix.in.skyrix.com":443];
235
236   [sock runIMAP4Test:@"NGActiveSSLSocket":@"skyrix.in.skyrix.com":993];
237
238   [sock runHTTPTest:@"NGActiveSSLSocket":@"localhost":505];
239 #endif
240   
241   [[SockTest test:@"NGActiveSSLSocket":@"localhost":505] runTest:@"XmlRpc"];
242   
243   [pool release];
244   return 0;
245 }