SHA256
23 12 25
Прошли тесты на создание сессии - посути всё работает (но добавление блоков пока не работает)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package test.it;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class AddUserIT {
|
||||
|
||||
@Test
|
||||
void addUser_shouldReturn200_orAlreadyExists() {
|
||||
try (WsTestClient client = new WsTestClient(TestConfig.WS_URI)) {
|
||||
|
||||
String reqId = "it-adduser-1";
|
||||
String resp = client.request(reqId, JsonBuilders.addUser(reqId), Duration.ofSeconds(5));
|
||||
|
||||
int st = JsonParsers.status(resp);
|
||||
|
||||
// ВАЖНО: тут подставь свой реальный код "уже существует", если он не 200.
|
||||
// Я оставляю пример: 409.
|
||||
boolean created = (st == 200);
|
||||
boolean already = (st == 409);
|
||||
|
||||
if (created) {
|
||||
System.out.println("✅ AddUser: создан/добавлен (status=200)");
|
||||
} else if (already) {
|
||||
System.out.println("✅ AddUser: возможно уже есть в базе (status=409)");
|
||||
} else {
|
||||
fail("❌ AddUser: неожиданный status=" + st + ", resp=" + resp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package test.it;
|
||||
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
public final class JsonBuilders {
|
||||
private JsonBuilders(){}
|
||||
|
||||
public static String addUser(String requestId) {
|
||||
return """
|
||||
{
|
||||
"op": "AddUser",
|
||||
"requestId": "%s",
|
||||
"payload": {
|
||||
"login": "%s",
|
||||
"blockchainName": "%s",
|
||||
"loginKey": "%s",
|
||||
"deviceKey": "%s",
|
||||
"bchLimit": %d
|
||||
}
|
||||
}
|
||||
""".formatted(
|
||||
requestId,
|
||||
TestConfig.TEST_LOGIN,
|
||||
TestConfig.TEST_BCH_NAME,
|
||||
TestConfig.LOGIN_PUBKEY_B64,
|
||||
TestConfig.DEVICE_PUBKEY_B64,
|
||||
TestConfig.TEST_BCH_LIMIT
|
||||
);
|
||||
}
|
||||
|
||||
public static String authChallenge(String requestId) {
|
||||
return """
|
||||
{
|
||||
"op": "AuthChallenge",
|
||||
"requestId": "%s",
|
||||
"payload": { "login": "%s" }
|
||||
}
|
||||
""".formatted(requestId, TestConfig.TEST_LOGIN);
|
||||
}
|
||||
|
||||
public static String createAuthSession(String requestId, String authNonce, String storagePwd) {
|
||||
long timeMs = System.currentTimeMillis();
|
||||
String sigB64 = signAuthorificated(authNonce, timeMs);
|
||||
|
||||
return """
|
||||
{
|
||||
"op": "CreateAuthSession",
|
||||
"requestId": "%s",
|
||||
"payload": {
|
||||
"storagePwd": "%s",
|
||||
"timeMs": %d,
|
||||
"signatureB64": "%s",
|
||||
"clientInfo": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(requestId, storagePwd, timeMs, sigB64, TestConfig.TEST_CLIENT_INFO);
|
||||
}
|
||||
|
||||
public static String listSessions(String requestId, long timeMs, String signatureB64) {
|
||||
if (signatureB64 == null) signatureB64 = "";
|
||||
return """
|
||||
{
|
||||
"op": "ListSessions",
|
||||
"requestId": "%s",
|
||||
"payload": {
|
||||
"timeMs": %d,
|
||||
"signatureB64": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(requestId, timeMs, signatureB64);
|
||||
}
|
||||
|
||||
public static String refreshSession(String requestId, String sessionId, String sessionPwd) {
|
||||
return """
|
||||
{
|
||||
"op": "RefreshSession",
|
||||
"requestId": "%s",
|
||||
"payload": {
|
||||
"sessionId": "%s",
|
||||
"sessionPwd": "%s",
|
||||
"clientInfo": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(requestId, sessionId, sessionPwd, TestConfig.TEST_CLIENT_INFO);
|
||||
}
|
||||
|
||||
public static String closeActiveSession(String requestId, String sessionId, long timeMs, String signatureB64) {
|
||||
if (signatureB64 == null) signatureB64 = "";
|
||||
return """
|
||||
{
|
||||
"op": "CloseActiveSession",
|
||||
"requestId": "%s",
|
||||
"payload": {
|
||||
"sessionId": "%s",
|
||||
"timeMs": %d,
|
||||
"signatureB64": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(requestId, sessionId, timeMs, signatureB64);
|
||||
}
|
||||
|
||||
public static String signAuthorificated(String authNonce, long timeMs) {
|
||||
String preimageStr = "AUTHORIFICATED:" + timeMs + authNonce;
|
||||
byte[] preimage = preimageStr.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] sig = Ed25519Util.sign(preimage, TestConfig.DEVICE_PRIV_KEY);
|
||||
return Base64.getEncoder().encodeToString(sig);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package test.it;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class JsonParsers {
|
||||
private JsonParsers(){}
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
public static int status(String json) {
|
||||
try {
|
||||
JsonNode root = MAPPER.readTree(json);
|
||||
return root.has("status") ? root.get("status").asInt() : -1;
|
||||
} catch (Exception e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public static String authNonce(String json) {
|
||||
try {
|
||||
JsonNode root = MAPPER.readTree(json);
|
||||
JsonNode payload = root.get("payload");
|
||||
if (payload != null && payload.has("authNonce")) return payload.get("authNonce").asText();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String sessionId(String json) {
|
||||
try {
|
||||
JsonNode root = MAPPER.readTree(json);
|
||||
JsonNode payload = root.get("payload");
|
||||
if (payload != null && payload.has("sessionId")) return payload.get("sessionId").asText();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String sessionPwd(String json) {
|
||||
try {
|
||||
JsonNode root = MAPPER.readTree(json);
|
||||
JsonNode payload = root.get("payload");
|
||||
if (payload != null && payload.has("sessionPwd")) return payload.get("sessionPwd").asText();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String storagePwd(String json) {
|
||||
try {
|
||||
JsonNode root = MAPPER.readTree(json);
|
||||
JsonNode payload = root.get("payload");
|
||||
if (payload != null && payload.has("storagePwd")) return payload.get("storagePwd").asText();
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> sessionIds(String json) {
|
||||
List<String> res = new ArrayList<>();
|
||||
try {
|
||||
JsonNode root = MAPPER.readTree(json);
|
||||
JsonNode payload = root.get("payload");
|
||||
if (payload == null) return res;
|
||||
JsonNode arr = payload.get("sessions");
|
||||
if (arr == null || !arr.isArray()) return res;
|
||||
|
||||
for (JsonNode s : arr) {
|
||||
JsonNode id = s.get("sessionId");
|
||||
if (id != null && !id.isNull()) res.add(id.asText());
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package test.it;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class SessionsIT {
|
||||
|
||||
@BeforeAll
|
||||
static void ensureUserExists() {
|
||||
try (WsTestClient client = new WsTestClient(TestConfig.WS_URI)) {
|
||||
String reqId = "it-adduser-beforeall";
|
||||
String resp = client.request(reqId, JsonBuilders.addUser(reqId), Duration.ofSeconds(5));
|
||||
int st = JsonParsers.status(resp);
|
||||
|
||||
// 200 или "уже есть" — ок
|
||||
if (!(st == 200 || st == 409)) {
|
||||
fail("User precondition failed. status=" + st + ", resp=" + resp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sessions_flow_shouldCreateListRefreshCloseCorrectly() {
|
||||
String s1Id, s1Pwd;
|
||||
String s2Id, s2Pwd;
|
||||
|
||||
// --- create session1 ---
|
||||
try (WsTestClient c = new WsTestClient(TestConfig.WS_URI)) {
|
||||
String r1 = "it-auth-1";
|
||||
String resp1 = c.request(r1, JsonBuilders.authChallenge(r1), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp1));
|
||||
String nonce = JsonParsers.authNonce(resp1);
|
||||
assertNotNull(nonce);
|
||||
|
||||
String r2 = "it-create-1";
|
||||
String storagePwd = TestConfig.fakeStoragePwd();
|
||||
String resp2 = c.request(r2, JsonBuilders.createAuthSession(r2, nonce, storagePwd), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp2));
|
||||
|
||||
s1Id = JsonParsers.sessionId(resp2);
|
||||
s1Pwd = JsonParsers.sessionPwd(resp2);
|
||||
assertNotNull(s1Id);
|
||||
assertNotNull(s1Pwd);
|
||||
}
|
||||
|
||||
// --- create session2 and list inside (AUTH_STATUS_USER) ---
|
||||
try (WsTestClient c = new WsTestClient(TestConfig.WS_URI)) {
|
||||
String r1 = "it-auth-2";
|
||||
String resp1 = c.request(r1, JsonBuilders.authChallenge(r1), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp1));
|
||||
String nonce = JsonParsers.authNonce(resp1);
|
||||
assertNotNull(nonce);
|
||||
|
||||
String r2 = "it-create-2";
|
||||
String resp2 = c.request(r2, JsonBuilders.createAuthSession(r2, nonce, TestConfig.fakeStoragePwd()), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp2));
|
||||
|
||||
s2Id = JsonParsers.sessionId(resp2);
|
||||
s2Pwd = JsonParsers.sessionPwd(resp2);
|
||||
assertNotNull(s2Id);
|
||||
assertNotNull(s2Pwd);
|
||||
|
||||
// list inside session2 (у тебя это AUTH_STATUS_USER без подписи)
|
||||
String r3 = "it-list-in-session2";
|
||||
String resp3 = c.request(r3, JsonBuilders.listSessions(r3, 0L, ""), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp3));
|
||||
List<String> ids = JsonParsers.sessionIds(resp3);
|
||||
|
||||
assertTrue(ids.contains(s1Id), "Must contain session1");
|
||||
assertTrue(ids.contains(s2Id), "Must contain session2");
|
||||
}
|
||||
|
||||
// --- list in AUTH_IN_PROGRESS (подпись по nonce) ---
|
||||
try (WsTestClient c = new WsTestClient(TestConfig.WS_URI)) {
|
||||
String r1 = "it-auth-list";
|
||||
String resp1 = c.request(r1, JsonBuilders.authChallenge(r1), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp1));
|
||||
String nonce = JsonParsers.authNonce(resp1);
|
||||
assertNotNull(nonce);
|
||||
|
||||
long timeMs = System.currentTimeMillis();
|
||||
String sig = JsonBuilders.signAuthorificated(nonce, timeMs);
|
||||
|
||||
String r2 = "it-list-auth-in-progress";
|
||||
String resp2 = c.request(r2, JsonBuilders.listSessions(r2, timeMs, sig), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp2));
|
||||
|
||||
List<String> ids = JsonParsers.sessionIds(resp2);
|
||||
assertTrue(ids.contains(s1Id));
|
||||
assertTrue(ids.contains(s2Id));
|
||||
}
|
||||
|
||||
// --- refresh session1 and close session2 (from session1) ---
|
||||
try (WsTestClient c = new WsTestClient(TestConfig.WS_URI)) {
|
||||
|
||||
String r1 = "it-refresh-s1";
|
||||
String resp1 = c.request(r1, JsonBuilders.refreshSession(r1, s1Id, s1Pwd), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp1));
|
||||
assertNotNull(JsonParsers.storagePwd(resp1));
|
||||
|
||||
String r2 = "it-close-s2";
|
||||
String resp2 = c.request(r2, JsonBuilders.closeActiveSession(r2, s2Id, 0L, ""), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp2));
|
||||
}
|
||||
|
||||
// --- verify only session1 remains (AUTH_IN_PROGRESS list) ---
|
||||
try (WsTestClient c = new WsTestClient(TestConfig.WS_URI)) {
|
||||
String r1 = "it-auth-list2";
|
||||
String resp1 = c.request(r1, JsonBuilders.authChallenge(r1), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp1));
|
||||
String nonce = JsonParsers.authNonce(resp1);
|
||||
assertNotNull(nonce);
|
||||
|
||||
long timeMs = System.currentTimeMillis();
|
||||
String sig = JsonBuilders.signAuthorificated(nonce, timeMs);
|
||||
|
||||
String r2 = "it-list-after-close-s2";
|
||||
String resp2 = c.request(r2, JsonBuilders.listSessions(r2, timeMs, sig), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp2));
|
||||
|
||||
List<String> ids = JsonParsers.sessionIds(resp2);
|
||||
assertTrue(ids.contains(s1Id));
|
||||
assertFalse(ids.contains(s2Id));
|
||||
}
|
||||
|
||||
// --- close session1 in AUTH_IN_PROGRESS ---
|
||||
try (WsTestClient c = new WsTestClient(TestConfig.WS_URI)) {
|
||||
String r1 = "it-auth-close-s1";
|
||||
String resp1 = c.request(r1, JsonBuilders.authChallenge(r1), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp1));
|
||||
String nonce = JsonParsers.authNonce(resp1);
|
||||
assertNotNull(nonce);
|
||||
|
||||
long timeMs = System.currentTimeMillis();
|
||||
String sig = JsonBuilders.signAuthorificated(nonce, timeMs);
|
||||
|
||||
String r2 = "it-close-s1";
|
||||
String resp2 = c.request(r2, JsonBuilders.closeActiveSession(r2, s1Id, timeMs, sig), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp2));
|
||||
}
|
||||
|
||||
// --- verify empty list ---
|
||||
try (WsTestClient c = new WsTestClient(TestConfig.WS_URI)) {
|
||||
String r1 = "it-auth-list-empty";
|
||||
String resp1 = c.request(r1, JsonBuilders.authChallenge(r1), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp1));
|
||||
String nonce = JsonParsers.authNonce(resp1);
|
||||
assertNotNull(nonce);
|
||||
|
||||
long timeMs = System.currentTimeMillis();
|
||||
String sig = JsonBuilders.signAuthorificated(nonce, timeMs);
|
||||
|
||||
String r2 = "it-list-empty";
|
||||
String resp2 = c.request(r2, JsonBuilders.listSessions(r2, timeMs, sig), Duration.ofSeconds(5));
|
||||
assertEquals(200, JsonParsers.status(resp2));
|
||||
|
||||
List<String> ids = JsonParsers.sessionIds(resp2);
|
||||
assertTrue(ids.isEmpty(), "Sessions must be empty");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package test.it;
|
||||
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
public final class TestConfig {
|
||||
private TestConfig(){}
|
||||
|
||||
public static final String WS_URI = "ws://localhost:7070/ws";
|
||||
public static final String TEST_LOGIN = "anya24";
|
||||
public static final String TEST_BCH_NAME = TEST_LOGIN + "0001";
|
||||
public static final int TEST_BCH_LIMIT = 1_000_000;
|
||||
public static final String TEST_CLIENT_INFO = "JavaTestClient/1.0";
|
||||
|
||||
public static final byte[] LOGIN_PRIV_KEY;
|
||||
public static final String LOGIN_PUBKEY_B64;
|
||||
|
||||
public static final byte[] DEVICE_PRIV_KEY;
|
||||
public static final String DEVICE_PUBKEY_B64;
|
||||
|
||||
static {
|
||||
LOGIN_PRIV_KEY = Ed25519Util.generatePrivateKeyFromString("test-ed25519-login-11" + TEST_LOGIN);
|
||||
byte[] loginPub = Ed25519Util.derivePublicKey(LOGIN_PRIV_KEY);
|
||||
LOGIN_PUBKEY_B64 = Ed25519Util.keyToBase64(loginPub);
|
||||
|
||||
DEVICE_PRIV_KEY = Ed25519Util.generatePrivateKeyFromString("test-ed25519-device-" + TEST_LOGIN);
|
||||
byte[] devicePub = Ed25519Util.derivePublicKey(DEVICE_PRIV_KEY);
|
||||
DEVICE_PUBKEY_B64 = Ed25519Util.keyToBase64(devicePub);
|
||||
}
|
||||
|
||||
public static String fakeStoragePwd() {
|
||||
byte[] data = new byte[32];
|
||||
for (int i = 0; i < data.length; i++) data[i] = (byte) (i + 1);
|
||||
return Base64.getEncoder().encodeToString(data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user