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 19 public class NotifyAliveConcurrentTest { 20 21 /* TODO: Fixme 22 23 protected final UpnpServiceImpl upnpService = new UpnpServiceImpl(new TestModel.NetworkDisabledRouterConfiguration()) { 24 25 @Override 26 public StreamResponseMessage send(StreamRequestMessage msg) { 27 28 // GET request for the device descriptor must arrive here 29 UpnpRequest request = msg.getOperation(); 30 Assert.assertEquals(request.getMethod(), UpnpRequest.Method.GET); 31 if (request.getURI().toString().equals(TestModel.getDeviceDescriptorURL().toString())) { 32 33 // Now, to simulate concurrency, we send another notify before the descriptor retrieval completes 34 NotifyAliveConcurrentTest.this.upnpService.received(createMessage()); 35 36 // Send the mock response back 37 return new StreamResponseMessage(TestModel.getSampleDeviceDescriptorUDA10(), new ContentTypeHeader()); 38 39 } else if (request.getURI().toString().equals(TestModel.getServiceOneDescriptorURL().toString())) { 40 41 // Send the mock response back 42 return new StreamResponseMessage(TestModel.getSampleServiceOneDescriptorUDA10(), new ContentTypeHeader()); 43 44 } else if (request.getURI().toString().equals(TestModel.getServiceTwoDescriptorURL().toString())) { 45 46 // Send the mock response back 47 return new StreamResponseMessage(TestModel.getSampleServiceTwoDescriptorUDA10(), new ContentTypeHeader()); 48 49 } else if (request.getURI().toString().equals(TestModel.getServiceThreeDescriptorURL().toString())) { 50 51 // Send the mock response back 52 return new StreamResponseMessage(TestModel.getSampleServiceThreeDescriptorUDA10(), new ContentTypeHeader()); 53 54 } else { 55 throw new RuntimeException("Unknown (non-mocked) stream request: " + msg); 56 } 57 } 58 }; 59 60 @Test 61 public void notifyAliveConcurrent() throws Exception { 62 63 TestListener listener = new TestListener(); 64 65 MockUpnpService upnpService = new Upnp 66 upnpService.start(false); 67 68 upnpService.addListener(listener); 69 70 upnpService.received(createMessage()); 71 72 Thread.sleep(1000); // TODO: This is pretty random but I don't see how we can simulate concurrency otherwise 73 assert listener.valid; 74 } 75 76 protected IncomingDatagramMessage createMessage() { 77 try { 78 79 RemoteDevice device = TestModel.getRemoteDevice(); 80 81 IncomingNotificationRequest msg = 82 new IncomingNotificationRequest( 83 new IncomingDatagramMessage<UpnpRequest>( 84 new UpnpRequest(UpnpRequest.Method.NOTIFY), 85 InetAddress.getByName("127.0.0.1"), 86 Constants.UPNP_MULTICAST_PORT, 87 InetAddress.getByName("127.0.0.1") 88 ) 89 ); 90 91 msg.getHeaders().add(UpnpHeader.Type.HOST, new HostHeader(Constants.IPV4_UPNP_MULTICAST_GROUP, Constants.UPNP_MULTICAST_PORT)); 92 msg.getHeaders().add(UpnpHeader.Type.MAX_AGE, new MaxAgeHeader(2000)); 93 msg.getHeaders().add(UpnpHeader.Type.LOCATION, new LocationHeader(device.getDescriptorURL())); 94 msg.getHeaders().add(UpnpHeader.Type.NT, new RootDeviceHeader()); 95 msg.getHeaders().add(UpnpHeader.Type.NTS, new NTSHeader(NotificationSubtype.ALIVE)); 96 msg.getHeaders().add(UpnpHeader.Type.SERVER, new ServerHeader()); 97 msg.getHeaders().add(UpnpHeader.Type.USN, new USNRootDeviceHeader(device.getUdn())); 98 99 return msg; 100 } catch (Exception ex) { 101 throw new RuntimeException(ex); 102 } 103 104 } 105 106 protected class TestListener implements RegistryListener { 107 108 public boolean valid = false; 109 110 public void remoteDeviceAdded(Registry registry, RemoteDevice device) { 111 assert !valid; 112 assert !device.isLocal(); 113 assert device.isDescribed(); 114 TestModel.assertTestDataMatch(device); 115 TestModel.assertTestDataMatchServiceOne(device.getDeviceServices().get(0).getService()); 116 TestModel.assertTestDataMatchServiceTwo(device.getEmbeddedDevices().get(0).getDeviceServices().get(0).getService()); 117 TestModel.assertTestDataMatchServiceThree(device.getEmbeddedDevices().get(0).getEmbeddedDevices().get(0).getDeviceServices().get(0).getService()); 118 Assert.assertEquals(device.getMaxAge(), new Integer(2000)); 119 valid = true; 120 } 121 122 public void remoteDeviceUpdated(Registry registry, RemoteDevice device) { 123 124 } 125 126 public void remoteDeviceRemoved(Registry registry, RemoteDevice device) { 127 128 } 129 130 public void localDeviceAdded(Registry registry, LocalDevice device) { 131 132 } 133 134 public void localDeviceRemoved(Registry registry, LocalDevice device) { 135 136 } 137 } 138 */ 139 140 }