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.MatcherAssert.assertThat;
11 import static org.hamcrest.core.Is.is;
12 import static org.junit.Assert.assertEquals;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.List;
18 import org.junit.Test;
20 public class BGPBinaryFileParserTest {
22 private static final byte FF = (byte) 255;
24 private static List<byte[]> extractFromFile(final String fileName) throws IOException {
25 final InputStream is = BGPBinaryFileParserTest.class.getResourceAsStream(fileName);
27 throw new IOException("Failed to get resource " + fileName);
30 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
31 final byte[] data = new byte[1000];
33 while ((nread = is.read(data, 0, data.length)) != -1) {
34 bis.write(data, 0, nread);
37 return BinaryBGPDumpFileParser.parseMessages(bis.toByteArray());
41 public void testCorrectExtraction() throws IOException {
42 final List<byte[]> parsedMessages = extractFromFile("/BgpMessages.bin");
44 assertThat(parsedMessages.size(), is(43));
47 assertThat(parsedMessages.get(0).length, is(19));
48 checkMarker(parsedMessages);
49 assertThat(parsedMessages.get(0)[16], is((byte) 0));
50 assertThat(parsedMessages.get(0)[17], is((byte) 19));
51 assertThat(parsedMessages.get(0)[18], is((byte) 4));
54 assertThat(parsedMessages.get(38).length, is(91));
55 checkMarker(parsedMessages);
56 assertThat(parsedMessages.get(38)[16], is((byte) 0));
57 assertThat(parsedMessages.get(38)[17], is((byte) 91));
58 assertThat(parsedMessages.get(38)[18], is((byte) 2));
59 assertThat(parsedMessages.get(38)[90], is((byte) 236));
63 private void checkMarker(final List<byte[]> parsedMessages) {
64 for (int i = 0; i < 16; i++) {
65 assertThat(parsedMessages.get(0)[i], is(FF));
70 * In BgpMessages_wrong_header file, first FF sequence is corrupted.
73 public void testCorruptedHeader() throws IOException {
74 final List<byte[]> parsedMessages = extractFromFile("/BgpMessages_wrong_header.bin");
75 assertEquals(42, parsedMessages.size());