Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanOK authored Dec 30, 2021
1 parent f189dee commit 59fd14e
Show file tree
Hide file tree
Showing 9 changed files with 846 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/my/smartdec/detect/app/cli/Arguments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package my.smartdec.detect.app.cli;

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;

/**
*
*/
public interface Arguments {

/**
* @param keys keys
* @return optional
*/
Optional<String> value(Collection<String> keys);

/**
* @param keys keys
* @return optional
*/
default Optional<String> value(final String... keys) {
return this.value(Arrays.asList(keys));
}
}
43 changes: 43 additions & 0 deletions src/main/java/my/smartdec/detect/app/cli/ArgumentsDefault.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package my.smartdec.detect.app.cli;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

/**
*
*/
public final class ArgumentsDefault implements Arguments {

/**
*
*/
private final List<String> arguments;

/**
* @param args arguments
*/
public ArgumentsDefault(final List<String> args) {
this.arguments = args;
}

/**
* @param strings arguments
*/
public ArgumentsDefault(final String... strings) {
this(Arrays.asList(strings));
}

@Override
public Optional<String> value(final Collection<String> keys) {
return IntStream
.range(0, this.arguments.size())
.filter(index -> keys.contains(this.arguments.get(index)))
.map(index -> index + 1)
.filter(index -> index < this.arguments.size())
.mapToObj(this.arguments::get)
.findAny();
}
}
200 changes: 200 additions & 0 deletions src/main/java/my/smartdec/detect/app/cli/DetectionAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package my.smartdec.detect.app.cli;

import my.smartdec.detect.RulesCached;
import my.smartdec.detect.RulesXml;
import my.smartdec.detect.app.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.net.URI;
import java.nio.file.*;
import java.util.*;
import java.util.function.Function;

/**
*
*/
public final class DetectionAll {

/**
* @param args args
* @throws Exception exception
*/
public static void main(final String... args) throws Exception {
DetectionAll.main(new ArgumentsDefault(args));
}

/**
* @param arguments args
* @throws Exception exception
*/
public static void main(final Arguments arguments) throws Exception {

/*Path src = arguments
.value("-p", "--path")
.map(Paths::get)
.filter(Files::exists)
.orElseThrow(IllegalArgumentException::new);*/

Function<SourceLanguage, RulesXml.Source> defaultRules =
sourceLanguage -> () -> {
String rulesFileName = sourceLanguage.rulesFileName();
URI uri = RulesXml
.class
.getResource(rulesFileName)
.toURI();
System.out.println(uri);

return Paths.get(uri);
};

Function<SourceLanguage, RulesXml.Source> rules = arguments
.value("-r", "--rules")
.map(Paths::get)
.filter(Files::isRegularFile)
.<Function<SourceLanguage, RulesXml.Source>>
map(path -> language -> () -> path)
.orElse(defaultRules);

//new Tooltest(src, rules).run();

//测试代码文件夹路径
// code1 result1200
// code2 result28800
///code3 result1
String fpath = "E:\\SmartContract\\mytest\\code3\\";
//获取文件夹下的文件目录
String[] filelists = getFileNames(fpath);
//输出结果的保存路径
String outpath = "E:\\SmartContract\\mytest\\result\\";
PrintWriter printWriter = new PrintWriter(new FileWriter( outpath + "result1.txt"));
//遍历批量测试
for (String itemName : filelists) {
//获取文件路径 例如:E:\SmartContract\codetest\0x1d4c723c9dfd9feb1cef39e2899693ccbc839fbe.sol
String erch = fpath + itemName;
//System.out.println(erch);

new DetectionAll(Paths.get(erch), rules).run(itemName.substring(0,42), printWriter);
}
printWriter.close();
}

/**
*
*/
private final Path source;
/**
*
*/
private final Function<
? super SourceLanguage,
? extends RulesXml.Source
> rules;

/**
* @param src source
* @param rs rules
*/
public DetectionAll(
final Path src,
final Function<
? super SourceLanguage,
? extends RulesXml.Source
> rs
) {
this.source = src;
this.rules = rs;
}

/**
*
* @param sourceLanguage source language
* @return directory analysis
* @throws Exception exception
*/
private DirectoryAnalysis makeDirectoryAnalysis(
final SourceLanguage sourceLanguage
) throws Exception {
return new DirectoryAnalysisDefault(
this.source,
p -> p.toString().endsWith(sourceLanguage.fileExtension()),
new TreeFactoryDefault(
DocumentBuilderFactory
.newInstance()
.newDocumentBuilder(),
sourceLanguage
),
new RulesCached(
new RulesXml(
this.rules.apply(sourceLanguage),
XPathFactory.newInstance().newXPath(),
Throwable::printStackTrace
)
)
);
}

/**
* @throws Exception exception
*/
public void run(String name, PrintWriter pw) throws Exception {

new ReportDefault(
new DirectoryAnalysisCombined(
makeDirectoryAnalysis(new SourceLanguages.Solidity()),
makeDirectoryAnalysis(new SourceLanguages.Vyper())
),
info -> {
System.out.println("````````````````````````````````````````````````");
System.out.println(info.file());
Map<String, Integer> result = new HashMap<>();
info.treeReport().streamUnchecked().forEach(
tree -> tree.contexts().forEach(
context -> {
System.out.printf(
"ruleId: %s%n"
+ "patternId: %s%n"
+ "severity: %d%n"
+ "line: %d%ncolumn: %d%n"
+ "content: %s%n%n",
tree.rule().id(),
tree.pattern().id(),
tree.pattern().severity(),
context.getStart().getLine(),
context
.getStart()
.getCharPositionInLine(),
context.getText()
);
result.compute(
tree.rule().id(),
(k, v) -> Optional
.ofNullable(v)
.map(i -> i + 1)
.orElse(1)
);
}
)
);
// 统计违反规则
result.forEach((k, v) -> System.out.println(k + " : " + v));
System.out.println();
//pw.println(name);
result.forEach(
(k, v) ->pw.println(name + " : " + k + " : " + v )

);
}
).print();
}
//获取文件路径目录
public static String[] getFileNames(String fpath) {
File filepath = new File(fpath);
if(!filepath.exists()){
System.out.println("路径不存在");
}
return filepath.list();
}

}
Loading

0 comments on commit 59fd14e

Please sign in to comment.