1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* Unit tests for DKPort
Copyright (C) 2010 Free Software Foundation, Inc.
Written by: Niels Grewe <niels.grewe@halbordnung.de>
Created: June 2010
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 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
Library 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., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import <Foundation/NSDebug.h>
#import <Foundation/NSObject.h>
#import "ObjectTesting.h"
#define INCLUDE_RUNTIME_H
#include "../Source/config.h"
#undef INCLUDE_RUNTIME_H
#import "DBusKit/DKProxy.h"
#import "../Source/DKEndpoint.h"
#import "DBusKit/DKPort.h"
#import "DBusKit/NSConnection+DBus.h"
#import <Foundation/NSArray.h>
#import <Foundation/NSException.h>
#import <Foundation/NSThread.h>
#include <unistd.h>
@interface DKProxy (Private)
- (SEL)_unmangledSelector: (SEL)selector
interface: (NSString**)interface;
- (void)DBusBuildMethodCache;
- (NSDictionary*)_interfaces;
@end
@interface TestDKProxy: NSObject
@end
@interface NSObject (FakeDBusSelectors)
- (NSString*)Introspect;
- (NSString*)GetId;
- (NSString*)Hello;
- (NSString*)ListNames;
- (char*)GetNameOwner: (char*)name;
- (BOOL)NameHasOwner: (NSString*)name;
@end
@implementation DKProxy (ArpWrapping)
- (void)arpWrappedNameHasOwner: (NSString*)name
{
NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];
NS_DURING
{
PASS([(id)self NameHasOwner: name], "-NameHasOwner: returns YES")
}
NS_HANDLER
{
NSLog(@"Got exception: %@", localException);
PASS(0, "-NameHasOwner: does not raise an exception")
}
NS_ENDHANDLER
[arp release];
[NSThread exit];
}
@end
@implementation TestDKProxy
- (void)testSelectorUnmangling
{
NSString *mangledString = @"_DKIf_org_freedesktop_DBus_DKIfEnd_GetNameOwner:";
SEL mangledSelector = 0;
SEL unmangledSel = 0;
NSString *interface = nil;
NSConnection *conn = nil;
id proxy = nil;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
proxy = [conn rootProxy];
// Call a method to trigger cache generation:
[proxy NameHasOwner: @"org.freedesktop.DBus"];
sel_registerName([mangledString UTF8String]);
mangledSelector = NSSelectorFromString(mangledString);
unmangledSel = [proxy _unmangledSelector: mangledSelector
interface: &interface];
PASS_EQUAL(@"GetNameOwner:", NSStringFromSelector(unmangledSel),
"unmangled selector is correct")
PASS_EQUAL(@"org.freedesktop.DBus", interface, "interface is correct")
}
- (void)testSendIntrospectMessage
{
NSConnection *conn = nil;
id aProxy = nil;
id returnValue = nil;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
aProxy = [conn rootProxy];
returnValue = [aProxy Introspect];
PASS(nil != returnValue, "-Introspect works")
PASS([returnValue isKindOfClass: [NSString class]],
"-Introspect returns NSString")
PASS([returnValue length] > 0,
"length of returned string is greater than zero")
}
- (void)testBuildMethodCache
{
NSConnection *conn = nil;
id aProxy = nil;
NSDictionary *interfaces = nil;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
aProxy = [conn rootProxy];
[aProxy DBusBuildMethodCache];
interfaces = [aProxy _interfaces];
PASS(nil != interfaces, "-_interfaces works")
PASS([interfaces count] > 0, "number of interfaces is greater than zero")
}
- (void)testSendGetId
{
NSConnection *conn = nil;
id aProxy = nil;
id returnValue = nil;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
aProxy = [conn rootProxy];
returnValue = [aProxy GetId];
PASS(nil != returnValue, "-GetId works")
PASS([returnValue isKindOfClass: [NSString class]],
"-GetId returns an NSString")
PASS([returnValue length] > 0,
"length of returned string is greater than zero")
}
- (void)testExceptionOnSecondHello
{
NSConnection *conn = nil;
id aProxy = nil;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
aProxy = [conn rootProxy];
PASS_EXCEPTION([aProxy Hello], @"DKDBusRemoteErrorException",
"sending Hello message raises DKDBusRemoteErrorException")
}
- (void)testUnboxedMethodCall
{
NSConnection *conn = nil;
id aProxy = nil;
char *returnValue = NULL;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
aProxy = [conn rootProxy];
returnValue = [aProxy GetNameOwner: "org.freedesktop.DBus"];
PASS(NULL != returnValue, "-GetNameOwner: works")
}
- (void)testMixedBoxingStateMethodCall
{
NSConnection *conn = nil;
id aProxy = nil;
BOOL returnValue = NO;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
aProxy = [conn rootProxy];
returnValue = [aProxy NameHasOwner: @"org.freedesktop.DBus"];
PASS(returnValue, "-NameHasOwner: returns YES")
}
- (void)testProxyAtPath
{
NSConnection *conn = nil;
id aProxy = nil;
id returnValue = nil;
NSWarnMLog(@"This test is an expected failure if the system message bus is not available!");
conn = [NSConnection connectionWithReceivePort: [DKPort systemBusPort]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"
onBus: DKDBusSystemBus] autorelease]];
aProxy = [conn proxyAtPath: @"/org/freedesktop/DBus"];
PASS_RUNS(returnValue = [aProxy Introspect],
"-Introspect does not raise an exception")
PASS(nil != returnValue, "-Introspect works")
PASS([returnValue isKindOfClass: [NSString class]],
"-Introspect returns an NSString");
PASS([returnValue length] > 0,
"length of returned string is greater than zero")
}
- (void)testThreadedMethodCalls
{
NSConnection *conn = nil;
id aProxy = nil;
NSString *name = @"org.freedesktop.DBus";
NSMutableArray *threads = [NSMutableArray new];
NSUInteger count = 0;
NSWarnMLog(@"This test is an expected failure if the session message bus is not available!");
[DKPort enableWorkerThread];
conn = [NSConnection connectionWithReceivePort: [DKPort port]
sendPort: [[[DKPort alloc] initWithRemote: @"org.freedesktop.DBus"] autorelease]];
aProxy = [conn rootProxy];
/*
* NOTE: D-Bus does not seem to handle more than five concurrent calls very
* well and will sometimes start complaining about being OOM.
*/
for (count = 0; count < 5; count++)
{
NSThread *aThread = [[NSThread alloc] initWithTarget: aProxy
selector: @selector(arpWrappedNameHasOwner:)
object: name];
[threads addObject: aThread];
[aThread start];
[aThread release];
}
NSLog(@"Sleeping 6 seconds to allow threads to terminate:");
sleep(6);
for (count = 0;count < 5; count++)
{
PASS([(NSThread*)[threads objectAtIndex: count] isFinished],
"threaded call works for thread %"PRIuPTR"", count)
}
}
@end
@interface TestDKDBus: NSObject
@end
@implementation TestDKDBus
- (void)testGetSessionBus
{
PASS(nil != [DKDBus sessionBus], "+sessionBus works")
}
- (void)testGetSystemBus
{
PASS(nil != [DKDBus systemBus], "+systemBus works")
}
- (void)useSessionBus
{
PASS(nil != [[DKDBus sessionBus] GetId], "-GetId works on session bus")
}
- (void)useSystemBus
{
PASS(nil != [[DKDBus systemBus] GetId], "-GetId works on system bus")
}
@end
int
main (void)
{
START_SET("DKProxy")
TestDKProxy *test = [TestDKProxy new];
[test testSelectorUnmangling];
[test testSendIntrospectMessage];
[test testBuildMethodCache];
[test testSendGetId];
[test testExceptionOnSecondHello];
[test testUnboxedMethodCall];
[test testMixedBoxingStateMethodCall];
[test testProxyAtPath];
[test testThreadedMethodCalls];
DESTROY(test);
END_SET("DKProxy")
START_SET("DKDBus")
TestDKDBus *test1 = [TestDKDBus new];
[test1 testGetSessionBus];
[test1 testGetSystemBus];
[test1 useSessionBus];
[test1 useSystemBus];
DESTROY(test1);
END_SET("DKDBus")
return 0;
}
|