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