pEpObjCTests/PEPSessionProviderTest.m
author Dirk Zimmermann <dz@pep.security>
Thu, 21 Mar 2019 12:45:12 +0100
branchIOS-1480
changeset 904 fe9563f1982b
parent 738 7ac89df4a502
permissions -rw-r--r--
IOS-1480 Close branch.
andreas@389
     1
//
andreas@389
     2
//  PEPSessionProviderTest.m
andreas@389
     3
//  pEpObjCAdapterTests
andreas@389
     4
//
andreas@389
     5
//  Created by Andreas Buff on 17.01.18.
andreas@389
     6
//  Copyright © 2018 p≡p. All rights reserved.
andreas@389
     7
//
andreas@389
     8
andreas@389
     9
#import <XCTest/XCTest.h>
andreas@389
    10
andreas@389
    11
#import "PEPSession.h"
andreas@389
    12
#import "PEPInternalSession.h"
andreas@389
    13
#import "PEPSessionProvider.h"
andreas@389
    14
andreas@389
    15
@interface PEPSessionProviderTest : XCTestCase
andreas@389
    16
andreas@389
    17
@end
andreas@389
    18
andreas@389
    19
@implementation PEPSessionProviderTest
andreas@389
    20
dirk@738
    21
- (void)tearDown
dirk@738
    22
{
dirk@738
    23
    [PEPSession cleanup];
dirk@738
    24
}
dirk@738
    25
andreas@389
    26
- (void)testSeperatedSessionPerThread {
andreas@389
    27
    // Get main session
andreas@389
    28
    PEPInternalSession *sessionMain = [PEPSessionProvider session];
andreas@389
    29
    __block PEPInternalSession *sessionBackground = nil;
andreas@389
    30
    XCTestExpectation *exp = [self expectationWithDescription:@"background session created"];
andreas@389
    31
andreas@389
    32
    // Get background session
andreas@389
    33
    dispatch_queue_t backgroundQueue = dispatch_queue_create("PEPSessionProviderTest.peptest1", 0);
andreas@389
    34
    dispatch_async(backgroundQueue, ^{
andreas@389
    35
        sessionBackground = [PEPSessionProvider session];
andreas@389
    36
        [exp fulfill];
andreas@389
    37
    });
andreas@389
    38
andreas@389
    39
    [self waitForExpectationsWithTimeout:1.0 handler:^(NSError * _Nullable error) {
andreas@389
    40
        if (error) { XCTFail(@"timeout: %@", error); }
andreas@389
    41
    }];
andreas@389
    42
    XCTAssertNotNil(sessionMain);
andreas@389
    43
    XCTAssertNotNil(sessionBackground);
andreas@389
    44
andreas@389
    45
    // Make sure we have seperated sessions
andreas@389
    46
    XCTAssertNotEqual(sessionBackground, sessionMain,
andreas@389
    47
                      @"We should have seperated sessions, one per thread");
andreas@389
    48
}
andreas@389
    49
andreas@389
    50
- (void)testMainSessionDoesNotChange {
andreas@389
    51
    // Get main session
andreas@389
    52
    PEPInternalSession *sessionMain = [PEPSessionProvider session];
andreas@389
    53
    __block PEPInternalSession *sessionBackground = nil;
andreas@389
    54
    XCTestExpectation *exp = [self expectationWithDescription:@"background session created"];
andreas@389
    55
andreas@389
    56
    // Get background session
andreas@389
    57
    dispatch_queue_t backgroundQueue = dispatch_queue_create("PEPSessionProviderTest.peptest1", 0);
andreas@389
    58
    dispatch_async(backgroundQueue, ^{
andreas@389
    59
        sessionBackground = [PEPSessionProvider session];
andreas@389
    60
        [exp fulfill];
andreas@389
    61
    });
andreas@389
    62
andreas@389
    63
    [self waitForExpectationsWithTimeout:1.0 handler:^(NSError * _Nullable error) {
andreas@389
    64
        if (error) { XCTFail(@"timeout: %@", error); }
andreas@389
    65
    }];
andreas@389
    66
    // Get main session again
andreas@389
    67
    PEPInternalSession *sessionMain2 = [PEPSessionProvider session];
andreas@389
    68
    XCTAssertNotNil(sessionMain);
andreas@389
    69
    XCTAssertNotNil(sessionMain2);
andreas@389
    70
    XCTAssertNotNil(sessionBackground);
andreas@389
    71
    XCTAssertEqual(sessionMain, sessionMain2, @"The main session stayed the same (was kept \
andreas@389
    72
                   alive, was not recreated)");
andreas@389
    73
}
andreas@389
    74
andreas@389
    75
- (void)testNewMainSessionAfterCleanup {
andreas@389
    76
    // Get main session
andreas@389
    77
    PEPInternalSession *sessionMain = [PEPSessionProvider session];
andreas@389
    78
    __block PEPInternalSession *sessionBackground = nil;
andreas@389
    79
    XCTestExpectation *exp = [self expectationWithDescription:@"background session created"];
andreas@389
    80
andreas@389
    81
    // Get background session
andreas@389
    82
    dispatch_queue_t backgroundQueue = dispatch_queue_create("PEPSessionProviderTest.peptest1", 0);
andreas@389
    83
    dispatch_async(backgroundQueue, ^{
andreas@389
    84
        sessionBackground = [PEPSessionProvider session];
andreas@389
    85
        [exp fulfill];
andreas@389
    86
    });
andreas@389
    87
andreas@389
    88
    [self waitForExpectationsWithTimeout:1.0 handler:^(NSError * _Nullable error) {
andreas@389
    89
        if (error) { XCTFail(@"timeout: %@", error); }
andreas@389
    90
    }];
andreas@389
    91
    // Get main session again
andreas@389
    92
    PEPInternalSession *sessionMain2 = [PEPSessionProvider session];
andreas@389
    93
    XCTAssertNotNil(sessionMain);
andreas@389
    94
    XCTAssertNotNil(sessionMain2);
andreas@389
    95
    XCTAssertNotNil(sessionBackground);
andreas@389
    96
    XCTAssertEqual(sessionMain, sessionMain2, @"The main session stayed the same (was kept \
andreas@389
    97
                   alive, was not recreated)");
andreas@389
    98
    [PEPSession cleanup];
andreas@389
    99
    PEPInternalSession *sessionMainAfterCleanup = [PEPSessionProvider session];
andreas@389
   100
    XCTAssertNotNil(sessionMainAfterCleanup);
andreas@389
   101
    XCTAssertNotEqual(sessionMainAfterCleanup, sessionMain,
andreas@389
   102
                      @"We got a new main session after cleanup");
andreas@389
   103
}
andreas@389
   104
andreas@389
   105
@end
andreas@389
   106