|
| 1 | +// |
| 2 | +// Copyright 2010-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | +// You may not use this file except in compliance with the License. |
| 6 | +// A copy of the License is located at |
| 7 | +// |
| 8 | +// http://aws.amazon.com/apache2.0 |
| 9 | +// |
| 10 | +// or in the "license" file accompanying this file. This file is distributed |
| 11 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | +// express or implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +#if !AWS_TEST_BJS_INSTEAD |
| 17 | + |
| 18 | +#import <XCTest/XCTest.h> |
| 19 | +#import "AWSCore.h" |
| 20 | +#import "AWSTestUtility.h" |
| 21 | + |
| 22 | +NSString *TestsIdentityPoolId = nil; |
| 23 | + |
| 24 | +@interface AWSUnAuthenticatedIdentityProvider : NSObject<AWSIdentityProvider, AWSIdentityProviderManager> |
| 25 | + |
| 26 | +@end |
| 27 | + |
| 28 | +@implementation AWSUnAuthenticatedIdentityProvider |
| 29 | + |
| 30 | +- (NSString *)identityProviderName { |
| 31 | + return nil; |
| 32 | +} |
| 33 | + |
| 34 | +- (AWSTask<NSString *> *)token { |
| 35 | + return [AWSTask taskWithResult: nil]; |
| 36 | +} |
| 37 | + |
| 38 | +- (AWSTask<NSDictionary<NSString *, NSString *> *> *)logins { |
| 39 | + return [[self token] continueWithSuccessBlock:^id _Nullable(AWSTask<NSString *> * _Nonnull task) { |
| 40 | + return [AWSTask taskWithResult:[NSDictionary new]]; |
| 41 | + }]; |
| 42 | +} |
| 43 | + |
| 44 | +@end |
| 45 | + |
| 46 | +@interface AWSCognitoCredentialsProviderConcurrencyTests : XCTestCase |
| 47 | + |
| 48 | +@property AWSRegionType region; |
| 49 | + |
| 50 | +@end |
| 51 | + |
| 52 | +@implementation AWSCognitoCredentialsProviderConcurrencyTests |
| 53 | + |
| 54 | +#pragma mark - Set up/Tear down |
| 55 | + |
| 56 | ++ (void)setUp { |
| 57 | + [super setUp]; |
| 58 | + [AWSTestUtility setupSessionCredentialsProvider]; |
| 59 | + AWSServiceConfiguration *configuration = [AWSServiceManager defaultServiceManager].defaultServiceConfiguration; |
| 60 | + [AWSCognitoIdentity registerCognitoIdentityWithConfiguration:configuration |
| 61 | + forKey:@"Session"]; |
| 62 | + |
| 63 | + NSDictionary *testConfig = [AWSTestUtility getIntegrationTestConfigurationForPackageId: @"core"]; |
| 64 | + TestsIdentityPoolId = testConfig[@"identityPoolId"]; |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +- (void)setUp { |
| 69 | + [super setUp]; |
| 70 | + self.region = [AWSTestUtility getRegionFromTestConfiguration]; |
| 71 | +} |
| 72 | + |
| 73 | +- (void)tearDown { |
| 74 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 75 | + AWSCognitoCredentialsProvider *provider = [[AWSCognitoCredentialsProvider alloc] |
| 76 | + initWithRegionType:self.region |
| 77 | + identityPoolId:TestsIdentityPoolId]; |
| 78 | + [provider clearKeychain]; |
| 79 | + [super tearDown]; |
| 80 | +} |
| 81 | + |
| 82 | +- (void)testMultiThreadedWithEnhancedFlow { |
| 83 | + AWSCognitoCredentialsProviderHelper *identityProvider = [[AWSCognitoCredentialsProviderHelper alloc] |
| 84 | + initWithRegionType: self.region |
| 85 | + identityPoolId:TestsIdentityPoolId |
| 86 | + useEnhancedFlow:YES |
| 87 | + identityProviderManager:[AWSUnAuthenticatedIdentityProvider new]]; |
| 88 | + |
| 89 | + AWSCognitoCredentialsProvider *provider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:self.region |
| 90 | + identityProvider:identityProvider]; |
| 91 | + |
| 92 | + dispatch_queue_t processingQueue = dispatch_queue_create("com.amazon.testqueue", DISPATCH_QUEUE_CONCURRENT); |
| 93 | + |
| 94 | + XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription: @"Credential succeeded"]; |
| 95 | + [expectation setExpectedFulfillmentCount:100]; |
| 96 | + for (int i = 0; i < 100; i++) { |
| 97 | + |
| 98 | + dispatch_async (processingQueue, ^{ |
| 99 | + [provider clearCredentials]; |
| 100 | + [identityProvider setIdentityId:nil]; |
| 101 | + [[provider credentials] continueWithBlock:^id(AWSTask *task) { |
| 102 | + XCTAssertNotNil(provider.identityId, @"Unable to get identityId"); |
| 103 | + |
| 104 | + AWSCredentials *credentials = task.result; |
| 105 | + XCTAssertNotNil(credentials.secretKey); |
| 106 | + XCTAssertNotNil(credentials.sessionKey); |
| 107 | + XCTAssertNotNil(credentials.expiration); |
| 108 | + [expectation fulfill]; |
| 109 | + return nil; |
| 110 | + }] ; |
| 111 | + |
| 112 | + }); |
| 113 | + } |
| 114 | + [self waitForExpectations:[NSArray arrayWithObject:expectation] timeout:10]; |
| 115 | + |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +@end |
| 120 | + |
| 121 | +#endif |
0 commit comments