View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.test.ssdp;
17  
18  import org.fourthline.cling.UpnpService;
19  import org.fourthline.cling.mock.MockUpnpService;
20  import org.fourthline.cling.model.Constants;
21  import org.fourthline.cling.model.message.IncomingDatagramMessage;
22  import org.fourthline.cling.model.message.UpnpResponse;
23  import org.fourthline.cling.model.message.discovery.IncomingSearchResponse;
24  import org.fourthline.cling.model.message.header.EXTHeader;
25  import org.fourthline.cling.model.message.header.HostHeader;
26  import org.fourthline.cling.model.message.header.LocationHeader;
27  import org.fourthline.cling.model.message.header.MaxAgeHeader;
28  import org.fourthline.cling.model.message.header.STAllHeader;
29  import org.fourthline.cling.model.message.header.UDNHeader;
30  import org.fourthline.cling.model.message.header.USNRootDeviceHeader;
31  import org.fourthline.cling.model.message.header.UpnpHeader;
32  import org.fourthline.cling.model.meta.LocalDevice;
33  import org.fourthline.cling.model.meta.RemoteDevice;
34  import org.fourthline.cling.test.data.SampleData;
35  import org.fourthline.cling.test.data.SampleDeviceRoot;
36  import org.testng.annotations.Test;
37  
38  import java.net.InetAddress;
39  import java.net.UnknownHostException;
40  
41  import static org.testng.Assert.assertEquals;
42  
43  /**
44   * @author Christian Bauer
45   */
46  public class SearchResponseTest {
47  
48      @Test
49      public void receivedValidResponse() throws Exception {
50  
51          MockUpnpService upnpService = new MockUpnpService();
52  
53          RemoteDevice rd = SampleData.createRemoteDevice();
54  
55          IncomingSearchResponse msg = createResponseMessage(new STAllHeader());
56          msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
57          msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
58          msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(rd.getIdentity().getMaxAgeSeconds()));
59  
60          upnpService.getProtocolFactory().createReceivingAsync(msg).run();
61          Thread.sleep(100);
62          assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 1);
63      }
64  
65      @Test
66      public void receivedInvalidSearchResponses() throws Exception {
67  
68          MockUpnpService upnpService = new MockUpnpService();
69  
70          RemoteDevice rd = SampleData.createRemoteDevice();
71  
72          // Missing USN header
73          IncomingSearchResponse msg = createResponseMessage(new STAllHeader());
74          upnpService.getProtocolFactory().createReceivingAsync(msg).run();
75          Thread.sleep(100);
76          assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 0);
77  
78          // Missing location header
79          msg = createResponseMessage(new STAllHeader());
80          msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
81          upnpService.getProtocolFactory().createReceivingAsync(msg).run();
82          Thread.sleep(100);
83          assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 0);
84  
85          // Missing max age header
86          msg = createResponseMessage(new STAllHeader());
87          msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
88          msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
89          upnpService.getProtocolFactory().createReceivingAsync(msg).run();
90          Thread.sleep(100);
91          assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 0);
92  
93      }
94  
95      @Test
96      public void receivedAlreadyKnownLocalUDN() throws Exception {
97  
98          MockUpnpService upnpService = new MockUpnpService();
99  
100         LocalDevice localDevice = SampleData.createLocalDevice();
101         upnpService.getRegistry().addDevice(localDevice);
102 
103         RemoteDevice rd = SampleData.createRemoteDevice();
104 
105         IncomingSearchResponse msg = createResponseMessage(new STAllHeader());
106         msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
107         msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
108         msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(rd.getIdentity().getMaxAgeSeconds()));
109 
110         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
111         Thread.sleep(100);
112         assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 0);
113     }
114 
115     @Test
116     public void receiveEmbeddedTriggersUpdate() throws Exception {
117 
118         UpnpService upnpService = new MockUpnpService(false, true);
119 
120         RemoteDevice rd = SampleData.createRemoteDevice();
121         RemoteDevice embedded = rd.getEmbeddedDevices()[0];
122 
123         upnpService.getRegistry().addDevice(rd);
124 
125         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
126 
127         IncomingSearchResponse msg = createResponseMessage(new STAllHeader());
128         msg.getHeaders().add(UpnpHeader.Type.USN, new UDNHeader(embedded.getIdentity().getUdn()));
129         msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
130         msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(rd.getIdentity().getMaxAgeSeconds()));
131 
132         Thread.sleep(1000);
133         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
134 
135         Thread.sleep(1000);
136         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
137 
138         Thread.sleep(1000);
139         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
140 
141         upnpService.shutdown();
142     }
143 
144     protected IncomingSearchResponse createResponseMessage(UpnpHeader stHeader) throws UnknownHostException {
145         IncomingSearchResponse msg = new IncomingSearchResponse(
146                 new IncomingDatagramMessage<>(
147                         new UpnpResponse(UpnpResponse.Status.OK),
148                         InetAddress.getByName("127.0.0.1"),
149                         Constants.UPNP_MULTICAST_PORT,
150                         InetAddress.getByName("127.0.0.1")
151                 )
152         );
153 
154         msg.getHeaders().add(UpnpHeader.Type.ST, stHeader);
155         msg.getHeaders().add(UpnpHeader.Type.EXT, new EXTHeader());
156         msg.getHeaders().add(UpnpHeader.Type.HOST, new HostHeader());
157         return msg;
158 
159     }
160     
161 }