2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.infra.bgp.distro;
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;
37 public class BgpDistributionTest {
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;
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;
48 public void setUp() throws Exception {
49 SSLContext sslcontext = SSLContexts.custom()
50 .loadTrustMaterial(getClass().getResource("/honeycomb-keystore"),
51 CERT_PASSWORD.toCharArray(), new TrustSelfSignedStrategy())
54 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
55 CloseableHttpClient httpclient = HttpClients.custom()
56 .setSSLSocketFactory(sslsf)
58 Unirest.setHttpClient(httpclient);
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");
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);
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);
85 final byte[] msg = readMessage(inputStream);
86 LOG.info("Received BGP message: {}", msg);
88 Assert.assertEquals(BGP_OPEN_MSG_TYPE, msg[BGP_MSG_TYPE_OFFSET]);