View Javadoc
1   package org.apache.commons.jcs3.utils.discovery;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.concurrent.Callable;
25  import java.util.concurrent.ExecutionException;
26  import java.util.concurrent.ExecutorService;
27  import java.util.concurrent.Executors;
28  import java.util.concurrent.Future;
29  import java.util.concurrent.TimeUnit;
30  import java.util.concurrent.TimeoutException;
31  
32  import org.apache.commons.jcs3.utils.discovery.UDPDiscoveryMessage.BroadcastType;
33  import org.apache.commons.jcs3.utils.net.HostNameUtil;
34  import org.apache.commons.jcs3.utils.serialization.EncryptingSerializer;
35  
36  import junit.framework.TestCase;
37  
38  /**
39   * Tests for the sender with EncryptingSerializer.
40   */
41  public class UDPDiscoverySenderEncryptedUnitTest
42  	extends TestCase
43  {
44      /** multicast address to send/receive on */
45      private static final String ADDRESS = "228.4.5.9";
46  
47      /** multicast address to send/receive on */
48      private static final int PORT = 5556;
49  
50      /** imaginary host address for sending */
51      private static final String SENDING_HOST = "imaginary host address";
52  
53      /** imaginary port for sending */
54      private static final int SENDING_PORT = 1;
55  
56      /** receiver instance for tests */
57      private UDPDiscoveryReceiver receiver;
58  
59      /** sender instance for tests */
60      private UDPDiscoverySender sender;
61  
62      /**
63       * Set up the receiver. Maybe better to just code sockets here? Set up the sender for sending
64       * the message.
65       * <p>
66       * @throws Exception on error
67       */
68      @Override
69      protected void setUp()
70          throws Exception
71      {
72          super.setUp();
73  
74          EncryptingSerializer serializer = new EncryptingSerializer();
75          serializer.setPreSharedKey("my_key");
76  
77          receiver = new UDPDiscoveryReceiver( null, null, ADDRESS, PORT );
78          receiver.setSerializer(serializer);
79          final Thread t = new Thread( receiver );
80          t.start();
81  
82          sender = new UDPDiscoverySender(null, ADDRESS, PORT, 1, serializer);
83      }
84  
85      /**
86       * Kill off the sender and receiver.
87       * <p>
88       * @throws Exception on error
89       */
90      @Override
91      protected void tearDown()
92          throws Exception
93      {
94          receiver.shutdown();
95          sender.close();
96          super.tearDown();
97      }
98  
99      /**
100      * Test sending a live messages.
101      * <p>
102      * @throws Exception on error
103      */
104     public void testPassiveBroadcast()
105         throws Exception
106     {
107         if (HostNameUtil.getMulticastNetworkInterface() == null)
108         {
109             System.out.println("This machine does not support multicast");
110             return;
111         }
112 
113         // SETUP
114         final ArrayList<String> cacheNames = new ArrayList<>();
115 
116         // DO WORK
117         sender.passiveBroadcast( SENDING_HOST, SENDING_PORT, cacheNames, 1L );
118 
119         // VERIFY
120         // grab the sent message
121         final UDPDiscoveryMessage msg = getMessage();
122         assertNotNull("message not received", msg);
123         assertEquals( "wrong port", SENDING_PORT, msg.getPort() );
124         assertEquals( "wrong message type", BroadcastType.PASSIVE, msg.getMessageType() );
125     }
126 
127     /**
128      * Test sending a remove broadcast.
129      * <p>
130      * @throws Exception on error
131      */
132     public void testRemoveBroadcast()
133         throws Exception
134     {
135         if (HostNameUtil.getMulticastNetworkInterface() == null)
136         {
137             System.out.println("This machine does not support multicast");
138             return;
139         }
140 
141         // SETUP
142         final ArrayList<String> cacheNames = new ArrayList<>();
143 
144         // DO WORK
145         sender.removeBroadcast( SENDING_HOST, SENDING_PORT, cacheNames, 1L );
146 
147         // VERIFY
148         // grab the sent message
149         final UDPDiscoveryMessage msg = getMessage();
150         assertNotNull("message not received", msg);
151         assertEquals( "wrong port", SENDING_PORT, msg.getPort() );
152         assertEquals( "wrong message type", BroadcastType.REMOVE, msg.getMessageType() );
153     }
154 
155     /**
156      * Test sending a request broadcast.
157      * <p>
158      * @throws Exception on error
159      */
160     public void testRequestBroadcast()
161         throws Exception
162     {
163         if (HostNameUtil.getMulticastNetworkInterface() == null)
164         {
165             System.out.println("This machine does not support multicast");
166             return;
167         }
168 
169         // DO WORK
170         sender.requestBroadcast();
171 
172         // VERIFY
173         // grab the sent message
174         final UDPDiscoveryMessage msg = getMessage();
175         assertNotNull("message not received", msg);
176         assertEquals( "wrong message type", BroadcastType.REQUEST, msg.getMessageType() );
177 
178 
179     }
180 
181     /**
182      * Wait for multicast message for 3 seconds
183      *
184      * @return the object message or null if nothing received within 3 seconds
185      */
186     private UDPDiscoveryMessage getMessage() {
187     	ExecutorService executor = Executors.newCachedThreadPool();
188         Callable<Object> task = new Callable<Object>() {
189            @Override
190         public Object call() throws IOException {
191               return receiver.waitForMessage();
192            }
193         };
194         Future<Object> future = executor.submit(task);
195         try {
196         	Object obj = future.get(3, TimeUnit.SECONDS);
197 
198         	assertTrue( "unexpected crap received", obj instanceof UDPDiscoveryMessage );
199 
200             return (UDPDiscoveryMessage) obj;
201         } catch (InterruptedException | ExecutionException | TimeoutException ex) {
202         	return null;
203         }
204     }
205 }