Skip to content
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 @@ -41,10 +41,10 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.server.ServerWebExchange;

import java.util.Collections;
Expand All @@ -66,6 +66,8 @@
@MockitoSettings(strictness = Strictness.LENIENT)
public final class SignServiceVersionOneTest {

private static final int DELAY = 5;

private SignService signService;

private ServerWebExchange exchange;
Expand All @@ -76,12 +78,10 @@ public final class SignServiceVersionOneTest {

private ShenyuContext passed;

@Value("${shenyu.sign.delay:5}")
private int delay;

@BeforeEach
public void setup() {
this.signService = new ComposableSignService(new DefaultExtractor(), new DefaultSignProvider());
ReflectionTestUtils.setField(this.signService, "delay", DELAY);

final String path = "/test-api/demo/test";
PluginData signData = new PluginData();
Expand Down Expand Up @@ -129,6 +129,19 @@ public void normalTest() {
assertEquals(ret, VerifyResult.success());
}

@Test
public void normalTestWithinConfiguredDelay() {
String timestamp = String.valueOf(System.currentTimeMillis() - 60_000L);
this.exchange = buildServerWebExchange("http://localhost/test-api/demo/test",
timestamp,
appKey,
buildSign(secretKey, timestamp, "/test-api/demo/test", null, null));
this.exchange.getAttributes().put(Constants.CONTEXT, this.passed);

VerifyResult ret = this.signService.signatureVerify(this.exchange);
assertEquals(ret, VerifyResult.success());
}

@Test
public void nullTimestampTest() {
String timestamp = String.valueOf(System.currentTimeMillis());
Expand Down Expand Up @@ -175,15 +188,15 @@ public void nullAppKeyTest() {

@Test
public void overdueTest() {
String errorTimestamp = String.valueOf(System.currentTimeMillis() - ((long) (delay + 1) * 1000 * 60));
String errorTimestamp = String.valueOf(System.currentTimeMillis() - ((long) (DELAY + 1) * 1000 * 60));
this.exchange = buildServerWebExchange("http://localhost/test-api/demo/test",
errorTimestamp,
appKey,
buildSign(secretKey, errorTimestamp, "/test-api/demo/test", null, null));
this.exchange.getAttributes().put(Constants.CONTEXT, this.passed);

VerifyResult ret = this.signService.signatureVerify(this.exchange);
assertEquals(ret, VerifyResult.fail(String.format(ShenyuResultEnum.SIGN_TIME_IS_TIMEOUT.getMsg(), delay)));
assertEquals(ret, VerifyResult.fail(String.format(ShenyuResultEnum.SIGN_TIME_IS_TIMEOUT.getMsg(), DELAY)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.server.ServerWebExchange;

import java.net.URI;
Expand All @@ -68,6 +68,8 @@
@MockitoSettings(strictness = Strictness.LENIENT)
public final class SignServiceVersionTwoTest {

private static final int DELAY = 5;

private SignService signService;

private ServerWebExchange exchange;
Expand All @@ -78,12 +80,10 @@ public final class SignServiceVersionTwoTest {

private ShenyuContext passed;

@Value("${shenyu.sign.delay:5}")
private int delay;

@BeforeEach
public void setup() {
this.signService = new ComposableSignService(new DefaultExtractor(), new DefaultSignProvider());
ReflectionTestUtils.setField(this.signService, "delay", DELAY);

final String path = "/test-api/demo/test";
PluginData signData = new PluginData();
Expand Down Expand Up @@ -131,6 +131,19 @@ public void normalTest() {
assertEquals(ret, VerifyResult.success());
}

@Test
public void normalTestWithinConfiguredDelay() {
String timestamp = String.valueOf(System.currentTimeMillis() - 60_000L);
String parameters = buildParameters(timestamp, appKey);
this.exchange = buildServerWebExchange("http://localhost/test-api/demo/test",
parameters,
buildSign(secretKey, parameters, URI.create("http://localhost/test-api/demo/test"), null));
this.exchange.getAttributes().put(Constants.CONTEXT, this.passed);

VerifyResult ret = this.signService.signatureVerify(this.exchange);
assertEquals(ret, VerifyResult.success());
}

@Test
public void nullTimestampTest() {

Expand Down Expand Up @@ -178,15 +191,15 @@ public void nullAppKeyTest() {

@Test
public void overdueTest() {
String errorTimestamp = String.valueOf(System.currentTimeMillis() - ((long) (delay + 1) * 1000 * 60));
String errorTimestamp = String.valueOf(System.currentTimeMillis() - ((long) (DELAY + 1) * 1000 * 60));
String parameters = buildParameters(errorTimestamp, appKey);
this.exchange = buildServerWebExchange("http://localhost/test-api/demo/test",
parameters,
buildSign(secretKey, parameters, URI.create("http://localhost/test-api/demo/test"), null));
this.exchange.getAttributes().put(Constants.CONTEXT, this.passed);

VerifyResult ret = this.signService.signatureVerify(this.exchange);
assertEquals(ret, VerifyResult.fail(String.format(ShenyuResultEnum.SIGN_TIME_IS_TIMEOUT.getMsg(), delay)));
assertEquals(ret, VerifyResult.fail(String.format(ShenyuResultEnum.SIGN_TIME_IS_TIMEOUT.getMsg(), DELAY)));
}

@Test
Expand Down
Loading