add Upload test case

This commit is contained in:
CaiHQ 2023-04-07 20:25:30 +08:00
parent 8cf51f2ab5
commit 3408787a7b
6 changed files with 230 additions and 13 deletions

View File

@ -5,7 +5,7 @@ plugins {
id 'signing'
}
group "org.bdware.bdcontract"
version "0.6.0"
version "0.7.1"
sourceCompatibility = 1.8
repositories {
mavenCentral()
@ -13,9 +13,9 @@ repositories {
}
dependencies {
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'org.bdware.bdcontract:gmhelper:0.1.0'
implementation 'org.bdware.bdcontract:sdk-java:1.0.0'
implementation 'com.google.code.gson:gson:2.9.1'
implementation project(":gmhelper")
implementation project(":sdk-java")
implementation 'org.bouncycastle:bcpkix-jdk15on:1.69'
implementation 'org.bouncycastle:bcprov-jdk15on:1.69'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'

View File

@ -34,6 +34,7 @@ public class HTTPTool {
String privateKey;
String publicKey;
String ypkPath;
int remoteDebugPort;
}
public static void deployWithYpk(String deployConfigPath, String ypkPath) {
@ -119,7 +120,7 @@ public class HTTPTool {
System.out.println(TAG + "startContract:" + fileNames.get(0));
if (config.asDebug) {
ext.startContractAsDebug(fileNames.get(0), config.createParam, new ResultCallback() {
ext.startContractAsDebug(fileNames.get(0), config.createParam,config.remoteDebugPort, new ResultCallback() {
@Override
public void onResult(JsonObject r) {
System.out.println(TAG + r);
@ -127,7 +128,7 @@ public class HTTPTool {
}
});
} else {
ext.startContract(fileNames.get(0), config.createParam, new ResultCallback() {
ext.startContract(fileNames.get(0), config.createParam,config.remoteDebugPort, new ResultCallback() {
@Override
public void onResult(JsonObject r) {
System.out.println(TAG + r);
@ -151,9 +152,16 @@ public class HTTPTool {
String url = "http://%s/Upload?%s&sign=%s";
File file = new File(config.ypkPath);
String argWithoutSig = "path=%s&fileName=%s&isPrivate=true&order=%d&count=%d&pubKey=%s";
SM2KeyPair keyPair = SM2KeyPair.fromJson(String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey));
SM2KeyPair keyPair;
try {
keyPair = SM2KeyPair.fromJson(String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey));
} catch (Exception e) {
System.out.println("[HttpTool] parse key error:\n" + String.format("{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}", config.publicKey, config.privateKey));
e.printStackTrace();
return;
}
String pubKey = keyPair.getPublicKeyStr();
int trunc = 490 * 1024;
int trunc = 300 * 1024;
byte[] buff = new byte[trunc];
int order = 0;
int count = (int) ((file.length() - 1) / (trunc)) + 1;

View File

@ -0,0 +1,133 @@
package org.bdware.ypkdeploy;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.bdware.client.ContractRequest;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
public class RouterSetter {
static class Task {
List<SetUpperTask> tasks;
}
static class SetUpperTask {
public String nodeName;
public String publicKey;
public String privateKey;
public String upperIP;
public String ipAndPort;
}
public static ExecutorService es = Executors.newFixedThreadPool(100);
public static void goByStr(String content) {
Task entry = new Gson().fromJson(content, Task.class);
AtomicInteger count = new AtomicInteger(0);
for (SetUpperTask task : entry.tasks)
es.execute(new Runnable() {
@Override
public void run() {
try {
executeTask(task);
} catch (Exception e) {
e.printStackTrace();
}
count.incrementAndGet();
}
});
for (; count.get() < entry.tasks.size(); ) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("Sleep 10s, count:" + count.get());
}
}
private static void executeTask(SetUpperTask task) throws Exception {
SM2KeyPair keyPair = SM2KeyPair.fromJson(new Gson().toJson(task));
SmartContractClientExt ext = new SmartContractClientExt(String.format("ws://%s/SCIDE/SCExecutor", task.ipAndPort),
keyPair);
System.out.println("[RouterSetter] try connect " + task.nodeName + " " + task.ipAndPort);
ext.waitForConnect();
System.out.println("[RouterSetter] try login " + task.nodeName + " " + task.ipAndPort);
ext.loginSync();
System.out.println("[RouterSetter] loginDone " + task.nodeName + " " + task.ipAndPort);
ContractRequest cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setPubKey");
cr.setArg(String.format("{\"pubKey\":\"%s\"}", task.publicKey));
cr.doSignature(keyPair);
JsonObject result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setPubKey " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setUpperIP");
cr.setArg(String.format("{\"upperIP\":\"%s\"}", task.upperIP));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setUpperIP " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setName");
cr.setArg(String.format("{\"name\":\"%s\"}", task.nodeName));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setName " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("setUpperPort");
cr.setArg(String.format("{\"upperPort\":\"%s\"}", 21060));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setUpperPort " + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setAction("setSignature");
cr.setContractID("Router");
String toSign = task.nodeName + "|" + task.publicKey;
String signature =
ByteUtils.toHexString(
SM2Util.sign(
keyPair.getPrivateKeyParameter(), toSign.getBytes()));
cr.setArg(String.format("{\"signature\":\"%s\"}", signature));
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] setSignature" + task.nodeName + " result:" + result);
cr = new ContractRequest();
cr.setContractID("Router");
cr.setAction("reInit");
cr.setArg("");
cr.doSignature(keyPair);
result = ext.executeContractSync(cr);
System.out.println("[RouterSetter] reInit " + task.nodeName + " result:" + result);
}
public static void goByPath(String path) {
String conf = DeployConfGenerator.getFileContent(path);
goByStr(conf);
}
public static void main(String[] args) {
System.out.println("Usage: set deployentry.json or ");
if (args.length > 0)
goByPath(args[0]);
else
goByPath("./setRouter.json");
}
}

View File

@ -30,11 +30,11 @@ public class SmartContractClientExt extends SmartContractClient {
}
public void startContract(String project) {
startContract(project, null, null);
startContract(project, null, 0, null);
}
public void startContract(String project, JsonElement createParam) {
startContract(project, createParam, null);
startContract(project, createParam, 0, null);
}
public void startContractByYpk(String path, boolean isPrivate, JsonElement createParam, ResultCallback rc) {
@ -93,7 +93,7 @@ public class SmartContractClientExt extends SmartContractClient {
this.sendMsg(new Gson().toJson(ret));
}
public void startContractAsDebug(String project, JsonElement createParam, ResultCallback rc) {
public void startContractAsDebug(String project, JsonElement createParam, int remoteDebugPort, ResultCallback rc) {
String reqID = System.currentTimeMillis() + "";
Map<String, Object> ret = new HashMap<>();
ret.put("action", "startContractAsDebug");
@ -102,6 +102,8 @@ public class SmartContractClientExt extends SmartContractClient {
ret.put("requestID", reqID);
ret.put("path", "/" + project + "/manifest.json");
ret.put("createParam", createParam);
if (remoteDebugPort != 0)
ret.put("remoteDebugPort", remoteDebugPort);
String content =
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
String sig;
@ -122,7 +124,7 @@ public class SmartContractClientExt extends SmartContractClient {
this.sendMsg(new Gson().toJson(ret));
}
public void startContract(String project, JsonElement createParam, ResultCallback rc) {
public void startContract(String project, JsonElement createParam, int remoteDebugPort, ResultCallback rc) {
String reqID = System.currentTimeMillis() + "";
Map<String, Object> ret = new HashMap<>();
ret.put("action", "startContract");
@ -131,6 +133,8 @@ public class SmartContractClientExt extends SmartContractClient {
ret.put("requestID", reqID);
ret.put("path", "/" + project + "/manifest.json");
ret.put("createParam", createParam);
if (remoteDebugPort != 0)
ret.put("remoteDebugPort", remoteDebugPort);
String content =
String.format("Sole|%s|%s", ret.get("path"), getKeyPair().getPublicKeyStr());
String sig;

View File

@ -31,5 +31,8 @@ public class EntryTest {
public void deployShanxiProxy() {
HTTPTool.deploy("./testinput/debugconf-shanxiproxy.json");
}
@Test
public void deployRemoteDebug() {
HTTPTool.deploy("./testinput/debugconf-remotedebug.json");
}
}

View File

@ -0,0 +1,69 @@
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.junit.Test;
import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class HttpUploadTest {
@Test
public void run() {
String url = "http://%s/Upload?%s&sign=%s";
File file = new File("/Users/huaqiancai/BDWare/bdcontract-bundle/agent-backend/BDWareProjectDir/publicCompiled/DoipDist-0.3.3.ypk");
String argWithoutSig = "path=%s&fileName=%s&isPrivate=true&contractID=%s&order=%d&count=%d&pubKey=%s";
SM2KeyPair keyPair;
try {
keyPair = SM2KeyPair.fromJson("{\"privateKey\":\"589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8\",\"publicKey\":\"04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd\"}");
} catch (Exception e) {
e.printStackTrace();
return;
}
String pubKey = keyPair.getPublicKeyStr();
int trunc = 300 * 1024;
byte[] buff = new byte[trunc];
int order = 0;
int count = (int) ((file.length() - 1) / (trunc)) + 1;
try {
FileInputStream fin = new FileInputStream(file);
for (int len = 0; (len = fin.read(buff)) > 0; order++) {
CloseableHttpClient client = HttpClients.createDefault();
String arg = String.format(argWithoutSig, "/ypks", file.getName(), "CodeRepo", order, count, pubKey);
String sign = ByteUtils.toHexString(SM2Util.sign(keyPair.getPrivateKeyParameter(), arg.getBytes(StandardCharsets.UTF_8)));
String urlStr = String.format(url, "127.0.0.1:18000", arg, sign);
System.out.println("urlStr:" + urlStr);
HttpPost httpPost = new HttpPost(urlStr);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
if (len == trunc)
builder.addBinaryBody("file", buff, ContentType.APPLICATION_OCTET_STREAM, file.getName());
else {
byte[] bu = new byte[len];
System.arraycopy(buff, 0, bu, 0, len);
builder.addBinaryBody("file", bu, ContentType.APPLICATION_OCTET_STREAM, file.getName());
}
HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
CloseableHttpResponse response = client.execute(httpPost);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
response.getEntity().writeTo(bo);
System.out.println(order + "/" + count + " " + bo);
client.close();
}
} catch (IOException | CryptoException e) {
e.printStackTrace();
}
}
}