Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDKS-3229 DefaultStorageClient Stress Test (master) #431

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void testSha256Pinning() throws InterruptedException {
ServerConfig serverConfig = ServerConfig.builder()
.context(context)
.url("https://api.ipify.org")
.pin("Pf/hyG+ywUC8g3d1abF9kQn83lY6NwJUTUnqe03UMIY=")
.pin("2lFvaIHpsTcbb5uqa08S2k6wzLKscXXx1k1hKoX9R1Q=")
.build();

OkHttpClient client = OkHttpClientProvider.getInstance().lookup(serverConfig);
Expand Down Expand Up @@ -122,7 +122,7 @@ public void testMultiplePinning() throws InterruptedException {
ServerConfig serverConfig = ServerConfig.builder()
.context(context)
.url("https://api.ipify.org")
.pin("Pf/hyG+ywUC8g3d1abF9kQn83lY6NwJUTUnqe03UMIY=")
.pin("2lFvaIHpsTcbb5uqa08S2k6wzLKscXXx1k1hKoX9R1Q=")
.pin("invalid")
.build();

Expand Down Expand Up @@ -214,7 +214,7 @@ public void testBuildStepWithCustomPin() throws InterruptedException {
.context(context)
.url("https://api.ipify.org")
.buildStep(builder -> builder.certificatePinner(
new CertificatePinner.Builder().add("api.ipify.org", "sha1/KmdlCymwI4zbla1kcWu2fBEwKA8=" ).build()))
new CertificatePinner.Builder().add("api.ipify.org", "sha1/FAx66BsuUMrmrBnZ8F0GKxBZxLs=" ).build()))
.build();

OkHttpClient client = OkHttpClientProvider.getInstance().lookup(serverConfig);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2024 ForgeRock. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

package org.forgerock.android.auth;

import static org.junit.Assert.assertNotNull;

import android.content.Context;
import android.util.Log;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.forgerock.android.auth.exception.AccountLockException;
import org.forgerock.android.auth.exception.MechanismCreationException;
import org.forgerock.android.auth.exception.OathMechanismException;
import org.forgerock.android.auth.util.Base32String;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RunWith(AndroidJUnit4.class)
public class DefaultStorageClientStressTest {

private final Context context = ApplicationProvider.getApplicationContext();
private final Random rand = new Random();

private static final String[] ALGORITHMS = {"SHA1", "SHA256", "SHA512"};
private static final String ISSUER = "ISSUER_";
private static final String ACCOUNT_NAME = "ACCOUNT_NAME_";
private static final String TAG = DefaultStorageClientStressTest.class.getSimpleName();

private DefaultStorageClient defaultStorage;

@Before
public void setUp() {
defaultStorage = new DefaultStorageClient(context);
OathCodeGenerator.getInstance(defaultStorage);
}

@After
public void cleanUp() {
defaultStorage.removeAll();
}

@Test
public void testStoreOneHundredAccounts() throws InterruptedException {
int numberOfAccounts = 50;
int numberOfThreads = 4;
ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
CountDownLatch latch = new CountDownLatch(numberOfThreads);

for (int i = 0; i < numberOfThreads; i++) {
int threadIndex = i;
service.execute(() -> {
try {
storeAndRetrieveAccounts(threadIndex, numberOfAccounts);
} catch (Exception e) {
throw new RuntimeException(e);
}
latch.countDown();
});
}

latch.await();
service.shutdown();
}

private void storeAndRetrieveAccounts(int threadIndex,int numberOfAccounts)
throws MechanismCreationException, OathMechanismException, AccountLockException {
long startTime = System.currentTimeMillis();

// Create accounts
for (int i = 0; i < numberOfAccounts; i++) {
String issuer = ISSUER + threadIndex + "_" + i;
String accountName = ACCOUNT_NAME + threadIndex + "_" + i;
OathMechanism.TokenType tokenType = getRandomTokenType();
int period = getRandomPeriod();
int digits = getRandomDigits();
int counter = getRandomCounter();
String algorithm = getRandomAlgorithm();
String mechanismUid = getRandomMechanismUid();
String secret = getRandomSharedSecret();

Account account = MockModelBuilder.createAccount(issuer, accountName);
Mechanism mechanism = MockModelBuilder.createOath(mechanismUid, issuer, accountName,
tokenType, algorithm, secret, digits, counter, period);
defaultStorage.setAccount(account);
defaultStorage.setMechanism(mechanism);
}

// Retrieve and verify accounts
for (int i = 0; i < numberOfAccounts; i++) {
String issuer = ISSUER + threadIndex + "_" + i;
String accountName = ACCOUNT_NAME + threadIndex + "_" + i;
// Verify account
Account account = defaultStorage.getAccount(issuer + "-" + accountName);
assertNotNull(account);
// Verify mechanism
List<Mechanism> mechanismList = defaultStorage.getMechanismsForAccount(account);
OathMechanism mechanism = (OathMechanism) mechanismList.get(0);
assertNotNull(mechanism);
// Verify token code
OathTokenCode tokenCode = mechanism.getOathTokenCode();
assertNotNull(tokenCode);
}

Log.d(TAG, "Stored and retrieved " + numberOfAccounts +
" accounts in " + (System.currentTimeMillis() - startTime)/1000 + " seconds");
}

private String getRandomSharedSecret() {
int sharedSecretByteLength = Math.max(8, (int) Math.ceil(32 / 2d));
byte[] sharedSecret = new byte[sharedSecretByteLength];
rand.nextBytes(sharedSecret);
return Base32String.encode(sharedSecret);
}

private int getRandomDigits() {
return rand.nextBoolean() ? 6 : 8;
}

private int getRandomCounter() {
return rand.nextInt(200);
}

private int getRandomPeriod() {
return rand.nextBoolean() ? 30 : 60;
}

private String getRandomAlgorithm() {
return ALGORITHMS[rand.nextInt(ALGORITHMS.length)];
}

private String getRandomMechanismUid() {
return UUID.randomUUID().toString();
}

private OathMechanism.TokenType getRandomTokenType() {
return rand.nextBoolean() ? OathMechanism.TokenType.HOTP : OathMechanism.TokenType.TOTP;
}

}
Loading