Skip to content

Commit

Permalink
test(bacnet): fix pcap download
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Oct 2, 2023
1 parent c088d5e commit b3800fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,32 @@
*/
package org.apache.plc4x.java.bacnetip;

import org.apache.commons.io.FileUtils;
import org.apache.plc4x.java.bacnetip.readwrite.BVLC;
import org.apache.plc4x.test.generator.ParserSerializerTestsuiteGenerator;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class BACnetParserSerializerTestSuiteGenerator {

public static void main(String... args) {
String pcapFile = Path.of("plc4j/utils/test-generator/src/test/resources/bacnet-stack-services.cap").toAbsolutePath().toString();
public static void main(String... args) throws Exception {
String pcapFile = DownloadAndCache("bacnet-stack-services.cap");
String xmlTestSuiteFile = Path.of("protocols/bacnetip/src/test/resources/protocols/bacnet/ParserSerializerTestsuite.xml").toAbsolutePath().toString();
ParserSerializerTestsuiteGenerator.main("-tBACnet/IP", "-pbacnetip", BVLC.class.getName(), pcapFile, xmlTestSuiteFile);
}

private static String DownloadAndCache(String file) throws IOException {
String tempDirectory = FileUtils.getTempDirectoryPath();
File pcapFile = FileSystems.getDefault().getPath(tempDirectory, RandomPackagesTest.class.getSimpleName(), file).toFile();
FileUtils.createParentDirectories(pcapFile);
if (!pcapFile.exists()) {
URL source = new URL("https://kargs.net/captures/" + file);
FileUtils.copyURLToFile(source, pcapFile);
}
return pcapFile.getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package org.apache.plc4x.test.generator
import org.opentest4j.TestAbortedException
import spock.lang.IgnoreIf

import java.nio.file.FileSystems

import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo
import static spock.util.matcher.HamcrestSupport.*
import spock.lang.Specification
Expand Down Expand Up @@ -65,12 +67,11 @@ class ParserSerializerTestsuiteGeneratorSpec extends Specification {
}
if (!new File('/bin/sh').canExecute()) throw new TestAbortedException("No bin sh")
def testSuitePath = Files.createTempFile("parser-serializer-testsuite", ".xml")
URL pcap = ParserSerializerTestsuiteGeneratorSpec.getResource("/bacnet-stack-services.cap");
File pcapFile = new File(pcap.toURI());
def pcapFile = DownloadAndCache("bacnet-stack-services.cap")
ParserSerializerTestsuiteGenerator.exitFunc = (it) -> println("exiting with $it")

when:
ParserSerializerTestsuiteGenerator.main("-d", "-t TODO: name me", "-l", DummyMessageRootType.class.name, pcapFile.path, testSuitePath.toString())
ParserSerializerTestsuiteGenerator.main("-d", "-t TODO: name me", "-l", DummyMessageRootType.class.name, pcapFile, testSuitePath.toString())

then:
assert Files.exists(testSuitePath)
Expand All @@ -79,4 +80,16 @@ class ParserSerializerTestsuiteGeneratorSpec extends Specification {
def actual = testSuitePath.toFile().text
that actual, isIdenticalTo(expected).ignoreComments().ignoreWhitespace()
}

private String DownloadAndCache(String file) throws IOException {
String tempDirectory = FileUtils.getTempDirectoryPath();
File pcapFile = FileSystems.getDefault().getPath(tempDirectory, RandomPackagesTest.class.getSimpleName(), file).toFile();
FileUtils.createParentDirectories(pcapFile);
if (!pcapFile.exists()) {
URL source = new URL("https://kargs.net/captures/" + file);
LOGGER.info("Downloading {}", source);
FileUtils.copyURLToFile(source, pcapFile);
}
return pcapFile.getAbsolutePath();
}
}
Binary file not shown.

0 comments on commit b3800fc

Please sign in to comment.