parent
c882780adf
commit
1534e32307
@ -0,0 +1,21 @@ |
||||
/* |
||||
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||
* 项目名称:C++ 信息安全性设计准则 |
||||
* 项目描述:用于检查C++源代码的安全性设计准则的Sonarqube插件 |
||||
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||
*/ |
||||
package com.keyware.sonar.cxx; |
||||
|
||||
import org.sonar.api.batch.fs.InputFile; |
||||
import org.sonar.api.batch.sensor.internal.SensorContextTester; |
||||
|
||||
public class CxxFileTester { |
||||
|
||||
public InputFile cxxFile; |
||||
public SensorContextTester context; |
||||
|
||||
public InputFile asInputFile() { |
||||
return cxxFile; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,68 @@ |
||||
/* |
||||
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||
* 项目名称:C++ 信息安全性设计准则 |
||||
* 项目描述:用于检查C++源代码的安全性设计准则的Sonarqube插件 |
||||
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||
*/ |
||||
package com.keyware.sonar.cxx; |
||||
|
||||
import org.apache.commons.io.ByteOrderMark; |
||||
import org.apache.commons.io.input.BOMInputStream; |
||||
import org.sonar.api.batch.fs.internal.DefaultInputFile; |
||||
import org.sonar.api.batch.fs.internal.TestInputFileBuilder; |
||||
import org.sonar.api.batch.sensor.internal.SensorContextTester; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.IOException; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.nio.charset.Charset; |
||||
import java.nio.file.Path; |
||||
|
||||
public class CxxFileTesterHelper { |
||||
|
||||
private CxxFileTesterHelper() { |
||||
// utility class
|
||||
} |
||||
|
||||
public static CxxFileTester create(String fileName) |
||||
throws UnsupportedEncodingException, IOException { |
||||
var path = "src/test/resources/com/keyware/sonar/cxx/rules/checkers/" + fileName; |
||||
return create(path, ".", Charset.defaultCharset()); |
||||
} |
||||
|
||||
public static CxxFileTester create(String fileName, String basePath, Charset charset) |
||||
throws UnsupportedEncodingException, IOException { |
||||
var tester = new CxxFileTester(); |
||||
|
||||
tester.context = SensorContextTester.create(new File(basePath)); |
||||
tester.cxxFile = createInputFile(fileName, basePath, charset); |
||||
tester.context.fileSystem().add(tester.cxxFile); |
||||
|
||||
return tester; |
||||
} |
||||
|
||||
private static DefaultInputFile createInputFile(String fileName, String basePath, Charset charset) throws IOException { |
||||
var fb = TestInputFileBuilder.create("", fileName); |
||||
|
||||
fb.setCharset(charset); |
||||
fb.setProjectBaseDir(Path.of(basePath)); |
||||
fb.setContents(getSourceCode(Path.of(basePath, fileName).toFile(), charset)); |
||||
|
||||
return fb.build(); |
||||
} |
||||
|
||||
private static String getSourceCode(File filename, Charset defaultCharset) throws IOException { |
||||
try (var bomInputStream = new BOMInputStream(new FileInputStream(filename), |
||||
ByteOrderMark.UTF_8, |
||||
ByteOrderMark.UTF_16LE, |
||||
ByteOrderMark.UTF_16BE, |
||||
ByteOrderMark.UTF_32LE, |
||||
ByteOrderMark.UTF_32BE)) { |
||||
ByteOrderMark bom = bomInputStream.getBOM(); |
||||
Charset charset = bom != null ? Charset.forName(bom.getCharsetName()) : defaultCharset; |
||||
byte[] bytes = bomInputStream.readAllBytes(); |
||||
return new String(bytes, charset); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,33 @@ |
||||
/* |
||||
* Copyright (c) 2023 - 2024. KeyWare.Co.Ltd All rights reserved. |
||||
* 项目名称:C++ 信息安全性设计准则 |
||||
* 项目描述:用于检查C++源代码的安全性设计准则的Sonarqube插件 |
||||
* 版权说明:本软件属北京关键科技股份有限公司所有,在未获得北京关键科技股份有限公司正式授权情况下,任何企业和个人,不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。 |
||||
*/ |
||||
package com.keyware.sonar.cxx.rules.checkers; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import org.sonar.cxx.CxxAstScanner; |
||||
import com.keyware.sonar.cxx.CxxFileTesterHelper; |
||||
import org.sonar.cxx.squidbridge.api.SourceFile; |
||||
import org.sonar.cxx.squidbridge.checks.CheckMessagesVerifier; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* TODO ABCVarNameCheckerTest |
||||
* |
||||
* @author GuoXin |
||||
* @date 2024/1/8 |
||||
*/ |
||||
public class ABCVarNameCheckerTest { |
||||
@Test |
||||
public void checkTest() throws IOException { |
||||
var checker = new ABCVarNameChecker(); |
||||
var tester = CxxFileTesterHelper.create("ABCVarNameChecker.cc"); |
||||
SourceFile file = CxxAstScanner.scanSingleInputFile(tester.asInputFile(), checker); |
||||
CheckMessagesVerifier.verify(file.getCheckMessages()) |
||||
.next().atLine(3).withMessage("变量名称不可以是ABC") |
||||
.noMore(); |
||||
} |
||||
} |
@ -0,0 +1,6 @@ |
||||
// ABCVarNameChecker
|
||||
int main(void){ |
||||
int ABC = 1; // error
|
||||
int abc = 2; |
||||
return 0; |
||||
} |
Loading…
Reference in new issue