//
// $Id: SocketAddressTest.m,v 1.8 2005/05/18 06:46:56 will_mason Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#import "SocketAddressTest.h"
#import <ObjectiveLib/SocketAddress.h>
#import <Foundation/NSString.h>
#import <Foundation/NSException.h>
#import <Foundation/NSFileManager.h>

@implementation SocketAddressTest

- (void) testCopyInternetAddresses
{
    OLInternetAddress* addr;
    OLInternetAddress* addr2;

    NS_DURING

        addr = [OLInternetAddress addressWithCurrentHostAndPort: 0];
        addr2 = [addr copy];
        if (![addr isEqual: addr2])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" should be equal to \"%s\"",
                [[addr description] cString], [[addr2 description] cString]];
        }
        [addr2 release];

    NS_HANDLER

        [self errInFile: __FILE__ line: __LINE__
            format: "Could not resolve current host - %s: %s",
            [[localException name] cString], [[localException reason] cString]];

    NS_ENDHANDLER

    NS_DURING

        addr = [OLInternetAddress addressWithCurrentHostAndService: @"telnet"];
        addr2 = [addr copy];
        if (![addr isEqual: addr2])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" should be equal to \"%s\"",
                [[addr description] cString], [[addr2 description] cString]];
        }
        [addr2 release];

    NS_HANDLER

        [self errInFile: __FILE__ line: __LINE__
            format: "Could not resolve telnet service on current host - %s: %s",
            [[localException name] cString], [[localException reason] cString]];

    NS_ENDHANDLER

    NS_DURING

        addr = [OLInternetAddress addressWithHost: @"gnustep.org" service: @"telnet"];
        addr2 = [addr copy];
        if (![addr isEqual: addr2])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" should not be equal to \"%s\"",
                [[addr description] cString], [[addr2 description] cString]];
        }
        [addr2 release];

    NS_HANDLER

        [self errInFile: __FILE__ line: __LINE__
            format: "Could not resolve telnet service on host gnustep.org - %s: %s",
            [[localException name] cString], [[localException reason] cString]];

    NS_ENDHANDLER
}

#if defined(OL_HAVE_UNIX_SOCKETS)

- (void) testCopyUnixAddress
{
    OLUnixAddress* addr;
    OLUnixAddress* addr2;

    addr = [[OLUnixAddress alloc] initWithPath: @"test.socket"];
    addr2 = [addr copy];
    if (![addr isEqual: addr2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should be equal to \"%s\"",
            [[addr path] cString], [[addr2 path] cString]];
    }
    [addr release];
    [addr2 release];
}

#endif

- (void) testInternetAddresses
{
    OLInternetAddress* addr;
    OLInternetAddress* addr2;

    NS_DURING

        addr = [OLInternetAddress addressWithCurrentHostAndPort: 0];
        [self logMessage: "Current host is %s", [[addr description] cString]];
        if ([addr port] != 0)
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "Expected 0, but got %u", [addr port]];
        }
        addr2 = [OLInternetAddress addressWithCurrentHostAndPort: 0];
        if (![addr isEqual: addr2])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" should be equal to \"%s\"",
                [[addr description] cString], [[addr2 description] cString]];
        }

    NS_HANDLER

        [self errInFile: __FILE__ line: __LINE__
            format: "Could not resolve current host - %s: %s",
            [[localException name] cString], [[localException reason] cString]];

    NS_ENDHANDLER

    NS_DURING

        addr = [OLInternetAddress addressWithCurrentHostAndService: @"telnet"];
        [self logMessage: "Current telnet host is %s", [[addr description] cString]];
        if ([addr port] != 23)
            [self logMessage: "Expected 23, but got %u", [addr port]];

    NS_HANDLER

        [self errInFile: __FILE__ line: __LINE__
            format: "Could not resolve telnet service on current host - %s: %s",
            [[localException name] cString], [[localException reason] cString]];

    NS_ENDHANDLER

    NS_DURING

        addr = [OLInternetAddress addressWithHost: @"gnustep.org" port: 0];
        [self logMessage: "gnustep.org is %s", [[addr description] cString]];

    NS_HANDLER

        [self errInFile: __FILE__ line: __LINE__
            format: "Could not resolve host gnustep.org - %s: %s",
            [[localException name] cString], [[localException reason] cString]];

    NS_ENDHANDLER

    NS_DURING

        addr = [OLInternetAddress addressWithHost: @"gnustep.org" service: @"telnet"];
        [self logMessage: "gnustep.org telnet is %s", [[addr description] cString]];
        if ([addr port] != 23)
            [self logMessage: "Expected 23, but got %u", [addr port]];
        addr2 = [OLInternetAddress addressWithHost: @"gnu.org" service: @"telnet"];
        if ([addr isEqual: addr2])
        {
            [self errInFile: __FILE__ line: __LINE__
                format: "\"%s\" should not be equal to \"%s\"",
                [[addr description] cString], [[addr2 description] cString]];
        }

    NS_HANDLER

        [self errInFile: __FILE__ line: __LINE__
            format: "Could not resolve telnet service on host gnustep.org - %s: %s",
            [[localException name] cString], [[localException reason] cString]];

    NS_ENDHANDLER
}

#if defined(OL_HAVE_UNIX_SOCKETS)

- (void) testUnixAddress
{
    OLUnixAddress* addr;
    OLUnixAddress* addr2;
    NSString* path = [NSString stringWithFormat: @"%@/test.socket",
        [[NSFileManager defaultManager] currentDirectoryPath]];

    addr = [[OLUnixAddress alloc] initWithPath: @"test.socket"];
    [self logMessage: "Got path for socket of %s", [[addr path] cString]];
    addr2 = [[OLUnixAddress alloc] initWithPath: path];
    if (![addr isEqual: addr2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should be equal to \"%s\"",
            [[addr path] cString], [[addr2 path] cString]];
    }
    [addr release];
    addr = [[OLUnixAddress alloc] initWithPath: @"doggyboy"];
    if ([addr isEqual: addr2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should not be equal to \"%s\"",
            [[addr path] cString], [[addr2 path] cString]];
    }
    [addr release];
    [addr2 release];
    addr = [OLUnixAddress addressWithPath: @"test.socket"];
    addr2 = [OLUnixAddress addressWithPath: path];
    if (![addr isEqual: addr2])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should be equal to \"%s\"",
            [[addr path] cString], [[addr2 path] cString]];
    }
}

#endif

- (void) testWildcards
{
    OLInternetAddress* addr;

    addr = [[OLInternet4Address alloc] init];
    if (![addr isLocalWildcard])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should be a local wildcard address",
            [[addr description] cString]];
    }
    [addr release];
#if defined(OL_HAVE_INET6_SOCKETS)
    addr = [[OLInternet6Address alloc] init];
    if (![addr isLocalWildcard])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should be a local wildcard address",
            [[addr description] cString]];
    }
    [addr release];
#endif
    addr = [[OLInternet4Address alloc] initWithPort: 30001];
    if (![addr isLocalWildcard])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should be a local wildcard address",
            [[addr description] cString]];
    }
    if ([addr port] != 30001)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 30001, but got %u", [addr port]];
    }
    [addr release];
#if defined(OL_HAVE_INET6_SOCKETS)
    addr = [[OLInternet6Address alloc] initWithPort: 30001];
    if (![addr isLocalWildcard])
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "\"%s\" should be a local wildcard address",
            [[addr description] cString]];
    }
    if ([addr port] != 30001)
    {
        [self errInFile: __FILE__ line: __LINE__
            format: "Expected 30001, but got %u", [addr port]];
    }
    [addr release];
#endif
}

@end
