-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathEDOServiceUITest.m
More file actions
614 lines (525 loc) · 27 KB
/
EDOServiceUITest.m
File metadata and controls
614 lines (525 loc) · 27 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//
// Copyright 2019 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "Service/Tests/FunctionalTests/EDOServiceUIBaseTest.h"
#include <objc/runtime.h>
#import <CoreImage/CoreImage.h>
#import "Channel/Sources/EDOHostPort.h"
#import "Service/Sources/EDOClientService.h"
#import "Service/Sources/EDOHostNamingService.h"
#import "Service/Sources/EDOHostService.h"
#import "Service/Sources/EDORemoteException.h"
#import "Service/Sources/EDOServiceError.h"
#import "Service/Sources/NSObject+EDOBlockedType.h"
#import "Service/Sources/NSObject+EDOValueObject.h"
#import "Service/Tests/FunctionalTests/EDOTestDummyInTest.h"
#import "Service/Tests/TestsBundle/EDOTestClassDummy.h"
#import "Service/Tests/TestsBundle/EDOTestDummy.h"
#import "Service/Tests/TestsBundle/EDOTestProtocol.h"
#import "Service/Tests/TestsBundle/EDOTestProtocolInTest.h"
#import "Service/Tests/TestsBundle/EDOTestValueType.h"
static NSString *const kTestServiceName = @"com.google.edo.testService";
@interface EDOUITestAppUITests : EDOServiceUIBaseTest
@end
@implementation EDOUITestAppUITests
- (void)tearDown {
EDOSetClientErrorHandler(nil);
[super tearDown];
}
- (void)testServiceNonExist {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
// The temporary service is created and self is wrapped as a remote object being sent
// to the application process without the service created locally here.
XCTAssertEqualObjects([remoteDummy returnClassNameWithObject:self], @"EDOObject");
}
- (void)testValueAndIdOutParameter {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:7];
EDOHostService *service =
[EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] initWithValue:9]
queue:dispatch_get_main_queue()];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertEqualObjects([remoteDummy class], NSClassFromString(@"EDOObject"));
NSError *errorOut;
XCTAssertFalse([remoteDummy returnBoolWithError:nil]);
XCTAssertTrue([remoteDummy returnBoolWithError:&errorOut]);
XCTAssertEqual(errorOut.code, 7);
EDOTestDummy *dummyOut;
XCTAssertThrows([remoteDummy voidWithOutObject:nil]);
[remoteDummy voidWithOutObject:&dummyOut];
XCTAssertEqualObjects([dummyOut class], NSClassFromString(@"EDOObject"));
XCTAssertEqual(dummyOut.value, 12);
EDOTestDummyInTest *dummyInTestOut;
// 11 + 7 + 12
XCTAssertEqual(
[dummyOut selWithOutEDO:&dummyInTestOut dummy:[[EDOTestDummyInTest alloc] initWithValue:11]],
30);
// dummyInTestOut is created in this process and need to be unwrapped to the local address
// as an out parameter.
XCTAssertEqualObjects([dummyInTestOut class], [EDOTestDummyInTest class]);
XCTAssertEqual(dummyInTestOut.value.intValue, 30);
EDOTestDummyInTest *dummyInTestOutRet = [dummyOut selWithInOutEDO:&dummyInTestOut];
XCTAssertEqual(dummyInTestOut.value.intValue, 30);
XCTAssertEqual(dummyInTestOutRet.value.intValue, 49); // 12 + 7 + 30
EDOTestDummyInTest *dummyInNil;
XCTAssertNil([dummyOut selWithInOutEDO:&dummyInNil]);
XCTAssertThrows([dummyOut selWithInOutEDO:nil]);
id arrayClass = EDO_REMOTE_CLASS(NSArray, EDOTEST_APP_SERVICE_PORT);
NSArray<NSString *> *array = [arrayClass arrayWithObjects:@"foo", @"bar", @"baz", nil];
XCTAssertEqual(array.count, 1);
XCTAssertEqualObjects(array[0], @"foo");
[service invalidate];
}
- (void)testProtocolParameter {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:10];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
// The protocol is loaded in the both sides so it shouldn't throw an exception.
XCTAssertNoThrow([remoteDummy voidWithProtocol:@protocol(EDOTestProtocol)]);
// This protocol isn't loaded on the app-side.
id assertBlock = ^{
[remoteDummy voidWithProtocol:@protocol(EDOTestProtocolInTest)];
};
[self assertBlock:assertBlock
throwsExceptionContaining:@"EDOTestProtocolInTest couldn't be loaded"];
// Calling a method from the protocol
XCTAssertTrue([[remoteDummy protocolName] isEqualToString:@"EDOTestProtocolInApp"]);
// Getting a protocol that wasn't loaded on the test side
XCTAssertThrowsSpecificNamed([remoteDummy returnWithProtocolInApp], NSException,
NSInternalInconsistencyException);
}
- (void)testBlockedParameter {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:0];
EDOBlockedTestDummyInTest *testDummy = [[EDOBlockedTestDummyInTest alloc] initWithValue:0];
EDOHostService *service = [EDOHostService serviceWithPort:2234
rootObject:testDummy
queue:dispatch_get_main_queue()];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertNoThrow([remoteDummy callBackToTest:testDummy withValue:0]);
XCTAssertNoThrow([remoteDummy createEDOWithPort:2234]);
XCTAssertNoThrow([remoteDummy selWithInOutEDO:&testDummy]);
[EDOBlockedTestDummyInTest edo_disallowRemoteInvocation];
XCTAssertThrows([remoteDummy callBackToTest:testDummy withValue:0]);
XCTAssertThrows([remoteDummy createEDOWithPort:2234]);
XCTAssertThrows([remoteDummy selWithInOutEDO:&testDummy]);
EDOTestDummyInTest *plainTestDummy = [[EDOTestDummyInTest alloc] initWithValue:0];
XCTAssertNoThrow([remoteDummy callBackToTest:plainTestDummy withValue:0]);
XCTAssertNoThrow([remoteDummy voidWithProtocol:@protocol(NSSecureCoding)]);
EDOAlwaysAllowedTestDummyInTest *allowedDummy = [[EDOAlwaysAllowedTestDummyInTest alloc] init];
EDOAlwaysAllowedTestDummyInTestSubclass *allowedDummySubclass =
[[EDOAlwaysAllowedTestDummyInTestSubclass alloc] init];
XCTAssertThrows([remoteDummy callBackToTest:allowedDummy withValue:0]);
XCTAssertThrows([remoteDummy callBackToTest:allowedDummySubclass withValue:0]);
[EDOAlwaysAllowedTestDummyInTest edo_alwaysAllowRemoteInvocation];
XCTAssertNoThrow([remoteDummy callBackToTest:allowedDummy withValue:0]);
XCTAssertNoThrow([remoteDummy callBackToTest:allowedDummySubclass withValue:0]);
XCTAssertThrows([EDOAlwaysAllowedTestDummyInTest edo_disallowRemoteInvocation]);
XCTAssertNoThrow([EDOAlwaysAllowedTestDummyInTestSubclass edo_disallowRemoteInvocation]);
XCTAssertThrows([remoteDummy callBackToTest:allowedDummySubclass withValue:0]);
// Verifies the blocked type can be passed when it is passed by value.
CIColor *color = [[CIColor alloc] initWithRed:1.0 green:0.0 blue:0.0];
XCTAssertEqual([remoteDummy colorRed:color], 1.0);
XCTAssertEqual([remoteDummy colorRed:[color passByValue]], 1.0);
[CIColor edo_disallowRemoteInvocation];
XCTAssertThrows([remoteDummy colorRed:color]);
[CIColor edo_enableValueType];
XCTAssertEqual([remoteDummy colorRed:color], 1.0);
[service invalidate];
}
- (void)testTwoWayAndMultiplexInvocation {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOHostService *service =
[EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] initWithValue:9]
queue:dispatch_get_main_queue()];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertEqualObjects([remoteDummy class], NSClassFromString(@"EDOObject"));
EDOTestDummyInTest *dummy = [[EDOTestDummyInTest alloc] initWithValue:8];
// Test: [A callBackToTest:B withValue:7]
// App: A-> [B callTestDummy:A]
// Test: B-> [A selWithIdReturn:10]
// App: -> A.value = A.value(5) + value(10) * 2 = 25
// App: -> new A(A.value(5) + 10).value
// App: = 15
// Test: B-> (15) + B.value(8) + 3
// Test: = 26
// App: (26) + value(7) + A.value(25)
// Test: = 58
XCTAssertEqual([remoteDummy callBackToTest:dummy withValue:7], 58);
[service invalidate];
}
- (void)testMultiplexInvocationStressfully {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertEqualObjects([remoteDummy class], NSClassFromString(@"EDOObject"));
const int kStressTestAttempts = 5;
const int kConcurrentNumber = 15;
EDOTestDummyInTest *dummy = [[EDOTestDummyInTest alloc] initWithValue:8];
for (int i = 0; i < kStressTestAttempts; ++i) {
XCTestExpectation *nestedCallExpectation =
[self expectationWithDescription:@"nested invocation completed."];
// The inner dispatch_apply performs actual stressful execution, and the test considers deadlock
// will happen if dispatch_apply cannot complete within the timeout.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_apply(kConcurrentNumber, DISPATCH_APPLY_AUTO, ^(size_t iteration) {
[remoteDummy callBackToTest:dummy withValue:7];
});
[nestedCallExpectation fulfill];
});
[self waitForExpectations:@[ nestedCallExpectation ] timeout:5.f];
}
}
/** Verifies temporary service will handle remote invocations recursively. */
- (void)testTemporaryServiceHandlesRecursiveCall {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
dispatch_queue_t testQueue = dispatch_queue_create("com.google.edotest", DISPATCH_QUEUE_SERIAL);
XCTestExpectation *expectation = [self expectationWithDescription:@"recursive call completes."];
__block NSUInteger recursiveLayer = 5;
dispatch_async(testQueue, ^{
[remoteDummy voidWithBlockAssignedAndInvoked:^{
if (recursiveLayer > 0) {
--recursiveLayer;
[remoteDummy invokeBlock];
}
}];
[expectation fulfill];
});
[self waitForExpectations:@[ expectation ] timeout:1.0];
}
- (void)testDispatchAsyncEarlyReturn {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOHostService *service =
[EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] initWithValue:9]
queue:dispatch_get_main_queue()];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
EDOTestDummyInTest *dummy = [[EDOTestDummyInTest alloc] initWithValue:7];
dispatch_semaphore_t waitLock = dispatch_semaphore_create(0L);
XCTestExpectation *expectsInvoke = [self expectationWithDescription:@"Invoked the block."];
dummy.block = ^{
dispatch_semaphore_wait(waitLock, DISPATCH_TIME_FOREVER);
[expectsInvoke fulfill];
};
// Test: [A returnPlus10AndAsyncExecuteBlock:B]
// App: dispatch_async: [B invokeBlock]
// Test: acquire waitLock
// -> 5 + 10
// Test: = 15
// release waitLock
// App: dispatch_async: finish
XCTAssertEqual([remoteDummy returnPlus10AndAsyncExecuteBlock:dummy], 5 + 10);
dispatch_semaphore_signal(waitLock);
[self waitForExpectationsWithTimeout:1 handler:nil];
[service invalidate];
}
- (void)testDispatchAsyncManyTimes {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:8];
NS_VALID_UNTIL_END_OF_SCOPE dispatch_queue_t backgroundQueue =
dispatch_queue_create("com.google.edo.uitest", DISPATCH_QUEUE_SERIAL);
EDOTestDummyInTest *rootDummy = [[EDOTestDummyInTest alloc] initWithValue:9];
EDOHostService *service = [EDOHostService serviceWithPort:2234
rootObject:rootDummy
queue:backgroundQueue];
const int totalOfInvokes = 10;
XCTestExpectation *expectsTen = [self expectationWithDescription:@"Invoked many times."];
expectsTen.expectedFulfillmentCount = totalOfInvokes;
rootDummy.block = ^{
// This block is dispatched to a background queue.
XCTAssertFalse([NSThread isMainThread]);
[expectsTen fulfill];
};
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
// Dispatch to the background queue because we need to wrap dummy into the remote object, the
// current main queue doesn't have a host service to wrap it.
dispatch_async(backgroundQueue, ^{
for (int i = 0; i < totalOfInvokes; ++i) {
XCTAssertEqual([remoteDummy returnPlus10AndAsyncExecuteBlock:rootDummy], 8 + 10);
}
});
[self waitForExpectationsWithTimeout:5 handler:nil];
[service invalidate];
}
- (void)testServiceInvalidErrorHandling {
// Terminate the app if other tests launched it.
[[[XCUIApplication alloc] init] terminate];
NSString *exceptionName = @"UITest Exception";
__block NSError *currentError;
EDOSetClientErrorHandler(^(NSError *error) {
currentError = error;
[NSException raise:exceptionName format:@"%ld", (long)error.code]; // NOLINT
});
XCTAssertThrowsSpecificNamed([EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT],
NSException, exceptionName);
XCTAssertEqualObjects(currentError.domain, EDOServiceErrorDomain);
XCTAssertEqual(currentError.code, EDOServiceErrorCannotConnect);
}
- (void)testRemoteObjectShouldFailAfterServiceTerminated {
NSString *exceptionName = @"UITest Exception";
__block NSError *currentError;
EDOSetClientErrorHandler(^(NSError *error) {
currentError = error;
[NSException raise:exceptionName format:@"%ld", (long)error.code]; // NOLINT
});
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:8];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertEqual(remoteDummy.value, 8);
[remoteDummy invalidateService];
// The remote object should fail after the remote service is down.
XCTAssertThrowsSpecificNamed([remoteDummy voidWithValuePlusOne], NSException, exceptionName);
XCTAssertEqualObjects(currentError.domain, EDOServiceErrorDomain);
XCTAssertEqual(currentError.code, EDOServiceErrorCannotConnect);
currentError = nil;
// The new service is created on the same port and the cached remote object from the previous
// service should fail.
// TODO(haowoo): Refactor this after moving to the new exception/error handler logic.
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:8];
XCTAssertThrowsSpecificNamed([remoteDummy voidWithValuePlusOne], NSException,
NSDestinationInvalidException);
}
- (void)testAllocAndClassMethod {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
// Create a local service so it can wrap and deref the any returned EDOObjects.
EDOHostService *service = [EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] init]
queue:dispatch_get_main_queue()];
// @see EDOTestClassDummyStub.m
XCTAssertEqual([EDOTestClassDummy classMethodWithInt:7], 16);
XCTAssertEqual([EDOTestClassDummy classMethodWithIdReturn:8].value, 8);
EDOTestClassDummy *dummy = [EDOTestClassDummy classMethodWithIdReturn:8];
XCTAssertEqualObjects([dummy class], NSClassFromString(@"EDOObject"));
[service invalidate];
// Validate the class methods don't exist locally.
unsigned int methodCount = 0;
Class dummyMeta = object_getClass([EDOTestClassDummy class]);
Method *methods = class_copyMethodList(dummyMeta, &methodCount);
for (unsigned int i = 0; i < methodCount; i++) {
Method method = methods[i];
char const *selectorName = sel_getName(method_getName(method));
// Make sure those class methods are not defined locally.
XCTAssertTrue(strcmp(selectorName, sel_getName(@selector(classMethodWithInt:))) != 0);
XCTAssertTrue(strcmp(selectorName, sel_getName(@selector(classMethodWithIdReturn:))) != 0);
}
free(methods);
}
/**
* Verifies `isKindOfClass:` returns true only if the class object belongs to the callee process.
*/
- (void)testIsKindOfClassOnlyResolvesInSameProcess {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
// Create a local service so it can wrap and deref the any returned EDOObjects.
EDOHostService *service = [EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] init]
queue:dispatch_get_main_queue()];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
NSArray<NSNumber *> *remoteArray = [remoteDummy returnArray];
EDOHostPort *hostPort = [EDOHostPort hostPortWithLocalPort:EDOTEST_APP_SERVICE_PORT];
Class remoteArrayClass = [EDOClientService classObjectWithName:@"NSArray" hostPort:hostPort];
XCTAssertTrue([remoteArray isKindOfClass:remoteArrayClass]);
XCTAssertFalse([remoteArray isKindOfClass:[NSArray class]]);
[service invalidate];
}
- (void)testRemoteObjectCopy {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
NSArray<NSNumber*> *remoteArray = [dummy returnArray];
NSArra<NSNumber*>y *remoteArrayCopy;
XCTAssertNoThrow(remoteArrayCopy = [remoteArray copy]);
XCTAssertEqual(remoteArray, remoteArrayCopy);
}
- (void)testRemoteObjectMutableCopy {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
NSArray<NSNumber*> *remoteArray = [dummy returnArray];
NSMutableArray<NSNumber*> *remoteArrayCopy = [remoteArray mutableCopy];
XCTAssertNotEqual(remoteArray, remoteArrayCopy);
XCTAssertEqualObjects(remoteArray, remoteArrayCopy);
[remoteArrayCopy addObject:@"test"];
XCTAssertEqualObjects([remoteArrayCopy lastObject], @"test");
}
- (void)testInsertRemoteObjectToDictionary {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
XCTAssertNoThrow([dict setObject:dummy forKey:@"key"]);
}
- (void)testBrokenChannelAfterServiceClosed {
XCUIApplication *app = [self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertEqualObjects([remoteDummy class], NSClassFromString(@"EDOObject"));
[app terminate];
app = [self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *newRemoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertNotEqualObjects(remoteDummy, newRemoteDummy);
__block Class clazz;
XCTestExpectation *expectsReturn = [self expectationWithDescription:@"Returned in time."];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
clazz = [newRemoteDummy class];
[expectsReturn fulfill];
});
[self waitForExpectationsWithTimeout:15 handler:nil];
XCTAssertEqualObjects(clazz, NSClassFromString(@"EDOObject"));
XCTAssertThrowsSpecificNamed([remoteDummy returnInt], NSException, NSDestinationInvalidException);
}
#if TARGET_IPHONE_SIMULATOR && !TARGET_OS_IPHONE
/**
* Tests requesting service ports info of the application process, and verifies the port info with
* service name.
* This test can only run on simulators, since on real device, with UTP runner the test will start
* a naming service on the same port.
* TODO(b/224637250): fix if it can be handled in the future.
*/
- (void)testFetchServicePortsInfo {
[self launchApplicationWithServiceName:kTestServiceName initValue:5];
EDOHostNamingService *service;
XCTAssertNoThrow(service =
[EDOClientService rootObjectWithPort:EDOHostNamingService.namingServerPort]);
XCTAssertFalse([service portForServiceWithName:kTestServiceName] == 0);
}
#endif // TARGET_IPHONE_SIMULATOR && !TARGET_OS_IPHONE
/** Tests running multiple naming services in the same host, and verifies that exception happens. */
- (void)testStartMultipleNamingServiceObject {
[self launchApplicationWithServiceName:kTestServiceName initValue:5];
EDOHostNamingService *localService = EDOHostNamingService.sharedService;
XCTAssertFalse([localService start]);
}
/** Tests passing local object to remote dummy using pass by value. */
- (void)testPassByValueWithLocalObject {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
NSArray<NSString *> *localArray = @[ @"foo" ];
dummy.valueObject = [localArray passByValue];
NSArray<NSString *> *remoteArray = (NSArray *)dummy.valueObject;
XCTAssertEqualObjects(localArray[0], remoteArray[0]);
NSString *remoteClassName = [dummy returnClassNameWithObject:remoteArray];
// Ensure that in the remote side, the object is passed by value instead of EDOObject.
XCTAssertNotEqualObjects(remoteClassName, @"EDOObject");
NSString *localClassName = NSStringFromClass(object_getClass(remoteArray));
// Ensure that in the local side, the object is fetched by reference as normal.
XCTAssertEqualObjects(localClassName, @"EDOObject");
}
/** Tests local object is released on the execution queue if it is released by EDOHostService. */
- (void)testLocalObjectReleaseOnHostExecutionQueue {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOHostService *service =
[EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] initWithValue:9]
queue:dispatch_get_main_queue()];
__block EDOTestDummyInTest *dummy = [[EDOTestDummyInTest alloc] init];
__weak EDOTestDummyInTest *weakDummy = dummy;
EDOTestDummy *remoteDummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTestExpectation *expectation = [self expectationWithDescription:@"dummy is released on main."];
dummy.deallocHandlerBlock = ^{
XCTAssertTrue([NSThread isMainThread]);
[expectation fulfill];
};
dummy.block = ^{
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dummy = nil;
});
};
[remoteDummy returnPlus10AndAsyncExecuteBlock:dummy];
[self waitForExpectations:@[ expectation ] timeout:2.0f];
XCTAssertNil(weakDummy);
[service invalidate];
}
- (void)testRemoteExceptionRevealsInformation {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOHostService *service =
[EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] initWithValue:9]
queue:dispatch_get_main_queue()];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
EDORemoteException *remoteException;
@try {
[dummy selWithThrow];
} @catch (EDORemoteException *exception) {
remoteException = exception;
}
XCTAssertNotNil(remoteException);
XCTAssertEqualObjects(remoteException.name, @"Dummy Just Throw 5");
XCTAssertEqualObjects(remoteException.reason, @"Just Throw");
NSString *callStackSymbols = [remoteException.callStackSymbols componentsJoinedByString:@"|"];
XCTAssertTrue([callStackSymbols containsString:@"testRemoteExceptionRevealsInformation"]);
XCTAssertTrue([callStackSymbols containsString:@"[EDOTestDummy selWithThrow]"]);
XCTAssertTrue([callStackSymbols containsString:@"TestsHost"]);
[service invalidate];
}
/** Verifies that eDO reveals decoding error at the server side. */
- (void)testDecodingErrorAtServerSideIsPropagated {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOHostService *service =
[EDOHostService serviceWithPort:2234
rootObject:[[EDOTestDummyInTest alloc] initWithValue:9]
queue:dispatch_get_main_queue()];
[self addTeardownBlock:^{
[service invalidate];
}];
EDOTestValueType *valueObject = [[EDOTestValueType alloc] init];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
XCTAssertEqualObjects([dummy returnClassNameWithObject:[valueObject passByValue]],
@"EDOTestValueType");
[NSKeyedArchiver setClassName:@"EDONotExist" forClass:[EDOTestValueType class]];
[self addTeardownBlock:^{
[NSKeyedArchiver setClassName:@"EDOTestValueType" forClass:[EDOTestValueType class]];
}];
id assertBlock = ^{
[dummy returnClassNameWithObject:[valueObject passByValue]];
};
[self assertBlock:assertBlock
throwsExceptionContaining:@"cannot decode object of class (EDONotExist)"];
}
/** Verifies that eDO reveals polished error message for containers encoding failure. */
- (void)testEncodeArrayThrowsExceptionWhenContainerItemsDoNotCodable {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
NSArray<id> *codableArray = @[ @(1), @"2", @[ @(3), @"4" ] ];
XCTAssertNoThrow([dummy voidWithObjectArray:[codableArray passByValue]]);
NSArray<id> *nonCodableArray = @[ @(1), @"2", @[ @(3), [[NSObject alloc] init] ] ];
XCTAssertNoThrow([dummy voidWithObjectArray:nonCodableArray]);
NSException *capturedException = nil;
@try {
[dummy voidWithObjectArray:[nonCodableArray passByValue]];
} @catch (NSException *exception) {
capturedException = exception;
}
XCTAssertNotNil(capturedException);
XCTAssertTrue([capturedException.reason
containsString:@"eDO fails to encode a parameter which is sent for remote invocation."]);
}
/** Verifies the errors are thrown when eDO making invocations with pointer arguments. */
- (void)testThrowExceptionForMethodsWithPointer {
[self launchApplicationWithPort:EDOTEST_APP_SERVICE_PORT initValue:5];
EDOTestDummy *dummy = [EDOClientService rootObjectWithPort:EDOTEST_APP_SERVICE_PORT];
NSException *capturedException;
int value = 0;
@try {
[dummy voidWithNullCPointer:&value];
} @catch (NSException *e) {
capturedException = e;
}
XCTAssertEqualObjects(capturedException.reason,
@"Failed to make remote invocation to [EDOTestDummy "
@"voidWithNullCPointer:]: the 1st parameter is a non-nil C pointer!");
@try {
[dummy returnIntPointer];
} @catch (NSException *e) {
capturedException = e;
}
XCTAssertEqualObjects(capturedException.reason,
@"Failed to make remote invocation to [EDOTestDummy returnIntPointer]: the "
@"return type is a pointer!");
}
@end