Skip to content

Commit

Permalink
#360 - Create stress/load test which should cover all APIs with mocke…
Browse files Browse the repository at this point in the history
…d OP
  • Loading branch information
duttarnab committed Sep 4, 2019
1 parent 52647ef commit ae226f7
Show file tree
Hide file tree
Showing 20 changed files with 395 additions and 26 deletions.
6 changes: 6 additions & 0 deletions oxd-server/pom.xml
Expand Up @@ -442,6 +442,12 @@
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Expand Up @@ -3,6 +3,7 @@
*/
package org.gluu.oxd.server;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class ServerLauncher {
*/
private static final Logger LOG = LoggerFactory.getLogger(ServerLauncher.class);

private static final Injector INJECTOR = Guice.createInjector(new GuiceModule());
private static Injector INJECTOR = Guice.createInjector(new GuiceModule());
private static boolean setUpSuite = false;

public static void configureServices(OxdServerConfiguration configuration) {
Expand Down Expand Up @@ -127,6 +128,10 @@ public static Injector getInjector() {
return INJECTOR;
}

public static void setInjector(AbstractModule module) {
INJECTOR = Guice.createInjector(module);
}

public static boolean isSetUpSuite() {
return setUpSuite;
}
Expand Down
Expand Up @@ -6,6 +6,8 @@
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import org.gluu.oxd.server.OxdServerConfiguration;
import org.gluu.oxd.server.op.OpClientFactory;
import org.gluu.oxd.server.op.OpClientFactoryImpl;
import org.gluu.oxd.server.persistence.H2PersistenceProvider;
import org.gluu.oxd.server.persistence.PersistenceService;
import org.gluu.oxd.server.persistence.PersistenceServiceImpl;
Expand Down Expand Up @@ -34,5 +36,6 @@ protected void configure() {
bind(DiscoveryService.class).in(Singleton.class);
bind(ValidationService.class).in(Singleton.class);
bind(StateService.class).in(Singleton.class);
bind(OpClientFactory.class).to(OpClientFactoryImpl.class).in(Singleton.class);
}
}
Expand Up @@ -99,6 +99,10 @@ public OxAuthCryptoProvider getCryptoProvider() throws Exception {
return new OxAuthCryptoProvider(conf.getCryptProviderKeyStorePath(), conf.getCryptProviderKeyStorePassword(), conf.getCryptProviderDnName());
}

public OpClientFactory getOpClientFactory() {
return getInstance(OpClientFactory.class);
}

public Rp getRp() {
if (params instanceof HasOxdIdParams) {
getValidationService().validate((HasOxdIdParams) params);
Expand Down
Expand Up @@ -56,7 +56,7 @@ public IOpResponse execute(GetAuthorizationCodeParams params) {
getStateService().putNonce(nonce);
getStateService().putState(state);

final AuthorizeClient authorizeClient = new AuthorizeClient(getDiscoveryService().getConnectDiscoveryResponse(site).getAuthorizationEndpoint());
final AuthorizeClient authorizeClient = getOpClientFactory().createAuthorizeClient(getDiscoveryService().getConnectDiscoveryResponse(site).getAuthorizationEndpoint());
authorizeClient.setRequest(request);
authorizeClient.setExecutor(getHttpService().getClientExecutor());
final AuthorizationResponse response = authorizeClient.exec();
Expand Down
Expand Up @@ -45,7 +45,7 @@ public IOpResponse execute(GetClientTokenParams params) {
try {
final AuthenticationMethod authenticationMethod = AuthenticationMethod.fromString(params.getAuthenticationMethod());
final String tokenEndpoint = getDiscoveryService().getConnectDiscoveryResponse(params.getOpHost(), params.getOpDiscoveryPath()).getTokenEndpoint();
final TokenClient tokenClient = new TokenClient(tokenEndpoint);
final TokenClient tokenClient = getOpClientFactory().createTokenClient(tokenEndpoint);
tokenClient.setExecutor(getHttpService().getClientExecutor());

final TokenResponse tokenResponse;
Expand Down
Expand Up @@ -50,7 +50,7 @@ public IOpResponse execute(GetTokensByCodeParams params) throws Exception {
tokenRequest.setAuthenticationMethod(AuthenticationMethod.CLIENT_SECRET_BASIC);


final TokenClient tokenClient = new TokenClient(discoveryResponse.getTokenEndpoint());
final TokenClient tokenClient = getOpClientFactory().createTokenClient(discoveryResponse.getTokenEndpoint());
tokenClient.setExecutor(getHttpService().getClientExecutor());
tokenClient.setRequest(tokenRequest);
final TokenResponse response = tokenClient.exec();
Expand All @@ -70,8 +70,7 @@ public IOpResponse execute(GetTokensByCodeParams params) throws Exception {
}

final Jwt idToken = Jwt.parse(response.getIdToken());

final Validator validator = new Validator(idToken, discoveryResponse, getKeyService());
final Validator validator = getOpClientFactory().createValidator(idToken, discoveryResponse, getKeyService());
validator.validateNonce(getStateService());
validator.validateIdToken(site.getClientId());
validator.validateAccessToken(response.getAccessToken());
Expand Down
Expand Up @@ -34,7 +34,7 @@ protected GetUserInfoOperation(Command command, final Injector injector) {
public IOpResponse execute(GetUserInfoParams params) throws IOException {
getValidationService().validate(params);

UserInfoClient client = new UserInfoClient(getDiscoveryService().getConnectDiscoveryResponseByOxdId(params.getOxdId()).getUserInfoEndpoint());
UserInfoClient client = getOpClientFactory().createUserInfoClient(getDiscoveryService().getConnectDiscoveryResponseByOxdId(params.getOxdId()).getUserInfoEndpoint());
client.setExecutor(getHttpService().getClientExecutor());
client.setRequest(new UserInfoRequest(params.getAccessToken()));

Expand Down
@@ -0,0 +1,19 @@
package org.gluu.oxd.server.op;

import org.gluu.oxauth.client.*;
import org.gluu.oxauth.model.jwt.Jwt;
import org.gluu.oxd.server.service.PublicOpKeyService;

public interface OpClientFactory {
public TokenClient createTokenClient(String url);

public UserInfoClient createUserInfoClient(String url);

public RegisterClient createRegisterClient(String url);

public OpenIdConfigurationClient createOpenIdConfigurationClient(String url);

public AuthorizeClient createAuthorizeClient(String url);

public Validator createValidator(Jwt idToken, OpenIdConfigurationResponse discoveryResponse, PublicOpKeyService keyService);
}
@@ -0,0 +1,36 @@
package org.gluu.oxd.server.op;

import org.gluu.oxauth.client.*;
import org.gluu.oxauth.model.jwt.Jwt;
import org.gluu.oxd.server.service.PublicOpKeyService;

public class OpClientFactoryImpl implements OpClientFactory {

public OpClientFactoryImpl() {
}

public TokenClient createTokenClient(String url) {
return new TokenClient(url);
}

public UserInfoClient createUserInfoClient(String url) {
return new UserInfoClient(url);
}

public RegisterClient createRegisterClient(String url) {
return new RegisterClient(url);
}

public OpenIdConfigurationClient createOpenIdConfigurationClient(String url) {
return new OpenIdConfigurationClient(url);
}

public AuthorizeClient createAuthorizeClient(String url) {
return new AuthorizeClient(url);
}

public Validator createValidator(Jwt idToken, OpenIdConfigurationResponse discoveryResponse, PublicOpKeyService keyService) {
return new Validator(idToken, discoveryResponse, keyService);
}

}
Expand Up @@ -4,10 +4,10 @@
package org.gluu.oxd.server.op;

import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gluu.oxd.common.Command;
import org.gluu.oxd.common.params.IParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Yuriy Zabrovarnyy
Expand Down
Expand Up @@ -245,7 +245,7 @@ private RegisterResponse registerClient(RegisterSiteParams params) {
throw new HttpException(ErrorResponseCode.NO_REGISTRATION_ENDPOINT);
}

final RegisterClient registerClient = new RegisterClient(registrationEndpoint);
final RegisterClient registerClient = getOpClientFactory().createRegisterClient(registrationEndpoint);
registerClient.setRequest(createRegisterClientRequest(params));
registerClient.setExecutor(getHttpService().getClientExecutor());
final RegisterResponse response = registerClient.exec();
Expand Down
@@ -0,0 +1,116 @@
package org.gluu.oxd.mock;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.gluu.oxauth.model.common.GrantType;
import org.gluu.oxd.client.ClientInterface;
import org.gluu.oxd.client.GetTokensByCodeResponse2;
import org.gluu.oxd.common.CoreUtils;
import org.gluu.oxd.common.params.*;
import org.gluu.oxd.common.response.GetLogoutUriResponse;
import org.gluu.oxd.common.response.RegisterSiteResponse;
import org.gluu.oxd.server.GetTokensByCodeTest;
import org.gluu.oxd.server.RegisterSiteTest;
import org.gluu.oxd.server.Tester;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.net.URLEncoder;
import java.util.UUID;

import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.gluu.oxd.server.TestUtils.notEmpty;

/**
* @author Yuriy Zabrovarnyy
* @version 0.9, 12/10/2015
*/

public class AuthorizationCodeFlowTest {

@Parameters({"host", "opHost", "redirectUrls", "userId", "userSecret"})
@Test(enabled = false)
public void mockTest(String host, String opHost, String redirectUrls, String userId, String userSecret) {
ClientInterface client = Tester.newClient(host);
//Register client
final RegisterSiteResponse site = registerSite(client, opHost, redirectUrls);
//Get Token by code
final GetTokensByCodeResponse2 tokens = requestTokens(client, site, userId, userSecret);
//Get User Info
GetUserInfoParams params = new GetUserInfoParams();
params.setOxdId(site.getOxdId());
params.setAccessToken(tokens.getAccessToken());

final JsonNode resp = client.getUserInfo(Tester.getAuthorization(), params);
assertNotNull(resp);
assertNotNull(resp.get("sub"));

//Get Logout Url
getLogoutUrl(client, site, redirectUrls);

}
public static RegisterSiteResponse registerSite(ClientInterface client, String opHost, String redirectUrls) {
return registerSite(client, opHost, redirectUrls, redirectUrls, "");
}

public static RegisterSiteResponse registerSite(ClientInterface client, String opHost, String redirectUrls, String postLogoutRedirectUrls, String logoutUri) {

final RegisterSiteParams params = new RegisterSiteParams();
params.setOpHost(opHost);
params.setPostLogoutRedirectUris(Lists.newArrayList(postLogoutRedirectUrls.split(" ")));
params.setClientFrontchannelLogoutUris(Lists.newArrayList(logoutUri));
params.setRedirectUris(Lists.newArrayList(redirectUrls.split(" ")));
params.setScope(Lists.newArrayList("openid", "uma_protection", "profile"));
params.setTrustedClient(true);
params.setGrantTypes(Lists.newArrayList(
GrantType.AUTHORIZATION_CODE.getValue(),
GrantType.OXAUTH_UMA_TICKET.getValue(),
GrantType.CLIENT_CREDENTIALS.getValue()));

final RegisterSiteResponse resp = client.registerSite(params);
//assertNotNull(resp);
//assertTrue(!Strings.isNullOrEmpty(resp.getOxdId()));
return resp;
}
private GetTokensByCodeResponse2 requestTokens(ClientInterface client, RegisterSiteResponse site, String userId, String userSecret) {

final String state = CoreUtils.secureRandomString();
final String nonce = "rrsenukn8h9hjmscjf5gq9i5tu";

final GetTokensByCodeParams params = new GetTokensByCodeParams();
params.setOxdId(site.getOxdId());
params.setCode(codeRequest(client, site.getOxdId(), userId, userSecret, state, nonce));
params.setState(state);

final GetTokensByCodeResponse2 resp = client.getTokenByCode(Tester.getAuthorization(), params);
assertNotNull(resp);
notEmpty(resp.getAccessToken());
notEmpty(resp.getIdToken());
return resp;
}

public static String codeRequest(ClientInterface client, String siteId, String userId, String userSecret, String state, String nonce) {
GetAuthorizationCodeParams params = new GetAuthorizationCodeParams();
params.setOxdId(siteId);
params.setUsername(userId);
params.setPassword(userSecret);
params.setState(state);
params.setNonce(nonce);

return client.getAuthorizationCode(Tester.getAuthorization(), params).getCode();
}

public static void getLogoutUrl(ClientInterface client, RegisterSiteResponse site, String postLogoutRedirectUrl) {
final GetLogoutUrlParams logoutParams = new GetLogoutUrlParams();
logoutParams.setOxdId(site.getOxdId());
logoutParams.setIdTokenHint("dummy_token");
logoutParams.setPostLogoutRedirectUri(postLogoutRedirectUrl);
logoutParams.setState(UUID.randomUUID().toString());
logoutParams.setSessionState(UUID.randomUUID().toString()); // here must be real session instead of dummy UUID

final GetLogoutUriResponse resp = client.getLogoutUri(Tester.getAuthorization(), logoutParams);
assertNotNull(resp);
}
}
@@ -0,0 +1,33 @@
package org.gluu.oxd.mock.guice;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import org.gluu.oxd.mock.service.OpClientFactoryMockImpl;
import org.gluu.oxd.server.OxdServerConfiguration;
import org.gluu.oxd.server.op.OpClientFactory;
import org.gluu.oxd.server.persistence.H2PersistenceProvider;
import org.gluu.oxd.server.persistence.PersistenceService;
import org.gluu.oxd.server.persistence.PersistenceServiceImpl;
import org.gluu.oxd.server.persistence.SqlPersistenceProvider;
import org.gluu.oxd.server.service.*;

public class MockAppModule extends AbstractModule {

@Override
protected void configure() {
bind(OxdServerConfiguration.class).toProvider(ConfigurationService.class);

bind(ConfigurationService.class).in(Singleton.class);
bind(PublicOpKeyService.class).in(Singleton.class);
bind(RpService.class).in(Singleton.class);
bind(HttpService.class).in(Singleton.class);
bind(IntrospectionService.class).in(Singleton.class);
bind(SqlPersistenceProvider.class).to(H2PersistenceProvider.class).in(Singleton.class);
bind(PersistenceService.class).to(PersistenceServiceImpl.class).in(Singleton.class);
bind(MigrationService.class).in(Singleton.class);
bind(DiscoveryService.class).in(Singleton.class);
bind(ValidationService.class).in(Singleton.class);
bind(StateService.class).in(Singleton.class);
bind(OpClientFactory.class).to(OpClientFactoryMockImpl.class).in(Singleton.class);
}
}
@@ -0,0 +1,35 @@
package org.gluu.oxd.mock.listener;

import org.testng.IAnnotationTransformer;
import org.testng.ITest;
import org.testng.annotations.ITestAnnotation;
import org.testng.annotations.Parameters;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class EnableMocksListener implements IAnnotationTransformer {

@Override
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod){

System.out.println("===============>"+testMethod.getName());
System.out.println("***************>"+isTestDisabled(testMethod.getName()));
if (isTestDisabled(testMethod.getName())) {
annotation.setEnabled(false);
} else {
annotation.setEnabled(true);
}
}

public boolean isTestDisabled(String testName){
if(testName.startsWith("mock")) {
return false;
} else {
return true;
}
}


}

0 comments on commit ae226f7

Please sign in to comment.