|
| 1 | +/* |
| 2 | + * Copyright (C) 2011-2026 4th Line GmbH, Switzerland and others |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License Version 1 or later |
| 6 | + * ("CDDL") (collectively, the "License"). You may not use this file |
| 7 | + * except in compliance with the License. See LICENSE.txt for more |
| 8 | + * information. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: CDDL-1.0 |
| 15 | + */ |
| 16 | +package org.jupnp.support.igd.callback; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 20 | + |
| 21 | +import java.net.URI; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.jupnp.model.action.ActionInvocation; |
| 25 | +import org.jupnp.model.message.UpnpResponse; |
| 26 | +import org.jupnp.model.meta.Action; |
| 27 | +import org.jupnp.model.meta.ActionArgument; |
| 28 | +import org.jupnp.model.meta.RemoteService; |
| 29 | +import org.jupnp.model.meta.StateVariable; |
| 30 | +import org.jupnp.model.meta.StateVariableTypeDetails; |
| 31 | +import org.jupnp.model.types.Datatype; |
| 32 | +import org.jupnp.model.types.UDAServiceId; |
| 33 | +import org.jupnp.model.types.UDAServiceType; |
| 34 | +import org.jupnp.model.types.UnsignedIntegerFourBytes; |
| 35 | +import org.jupnp.support.model.PortMapping; |
| 36 | + |
| 37 | +class PortMappingCallbackTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + void addUsesPortDatatypeAdvertisedByTheService() throws Exception { |
| 41 | + final PortMappingAdd callback = new PortMappingAdd(portMappingService(Datatype.Builtin.UI4.getDatatype(), true), |
| 42 | + mapping()) { |
| 43 | + @Override |
| 44 | + public void success(final ActionInvocation invocation) { |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void failure(final ActionInvocation invocation, final UpnpResponse operation, |
| 49 | + final String defaultMsg) { |
| 50 | + } |
| 51 | + }; |
| 52 | + final ActionInvocation<?> invocation = callback.getActionInvocation(); |
| 53 | + |
| 54 | + assertInstanceOf(UnsignedIntegerFourBytes.class, invocation.getInput("NewExternalPort").getValue()); |
| 55 | + assertInstanceOf(UnsignedIntegerFourBytes.class, invocation.getInput("NewInternalPort").getValue()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void deleteUsesPortDatatypeAdvertisedByTheService() throws Exception { |
| 60 | + final PortMappingDelete callback = new PortMappingDelete( |
| 61 | + portMappingService(Datatype.Builtin.UI4.getDatatype(), false), mapping()) { |
| 62 | + @Override |
| 63 | + public void success(final ActionInvocation invocation) { |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void failure(final ActionInvocation invocation, final UpnpResponse operation, |
| 68 | + final String defaultMsg) { |
| 69 | + } |
| 70 | + }; |
| 71 | + final ActionInvocation<?> invocation = callback.getActionInvocation(); |
| 72 | + |
| 73 | + assertInstanceOf(UnsignedIntegerFourBytes.class, invocation.getInput("NewExternalPort").getValue()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void addAllowsUnsetPorts() throws Exception { |
| 78 | + final PortMappingAdd callback = new PortMappingAdd(portMappingService(Datatype.Builtin.UI4.getDatatype(), true), |
| 79 | + new PortMapping()) { |
| 80 | + @Override |
| 81 | + public void success(final ActionInvocation invocation) { |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void failure(final ActionInvocation invocation, final UpnpResponse operation, |
| 86 | + final String defaultMsg) { |
| 87 | + } |
| 88 | + }; |
| 89 | + final ActionInvocation<?> invocation = callback.getActionInvocation(); |
| 90 | + |
| 91 | + assertNull(invocation.getInput("NewExternalPort").getValue()); |
| 92 | + assertNull(invocation.getInput("NewInternalPort").getValue()); |
| 93 | + } |
| 94 | + |
| 95 | + private PortMapping mapping() { |
| 96 | + return new PortMapping(36743, "192.168.1.99", PortMapping.Protocol.TCP, "jUPnP test"); |
| 97 | + } |
| 98 | + |
| 99 | + private RemoteService portMappingService(final Datatype<?> portDatatype, final boolean add) throws Exception { |
| 100 | + final StateVariable[] variables = new StateVariable[] { variable("ExternalPort", portDatatype), |
| 101 | + variable("InternalPort", portDatatype), variable("Protocol", Datatype.Builtin.STRING.getDatatype()), |
| 102 | + variable("InternalClient", Datatype.Builtin.STRING.getDatatype()), |
| 103 | + variable("LeaseDuration", Datatype.Builtin.UI4.getDatatype()), |
| 104 | + variable("Enabled", Datatype.Builtin.BOOLEAN.getDatatype()), |
| 105 | + variable("Description", Datatype.Builtin.STRING.getDatatype()) }; |
| 106 | + final Action action = add |
| 107 | + ? new Action("AddPortMapping", |
| 108 | + new ActionArgument[] { input("NewExternalPort", "ExternalPort"), |
| 109 | + input("NewProtocol", "Protocol"), input("NewInternalClient", "InternalClient"), |
| 110 | + input("NewInternalPort", "InternalPort"), input("NewLeaseDuration", "LeaseDuration"), |
| 111 | + input("NewEnabled", "Enabled"), input("NewPortMappingDescription", "Description") }) |
| 112 | + : new Action("DeletePortMapping", new ActionArgument[] { input("NewExternalPort", "ExternalPort"), |
| 113 | + input("NewProtocol", "Protocol") }); |
| 114 | + |
| 115 | + return new RemoteService(new UDAServiceType("WANIPConnection", 1), new UDAServiceId("WANIPConnection"), |
| 116 | + URI.create("service.xml"), URI.create("control"), URI.create("events"), new Action[] { action }, |
| 117 | + variables); |
| 118 | + } |
| 119 | + |
| 120 | + private StateVariable variable(final String name, final Datatype<?> datatype) { |
| 121 | + return new StateVariable(name, new StateVariableTypeDetails(datatype)); |
| 122 | + } |
| 123 | + |
| 124 | + private ActionArgument input(final String name, final String stateVariable) { |
| 125 | + return new ActionArgument(name, stateVariable, ActionArgument.Direction.IN); |
| 126 | + } |
| 127 | +} |
0 commit comments