@@ -0,0 +1,2366 @@
+/*%0A * Copyright 2013-2016 Netherlands Forensic Institute%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage nl.minvenj.nfi.metal;%0A%0Aimport static nl.minvenj.nfi.metal.Shorthand.con;%0Aimport static nl.minvenj.nfi.metal.Shorthand.def;%0Aimport static nl.minvenj.nfi.metal.Shorthand.ref;%0Aimport static nl.minvenj.nfi.metal.Shorthand.seq;%0A%0Aimport java.io.IOException;%0A%0Aimport org.junit.Assert;%0Aimport org.junit.Test;%0A%0Aimport nl.minvenj.nfi.metal.data.ByteStream;%0Aimport nl.minvenj.nfi.metal.data.Environment;%0Aimport nl.minvenj.nfi.metal.data.ParseResult;%0Aimport nl.minvenj.nfi.metal.encoding.Encoding;%0Aimport nl.minvenj.nfi.metal.token.Token;%0Aimport nl.minvenj.nfi.metal.util.InMemoryByteStream;%0A%0Apublic class DefSizeTest %7B%0A    public static final Token FORMAT =%0A        seq(%0A            def(%22length%22, con(4)),%0A            def(%22data%22, ref(%22length%22))%0A        );%0A%0A    @Test%0A    public void testValidLength() throws IOException %7B%0A        final ByteStream stream = new InMemoryByteStream(new byte%5B%5D%7B%0A            0x00, 0x00, 0x00, 0x02, // length = 2%0A            0x04, 0x08%0A        %7D);%0A        final ParseResult result = FORMAT.parse(new Environment(stream), new Encoding());%0A%0A        Assert.assertTrue(result.succeeded());%0A        Assert.assertArrayEquals(%0A            new byte%5B%5D%7B0x04, 0x08%7D,%0A            result.getEnvironment().order.flatten().get(%22data%22).getValue()%0A        );%0A    %7D%0A%0A    @Test%0A    public void testInvalidLength() throws IOException %7B%0A        final ByteStream stream = new InMemoryByteStream(new byte%5B%5D%7B%0A            -1, -1, -1, -1, // length = -1%0A            0x04, 0x08%0A        %7D);%0A        final ParseResult result = FORMAT.parse(new Environment(stream), new Encoding());%0A%0A        Assert.assertFalse(result.succeeded());%0A        Assert.assertEquals(-1, result.getEnvironment().order.flatten().get(%22length%22).asNumeric().intValue());%0A    %7D%0A%7D%0A

Test valid vs. invalid length reference handling