2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
23 #include <NGStreams/NGStreams.h>
24 #include <NGStreams/NGNet.h>
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;
32 @interface SockTest : NSObject
37 NGActiveSocket *socket;
41 + (id)test:(NSString *)sockClassName:(NSString *)host:(int)port;
45 @implementation SockTest
47 - (id)init:(NSString *)sockClassName:(NSString *)host:(int)port {
48 if ((self = [super init])) {
50 [[NGInternetSocketAddress addressWithPort:port onHost:host] retain];
51 NSLog(@"addr: %@", self->address);
53 if ((self->socketClass = NSClassFromString(sockClassName)) == Nil) {
54 [self logWithFormat:@"did not find socket class %@", sockClassName];
61 + (id)test:(NSString *)_s:(NSString *)host:(int)port {
62 return [[[self alloc] init:_s:host:port] autorelease];
66 [self->address release];
73 [self->txt writeString:@"HELO imap\r\n"];
74 NSLog(@"read: %@", [self->txt readLineAsString]);
79 BOOL isRespLine = YES;
81 BOOL hasContent = YES;
83 while ((s = [txt readLineAsString])) {
90 if ([s length] == 0) {
92 if (!hasContent) break;
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;
107 NSLog(@"SB: %@ (len=%u)", s, [s length]);
112 - (void)runHTTPTest {
115 if ((s = [[NSUserDefaults standardUserDefaults] stringForKey:@"url"])==nil)
118 NSLog(@"C: GET %@ HTTP/1.0", s);
119 [txt writeFormat:@"GET %@ HTTP/1.0\r\n\r\n", s];
124 - (void)runXmlRpcTest {
127 if ((s = [[NSUserDefaults standardUserDefaults] stringForKey:@"url"])==nil)
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"];
144 - (void)runIMAP4Test {
147 NSLog(@"reading IMAP server hello ...");
148 s = [self->txt readLineAsString];
156 [[self->socketClass socketConnectedToAddress:self->address] retain];
158 [[NGCTextStream textStreamWithSource:self->socket] retain];
163 [self->socket release];
166 - (void)handleException:(NSException *)_e {
167 [self logWithFormat:@"FAIL: %@", _e];
170 - (void)runTest:(NSString *)_name {
171 NSAutoreleasePool *pool;
174 pool = [[NSAutoreleasePool alloc] init];
176 NSLog(@"-------------------- RUN: %@", _name);
178 s = NSSelectorFromString([NSString stringWithFormat:@"run%@Test", _name]);
183 [self performSelector:s];
185 [self handleException:localException];
194 NSLog(@"-------------------- DONE: %@\n", _name);
201 @implementation TestSock
203 - (void)runSMTPTest:(NSString *)sockClassName:(NSString *)host:(int)port {
204 [[SockTest test:sockClassName:host:port] runTest:@"SMTP"];
207 - (void)runHTTPTest:(NSString *)sockClassName:(NSString *)host:(int)port {
208 [[SockTest test:sockClassName:host:port] runTest:@"HTTP"];
211 - (void)runIMAP4Test:(NSString *)sockClassName:(NSString *)host:(int)port {
212 [[SockTest test:sockClassName:host:port] runTest:@"IMAP4"];
217 int main(int argc, char **argv, char **env) {
218 NSAutoreleasePool *pool;
221 pool = [[NSAutoreleasePool alloc] init];
223 #if LIB_FOUNDATION_LIBRARY
224 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
227 sock = [[TestSock alloc] init];
230 [sock runSMTPTest:@"NGActiveSocket":@"imap.mdlink.de":25];
231 [sock runSMTPTest:@"NGActiveSocket":@"skyrix.in.skyrix.com":25];
233 [sock runHTTPTest:@"NGActiveSocket":@"www.skyrix.de":80];
234 [sock runHTTPTest:@"NGActiveSSLSocket":@"skyrix.in.skyrix.com":443];
236 [sock runIMAP4Test:@"NGActiveSSLSocket":@"skyrix.in.skyrix.com":993];
238 [sock runHTTPTest:@"NGActiveSSLSocket":@"localhost":505];
241 [[SockTest test:@"NGActiveSSLSocket":@"localhost":505] runTest:@"XmlRpc"];