use of com.tvd12.ezyfoxserver.setting.EzySettings in project ezyfox-server by youngmonkeys.
the class EzySimpleDataHandlerTest method handleRequestExceptionCase1.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void handleRequestExceptionCase1() throws Exception {
int zoneId = 1;
EzyServerContext serverContext = mock(EzyServerContext.class);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
EzyZone zone = mock(EzyZone.class);
when(zoneContext.getZone()).thenReturn(zone);
EzyZoneUserManager zoneUserManager = mock(EzyZoneUserManager.class);
when(zone.getUserManager()).thenReturn(zoneUserManager);
when(serverContext.getZoneContext(zoneId)).thenReturn(zoneContext);
EzyAbstractSession session = spy(EzyAbstractSession.class);
EzyChannel channel = mock(EzyChannel.class);
when(session.getChannel()).thenReturn(channel);
EzyServer server = mock(EzyServer.class);
when(serverContext.getServer()).thenReturn(server);
EzyServerControllers controllers = mock(EzyServerControllers.class);
EzyInterceptor streamingInterceptor = mock(EzyInterceptor.class);
when(controllers.getStreamingInterceptor()).thenReturn(streamingInterceptor);
EzyStreamingController streamingController = mock(EzyStreamingController.class);
when(controllers.getStreamingController()).thenReturn(streamingController);
EzyInterceptor loginInterceptor = mock(EzyInterceptor.class);
when(controllers.getInterceptor(EzyCommand.LOGIN)).thenReturn(loginInterceptor);
EzyController loginController = mock(EzyController.class);
when(controllers.getController(EzyCommand.LOGIN)).thenReturn(loginController);
when(server.getControllers()).thenReturn(controllers);
EzySessionManager sessionManager = mock(EzySessionManager.class);
when(server.getSessionManager()).thenReturn(sessionManager);
EzyCloseSession closeSession = mock(EzyCloseSession.class);
when(serverContext.get(EzyCloseSession.class)).thenReturn(closeSession);
EzySettings settings = mock(EzySettings.class);
when(settings.isDebug()).thenReturn(true);
when(server.getSettings()).thenReturn(settings);
EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
when(settings.getLogger()).thenReturn(loggerSetting);
EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
when(ignoredCommandsSetting.getCommands()).thenReturn(new HashSet<>());
EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
when(maxRequestPerSecond.getValue()).thenReturn(3);
when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
MyTestDataHandler handler = new MyTestDataHandler(serverContext, session);
when(session.getDelegate()).thenReturn(handler);
when(session.isActivated()).thenReturn(true);
EzyArray loginData = EzyEntityFactory.newArrayBuilder().append("zone").append("username").append("password").append(EzyEntityFactory.newObject()).build();
EzyArray requestData = EzyEntityFactory.newArrayBuilder().append(EzyCommand.LOGIN.getId()).append(loginData).build();
doThrow(new IllegalStateException("server maintain")).when(loginInterceptor).intercept(any(), any());
FieldUtil.setFieldValue(handler, "context", null);
handler.dataReceived(EzyCommand.LOGIN, requestData);
}
use of com.tvd12.ezyfoxserver.setting.EzySettings in project ezyfox-server by youngmonkeys.
the class EzySimpleDataHandlerTest method debugLogReceivedDataIsNotDebug.
@Test
public void debugLogReceivedDataIsNotDebug() {
// given
MyTestDataHandler sut = createHandler();
EzySettings settings = FieldUtil.getFieldValue(sut, "settings");
when(settings.isDebug()).thenReturn(false);
// when
MethodInvoker.create().object(sut).method("debugLogReceivedData").param(EzyConstant.class, EzyCommand.APP_ACCESS).param(EzyArray.class, EzyEntityFactory.EMPTY_ARRAY).call();
// then
verify(settings, times(1)).isDebug();
}
use of com.tvd12.ezyfoxserver.setting.EzySettings in project ezyfox-server by youngmonkeys.
the class EzySimpleDataHandlerTest method processMaxRequestPerSecondDisconnect.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void processMaxRequestPerSecondDisconnect() {
// given
EzySession session = spy(EzyAbstractSession.class);
EzyServerContext serverContext = mock(EzyServerContext.class);
EzyServer server = mock(EzyServer.class);
EzySessionManager sessionManager = mock(EzySessionManager.class);
when(server.getSessionManager()).thenReturn(sessionManager);
when(serverContext.getServer()).thenReturn(server);
EzySessionManagementSetting sessionManagementSetting = mock(EzySessionManagementSetting.class);
EzySessionManagementSetting.EzyMaxRequestPerSecond maxRequestPerSecond = mock(EzySessionManagementSetting.EzyMaxRequestPerSecond.class);
when(maxRequestPerSecond.getAction()).thenReturn(EzyMaxRequestPerSecondAction.DISCONNECT_SESSION);
when(sessionManagementSetting.getSessionMaxRequestPerSecond()).thenReturn(maxRequestPerSecond);
EzySettings settings = mock(EzySettings.class);
when(server.getSettings()).thenReturn(settings);
when(settings.getSessionManagement()).thenReturn(sessionManagementSetting);
EzyLoggerSetting loggerSetting = mock(EzyLoggerSetting.class);
EzyLoggerSetting.EzyIgnoredCommandsSetting ignoredCommandsSetting = mock(EzyLoggerSetting.EzyIgnoredCommandsSetting.class);
when(ignoredCommandsSetting.getCommands()).thenReturn(Collections.emptySet());
when(loggerSetting.getIgnoredCommands()).thenReturn(ignoredCommandsSetting);
when(settings.getLogger()).thenReturn(loggerSetting);
MyTestDataHandler sut = new MyTestDataHandler(serverContext, session);
// when
sut.processMaxRequestPerSecond();
// then
verify(sessionManager, times(1)).removeSession(session, EzyDisconnectReason.MAX_REQUEST_PER_SECOND);
}
use of com.tvd12.ezyfoxserver.setting.EzySettings in project ezyfox-server by youngmonkeys.
the class EzyHandShakeControllerTest method handleSocketSSLButEventNoEncryptionTest.
@Test
public void handleSocketSSLButEventNoEncryptionTest() {
// given
EzyHandshakeController sut = new EzyHandshakeController();
EzyServerContext serverContext = mock(EzyServerContext.class);
EzyHandShakeRequest request = mock(EzyHandShakeRequest.class);
EzyHandshakeParams params = mock(EzyHandshakeParams.class);
when(request.getParams()).thenReturn(params);
EzySession session = spy(EzyAbstractSession.class);
when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
when(request.getSession()).thenReturn(session);
EzyServer server = mock(EzyServer.class);
EzySettings settings = mock(EzySettings.class);
EzySocketSetting socketSetting = mock(EzySocketSetting.class);
when(settings.getSocket()).thenReturn(socketSetting);
when(socketSetting.isSslActive()).thenReturn(true);
when(serverContext.getServer()).thenReturn(server);
when(server.getSettings()).thenReturn(settings);
when(params.isEnableEncryption()).thenReturn(false);
// when
sut.handle(serverContext, request);
// then
Asserts.assertNull(session.getSessionKey());
}
use of com.tvd12.ezyfoxserver.setting.EzySettings in project ezyfox-server by youngmonkeys.
the class EzyHandShakeControllerTest method handleSocketSSLButSessionKeyNotNullTest.
@Test
public void handleSocketSSLButSessionKeyNotNullTest() {
// given
EzyHandshakeController sut = new EzyHandshakeController();
byte[] sessionKey = RandomUtil.randomShortAlphabetString().getBytes();
byte[] encryptedSessionKey = RandomUtil.randomShortAlphabetString().getBytes();
EzyServerContext serverContext = mock(EzyServerContext.class);
doAnswer(it -> {
EzyHandshakeEvent event = it.getArgumentAt(1, EzyHandshakeEvent.class);
event.setSessionKey(sessionKey);
event.setEncryptedSessionKey(encryptedSessionKey);
return null;
}).when(serverContext).handleEvent(any(EzyEventType.class), any(EzyHandshakeEvent.class));
EzyHandShakeRequest request = mock(EzyHandShakeRequest.class);
EzyHandshakeParams params = mock(EzyHandshakeParams.class);
when(request.getParams()).thenReturn(params);
EzySession session = spy(EzyAbstractSession.class);
when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
when(request.getSession()).thenReturn(session);
EzyServer server = mock(EzyServer.class);
EzySettings settings = mock(EzySettings.class);
EzySocketSetting socketSetting = mock(EzySocketSetting.class);
when(settings.getSocket()).thenReturn(socketSetting);
when(socketSetting.isSslActive()).thenReturn(true);
when(serverContext.getServer()).thenReturn(server);
when(server.getSettings()).thenReturn(settings);
String clientId = RandomUtil.randomShortHexString();
String clientType = RandomUtil.randomShortAlphabetString();
String clientVersion = RandomUtil.randomShortAlphabetString();
String reconnectToken = RandomUtil.randomShortHexString();
KeyPair keyPair = EzyKeysGenerator.builder().build().generate();
byte[] clientKey = keyPair.getPublic().getEncoded();
when(params.getClientId()).thenReturn(clientId);
when(params.getClientKey()).thenReturn(clientKey);
when(params.getClientType()).thenReturn(clientType);
when(params.getClientVersion()).thenReturn(clientVersion);
when(params.getReconnectToken()).thenReturn(reconnectToken);
when(params.isEnableEncryption()).thenReturn(true);
// when
sut.handle(serverContext, request);
// then
verify(serverContext, times(1)).handleEvent(any(EzyEventType.class), any(EzyHandshakeEvent.class));
verify(serverContext, times(1)).send(any(com.tvd12.ezyfoxserver.response.EzyResponse.class), any(EzySession.class), any(boolean.class));
verify(session, times(1)).setClientId(clientId);
verify(session, times(1)).setClientKey(clientKey);
verify(session, times(1)).setClientType(clientType);
verify(session, times(1)).setClientVersion(clientVersion);
verify(session, times(1)).setSessionKey(sessionKey);
}
Aggregations