Commit c2b34b1d authored by George Nash's avatar George Nash Committed by Rick Bell

Update Java stringToEndpoint method

The Java stringToEndpoint method now returns an OCEndpoint

In addition a collection of unit tests were added to verify
the code.

Change-Id: I6040d9b48ca214d83daf49a59f6b2ba8ff2903dd
Signed-off-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
parent 9caba7a4
package org.iotivity;
import static org.junit.Assert.*;
import org.junit.Test;
public class OCEndpointTest {
@Test
public void testStringToEndpoint() {
String[] uri = new String[1];
// IPV4 with port and uri
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coaps://10.211.55.3:56789/a/light", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV4, ep.getFlags() & OCTransportFlags.IPV4);
assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
assertNotEquals(OCTransportFlags.TCP, ep.getFlags() & OCTransportFlags.TCP);
assertEquals(56789, ep.getAddr().getIpv4().getPort());
assertEquals("/a/light", uri[0]);
assertArrayEquals(new short[]{10, 211, 55, 3}, ep.getAddr().getIpv4().getAddress());
OCEndpointUtil.freeEndpoint(ep);
// IPV6
ep = OCEndpointUtil.stringToEndpoint("coap://[ff02::158]", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
assertNotEquals(OCTransportFlags.TCP, (ep.getFlags() & OCTransportFlags.TCP));
assertEquals(5683, ep.getAddr().getIpv6().getPort());
assertNull(uri[0]);
assertArrayEquals(new short[]{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x58},
ep.getAddr().getIpv6().getAddress());
OCEndpointUtil.freeEndpoint(ep);
// IPV6 with uri
ep = OCEndpointUtil.stringToEndpoint("coaps://[ff02::158]/a/light", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
assertNotEquals(OCTransportFlags.TCP, (ep.getFlags() & OCTransportFlags.TCP));
assertEquals(5684, ep.getAddr().getIpv6().getPort());
assertEquals("/a/light", uri[0]);
assertArrayEquals(new short[]{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x58},
ep.getAddr().getIpv6().getAddress());
OCEndpointUtil.freeEndpoint(ep);
// IPV6 with port and uri
ep = OCEndpointUtil.stringToEndpoint("coaps://[fe80::12]:2439/a/light", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
assertNotEquals(OCTransportFlags.TCP, (ep.getFlags() & OCTransportFlags.TCP));
assertEquals(2439, ep.getAddr().getIpv6().getPort());
assertEquals("/a/light", uri[0]);
assertArrayEquals(new short[]{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12},
ep.getAddr().getIpv6().getAddress());
OCEndpointUtil.freeEndpoint(ep);
}
// The tests will fail on Windows. It does not yet support dns lookup.
@Test
public void testStringToEndpoint_dns_lookup() {
String[] uri = new String[1];
// dns lookup
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coap://openconnectivity.org", uri);
assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
assertNotEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
assertNotEquals(OCTransportFlags.TCP, ep.getFlags() & OCTransportFlags.TCP);
assertEquals(5683, ep.getAddr().getIpv4().getPort());
assertNull(uri[0]);
OCEndpointUtil.freeEndpoint(ep);
// dns lookup with uri
ep = OCEndpointUtil.stringToEndpoint("coap://openconnectivity.org/alpha", uri);
assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
assertNotEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
assertNotEquals(OCTransportFlags.TCP, ep.getFlags() & OCTransportFlags.TCP);
assertEquals(5683, ep.getAddr().getIpv4().getPort());
assertEquals("/alpha", uri[0]);
OCEndpointUtil.freeEndpoint(ep);
// dns lookup with port and uri
ep = OCEndpointUtil.stringToEndpoint("coaps://openconnectivity.org:3456/alpha", uri);
assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
assertNotEquals(OCTransportFlags.TCP, ep.getFlags() & OCTransportFlags.TCP);
assertEquals(3456, ep.getAddr().getIpv4().getPort());
assertEquals("/alpha", uri[0]);
OCEndpointUtil.freeEndpoint(ep);
}
// The tests will fail on Windows. It does not yet support tcp.
@Test
public void testStringToEndpoint_tcp() {
String[] uri = new String[1];
// IPv4 over tcp and uri
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://10.211.55.3/a/light", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV4, ep.getFlags() & OCTransportFlags.IPV4);
assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
assertEquals(OCTransportFlags.TCP, ep.getFlags() & OCTransportFlags.TCP);
assertEquals(5684, ep.getAddr().getIpv4().getPort());
assertEquals("/a/light", uri[0]);
assertArrayEquals(new short[]{10, 211, 55, 3}, ep.getAddr().getIpv4().getAddress());
OCEndpointUtil.freeEndpoint(ep);
// IPv4 over tcp and port
ep = OCEndpointUtil.stringToEndpoint("coap+tcp://1.2.3.4:2568", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV4, (ep.getFlags() & OCTransportFlags.IPV4));
assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
assertEquals(OCTransportFlags.TCP, (ep.getFlags() & OCTransportFlags.TCP));
assertEquals(2568, ep.getAddr().getIpv4().getPort());
assertNull(uri[0]);
assertArrayEquals(new short[]{1, 2, 3, 4}, ep.getAddr().getIpv4().getAddress());
OCEndpointUtil.freeEndpoint(ep);
// IPv6 over tcp
ep = OCEndpointUtil.stringToEndpoint("coap+tcp://[ff02::158]", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertNotEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
assertEquals(OCTransportFlags.TCP, (ep.getFlags() & OCTransportFlags.TCP));
assertEquals(5683, ep.getAddr().getIpv6().getPort());
assertNull(uri[0]);
assertArrayEquals(new short[]{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x58},
ep.getAddr().getIpv6().getAddress());
OCEndpointUtil.freeEndpoint(ep);
// IPv6 over tcp with uri
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://[ff02::158]/a/light", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
assertEquals(OCTransportFlags.TCP, (ep.getFlags() & OCTransportFlags.TCP));
assertEquals(5684, ep.getAddr().getIpv6().getPort());
assertEquals("/a/light", uri[0]);
assertArrayEquals(new short[]{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x58},
ep.getAddr().getIpv6().getAddress());
OCEndpointUtil.freeEndpoint(ep);
// IPv6 over tcp with port and uri
ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://[fe80::12]:2439/a/light", uri);
assertNotNull(ep);
assertEquals(OCTransportFlags.IPV6, (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, (ep.getFlags() & OCTransportFlags.SECURED));
assertEquals(OCTransportFlags.TCP, (ep.getFlags() & OCTransportFlags.TCP));
assertEquals(2439, ep.getAddr().getIpv6().getPort());
assertEquals("/a/light", uri[0]);
assertArrayEquals(new short[]{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12},
ep.getAddr().getIpv6().getAddress());
OCEndpointUtil.freeEndpoint(ep);
}
// The tests will fail on Windows. It does not yet support tcp or dns lookup.
@Test
public void testStringToEndpoint_tcp_and_dns_lookup() {
String[] uri = new String[1];
// dns lookup over tcp with port
OCEndpoint ep = OCEndpointUtil.stringToEndpoint("coaps+tcp://openconnectivity.org:3456", uri);
assertNotNull(ep);
assertTrue(OCTransportFlags.IPV4 == (ep.getFlags() & OCTransportFlags.IPV4) ||
OCTransportFlags.IPV6 == (ep.getFlags() & OCTransportFlags.IPV6));
assertEquals(OCTransportFlags.SECURED, ep.getFlags() & OCTransportFlags.SECURED);
assertEquals(OCTransportFlags.TCP, ep.getFlags() & OCTransportFlags.TCP);
assertEquals(3456, ep.getAddr().getIpv4().getPort());
assertNull(uri[0]);
OCEndpointUtil.freeEndpoint(ep);
}
}
......@@ -87,8 +87,21 @@ SWIGEXPORT jobject JNICALL Java_org_iotivity_OCEndpointUtilJNI_toString(JNIEnv *
%apply oc_string_t *INPUT { oc_string_t *endpoint_str };
%apply oc_string_t *OUTPUT { oc_string_t *uri };
/* TODO check the string_to_endpoint function output */
%rename(stringToEndpoint) oc_string_to_endpoint;
/* TODO figure out a clean way to return the uri param not as an array value */
%ignore oc_string_to_endpoint;
%rename(stringToEndpoint) jni_string_to_endpoint;
%inline %{
oc_endpoint_t * jni_string_to_endpoint(oc_string_t *endpoint_str, oc_string_t *uri) {
OC_DBG("JNI: %s\n", __func__);
oc_endpoint_t *ep = oc_new_endpoint();
if(oc_string_to_endpoint(endpoint_str, ep, uri) < 0) {
OC_DBG("JNI: oc_string_to_endpoint failed to parse %s\n", oc_string(*endpoint_str));
oc_free_endpoint(ep);
return NULL;
}
return ep;
}
%}
%rename(ipv6EndpointIsLinkLocal) oc_ipv6_endpoint_is_link_local;
%rename(compare) oc_endpoint_compare;
%rename(compareAddress) oc_endpoint_compare_address;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment