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