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.UpnpRequest;
23  import org.fourthline.cling.model.message.discovery.IncomingNotificationRequest;
24  import org.fourthline.cling.model.message.header.HostHeader;
25  import org.fourthline.cling.model.message.header.LocationHeader;
26  import org.fourthline.cling.model.message.header.MaxAgeHeader;
27  import org.fourthline.cling.model.message.header.NTSHeader;
28  import org.fourthline.cling.model.message.header.RootDeviceHeader;
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.model.types.NotificationSubtype;
35  import org.fourthline.cling.test.data.SampleData;
36  import org.fourthline.cling.test.data.SampleDeviceRoot;
37  import org.testng.annotations.Test;
38  
39  import java.net.InetAddress;
40  import java.net.UnknownHostException;
41  
42  import static org.testng.Assert.assertEquals;
43  
44  public class NotifyTest {
45  
46      @Test
47      public void receivedByeBye() throws Exception {
48  
49          UpnpService upnpService = new MockUpnpService();
50  
51          RemoteDevice rd = SampleData.createRemoteDevice();
52          upnpService.getRegistry().addDevice(rd);
53          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
54  
55          IncomingNotificationRequest msg = createRequestMessage();
56          msg.getHeaders().add(UpnpHeader.Type.NT, new RootDeviceHeader());
57          msg.getHeaders().add(UpnpHeader.Type.NTS, new NTSHeader(NotificationSubtype.BYEBYE));
58          msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
59  
60          upnpService.getProtocolFactory().createReceivingAsync(msg).run();
61  
62          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 0);
63      }
64  
65      @Test
66      public void receivedNoUDN() throws Exception {
67  
68          UpnpService upnpService = new MockUpnpService();
69  
70          RemoteDevice rd = SampleData.createRemoteDevice();
71          upnpService.getRegistry().addDevice(rd);
72  
73          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
74  
75          IncomingNotificationRequest msg = createRequestMessage();
76          msg.getHeaders().add(UpnpHeader.Type.NT, new RootDeviceHeader());
77          msg.getHeaders().add(UpnpHeader.Type.NTS, new NTSHeader(NotificationSubtype.BYEBYE));
78          // This is what we are testing, the missing header!
79          // msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
80  
81          upnpService.getProtocolFactory().createReceivingAsync(msg).run();
82  
83          // This should be unchanged from earlier state
84          assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
85      }
86  
87      @Test
88      public void receivedNoLocation() throws Exception {
89  
90          MockUpnpService upnpService = new MockUpnpService();
91  
92          RemoteDevice rd = SampleData.createRemoteDevice();
93  
94          IncomingNotificationRequest msg = createRequestMessage();
95          msg.getHeaders().add(UpnpHeader.Type.NTS, new NTSHeader(NotificationSubtype.ALIVE));
96          msg.getHeaders().add(UpnpHeader.Type.NT, new RootDeviceHeader());
97          msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
98          msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(rd.getIdentity().getMaxAgeSeconds()));
99          // We test the missing header
100         //msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
101 
102         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
103 
104         Thread.sleep(100);
105         assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 0);
106     }
107 
108     @Test
109     public void receivedNoMaxAge() throws Exception {
110 
111         MockUpnpService upnpService = new MockUpnpService();
112 
113         RemoteDevice rd = SampleData.createRemoteDevice();
114 
115         IncomingNotificationRequest msg = createRequestMessage();
116         msg.getHeaders().add(UpnpHeader.Type.NTS, new NTSHeader(NotificationSubtype.ALIVE));
117         msg.getHeaders().add(UpnpHeader.Type.NT, new RootDeviceHeader());
118         msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
119         msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
120         // We test the missing header
121         //msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(rd.getIdentity().getMaxAgeSeconds()));
122 
123         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
124 
125         Thread.sleep(100);
126         assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 0);
127     }
128 
129     @Test
130     public void receivedAlreadyKnownLocalUDN() throws Exception {
131 
132         MockUpnpService upnpService = new MockUpnpService();
133 
134         LocalDevice localDevice = SampleData.createLocalDevice();
135         upnpService.getRegistry().addDevice(localDevice);
136 
137         RemoteDevice rd = SampleData.createRemoteDevice();
138 
139         IncomingNotificationRequest msg = createRequestMessage();
140         msg.getHeaders().add(UpnpHeader.Type.NTS, new NTSHeader(NotificationSubtype.ALIVE));
141         msg.getHeaders().add(UpnpHeader.Type.NT, new RootDeviceHeader());
142         msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(rd.getIdentity().getUdn()));
143         msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
144         msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(rd.getIdentity().getMaxAgeSeconds()));
145 
146         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
147 
148         Thread.sleep(100);
149         assertEquals(upnpService.getRouter().getSentStreamRequestMessages().size(), 0);
150     }
151 
152     @Test
153     public void receiveEmbeddedTriggersUpdate() throws Exception {
154 
155         UpnpService upnpService = new MockUpnpService(false, true);
156 
157         RemoteDevice rd = SampleData.createRemoteDevice(
158                 SampleData.createRemoteDeviceIdentity(2)
159         );
160         RemoteDevice embedded = rd.getEmbeddedDevices()[0];
161 
162         upnpService.getRegistry().addDevice(rd);
163 
164         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
165 
166         IncomingNotificationRequest msg = createRequestMessage();
167         msg.getHeaders().add(UpnpHeader.Type.NTS, new NTSHeader(NotificationSubtype.ALIVE));
168         msg.getHeaders().add(UpnpHeader.Type.NT, new UDNHeader(embedded.getIdentity().getUdn()));
169         msg.getHeaders().add(UpnpHeader.Type.USN, new UDNHeader(embedded.getIdentity().getUdn()));
170         msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(SampleDeviceRoot.getDeviceDescriptorURL()));
171         msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(rd.getIdentity().getMaxAgeSeconds()));
172 
173         Thread.sleep(1000);
174         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
175 
176         Thread.sleep(1000);
177         upnpService.getProtocolFactory().createReceivingAsync(msg).run();
178 
179         Thread.sleep(1000);
180         assertEquals(upnpService.getRegistry().getRemoteDevices().size(), 1);
181 
182         upnpService.shutdown();
183     }
184 
185     protected IncomingNotificationRequest createRequestMessage() throws UnknownHostException {
186         IncomingNotificationRequest msg = new IncomingNotificationRequest(
187                 new IncomingDatagramMessage<>(
188                         new UpnpRequest(UpnpRequest.Method.NOTIFY),
189                         InetAddress.getByName("127.0.0.1"),
190                         Constants.UPNP_MULTICAST_PORT,
191                         InetAddress.getByName("127.0.0.1")
192                 )
193         );
194 
195         msg.getHeaders().add(UpnpHeader.Type.HOST, new HostHeader());
196         return msg;
197 
198     }
199 
200 }