Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * jndn-management |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 16 | import com.intel.jndn.management.enums.LocalControlHeader; |
| 17 | import com.intel.jndn.management.enums.Strategies; |
| 18 | import com.intel.jndn.management.types.RibEntry; |
| 19 | import com.intel.jndn.management.types.StrategyChoice; |
| 20 | import com.intel.jndn.mock.MockKeyChain; |
| 21 | import net.named_data.jndn.Face; |
| 22 | import net.named_data.jndn.KeyLocator; |
| 23 | import net.named_data.jndn.Name; |
| 24 | import net.named_data.jndn.encoding.EncodingException; |
| 25 | import net.named_data.jndn.security.KeyChain; |
| 26 | import net.named_data.jndn.security.SecurityException; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame^] | 30 | import java.io.IOException; |
| 31 | import java.util.List; |
| 32 | import java.util.Random; |
| 33 | import java.util.logging.Logger; |
| 34 | |
| 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.junit.Assert.assertFalse; |
| 37 | import static org.junit.Assert.assertNotNull; |
| 38 | import static org.junit.Assert.assertTrue; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame^] | 41 | * Testing Nfdc with real NFD instance (NFD must be run locally while executing the test). |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 42 | * |
| 43 | * @author Andrew Brown <andrew.brown@intel.com> |
| 44 | */ |
| 45 | public class NfdcIT { |
| 46 | private static final Logger LOG = Logger.getLogger(NfdcIT.class.getName()); |
| 47 | private Face face; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws SecurityException { |
| 51 | face = new Face("localhost"); |
| 52 | KeyChain keyChain = MockKeyChain.configure(new Name("/tmp/identity")); |
| 53 | face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName()); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testConnectivity() throws IOException, ManagementException { |
| 58 | KeyLocator keyLocator = Nfdc.getKeyLocator(face); |
| 59 | assertNotNull(keyLocator); |
| 60 | LOG.info("Connected to NFD with key locator: " + keyLocator.getKeyName().toUri()); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testStatusDatasets() throws Exception { |
| 65 | assertTrue(Nfdc.getForwarderStatus(face).getStartTimestamp() > 0); |
| 66 | assertFalse(Nfdc.getFaceList(face).isEmpty()); |
| 67 | assertFalse(Nfdc.getFibList(face).isEmpty()); |
| 68 | assertFalse(Nfdc.getRouteList(face).isEmpty()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testRoutes() throws EncodingException, IOException, ManagementException, InterruptedException { |
| 73 | Nfdc.register(face, new Name("/my/route/to/app/face"), 333); |
| 74 | int faceId = Nfdc.createFace(face, "udp4://127.0.0.1:56363"); |
| 75 | Nfdc.register(face, "udp4://127.0.0.1:56363", new Name("/my/test/route"), 999); |
| 76 | Nfdc.register(face, faceId, new Name("/"), 555); |
| 77 | |
| 78 | // check that route is created |
| 79 | Thread.sleep(1000); // NFD registers the route asynchronously |
| 80 | |
| 81 | boolean found = false; |
| 82 | for (RibEntry route : Nfdc.getRouteList(face)) { |
| 83 | LOG.info("Found route: " + route.getName().toUri()); |
| 84 | if (route.getName().equals(new Name("/my/test/route"))) { |
| 85 | found = true; |
| 86 | } |
| 87 | } |
| 88 | assertTrue(found); |
| 89 | |
| 90 | Nfdc.unregister(face, new Name("/my/route/to/app/face")); |
| 91 | |
| 92 | // remove the route |
| 93 | Nfdc.unregister(face, new Name("/my/test/route"), "udp4://127.0.0.1:56363"); |
| 94 | |
| 95 | // remove face |
| 96 | Nfdc.destroyFace(face, faceId); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testStrategies() throws Exception { |
| 101 | Name prefix = new Name("/test/strategy").append("random:" + new Random().nextInt()); |
| 102 | |
| 103 | List<StrategyChoice> choices = Nfdc.getStrategyList(face); |
| 104 | int oldSize = choices.size(); |
| 105 | |
| 106 | Nfdc.setStrategy(face, prefix, Strategies.CLIENT_CONTROL); |
| 107 | Thread.sleep(1000); // strategy takes a while to register |
| 108 | |
| 109 | choices = Nfdc.getStrategyList(face); |
| 110 | assertEquals(oldSize + 1, choices.size()); |
| 111 | |
| 112 | Nfdc.unsetStrategy(face, prefix); |
| 113 | } |
| 114 | |
| 115 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame^] | 116 | * LocalControlHeader works only with NFD < 0.3.4, broken otherwise. |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 117 | */ |
| 118 | @Test(expected = ManagementException.class) |
| 119 | public void testLocalControlHeader() throws Exception { |
| 120 | Nfdc.enableLocalControlHeader(face, LocalControlHeader.INCOMING_FACE_ID); |
| 121 | Thread.sleep(1000); // strategy takes a while to register |
| 122 | |
| 123 | // TODO: add asserts |
| 124 | |
| 125 | Nfdc.disableLocalControlHeader(face, LocalControlHeader.INCOMING_FACE_ID); |
| 126 | } |
| 127 | } |