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

Throw NullPointerException for setDi method

If null is passed in for the endpoint or for di when
calling the OCEndpointUtil.setDi method it will now
throw a NullPointerException that will avoid
a segfault from the C code.

Change-Id: I3f29486c21e08f3718752efbd108ce0401defa24
Signed-off-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
parent 1155be09
......@@ -174,4 +174,30 @@ public class OCEndpointTest {
OCEndpointUtil.freeEndpoint(ep);
}
@Test
public void test_throw_exception_on_setDi_when_endpoint_null() {
try {
OCUuid testUuid = OCUuidUtil.generateUuid();
OCEndpointUtil.setDi(null, testUuid);
fail("The call to setDi when endpoint is null should thow an exception.");
} catch (Exception e) {
assertEquals( e.getClass(), NullPointerException.class);
assertEquals(e.getMessage(), "OCEndpoint cannot be null.");
}
}
@Test
public void test_throw_exception_on_setDi_when_di_null() {
try {
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);
OCEndpointUtil.setDi(ep, null);
fail("The call to setDi when di is null should thow an exception.");
} catch (Exception e) {
assertEquals( e.getClass(), NullPointerException.class);
assertEquals(e.getMessage(), "OCUuid cannot be null.");
}
}
}
......@@ -62,7 +62,45 @@ void jni_free_endpoint(oc_endpoint_t *endpoint) {
endpoint = NULL;
}
%}
%ignore oc_endpoint_set_di;
%exception oc_endpoint_set_di {
/* The `oc_endpoint_t *endpoint` parameter is jarg1, the name is generated by SWIG. */
if(jarg1 == NULL) {
OC_DBG("JNI: OCEndpoint cannot be null.\n");
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/NullPointerException");
(*jenv)->ThrowNew(jenv, clazz, "OCEndpoint cannot be null.");
return;
}
/* The `oc_uuid_t *di` parameter is jarg2, the name is generated by SWIG. */
if(jarg2 == NULL) {
OC_DBG("JNI: OCUuid cannot be null.\n");
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/NullPointerException");
(*jenv)->ThrowNew(jenv, clazz, "OCUuid cannot be null.");
return;
}
$action
}
%rename(setDi) oc_endpoint_set_di;
/*
%inline %{
void jni_endpoint_set_di(oc_endpoint_t *endpoint, oc_uuid_t *di) {
if(endpoint == NULL) {
OC_DBG("JNI: OCEndpoint cannot be null.\n", __func__);
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
(*jenv)->ThrowNew(jenv, clazz, "OCEndpoint cannot be null.");
return;
}
if(di == NULL) {
OC_DBG("JNI: OCUuid cannot be null.\n", __func__);
jclass clazz = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
(*jenv)->ThrowNew(jenv, clazz, "OCUuid cannot be null.");
return;
}
oc_endpoint_set_di(endpoint, di);
}
%}
*/
%ignore oc_endpoint_to_string;
%typemap(jni) jobject toString "jobject";
......
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