Commit 356c9ddd by 钱炳权

Enhance code robustness

parent df2e1715
package com.example.fuzzControll.controller;
import com.example.fuzzControll.exception.AflnetException;
import com.example.fuzzControll.pojo.vo.AjaxResult;
import com.example.fuzzControll.pojo.vo.CmdStartParams;
import com.example.fuzzControll.service.TestService;
......@@ -28,14 +29,15 @@ public class TestControler {
@RequestMapping(value = "/testStart", method = RequestMethod.POST)
public AjaxResult list(@RequestBody final CmdStartParams cmdStartParams) {
try {
//todo 加个同步锁,执行到特征点就返回数据
new Thread(new Runnable() {
@Override
public void run() {
service.testStart(cmdStartParams);
}
}).start();
} catch (Exception e) {
log.error(e.getMessage());
} catch (AflnetException e) {
log.error(e.getDefaultMessage());
return AjaxResult.error("测试启动失败!");
}
return AjaxResult.success("测试启动成功!");
......@@ -48,7 +50,8 @@ public class TestControler {
public AjaxResult testStop() {
try {
service.testStop();
} catch (Exception e) {
} catch (AflnetException e) {
log.error(e.getDefaultMessage());
return AjaxResult.error("测试停止失败!");
}
return AjaxResult.success("测试停止成功!");
......
......@@ -9,9 +9,11 @@ import com.example.fuzzControll.pojo.vo.CmdStartParams;
import com.example.fuzzControll.service.TestService;
import com.example.fuzzControll.tools.CmdTools;
import com.example.fuzzControll.tools.TestControlTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service("testService")
@Slf4j
public class TestServiceImpl implements TestService {
KittyProperties kittyProperties = (KittyProperties) SpringContextUtil.getBean("kittyProperties");
......@@ -30,5 +32,6 @@ public class TestServiceImpl implements TestService {
@Override
public void testStop() {
TestControlTools.setIsRunning(false);
log.info("Aflnet has been stopped ! ");
}
}
......@@ -20,11 +20,12 @@ import java.util.*;
public class CmdTools {
Boolean send = false;
WebSocket socket = (WebSocket) SpringContextUtil.getBean("WebSocket");
Boolean show = true;
/**
* 运行不需要后台运行cmd
*/
public List<String> runCmd(String cmd,String caller) throws CmdException {
public List<String> runCmd(String cmd, String caller) throws CmdException {
List<String> result = new ArrayList<String>();
try {
Process process = Runtime.getRuntime().exec(cmd);
......@@ -33,7 +34,7 @@ public class CmdTools {
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
throw new CmdException(caller+" run cmd failed!");
throw new CmdException(caller + " run cmd failed!");
}
return result;
......@@ -45,14 +46,16 @@ public class CmdTools {
* 运行需要后台运行cmd
* 通过websocket返回数据
*/
public void runProgramCmd(String cmd) {
public void runProgramCmd(String cmd) throws AflnetException {
try {
Process process = Runtime.getRuntime().exec(cmd);
printMessageToWeb(process.getInputStream(), process);
printMessageToWeb(process.getInputStream());
printMessage(process.getErrorStream(), new ArrayList<String>());
process.waitFor();
log.info("Aflnet cmd have been run.");
} catch (Exception e) {
e.printStackTrace();
throw new AflnetException("Aflnet run error");
}
}
......@@ -118,7 +121,8 @@ public class CmdTools {
return result;
}
private void printMessageToWeb(final InputStream input, Process process) throws IOException {
private void printMessageToWeb(final InputStream input) throws IOException, AflnetException {
show = true;
Reader reader = new InputStreamReader(input);
BufferedReader bf = new BufferedReader(reader);
String line = null;
......@@ -127,12 +131,23 @@ public class CmdTools {
makeReturnEntity(line, returnEntity);
if (send) {
String data = JSONObject.toJSONString(returnEntity);
try {
socket.appointSending("backend", "web", data);
} catch (Exception ignored) {
}
}
}
}
private TestReturnEntity makeReturnEntity(String line, TestReturnEntity returnEntity) {
if (line.contains("All set and ready to roll!") || line.contains("american fuzzy") || line.contains("process timing overall results")) {
show = false;
} else if (line.contains("PROGRAM ABORT")) {
throw new AflnetException("Aflnet run failed !");
}
if (show) {
log.info(line);
}
if (line.contains("run time")) {
send = false;
int run_time = line.indexOf(":");
......@@ -222,8 +237,8 @@ public class CmdTools {
return returnEntity;
}
public String parse(CmdStartParams cmdStartParams) throws AflnetException{
if(cmdStartParams==null){
public String parse(CmdStartParams cmdStartParams) throws AflnetException {
if (cmdStartParams == null) {
throw new AflnetException("CmdStartParams is null!");
}
StringBuilder cmd = new StringBuilder();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment