parent
a4aa0ebff9
commit
883256a330
@ -0,0 +1,113 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||||
|
* 项目名称:信息安全性设计准则检查插件 |
||||||
|
* 项目描述:用于检查源代码的安全性设计准则的Sonarqube插件 |
||||||
|
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.keyware.sonar.java.utils; |
||||||
|
|
||||||
|
import org.sonar.plugins.html.checks.HtmlIssue; |
||||||
|
import org.sonar.plugins.html.checks.PreciseHtmlIssue; |
||||||
|
|
||||||
|
import javax.annotation.Nullable; |
||||||
|
import java.util.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Html检查消息验证器 |
||||||
|
* |
||||||
|
* @author GuoXin |
||||||
|
* @date 2024/1/20 |
||||||
|
*/ |
||||||
|
public final class HtmlCheckMessagesVerifier { |
||||||
|
|
||||||
|
public static HtmlCheckMessagesVerifier verify(Collection<HtmlIssue> messages) { |
||||||
|
return new HtmlCheckMessagesVerifier(messages); |
||||||
|
} |
||||||
|
|
||||||
|
private final Iterator<HtmlIssue> iterator; |
||||||
|
private HtmlIssue current; |
||||||
|
|
||||||
|
private static final Comparator<HtmlIssue> ORDERING = (left, right) -> { |
||||||
|
if (Objects.equals(left.line(), right.line())) { |
||||||
|
return left.message().compareTo(right.message()); |
||||||
|
} else if (left.line() == null) { |
||||||
|
return -1; |
||||||
|
} else if (right.line() == null) { |
||||||
|
return 1; |
||||||
|
} else { |
||||||
|
return left.line().compareTo(right.line()); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
private HtmlCheckMessagesVerifier(Collection<HtmlIssue> messages) { |
||||||
|
ArrayList<HtmlIssue> messagesList = new ArrayList<>(messages); |
||||||
|
messagesList.sort(ORDERING); |
||||||
|
iterator = messagesList.iterator(); |
||||||
|
} |
||||||
|
|
||||||
|
public HtmlCheckMessagesVerifier next() { |
||||||
|
if (!iterator.hasNext()) { |
||||||
|
throw new AssertionError("\nExpected violation"); |
||||||
|
} |
||||||
|
current = iterator.next(); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public void noMore() { |
||||||
|
if (iterator.hasNext()) { |
||||||
|
HtmlIssue next = iterator.next(); |
||||||
|
throw new AssertionError("\nNo more violations expected\ngot: at line " + next.line()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void checkStateOfCurrent() { |
||||||
|
if (current == null) { |
||||||
|
throw new IllegalStateException("Prior to this method you should call next()"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public HtmlCheckMessagesVerifier atLine(@Nullable Integer expectedLine) { |
||||||
|
checkStateOfCurrent(); |
||||||
|
if (!Objects.equals(expectedLine, current.line())) { |
||||||
|
throw new AssertionError("\nExpected: " + expectedLine + "\ngot: " + current.line()); |
||||||
|
} |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public HtmlCheckMessagesVerifier atLocation(int startLine, int startColumn, int endLine, int endColumn) { |
||||||
|
checkStateOfCurrent(); |
||||||
|
PreciseHtmlIssue preciseHtmlIssue = (PreciseHtmlIssue) current; |
||||||
|
if (!Objects.equals(startLine, current.line())) { |
||||||
|
throw new AssertionError("\nExpected: " + startLine + "\ngot: " + current.line()); |
||||||
|
} |
||||||
|
if (!Objects.equals(startColumn, preciseHtmlIssue.startColumn())) { |
||||||
|
throw new AssertionError("\nExpected: " + startColumn + "\ngot: " + preciseHtmlIssue.startColumn()); |
||||||
|
} |
||||||
|
if (!Objects.equals(endLine, preciseHtmlIssue.endLine())) { |
||||||
|
throw new AssertionError("\nExpected: " + endLine + "\ngot: " + preciseHtmlIssue.endLine()); |
||||||
|
} |
||||||
|
if (!Objects.equals(endColumn, preciseHtmlIssue.endColumn())) { |
||||||
|
throw new AssertionError("\nExpected: " + endColumn + "\ngot: " + preciseHtmlIssue.endColumn()); |
||||||
|
} |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public HtmlCheckMessagesVerifier withMessage(String expectedMessage) { |
||||||
|
checkStateOfCurrent(); |
||||||
|
String actual = current.message(); |
||||||
|
if (!actual.equals(expectedMessage)) { |
||||||
|
throw new AssertionError("\nExpected: \"" + expectedMessage + "\"\ngot: \"" + actual + "\""); |
||||||
|
} |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
public HtmlCheckMessagesVerifier withCost(@Nullable Double expectedCost) { |
||||||
|
checkStateOfCurrent(); |
||||||
|
if (!Objects.equals(expectedCost, current.cost())) { |
||||||
|
throw new AssertionError("\nExpected: " + expectedCost + "\ngot: " + current.cost()); |
||||||
|
} |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,43 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||||
|
* 项目名称:信息安全性设计准则检查插件 |
||||||
|
* 项目描述:用于检查源代码的安全性设计准则的Sonarqube插件 |
||||||
|
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.keyware.sonar.java.utils; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.extension.AfterEachCallback; |
||||||
|
import org.junit.jupiter.api.extension.ExtensionContext; |
||||||
|
import org.sonar.plugins.html.checks.HtmlIssue; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Html规则校验 |
||||||
|
* |
||||||
|
* @author GuoXin |
||||||
|
* @date 2024/1/20 |
||||||
|
*/ |
||||||
|
public class HtmlCheckMessagesVerifierRule implements AfterEachCallback { |
||||||
|
private final List<HtmlCheckMessagesVerifier> verifiers = new ArrayList<>(); |
||||||
|
|
||||||
|
public HtmlCheckMessagesVerifier verify(Collection<HtmlIssue> messages) { |
||||||
|
HtmlCheckMessagesVerifier verifier = HtmlCheckMessagesVerifier.verify(messages); |
||||||
|
verifiers.add(verifier); |
||||||
|
return verifier; |
||||||
|
} |
||||||
|
|
||||||
|
protected void verify() { |
||||||
|
for (HtmlCheckMessagesVerifier verifier : verifiers) { |
||||||
|
verifier.noMore(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void afterEach(ExtensionContext extensionContext) throws Exception { |
||||||
|
verify(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,66 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||||
|
* 项目名称:信息安全性设计准则检查插件 |
||||||
|
* 项目描述:用于检查源代码的安全性设计准则的Sonarqube插件 |
||||||
|
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.keyware.sonar.java.utils; |
||||||
|
|
||||||
|
import org.sonar.api.batch.fs.InputFile; |
||||||
|
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; |
||||||
|
import org.sonar.plugins.html.analyzers.ComplexityVisitor; |
||||||
|
import org.sonar.plugins.html.analyzers.PageCountLines; |
||||||
|
import org.sonar.plugins.html.api.HtmlConstants; |
||||||
|
import org.sonar.plugins.html.lex.PageLexer; |
||||||
|
import org.sonar.plugins.html.lex.VueLexer; |
||||||
|
import org.sonar.plugins.html.visitor.DefaultNodeVisitor; |
||||||
|
import org.sonar.plugins.html.visitor.HtmlAstScanner; |
||||||
|
import org.sonar.plugins.html.visitor.HtmlSourceCode; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileNotFoundException; |
||||||
|
import java.io.FileReader; |
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Html测试工具类 |
||||||
|
* |
||||||
|
* @author GuoXin |
||||||
|
* @date 2024/1/20 |
||||||
|
*/ |
||||||
|
public class HtmlTestHelper { |
||||||
|
|
||||||
|
private HtmlTestHelper() { |
||||||
|
} |
||||||
|
|
||||||
|
public static HtmlSourceCode scan(File file, DefaultNodeVisitor visitor) { |
||||||
|
FileReader fileReader; |
||||||
|
try { |
||||||
|
fileReader = new FileReader(file); |
||||||
|
} catch (FileNotFoundException e) { |
||||||
|
throw new IllegalStateException(e); |
||||||
|
} |
||||||
|
|
||||||
|
HtmlSourceCode result = new HtmlSourceCode( |
||||||
|
new TestInputFileBuilder("key", file.getPath()) |
||||||
|
.setLanguage(HtmlConstants.LANGUAGE_KEY) |
||||||
|
.setType(InputFile.Type.MAIN) |
||||||
|
.setModuleBaseDir(new File(".").toPath()) |
||||||
|
.setCharset(StandardCharsets.UTF_8) |
||||||
|
.build() |
||||||
|
); |
||||||
|
|
||||||
|
HtmlAstScanner walker = new HtmlAstScanner(List.of(new PageCountLines(), new ComplexityVisitor())); |
||||||
|
PageLexer lexer = file.getName().endsWith(".vue") ? new VueLexer() : new PageLexer(); |
||||||
|
walker.addVisitor(visitor); |
||||||
|
walker.scan( |
||||||
|
lexer.parse(fileReader), |
||||||
|
result |
||||||
|
); |
||||||
|
|
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue