Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
java-version: ${{ matrix.java }}
cache: maven

- name: Record Hazelcast dependency tree
run: mvn -B --no-transfer-progress dependency:tree -Dincludes=com.hazelcast:*

- name: Build and verify
run: mvn -B --no-transfer-progress clean verify

Expand Down
23 changes: 19 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<!-- 跳过单元测试 -->
<skip>true</skip>
<skipTests>true</skipTests>
<!-- Execute regression tests on every supported JDK. -->
<skip>false</skip>
<skipTests>false</skipTests>
<!-- forkMode 可设置值有 “never”, “once”, “always” 和 “pertest”。 pretest:
每一个测试创建一个新进程,为每个测试创建新的JVM是单独测试的最彻底方式,但也是最慢的,不适合hudson上持续回归。 once:在一个进程中进行所有测试。once为默认设置,在Hudson上持续回归时建议使用默认设置。
always:在一个进程中并行的运行脚本,Junit4.7以上版本才可以使用,surefire的版本要在2.6以上提供这个功能,其中 threadCount:执行时,指定可分配的线程数量。只和参数parallel配合使用有效。默认:5。 -->
<forkMode>once</forkMode>
<argLine>-Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m -Dfile.encoding=UTF-8</argLine>
<argLine>-Xmx1024m -Dfile.encoding=UTF-8</argLine>
<additionalClasspathElements>
<additionalClasspathElement>${basedir}/target/test-classes</additionalClasspathElement>
</additionalClasspathElements>
Expand Down Expand Up @@ -416,6 +416,8 @@
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<maven-nexus-staging-plugin.version>1.6.8</maven-nexus-staging-plugin.version>
<netty-socketio.version>2.0.9</netty-socketio.version>
<!-- netty-socketio 2.0.9 is compiled against the Hazelcast 3.x ABI. -->
<hazelcast.version>3.12.12</hazelcast.version>
<redisson.version>3.17.7</redisson.version>
</properties>

Expand Down Expand Up @@ -481,6 +483,19 @@
<version>${netty-socketio.version}</version>
</dependency>

