Commit 356c9ddd by 钱炳权

Enhance code robustness

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