From 5aac14a5193ea0af0c9edfa548819c5883c25125 Mon Sep 17 00:00:00 2001 From: Jocelyn Kevorkian Date: Wed, 1 Apr 2020 15:01:09 +0200 Subject: [PATCH 1/2] Tests for regex support in the valid key Only tested on cpp_stl_11, python, java, go, js. --- formats/valid_regex.ksy | 17 ++++++++++ spec/cpp_stl_11/test_valid_regex.cpp | 16 ++++++++++ spec/go/valid_regex_test.go | 31 +++++++++++++++++++ .../io/kaitai/struct/spec/TestValidRegex.java | 15 +++++++++ spec/javascript/test_valid_regex.js | 8 +++++ spec/ks/valid_regex.kst | 2 ++ spec/python/test_valid_regex.py | 11 +++++++ 7 files changed, 100 insertions(+) create mode 100644 formats/valid_regex.ksy create mode 100644 spec/cpp_stl_11/test_valid_regex.cpp create mode 100644 spec/go/valid_regex_test.go create mode 100644 spec/java/src/io/kaitai/struct/spec/TestValidRegex.java create mode 100644 spec/javascript/test_valid_regex.js create mode 100644 spec/ks/valid_regex.kst create mode 100644 spec/python/test_valid_regex.py diff --git a/formats/valid_regex.ksy b/formats/valid_regex.ksy new file mode 100644 index 000000000..aa8ff93aa --- /dev/null +++ b/formats/valid_regex.ksy @@ -0,0 +1,17 @@ +meta: + id: valid_regex +seq: + - id: foo1 + type: str + encoding: ASCII + size: 6 + valid: + regex: P[A-Z]{2}K-\d + - id: pad1 + size: 2 + - id: foo2 + type: str + encoding: ASCII + size: 10 + valid: + regex: "[A-Z]{4}p*-+U-+DEF" \ No newline at end of file diff --git a/spec/cpp_stl_11/test_valid_regex.cpp b/spec/cpp_stl_11/test_valid_regex.cpp new file mode 100644 index 000000000..3b827436c --- /dev/null +++ b/spec/cpp_stl_11/test_valid_regex.cpp @@ -0,0 +1,16 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + +#include +#include "valid_regex.h" +#include +#include +#include + +BOOST_AUTO_TEST_CASE(test_valid_regex) { + std::ifstream ifs("src/fixed_struct.bin", std::ifstream::binary); + kaitai::kstream ks(&ifs); + valid_regex_t* r = new valid_regex_t(&ks); + + + delete r; +} diff --git a/spec/go/valid_regex_test.go b/spec/go/valid_regex_test.go new file mode 100644 index 000000000..3a42232ee --- /dev/null +++ b/spec/go/valid_regex_test.go @@ -0,0 +1,31 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + +package spec + +import ( + "runtime/debug" + "os" + "testing" + "github.com/kaitai-io/kaitai_struct_go_runtime/kaitai" + . "test_formats" +) + +func TestValidRegex(t *testing.T) { + defer func() { + if r := recover(); r != nil { + debug.PrintStack() + t.Fatal("unexpected panic:", r) + } + }() + f, err := os.Open("../../src/fixed_struct.bin") + if err != nil { + t.Fatal(err) + } + s := kaitai.NewStream(f) + var r ValidRegex + err = r.Read(s, &r, &r) + if err != nil { + t.Fatal(err) + } + +} diff --git a/spec/java/src/io/kaitai/struct/spec/TestValidRegex.java b/spec/java/src/io/kaitai/struct/spec/TestValidRegex.java new file mode 100644 index 000000000..d43f8ad23 --- /dev/null +++ b/spec/java/src/io/kaitai/struct/spec/TestValidRegex.java @@ -0,0 +1,15 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + +package io.kaitai.struct.spec; + +import io.kaitai.struct.testformats.ValidRegex; +import org.testng.annotations.Test; +import static org.testng.Assert.*; +public class TestValidRegex extends CommonSpec { + + @Test + public void testValidRegex() throws Exception { + ValidRegex r = ValidRegex.fromFile(SRC_DIR + "fixed_struct.bin"); + + } +} diff --git a/spec/javascript/test_valid_regex.js b/spec/javascript/test_valid_regex.js new file mode 100644 index 000000000..60df42340 --- /dev/null +++ b/spec/javascript/test_valid_regex.js @@ -0,0 +1,8 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + +var assert = require('assert'); +var testHelper = require('testHelper'); + +testHelper('ValidRegex', 'src/fixed_struct.bin', function(r, ValidRegex) { + +}); diff --git a/spec/ks/valid_regex.kst b/spec/ks/valid_regex.kst new file mode 100644 index 000000000..b047aa91d --- /dev/null +++ b/spec/ks/valid_regex.kst @@ -0,0 +1,2 @@ +id: valid_regex +data: fixed_struct.bin \ No newline at end of file diff --git a/spec/python/test_valid_regex.py b/spec/python/test_valid_regex.py new file mode 100644 index 000000000..13950ef21 --- /dev/null +++ b/spec/python/test_valid_regex.py @@ -0,0 +1,11 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest + +from valid_regex import ValidRegex + +class TestValidRegex(unittest.TestCase): + def test_valid_regex(self): + with ValidRegex.from_file('src/fixed_struct.bin') as r: + + pass From 33a0fdce9c6ec07d11fc7dd28183ccd64eef1b7b Mon Sep 17 00:00:00 2001 From: Jocelyn Kevorkian Date: Sat, 4 Apr 2020 22:25:45 +0200 Subject: [PATCH 2/2] Regex support : add a test to check exception --- formats/valid_fail_regex.ksy | 10 ++++++++++ formats/valid_regex.ksy | 2 +- spec/cpp_stl_11/test_valid_fail_regex.cpp | 20 +++++++++++++++++++ spec/cpp_stl_98/test_valid_fail_regex.cpp | 20 +++++++++++++++++++ .../struct/spec/TestValidFailRegex.java | 15 ++++++++++++++ spec/ks/valid_fail_regex.kst | 3 +++ spec/ks/valid_regex.kst | 2 +- spec/python/test_valid_fail_regex.py | 12 +++++++++++ 8 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 formats/valid_fail_regex.ksy create mode 100644 spec/cpp_stl_11/test_valid_fail_regex.cpp create mode 100644 spec/cpp_stl_98/test_valid_fail_regex.cpp create mode 100644 spec/java/src/io/kaitai/struct/spec/TestValidFailRegex.java create mode 100644 spec/ks/valid_fail_regex.kst create mode 100644 spec/python/test_valid_fail_regex.py diff --git a/formats/valid_fail_regex.ksy b/formats/valid_fail_regex.ksy new file mode 100644 index 000000000..8fad5eee8 --- /dev/null +++ b/formats/valid_fail_regex.ksy @@ -0,0 +1,10 @@ +meta: + id: valid_fail_regex +seq: + - id: foo1 + type: str + encoding: ASCII + size: 6 + valid: + regex: P[a-z]{2}K-\d +#expecting lower case, where there are upper case letters in the file diff --git a/formats/valid_regex.ksy b/formats/valid_regex.ksy index aa8ff93aa..da5282d83 100644 --- a/formats/valid_regex.ksy +++ b/formats/valid_regex.ksy @@ -14,4 +14,4 @@ seq: encoding: ASCII size: 10 valid: - regex: "[A-Z]{4}p*-+U-+DEF" \ No newline at end of file + regex: "[A-Z]{4}p*-+U-+DEF" diff --git a/spec/cpp_stl_11/test_valid_fail_regex.cpp b/spec/cpp_stl_11/test_valid_fail_regex.cpp new file mode 100644 index 000000000..08c13e162 --- /dev/null +++ b/spec/cpp_stl_11/test_valid_fail_regex.cpp @@ -0,0 +1,20 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + +#include +#include "valid_fail_regex.h" +#include +#include +#include +#include "kaitai/exceptions.h" + +BOOST_AUTO_TEST_CASE(test_valid_fail_regex) { + std::ifstream ifs("src/fixed_struct.bin", std::ifstream::binary); + kaitai::kstream ks(&ifs); + valid_fail_regex_t* r = nullptr; + BOOST_CHECK_THROW( + r = new valid_fail_regex_t(&ks), + kaitai::validation_regex_match_error + ); + + delete r; +} diff --git a/spec/cpp_stl_98/test_valid_fail_regex.cpp b/spec/cpp_stl_98/test_valid_fail_regex.cpp new file mode 100644 index 000000000..54dfd5b19 --- /dev/null +++ b/spec/cpp_stl_98/test_valid_fail_regex.cpp @@ -0,0 +1,20 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + +#include +#include "valid_fail_regex.h" +#include +#include +#include +#include "kaitai/exceptions.h" + +BOOST_AUTO_TEST_CASE(test_valid_fail_regex) { + std::ifstream ifs("src/fixed_struct.bin", std::ifstream::binary); + kaitai::kstream ks(&ifs); + valid_fail_regex_t* r = 0; + BOOST_CHECK_THROW( + r = new valid_fail_regex_t(&ks), + kaitai::validation_regex_match_error + ); + + delete r; +} diff --git a/spec/java/src/io/kaitai/struct/spec/TestValidFailRegex.java b/spec/java/src/io/kaitai/struct/spec/TestValidFailRegex.java new file mode 100644 index 000000000..41f0ab2d2 --- /dev/null +++ b/spec/java/src/io/kaitai/struct/spec/TestValidFailRegex.java @@ -0,0 +1,15 @@ +// Autogenerated from KST: please remove this line if doing any edits by hand! + +package io.kaitai.struct.spec; + +import io.kaitai.struct.testformats.ValidFailRegex; +import org.testng.annotations.Test; +import static org.testng.Assert.*; +import io.kaitai.struct.KaitaiStream; +public class TestValidFailRegex extends CommonSpec { + + @Test(expectedExceptions = KaitaiStream.ValidationRegexMatchError.class) + public void testValidFailRegex() throws Exception { + ValidFailRegex r = ValidFailRegex.fromFile(SRC_DIR + "fixed_struct.bin"); + } +} diff --git a/spec/ks/valid_fail_regex.kst b/spec/ks/valid_fail_regex.kst new file mode 100644 index 000000000..9fae3f00a --- /dev/null +++ b/spec/ks/valid_fail_regex.kst @@ -0,0 +1,3 @@ +id: valid_fail_regex +data: fixed_struct.bin +exception: ValidationRegexMatchError diff --git a/spec/ks/valid_regex.kst b/spec/ks/valid_regex.kst index b047aa91d..1945a7eff 100644 --- a/spec/ks/valid_regex.kst +++ b/spec/ks/valid_regex.kst @@ -1,2 +1,2 @@ id: valid_regex -data: fixed_struct.bin \ No newline at end of file +data: fixed_struct.bin diff --git a/spec/python/test_valid_fail_regex.py b/spec/python/test_valid_fail_regex.py new file mode 100644 index 000000000..761a09b93 --- /dev/null +++ b/spec/python/test_valid_fail_regex.py @@ -0,0 +1,12 @@ +# Autogenerated from KST: please remove this line if doing any edits by hand! + +import unittest +import kaitaistruct + +from valid_fail_regex import ValidFailRegex + +class TestValidFailRegex(unittest.TestCase): + def test_valid_fail_regex(self): + with self.assertRaises(kaitaistruct.ValidationRegexMatchError): + with ValidFailRegex.from_file('src/fixed_struct.bin') as r: + pass