Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -48,11 +48,13 @@ testfile_ini.m SRCS_PLUGINS = OFPluginTests.m SRCS_SOCKETS = OFDNSResolverTests.m \ OFSocketTests.m \ OFTCPSocketTests.m \ - OFUDPSocketTests.m + OFUDPSocketTests.m \ + ${USE_SRCS_UNIX_SOCKETS} +SRCS_UNIX_SOCKETS = OFUNIXDatagramSocketTests.m SRCS_SUBPROCESSES = OFSubprocessTests.m SRCS_THREADS = OFThreadTests.m include ../buildsys.mk ADDED new_tests/OFUNIXDatagramSocketTests.m Index: new_tests/OFUNIXDatagramSocketTests.m ================================================================== --- new_tests/OFUNIXDatagramSocketTests.m +++ new_tests/OFUNIXDatagramSocketTests.m @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include +#include + +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFUNIXDatagramSocketTests: OTTestCase +@end + +@implementation OFUNIXDatagramSocketTests +- (void)testUNIXDatagramSockets +{ + OFUNIXDatagramSocket *sock = [OFUNIXDatagramSocket socket]; + OFString *path; + OFSocketAddress address1, address2; + char buffer[5]; + +#if defined(OF_HAVE_FILES) && !defined(OF_IOS) + path = [[OFSystemInfo temporaryDirectoryIRI] + IRIByAppendingPathComponent: [[OFUUID UUID] UUIDString]] + .fileSystemRepresentation; +#else + /* + * We can have sockets, including UNIX sockets, while file support is + * disabled. + * + * We also use this code path for iOS, as the temporaryDirectoryIRI is + * too long on the iOS simulator. + */ + path = [OFString stringWithFormat: @"/tmp/%@", + [[OFUUID UUID] UUIDString]]; +#endif + + @try { + address1 = [sock bindToPath: path]; + } @catch (OFBindSocketFailedException *e) { + switch (e.errNo) { + case EAFNOSUPPORT: + case EPERM: + OTSkip(@"UNIX datagram sockets unsupported"); + return; + default: + @throw e; + } + } + + @try { + [sock sendBuffer: "Hello" length: 5 receiver: &address1]; + + OTAssertEqual([sock receiveIntoBuffer: buffer + length: 5 + sender: &address2], 5); + OTAssertEqual(memcmp(buffer, "Hello", 5), 0); + OTAssertTrue(OFSocketAddressEqual(&address1, &address2)); + OTAssertEqual(OFSocketAddressHash(&address1), + OFSocketAddressHash(&address2)); + } @finally { +#ifdef OF_HAVE_FILES + [[OFFileManager defaultManager] removeItemAtPath: path]; +#endif + } +} +@end Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -39,12 +39,11 @@ ${USE_SRCS_UNIX_SOCKETS} SRCS_APPLETALK = OFDDPSocketTests.m SRCS_IPX = OFIPXSocketTests.m \ OFSPXSocketTests.m \ OFSPXStreamSocketTests.m -SRCS_UNIX_SOCKETS = OFUNIXDatagramSocketTests.m \ - OFUNIXStreamSocketTests.m +SRCS_UNIX_SOCKETS = OFUNIXStreamSocketTests.m SRCS_WINDOWS = OFWindowsRegistryKeyTests.m IOS_USER ?= mobile IOS_TMP ?= /tmp/objfw-test DELETED tests/OFUNIXDatagramSocketTests.m Index: tests/OFUNIXDatagramSocketTests.m ================================================================== --- tests/OFUNIXDatagramSocketTests.m +++ tests/OFUNIXDatagramSocketTests.m @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2008-2024 Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#include - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFUNIXDatagramSocket"; - -@implementation TestsAppDelegate (OFUNIXDatagramSocketTests) -- (void)UNIXDatagramSocketTests -{ - void *pool = objc_autoreleasePoolPush(); - OFString *path; - OFUNIXDatagramSocket *sock; - OFSocketAddress address1, address2; - char buffer[5]; - -#if defined(OF_HAVE_FILES) && !defined(OF_IOS) - path = [[OFSystemInfo temporaryDirectoryIRI] - IRIByAppendingPathComponent: [[OFUUID UUID] UUIDString]] - .fileSystemRepresentation; -#else - /* - * We can have sockets, including UNIX sockets, while file support is - * disabled. - * - * We also use this code path for iOS, as the temporaryDirectoryIRI is - * too long on the iOS simulator. - */ - path = [OFString stringWithFormat: @"/tmp/%@", - [[OFUUID UUID] UUIDString]]; -#endif - - TEST(@"+[socket]", (sock = [OFUNIXDatagramSocket socket])) - - @try { - TEST(@"-[bindToPath:]", R(address1 = [sock bindToPath: path])) - } @catch (OFBindSocketFailedException *e) { - switch (e.errNo) { - case EAFNOSUPPORT: - case EPERM: - [OFStdOut setForegroundColor: [OFColor lime]]; - [OFStdOut writeLine: - @"\r[OFUNIXDatagramSocket] -[bindToPath:]: " - @"UNIX datagram sockets unsupported, skipping " - @"tests"]; - - objc_autoreleasePoolPop(pool); - return; - default: - @throw e; - } - } - - @try { - TEST(@"-[sendBuffer:length:receiver:]", - R([sock sendBuffer: "Hello" length: 5 receiver: &address1])) - - TEST(@"-[receiveIntoBuffer:length:sender:]", - [sock receiveIntoBuffer: buffer - length: 5 - sender: &address2] == 5 && - memcmp(buffer, "Hello", 5) == 0 && - OFSocketAddressEqual(&address1, &address2) && - OFSocketAddressHash(&address1) == - OFSocketAddressHash(&address2)) - } @finally { -#ifdef OF_HAVE_FILES - [[OFFileManager defaultManager] removeItemAtPath: path]; -#endif - } - - objc_autoreleasePoolPop(pool); -} -@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -125,14 +125,10 @@ @interface TestsAppDelegate (OFStringTests) - (void)stringTests; @end -@interface TestsAppDelegate (OFUNIXDatagramSocketTests) -- (void)UNIXDatagramSocketTests; -@end - @interface TestsAppDelegate (OFUNIXStreamSocketTests) - (void)UNIXStreamSocketTests; @end @interface TestsAppDelegate (OFValueTests) Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -382,11 +382,10 @@ [self streamTests]; [self memoryStreamTests]; [self notificationCenterTests]; #ifdef OF_HAVE_SOCKETS # ifdef OF_HAVE_UNIX_SOCKETS - [self UNIXDatagramSocketTests]; [self UNIXStreamSocketTests]; # endif # ifdef OF_HAVE_IPX [self IPXSocketTests]; [self SPXSocketTests];