Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -118,10 +118,11 @@ 4B187E5B163EA3DA0049A832 /* OFDictionary+NSObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B187E38163EA3DA0049A832 /* OFDictionary+NSObject.m */; }; 4B187E5C163EA3DA0049A832 /* OFString+NSObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B187E39163EA3DA0049A832 /* OFString+NSObject.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B187E5E163EA3DA0049A832 /* OFString+NSObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B187E3A163EA3DA0049A832 /* OFString+NSObject.m */; }; 4B187E61163EA4530049A832 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B187E60163EA4530049A832 /* Foundation.framework */; }; 4B187E62163EA46D0049A832 /* ObjFW.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B3D23761337FBC800DD29B8 /* ObjFW.framework */; }; + 4B22BE201AE594A000CD320A /* OFKernelEventObserverTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B22BE1F1AE594A000CD320A /* OFKernelEventObserverTests.m */; }; 4B23CA92133811DC0047A1D9 /* TestPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6EF684123535B60076B512 /* TestPlugin.m */; }; 4B23CA93133811E20047A1D9 /* ObjFW.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B3D23761337FBC800DD29B8 /* ObjFW.framework */; }; 4B23CA941338121C0047A1D9 /* TestPlugin.bundle in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4B23CA8A133811610047A1D9 /* TestPlugin.bundle */; }; 4B24592A19B5397C0059F271 /* OFSHA224Hash.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B24592619B5397C0059F271 /* OFSHA224Hash.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B24592B19B5397C0059F271 /* OFSHA224Hash.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B24592719B5397C0059F271 /* OFSHA224Hash.m */; }; @@ -632,10 +633,11 @@ 4B187E39163EA3DA0049A832 /* OFString+NSObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "OFString+NSObject.h"; path = "src/bridge/OFString+NSObject.h"; sourceTree = ""; }; 4B187E3A163EA3DA0049A832 /* OFString+NSObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "OFString+NSObject.m"; path = "src/bridge/OFString+NSObject.m"; sourceTree = ""; }; 4B187E60163EA4530049A832 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; }; 4B19023A1338D6A2000374C9 /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; name = Makefile; path = src/Makefile; sourceTree = SOURCE_ROOT; }; 4B19023D1338D6D5000374C9 /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; name = Makefile; path = tests/Makefile; sourceTree = ""; }; + 4B22BE1F1AE594A000CD320A /* OFKernelEventObserverTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFKernelEventObserverTests.m; path = tests/OFKernelEventObserverTests.m; sourceTree = ""; }; 4B23CA8A133811610047A1D9 /* TestPlugin.bundle */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = TestPlugin.bundle; sourceTree = BUILT_PRODUCTS_DIR; }; 4B24592619B5397C0059F271 /* OFSHA224Hash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSHA224Hash.h; path = src/OFSHA224Hash.h; sourceTree = ""; }; 4B24592719B5397C0059F271 /* OFSHA224Hash.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSHA224Hash.m; path = src/OFSHA224Hash.m; sourceTree = ""; }; 4B24592819B5397C0059F271 /* OFSHA224Or256Hash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSHA224Or256Hash.h; path = src/OFSHA224Or256Hash.h; sourceTree = ""; }; 4B24592919B5397C0059F271 /* OFSHA224Or256Hash.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSHA224Or256Hash.m; path = src/OFSHA224Or256Hash.m; sourceTree = ""; }; @@ -1510,10 +1512,11 @@ 4BE5F0E512DF4259005C7A0C /* OFDateTests.m */, 4B6EF6701235358D0076B512 /* OFDictionaryTests.m */, 4BB4B54916776094002A2DCE /* OFHTTPClientTests.m */, 4B5B02C018B2897500CE6AE4 /* OFINIFileTests.m */, 4BAA60C714D09699006F068D /* OFJSONTests.m */, + 4B22BE1F1AE594A000CD320A /* OFKernelEventObserverTests.m */, 4B6EF6721235358D0076B512 /* OFListTests.m */, 4B6EF6731235358D0076B512 /* OFMD5HashTests.m */, 4B6EF6741235358D0076B512 /* OFNumberTests.m */, 4B6EF6751235358D0076B512 /* OFObjectTests.m */, 4B6EF6761235358D0076B512 /* OFPluginTests.m */, @@ -2171,10 +2174,11 @@ 4BF33AFF133807A20059CEF7 /* OFDateTests.m in Sources */, 4BF33B00133807A20059CEF7 /* OFDictionaryTests.m in Sources */, 4BB4B54A16776094002A2DCE /* OFHTTPClientTests.m in Sources */, 4B5B02C118B2897500CE6AE4 /* OFINIFileTests.m in Sources */, 4BAA60C814D09699006F068D /* OFJSONTests.m in Sources */, + 4B22BE201AE594A000CD320A /* OFKernelEventObserverTests.m in Sources */, 4BF33B03133807A20059CEF7 /* OFListTests.m in Sources */, 4BF33B04133807A20059CEF7 /* OFMD5HashTests.m in Sources */, 4BF33B05133807A20059CEF7 /* OFNumberTests.m in Sources */, 4BF33B06133807A20059CEF7 /* OFObjectTests.m in Sources */, 4BF33B07133807A20059CEF7 /* OFPluginTests.m in Sources */, Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -40,11 +40,12 @@ OFSHA224HashTests.m \ OFSHA256HashTests.m \ OFSHA384HashTests.m \ OFSHA512HashTests.m SRCS_PLUGINS = OFPluginTests.m -SRCS_SOCKETS = OFTCPSocketTests.m \ +SRCS_SOCKETS = OFKernelEventObserverTests.m \ + OFTCPSocketTests.m \ OFUDPSocketTests.m SRCS_THREADS = OFThreadTests.m IOS_USER ?= mobile IOS_TMP ?= /tmp/objfw-test ADDED tests/OFKernelEventObserverTests.m Index: tests/OFKernelEventObserverTests.m ================================================================== --- tests/OFKernelEventObserverTests.m +++ tests/OFKernelEventObserverTests.m @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 + * 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" + +#import "OFKernelEventObserver.h" +#import "OFString.h" +#import "OFTCPSocket.h" +#import "OFAutoreleasePool.h" + +#import "TestsAppDelegate.h" + +static OFString *module = @"OFKernelEventObserverSocket"; +static OFKernelEventObserver *observer; +static int events = 0; +static id expectedObject; +static bool readData = false; + +@interface ObserverDelegate: OFObject +- (void)objectIsReadyForReading: (id)object; +@end + +@implementation ObserverDelegate +- (void)objectIsReadyForReading: (id)object +{ + events++; + + OF_ENSURE(object == expectedObject); + + if ([object isListening]) { + OFTCPSocket *client = [object accept]; + + [observer addObjectForReading: client]; + [client writeBuffer: "0" + length: 1]; + + return; + } else if (readData) { + char buf; + + [object readIntoBuffer: &buf + length: 1]; + + OF_ENSURE(buf == '0'); + } +} +@end + +@implementation TestsAppDelegate (OFKernelEventObserverTests) +- (void)kernelEventObserverTests +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + ObserverDelegate *delegate = + [[[ObserverDelegate alloc] init] autorelease]; + OFTCPSocket *sock1 = [OFTCPSocket socket]; + OFTCPSocket *sock2 = [OFTCPSocket socket]; + uint16_t port; + + port = [sock1 bindToHost: @"127.0.0.1" + port: 0]; + [sock1 listen]; + + TEST(@"+[observer]", + (observer = [OFKernelEventObserver observer]) && + R([observer setDelegate: delegate])) + + TEST(@"-[addObjectForReading:]", + R([observer addObjectForReading: sock1])) + + [sock2 connectToHost: @"127.0.0.1" + port: port]; + TEST(@"-[observe] waiting for connection", + (expectedObject = sock1) && + [observer observeForTimeInterval: 0.01] == 1) + + TEST(@"-[observe] waiting for data", + (expectedObject = sock2) && + R([observer addObjectForReading: sock2]) && + [observer observeForTimeInterval: 0.01] == 1) + + TEST(@"-[observe] keeping event until read", + R(readData = true) && [observer observeForTimeInterval: 0.01] == 1) + + TEST(@"-[observer] time out due to no events", + R(readData = false) && [observer observeForTimeInterval: 0.01] == 0) + + TEST(@"-[observe] correct number of events", events == 3) + + [pool drain]; +} +@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -105,10 +105,14 @@ @end @interface TestsAppDelegate (OFJSONTests) - (void)JSONTests; @end + +@interface TestsAppDelegate (OFKernelEventObserverTests) +- (void)kernelEventObserverTests; +@end @interface TestsAppDelegate (OFListTests) - (void)listTests; @end @@ -174,16 +178,16 @@ @interface TestsAppDelegate (OFTCPSocketTests) - (void)TCPSocketTests; @end -@interface TestsAppDelegate (OFUDPSocketTests) -- (void)UDPSocketTests; +@interface TestsAppDelegate (OFThreadTests) +- (void)threadTests; @end -@interface TestsAppDelegate (OFThreadTests) -- (void)threadTests; +@interface TestsAppDelegate (OFUDPSocketTests) +- (void)UDPSocketTests; @end @interface TestsAppDelegate (OFURLTests) - (void)URLTests; @end Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -349,10 +349,11 @@ [self INIFileTests]; #endif #ifdef OF_HAVE_SOCKETS [self TCPSocketTests]; [self UDPSocketTests]; + [self kernelEventObserverTests]; #endif #ifdef OF_HAVE_THREADS [self threadTests]; #endif [self URLTests];