be927a64e69c60d967d44146a940712fd1c1b4e3
[honeycomb.git] / infra / bgp-distribution-test / src / test / java / io / fd / honeycomb / infra / bgp / distro / BgpDistributionTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.infra.bgp.distro;
18
19 import com.google.common.io.ByteStreams;
20 import com.mashape.unirest.http.Unirest;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.InetAddress;
24 import java.net.Socket;
25 import javax.net.ssl.SSLContext;
26 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
27 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.impl.client.HttpClients;
30 import org.apache.http.ssl.SSLContexts;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class BgpDistributionTest {
38
39     private static final Logger LOG = LoggerFactory.getLogger(BgpDistributionTest.class);
40     private static final String CERT_PASSWORD = "testing";
41     private static final int HELLO_WAIT = 2500;
42
43     private static final int BGP_MSG_TYPE_OFFSET = 18; // 16 (MARKER) + 2 (LENGTH);
44     private static final byte BGP_OPEN_MSG_TYPE = 1;
45     private static final int BGP_PORT = 1790;
46
47     @Before
48     public void setUp() throws Exception {
49         SSLContext sslcontext = SSLContexts.custom()
50             .loadTrustMaterial(getClass().getResource("/honeycomb-keystore"),
51                 CERT_PASSWORD.toCharArray(), new TrustSelfSignedStrategy())
52             .build();
53
54         SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
55         CloseableHttpClient httpclient = HttpClients.custom()
56             .setSSLSocketFactory(sslsf)
57             .build();
58         Unirest.setHttpClient(httpclient);
59     }
60
61     @Test(timeout = 120000)
62     public void test() throws Exception {
63         io.fd.honeycomb.infra.bgp.distro.Main.init();
64         LOG.info("Testing Honeycomb BGP distribution");
65         assertBgp();
66     }
67
68     private byte[] readMessage(final InputStream inputStream) throws IOException {
69         final int available = inputStream.available();
70         final byte[] msg = new byte[available];
71         ByteStreams.read(inputStream, msg, 0, available);
72         return msg;
73     }
74
75     private void assertBgp() throws Exception {
76         // Wait until BGP server is started
77         Thread.sleep(HELLO_WAIT);
78         final InetAddress bgpHost = InetAddress.getByName("127.0.0.1");
79         final InetAddress bgpPeerAddress = InetAddress.getByName("127.0.0.2");
80         try (final Socket localhost = new Socket(bgpHost, BGP_PORT, bgpPeerAddress, 0);
81              final InputStream inputStream = localhost.getInputStream()) {
82             // Wait until bgp message is sent
83             Thread.sleep(HELLO_WAIT);
84
85             final byte[] msg = readMessage(inputStream);
86             LOG.info("Received BGP message: {}", msg);
87
88             Assert.assertEquals(BGP_OPEN_MSG_TYPE, msg[BGP_MSG_TYPE_OFFSET]);
89         }
90     }
91 }