Final cleanup.
[bgpcep.git] / bgp / util / src / test / java / org / opendaylight / protocol / bgp / util / BGPBinaryFileParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.util;
9
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;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.List;
19
20 import org.junit.Test;
21
22 public class BGPBinaryFileParserTest {
23
24         private final byte ff = (byte) 255;
25
26         @Test
27         public void testCorrectExtraction() throws IOException {
28                 final List<byte[]> parsedMessages = extractFromFile("/BgpMessages.bin");
29
30                 assertThat(parsedMessages.size(), is(43));
31
32                 // 1st message
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));
38
39                 // 39th message
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));
46
47         }
48
49         private List<byte[]> extractFromFile(final String fileName) throws IOException {
50                 final InputStream is = BGPBinaryFileParserTest.class.getResourceAsStream(fileName);
51                 assertNotNull("File not found - " + fileName);
52                 if (is == null) {
53                         throw new IOException("Failed to get resource " + fileName);
54                 }
55
56                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
57                 final byte[] data = new byte[1000];
58                 int nRead = 0;
59                 while ((nRead = is.read(data, 0, data.length)) != -1) {
60                         bis.write(data, 0, nRead);
61                 }
62                 bis.flush();
63                 return BinaryBGPDumpFileParser.parseMessages(bis.toByteArray());
64         }
65
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));
69                 }
70         }
71
72         /**
73          * In BgpMessages_wrong_header file, first FF sequence is corrupted
74          * 
75          * @throws IOException
76          */
77         @Test
78         public void testCorruptedHeader() throws IOException {
79                 final List<byte[]> parsedMessages = extractFromFile("/BgpMessages_wrong_header.bin");
80                 assertEquals(42, parsedMessages.size());
81         }
82
83 }