<!-- Keep client and core aligned with netty-socketio's IMap/ITopic ABI.
Boot 2.6 defaults to Hazelcast 4, which is not binary compatible. -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>${hazelcast.version}</version>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>${hazelcast.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
<dependency>
<groupId>org.redisson</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

import org.springframework.util.StringUtils;

import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.AuthorizationResult;
import com.corundumstudio.socketio.HandshakeData;

/**
Expand All @@ -35,7 +36,6 @@ public class JWTAuthorizationListener implements AuthorizationListener {
private String authorizationHeaderName = AUTHORIZATION_HEADER;
private String authorizationParamName = AUTHORIZATION_PARAM;

@Override
public boolean isAuthorized(HandshakeData data) {

String token = obtainToken(data);
Expand All @@ -53,6 +53,12 @@ public boolean isAuthorized(HandshakeData data) {
return false;
}

@Override
public AuthorizationResult getAuthorizationResult(HandshakeData data) {
return isAuthorized(data) ? AuthorizationResult.SUCCESSFUL_AUTHORIZATION
: AuthorizationResult.FAILED_AUTHORIZATION;
}

protected String obtainToken(HandshakeData data) {
// 从header中获取token
String token = data.getHttpHeaders().get(getAuthorizationHeaderName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.corundumstudio.socketio.spring.boot;

import com.corundumstudio.socketio.store.HazelcastStoreFactory;
import com.corundumstudio.socketio.store.HazelcastPubSubStore;
import com.corundumstudio.socketio.store.Store;
import com.corundumstudio.socketio.store.pubsub.DispatchMessage;
import com.corundumstudio.socketio.store.pubsub.PubSubType;
import com.hazelcast.config.Config;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.*;

class HazelcastStoreCompatibilityTest {
private HazelcastInstance member;

@BeforeEach
void startIsolatedMember() throws Exception {
Config config = new Config();
config.setInstanceName("socketio-abi-" + UUID.randomUUID());
config.setProperty("hazelcast.phone.home.enabled", "false");
// Interface selection alone does not prevent binding the wildcard address.
config.setProperty("hazelcast.socket.bind.any", "false");
config.setProperty("hazelcast.operation.thread.count", "2");
config.setProperty("hazelcast.operation.generic.thread.count", "2");
config.setProperty("hazelcast.io.thread.count", "2");
config.getNetworkConfig().setPort(0);
config.getNetworkConfig().getInterfaces().setEnabled(true).addInterface("127.0.0.1");
JoinConfig join = config.getNetworkConfig().getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().setEnabled(false);
join.getAwsConfig().setEnabled(false);
// Hazelcast 4 introduced auto-detection; disable it when reproducing the ABI regression.
try {
Object autoDetection = join.getClass().getMethod("getAutoDetectionConfig").invoke(join);
autoDetection.getClass().getMethod("setEnabled", boolean.class).invoke(autoDetection, false);
} catch (NoSuchMethodException expectedOnHazelcast3) {
// Hazelcast 3 has no auto-detection.
}
member = Hazelcast.newHazelcastInstance(config);
}

@AfterEach
void stopMember() {
if (member != null) {
member.shutdown();
}
}

@Test
void upstreamStoreCanUseIMap() {
HazelcastStoreFactory factory = new HazelcastStoreFactory(member);
Store store = factory.createStore(UUID.randomUUID());
store.set("key", "value");
assertEquals("value", store.get("key"));
assertTrue(store.has("key"));
store.del("key");
assertFalse(store.has("key"));
assertNull(store.get("key"));
factory.<String, String>createMap("factory-map").put("key", "factory-value");
assertEquals("factory-value", factory.<String, String>createMap("factory-map").get("key"));
}

@Test
void upstreamPubSubCanUseITopic() throws InterruptedException {
HazelcastPubSubStore publisher = new HazelcastPubSubStore(member, member, 1L);
HazelcastPubSubStore subscriber = new HazelcastPubSubStore(member, member, 2L);
CountDownLatch received = new CountDownLatch(1);
AtomicReference<DispatchMessage> delivered = new AtomicReference<>();
subscriber.subscribe(PubSubType.DISPATCH, message -> {
delivered.set(message);
received.countDown();
}, DispatchMessage.class);
try {
publisher.publish(PubSubType.DISPATCH, new DispatchMessage("test-room", null, "/test"));
assertTrue(received.await(10, TimeUnit.SECONDS), "Actual Hazelcast topic must deliver the message");
assertEquals("test-room", delivered.get().getRoom());
assertEquals("/test", delivered.get().getNamespace());
assertEquals(Long.valueOf(1), delivered.get().getNodeId());
} finally {
subscriber.unsubscribe(PubSubType.DISPATCH);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.corundumstudio.socketio.spring.boot.listener;

import com.corundumstudio.socketio.AuthorizationResult;
import com.corundumstudio.socketio.HandshakeData;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.*;

class JWTAuthorizationListenerTest {
private final JWTAuthorizationListener listener = new JWTAuthorizationListener();

private HandshakeData handshake(String header, String parameter) {
DefaultHttpHeaders headers = new DefaultHttpHeaders();
if (header != null) {
headers.set(JWTAuthorizationListener.AUTHORIZATION_HEADER, header);
}
return new HandshakeData(headers,
parameter == null ? Collections.emptyMap()
: Collections.singletonMap("token", Collections.singletonList(parameter)),
null, "/", false);
}

@Test
void bridgePreservesExistingTokenDecisions() {
assertSame(AuthorizationResult.SUCCESSFUL_AUTHORIZATION,
listener.getAuthorizationResult(handshake(" token ", null)));
assertSame(AuthorizationResult.SUCCESSFUL_AUTHORIZATION,
listener.getAuthorizationResult(handshake(null, "query-token")));
assertSame(AuthorizationResult.SUCCESSFUL_AUTHORIZATION,
listener.getAuthorizationResult(handshake("", "query-token")));
assertSame(AuthorizationResult.FAILED_AUTHORIZATION,
listener.getAuthorizationResult(handshake(null, null)));
assertSame(AuthorizationResult.FAILED_AUTHORIZATION,
listener.getAuthorizationResult(handshake(null, " ")));
// A whitespace-only header takes precedence, as in the original boolean contract.
assertSame(AuthorizationResult.FAILED_AUTHORIZATION,
listener.getAuthorizationResult(handshake(" ", "query-token")));
}

@Test
void bridgeStillDispatchesToPublicBooleanOverride() {
JWTAuthorizationListener denying = new JWTAuthorizationListener() {
@Override
public boolean isAuthorized(HandshakeData data) {
return false;
}
};
assertSame(AuthorizationResult.FAILED_AUTHORIZATION,
denying.getAuthorizationResult(handshake("token", null)));
}

@Test
void bridgeDoesNotTurnExceptionsIntoSuccessfulAuthorization() {
IllegalStateException failure = new IllegalStateException("authorization failed");
JWTAuthorizationListener throwing = new JWTAuthorizationListener() {
@Override
public boolean isAuthorized(HandshakeData data) {
throw failure;
}
};
assertSame(failure, assertThrows(IllegalStateException.class,
() -> throwing.getAuthorizationResult(handshake("token", null))));
}
}
Loading