2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.util;
10 import static org.hamcrest.core.Is.is;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.List;
20 import org.junit.Test;
22 public class BGPBinaryFileParserTest {
24 private final byte ff = (byte) 255;
27 public void testCorrectExtraction() throws IOException {
28 final List<byte[]> parsedMessages = extractFromFile("/BgpMessages.bin");
30 assertThat(parsedMessages.size(), is(43));
33 assertThat(parsedMessages.get(0).length, is(19));
34 checkMarker(parsedMessages);
35 assertThat(parsedMessages.get(0)[16], is((byte) 0));
36 assertThat(parsedMessages.get(0)[17], is((byte) 19));
37 assertThat(parsedMessages.get(0)[18], is((byte) 4));
40 assertThat(parsedMessages.get(38).length, is(91));
41 checkMarker(parsedMessages);
42 assertThat(parsedMessages.get(38)[16], is((byte) 0));
43 assertThat(parsedMessages.get(38)[17], is((byte) 91));
44 assertThat(parsedMessages.get(38)[18], is((byte) 2));
45 assertThat(parsedMessages.get(38)[90], is((byte) 236));
49 private List<byte[]> extractFromFile(final String fileName) throws IOException {
50 final InputStream is = BGPBinaryFileParserTest.class.getResourceAsStream(fileName);
51 assertNotNull("File not found - " + fileName);
53 throw new IOException("Failed to get resource " + fileName);
56 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
57 final byte[] data = new byte[1000];
59 while ((nRead = is.read(data, 0, data.length)) != -1) {
60 bis.write(data, 0, nRead);
63 return BinaryBGPDumpFileParser.parseMessages(bis.toByteArray());
66 private void checkMarker(final List<byte[]> parsedMessages) {
67 for (int i = 0; i < 16; i++) {
68 assertThat(parsedMessages.get(0)[i], is(this.ff));
73 * In BgpMessages_wrong_header file, first FF sequence is corrupted
78 public void testCorruptedHeader() throws IOException {
79 final List<byte[]> parsedMessages = extractFromFile("/BgpMessages_wrong_header.bin");
80 assertEquals(42, parsedMessages.size());