dan-s1 commented on code in PR #8691: URL: https://github.com/apache/nifi/pull/8691#discussion_r1591577732
########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitPcap.java: ########## @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard; + +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; Review Comment: Replace JUnit 4 constructs with Junit 5 constructs ```suggestion import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitPcap.java: ########## @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard; + +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; +import org.apache.nifi.processors.standard.util.Pcap; +import org.apache.nifi.processors.standard.util.Pcap.Header; +import org.apache.nifi.processors.standard.util.Pcap.Packet; + + +public class TestSplitPcap { + + private Header hdr; + private Packet validPacket; + private Packet invalidPacket; + + @Before Review Comment: ```suggestion @BeforeEach ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/Pcap.java: ########## @@ -0,0 +1,734 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// MIT License + +// Copyright (c) 2015-2023 Kaitai Project + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. Review Comment: It is not necessary to include this as the license is mentioned in the NOTICE file ```suggestion ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPcap.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard.util; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; + +import org.apache.nifi.processors.standard.util.Pcap.Packet; +import org.apache.nifi.processors.standard.util.Pcap.Header; + +import org.junit.jupiter.api.BeforeEach; Review Comment: ```suggestion ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitPcap.java: ########## @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard; + +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; +import org.apache.nifi.processors.standard.util.Pcap; +import org.apache.nifi.processors.standard.util.Pcap.Header; +import org.apache.nifi.processors.standard.util.Pcap.Packet; + + +public class TestSplitPcap { + + private Header hdr; + private Packet validPacket; + private Packet invalidPacket; + + @Before + public void init(){ + // Create a header for the test PCAP + this.hdr = new Header( + new byte[]{(byte) 0xa1, (byte) 0xb2, (byte) 0xc3, (byte) 0xd4}, + 2, + 4, + 0, + (long) 0, + (long) 40, + "ETHERNET" + ); + + this.validPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 30, + (long) 30, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + this.invalidPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 10, + (long) 10, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + } + + @Test + public void testValidPackets() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.validPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); + + runner.run(); + + runner.assertAllFlowFilesTransferred(SplitPcap.REL_SUCCESS, 3); + runner.assertQueueEmpty(); + } + + @Test + public void testInvalidPackets() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.invalidPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); Review Comment: ```suggestion runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); ArrayList<Packet> packets = new ArrayList<>(); for (var loop = 0; loop < 3; loop++){ packets.add(this.invalidPacket); } Pcap testPcap = new Pcap(this.hdr, packets); runner.enqueue(testPcap.readBytesFull()); ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitPcap.java: ########## @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard; + +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; +import org.apache.nifi.processors.standard.util.Pcap; +import org.apache.nifi.processors.standard.util.Pcap.Header; +import org.apache.nifi.processors.standard.util.Pcap.Packet; + + +public class TestSplitPcap { + + private Header hdr; + private Packet validPacket; + private Packet invalidPacket; + + @Before + public void init(){ + // Create a header for the test PCAP + this.hdr = new Header( + new byte[]{(byte) 0xa1, (byte) 0xb2, (byte) 0xc3, (byte) 0xd4}, + 2, + 4, + 0, + (long) 0, + (long) 40, + "ETHERNET" + ); + + this.validPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 30, + (long) 30, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + this.invalidPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 10, + (long) 10, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + } + + @Test + public void testValidPackets() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.validPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); Review Comment: Align the code as below ```suggestion runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); ArrayList<Packet> packets = new ArrayList<>(); for (var loop = 0; loop < 3; loop++){ packets.add(this.validPacket); } Pcap testPcap = new Pcap(this.hdr, packets); runner.enqueue(testPcap.readBytesFull()); ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitPcap.java: ########## @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard; + +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; +import org.apache.nifi.processors.standard.util.Pcap; +import org.apache.nifi.processors.standard.util.Pcap.Header; +import org.apache.nifi.processors.standard.util.Pcap.Packet; + + +public class TestSplitPcap { + + private Header hdr; + private Packet validPacket; + private Packet invalidPacket; + + @Before + public void init(){ + // Create a header for the test PCAP + this.hdr = new Header( + new byte[]{(byte) 0xa1, (byte) 0xb2, (byte) 0xc3, (byte) 0xd4}, + 2, + 4, + 0, + (long) 0, + (long) 40, + "ETHERNET" + ); + + this.validPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 30, + (long) 30, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + this.invalidPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 10, + (long) 10, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + } + + @Test + public void testValidPackets() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.validPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); + + runner.run(); + + runner.assertAllFlowFilesTransferred(SplitPcap.REL_SUCCESS, 3); + runner.assertQueueEmpty(); + } + + @Test + public void testInvalidPackets() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.invalidPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); + + runner.run(); + + runner.assertAllFlowFilesTransferred(SplitPcap.REL_FAILURE, 1); + runner.assertQueueEmpty(); + } + + @Test + public void testPacketsTooBig() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "10"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.validPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); + + runner.run(); + + runner.assertAllFlowFilesTransferred(SplitPcap.REL_FAILURE, 1); + runner.assertQueueEmpty(); + } + + @Test + public void testOneInvalidPacket() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "10"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.validPacket); + } + + packets.add(this.invalidPacket); + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); Review Comment: ```suggestion runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "10"); ArrayList<Packet> packets = new ArrayList<>(); for (var loop = 0; loop < 3; loop++){ packets.add(this.validPacket); } packets.add(this.invalidPacket); Pcap testPcap = new Pcap(this.hdr, packets); runner.enqueue(testPcap.readBytesFull()); ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPcap.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard.util; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; + +import org.apache.nifi.processors.standard.util.Pcap.Packet; +import org.apache.nifi.processors.standard.util.Pcap.Header; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.junit.Assert; + +public class TestPcap { + + @BeforeEach + public void init() { + + } Review Comment: Not used for anything hence remove. ```suggestion ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitPcap.java: ########## @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard; + +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; +import org.apache.nifi.processors.standard.util.Pcap; +import org.apache.nifi.processors.standard.util.Pcap.Header; +import org.apache.nifi.processors.standard.util.Pcap.Packet; + + +public class TestSplitPcap { + + private Header hdr; + private Packet validPacket; + private Packet invalidPacket; + + @Before + public void init(){ + // Create a header for the test PCAP + this.hdr = new Header( + new byte[]{(byte) 0xa1, (byte) 0xb2, (byte) 0xc3, (byte) 0xd4}, + 2, + 4, + 0, + (long) 0, + (long) 40, + "ETHERNET" + ); + + this.validPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 30, + (long) 30, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + this.invalidPacket = new Packet( + (long) 1713184965, + (long) 1000, + (long) 10, + (long) 10, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET" + ); + + } + + @Test + public void testValidPackets() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.validPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); + + runner.run(); + + runner.assertAllFlowFilesTransferred(SplitPcap.REL_SUCCESS, 3); + runner.assertQueueEmpty(); + } + + @Test + public void testInvalidPackets() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "50"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.invalidPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); + + runner.run(); + + runner.assertAllFlowFilesTransferred(SplitPcap.REL_FAILURE, 1); + runner.assertQueueEmpty(); + } + + @Test + public void testPacketsTooBig() throws IOException { + TestRunner runner = TestRunners.newTestRunner(SplitPcap.class); + runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "10"); + + ArrayList<Packet> packets = new ArrayList<>(); + for (var loop = 0; loop < 3; loop++){ + packets.add(this.validPacket); + } + + Pcap testPcap = new Pcap(this.hdr, packets); + + runner.enqueue(testPcap.readBytesFull()); Review Comment: ```suggestion runner.setProperty(SplitPcap.PCAP_MAX_SIZE, "10"); ArrayList<Packet> packets = new ArrayList<>(); for (var loop = 0; loop < 3; loop++){ packets.add(this.validPacket); } Pcap testPcap = new Pcap(this.hdr, packets); runner.enqueue(testPcap.readBytesFull()); ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPcap.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard.util; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; + +import org.apache.nifi.processors.standard.util.Pcap.Packet; +import org.apache.nifi.processors.standard.util.Pcap.Header; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.junit.Assert; Review Comment: Replace JUnit 4 `Assert` with Junit 5 `Assertions` ```suggestion import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestPcap.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.nifi.processors.standard.util; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; + +import org.apache.nifi.processors.standard.util.Pcap.Packet; +import org.apache.nifi.processors.standard.util.Pcap.Header; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.junit.Assert; + +public class TestPcap { + + @BeforeEach + public void init() { + + } + + @Test + public void testReadBytesFull() { + + // Create a header for the test PCAP + Header hdr = new Header( + new byte[]{(byte) 0xa1, (byte) 0xb2, (byte) 0xc3, (byte) 0xd4}, + 2, + 4, + 0, + (long) 0, + (long) 40, + "ETHERNET" + ); + + // Create a sample packet + ArrayList<Packet> packets = new ArrayList<>(); + packets.add(new Packet( + (long) 1713184965, + (long) 1000, + (long) 30, + (long) 30, + new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, + }, + "ETHERNET")); + + // create test PCAP + + Pcap testPcap = new Pcap(hdr, packets); + + // Call the readBytesFull method + byte[] result = testPcap.readBytesFull(); + + // Assert the expected byte array length + Assert.assertEquals(70, result.length); + + // Assert the expected byte array values + ByteBuffer buffer = ByteBuffer.wrap(result);//.order(ByteOrder.LITTLE_ENDIAN); + Assert.assertEquals(0xa1b2c3d4, buffer.getInt()); + ByteBuffer LEBuffer = ByteBuffer.wrap(result).order(ByteOrder.LITTLE_ENDIAN); + LEBuffer.position(4); + Assert.assertEquals(2, LEBuffer.getShort()); + Assert.assertEquals(4, LEBuffer.getShort()); + Assert.assertEquals(0, LEBuffer.getInt()); + Assert.assertEquals(0, LEBuffer.getInt()); + Assert.assertEquals(40, LEBuffer.getInt()); + Assert.assertEquals(1, LEBuffer.getInt()); + + + Assert.assertEquals(1713184965, LEBuffer.getInt()); + Assert.assertEquals(1000, LEBuffer.getInt()); + Assert.assertEquals(30, LEBuffer.getInt()); + Assert.assertEquals(30, LEBuffer.getInt()); + byte[] bodyArray = new byte[30]; + LEBuffer.get(40, bodyArray, 0, 30).array(); + Assert.assertArrayEquals(new byte[]{ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, Review Comment: ```suggestion assertEquals(70, result.length); // Assert the expected byte array values ByteBuffer buffer = ByteBuffer.wrap(result);//.order(ByteOrder.LITTLE_ENDIAN); Assert.assertEquals(0xa1b2c3d4, buffer.getInt()); ByteBuffer LEBuffer = ByteBuffer.wrap(result).order(ByteOrder.LITTLE_ENDIAN); LEBuffer.position(4); assertEquals(2, LEBuffer.getShort()); assertEquals(4, LEBuffer.getShort()); assertEquals(0, LEBuffer.getInt()); assertEquals(0, LEBuffer.getInt()); assertEquals(40, LEBuffer.getInt()); assertEquals(1, LEBuffer.getInt()); assertEquals(1713184965, LEBuffer.getInt()); assertEquals(1000, LEBuffer.getInt()); assertEquals(30, LEBuffer.getInt()); assertEquals(30, LEBuffer.getInt()); byte[] bodyArray = new byte[30]; LEBuffer.get(40, bodyArray, 0, 30).array(); assertArrayEquals(new byte[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
