2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
24 #include <NGStreams/NGStreams.h>
25 #include <NGStreams/NGNet.h>
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;
33 @interface SockTest : NSObject
38 NGActiveSocket *socket;
42 + (id)test:(NSString *)sockClassName:(NSString *)host:(int)port;
46 @implementation SockTest
48 - (id)init:(NSString *)sockClassName:(NSString *)host:(int)port {
49 if ((self = [super init])) {
51 [[NGInternetSocketAddress addressWithPort:port onHost:host] retain];
52 NSLog(@"addr: %@", self->address);
54 if ((self->socketClass = NSClassFromString(sockClassName)) == Nil) {
55 [self logWithFormat:@"did not find socket class %@", sockClassName];
62 + (id)test:(NSString *)_s:(NSString *)host:(int)port {
63 return [[[self alloc] init:_s:host:port] autorelease];
67 [self->address release];
74 [self->txt writeString:@"HELO imap\r\n"];
75 NSLog(@"read: %@", [self->txt readLineAsString]);
80 BOOL isRespLine = YES;
82 BOOL hasContent = YES;
84 while ((s = [txt readLineAsString])) {
91 if ([s length] == 0) {
93 if (!hasContent) break;
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;
108 NSLog(@"SB: %@ (len=%u)", s, [s length]);
113 - (void)runHTTPTest {
116 if ((s = [[NSUserDefaults standardUserDefaults] stringForKey:@"url"])==nil)
119 NSLog(@"C: GET %@ HTTP/1.0", s);
120 [txt writeFormat:@"GET %@ HTTP/1.0\r\n\r\n", s];
125 - (void)runXmlRpcTest {
128 if ((s = [[NSUserDefaults standardUserDefaults] stringForKey:@"url"])==nil)
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"];
145 - (void)runIMAP4Test {
148 NSLog(@"reading IMAP server hello ...");
149 s = [self->txt readLineAsString];
157 [[self->socketClass socketConnectedToAddress:self->address] retain];
159 [[NGCTextStream textStreamWithSource:self->socket] retain];
164 [self->socket release];
167 - (void)handleException:(NSException *)_e {
168 [self logWithFormat:@"FAIL: %@", _e];
171 - (void)runTest:(NSString *)_name {
172 NSAutoreleasePool *pool;
175 pool = [[NSAutoreleasePool alloc] init];
177 NSLog(@"-------------------- RUN: %@", _name);
179 s = NSSelectorFromString([NSString stringWithFormat:@"run%@Test", _name]);
184 [self performSelector:s];
186 [self handleException:localException];
195 NSLog(@"-------------------- DONE: %@\n", _name);
202 @implementation TestSock
204 - (void)runSMTPTest:(NSString *)sockClassName:(NSString *)host:(int)port {
205 [[SockTest test:sockClassName:host:port] runTest:@"SMTP"];
208 - (void)runHTTPTest:(NSString *)sockClassName:(NSString *)host:(int)port {
209 [[SockTest test:sockClassName:host:port] runTest:@"HTTP"];
212 - (void)runIMAP4Test:(NSString *)sockClassName:(NSString *)host:(int)port {
213 [[SockTest test:sockClassName:host:port] runTest:@"IMAP4"];
218 int main(int argc, char **argv, char **env) {
219 NSAutoreleasePool *pool;
222 pool = [[NSAutoreleasePool alloc] init];
224 #if LIB_FOUNDATION_LIBRARY
225 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
228 sock = [[TestSock alloc] init];
231 [sock runSMTPTest:@"NGActiveSocket":@"imap.mdlink.de":25];
232 [sock runSMTPTest:@"NGActiveSocket":@"skyrix.in.skyrix.com":25];
234 [sock runHTTPTest:@"NGActiveSocket":@"www.skyrix.de":80];
235 [sock runHTTPTest:@"NGActiveSSLSocket":@"skyrix.in.skyrix.com":443];
237 [sock runIMAP4Test:@"NGActiveSSLSocket":@"skyrix.in.skyrix.com":993];
239 [sock runHTTPTest:@"NGActiveSSLSocket":@"localhost":505];
242 [[SockTest test:@"NGActiveSSLSocket":@"localhost":505] runTest:@"XmlRpc"];