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.serialization.EncryptingSerializer;
34  
35  import junit.framework.TestCase;
36  
37  /**
38   * Tests for the sender with EncryptingSerializer.
39   */
40  public class UDPDiscoverySenderEncryptedUnitTest
41  	extends TestCase
42  {
43      /** multicast address to send/receive on */
44      private static final String ADDRESS = "228.4.5.9";
45  
46      /** multicast address to send/receive on */
47      private static final int PORT = 5556;
48  
49      /** imaginary host address for sending */
50      private static final String SENDING_HOST = "imaginary host address";
51  
52      /** imaginary port for sending */
53      private static final int SENDING_PORT = 1;
54  
55      /** receiver instance for tests */
56      private UDPDiscoveryReceiver receiver;
57  
58      /** sender instance for tests */
59      private UDPDiscoverySender sender;
60      
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         // SETUP
108         final ArrayList<String> cacheNames = new ArrayList<>();
109 
110         // DO WORK
111         sender.passiveBroadcast( SENDING_HOST, SENDING_PORT, cacheNames, 1L );
112 
113         // VERIFY
114         // grab the sent message
115         final UDPDiscoveryMessage msg = getMessage();
116         assertNotNull("message not received", msg);
117         assertEquals( "wrong port", SENDING_PORT, msg.getPort() );
118         assertEquals( "wrong message type", BroadcastType.PASSIVE, msg.getMessageType() );
119     }
120 
121     /**
122      * Test sending a remove broadcast.
123      * <p>
124      * @throws Exception on error
125      */
126     public void testRemoveBroadcast()
127         throws Exception
128     {
129         // SETUP
130         final ArrayList<String> cacheNames = new ArrayList<>();
131 
132         // DO WORK
133         sender.removeBroadcast( SENDING_HOST, SENDING_PORT, cacheNames, 1L );
134 
135         // VERIFY
136         // grab the sent message
137         final UDPDiscoveryMessage msg = getMessage();
138         assertNotNull("message not received", msg);
139         assertEquals( "wrong port", SENDING_PORT, msg.getPort() );
140         assertEquals( "wrong message type", BroadcastType.REMOVE, msg.getMessageType() );
141     }
142 
143     /**
144      * Test sending a request broadcast.
145      * <p>
146      * @throws Exception on error
147      */
148     public void testRequestBroadcast()
149         throws Exception
150     {
151         // DO WORK
152         sender.requestBroadcast();
153 
154         // VERIFY
155         // grab the sent message
156         final UDPDiscoveryMessage msg = getMessage();
157         assertNotNull("message not received", msg);
158         assertEquals( "wrong message type", BroadcastType.REQUEST, msg.getMessageType() );
159 
160         
161     }
162     
163     /**
164      * Wait for multicast message for 3 seconds
165      * 
166      * @return the object message or null if nothing received within 3 seconds
167      */
168     private UDPDiscoveryMessage getMessage() {
169     	ExecutorService executor = Executors.newCachedThreadPool();
170         Callable<Object> task = new Callable<Object>() {
171            public Object call() throws IOException {
172               return receiver.waitForMessage();
173            }
174         };
175         Future<Object> future = executor.submit(task);
176         try {
177         	Object obj = future.get(3, TimeUnit.SECONDS);
178  
179         	assertTrue( "unexpected crap received", obj instanceof UDPDiscoveryMessage );
180 
181             return (UDPDiscoveryMessage) obj;
182         } catch (InterruptedException | ExecutionException | TimeoutException ex) {
183         	return null;
184         }
185     }
186 }