From 231beb54b77d0161799d5227865f4d6f7fcd56f8 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Sun, 28 Jul 2024 19:34:47 +0100 Subject: [PATCH] parser_spec: add partial_array_int + partial_array_struct tests --- spec/formats/partial_array_int.ksy | 17 ++++++++++++++++ spec/formats/partial_array_struct.ksy | 17 ++++++++++++++++ spec/parser_spec.rb | 28 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 spec/formats/partial_array_int.ksy create mode 100644 spec/formats/partial_array_struct.ksy diff --git a/spec/formats/partial_array_int.ksy b/spec/formats/partial_array_int.ksy new file mode 100644 index 0000000..acee14e --- /dev/null +++ b/spec/formats/partial_array_int.ksy @@ -0,0 +1,17 @@ +# Input file only has 7 bytes, but we aim to load 5x 2-byte objects as array. +# Expected to have 3 objects + 1 partial object. + +meta: + id: partial_array_int +seq: + - id: entries + repeat: expr + repeat-expr: 5 + type: entry +types: + entry: + seq: + - id: a + type: u1 + - id: b + type: u1 diff --git a/spec/formats/partial_array_struct.ksy b/spec/formats/partial_array_struct.ksy new file mode 100644 index 0000000..28a9edd --- /dev/null +++ b/spec/formats/partial_array_struct.ksy @@ -0,0 +1,17 @@ +# Input file only has 7 bytes, but we aim to load 5x 2-byte objects as array. +# Expected to have 3 objects + 1 partial object. + +meta: + id: partial_array_struct +seq: + - id: entries + repeat: expr + repeat-expr: 5 + type: entry +types: + entry: + seq: + - id: a + type: u1 + - id: b + type: u1 diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 8704680..2e2d338 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -43,6 +43,34 @@ module Kaitai::Struct::Visualizer expect(parser.data._debug['bar']).to eq({ start: 5 }) end + it 'handles EOF error in array' do + opts = {} + compiler = KSYCompiler.new(opts) + parser = Parser.new(compiler, 'input/7bytes.bin', ['formats/partial_array_struct.ksy'], opts) + + exc = parser.load + + # we want 5 entries of 2 bytes each = 10 bytes, only 7 bytes available + expect(exc).to be_a(EOFError) + + expect(parser.data.class::SEQ_FIELDS).to eq(%w[entries]) + + # 4 entries: 3 full, last one partial + expect(parser.data.entries.size).to eq(4) + + # 3 full entries + expect(parser.data.entries[0].a).to eq(49) + expect(parser.data.entries[0].b).to eq(50) + expect(parser.data.entries[1].a).to eq(51) + expect(parser.data.entries[1].b).to eq(52) + expect(parser.data.entries[2].a).to eq(53) + expect(parser.data.entries[2].b).to eq(54) + + # last entry is partial + expect(parser.data.entries[3].a).to eq(55) + expect(parser.data.entries[3].b).to be_nil + end + it 'handles validation error in seq' do opts = {} compiler = KSYCompiler.new(opts)