From: Chanhee Lee Date: Tue, 12 Oct 2021 01:04:17 +0000 (+0900) Subject: Add null checks and non-instrumented TCs for inference package. X-Git-Tag: submit/tizen/20211028.105706~20 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ca08ca43e13a3d0bc2849527d5cf0cf180f89e66;p=platform%2Fcore%2Fml%2Fbeyond.git Add null checks and non-instrumented TCs for inference package. * Change null returns into exceptions. Change-Id: I537cfed6aabc63f922a5a7100eaff26073b1bbf3 --- diff --git a/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceHandler.java b/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceHandler.java index 6294f62..2fb786f 100644 --- a/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceHandler.java +++ b/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceHandler.java @@ -24,7 +24,8 @@ import com.samsung.android.beyond.NativeInstance; import static com.samsung.android.beyond.inference.Option.TAG; public class InferenceHandler extends NativeInstance { - public InferenceHandler(InferenceMode inferenceMode) { + + InferenceHandler(InferenceMode inferenceMode) { registerNativeInstance(nativeCreateInference(inferenceMode.toString()), (Long instance) -> destroy(instance)); } diff --git a/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceModuleFactory.java b/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceModuleFactory.java index ebe592c..0d6da41 100644 --- a/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceModuleFactory.java +++ b/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceModuleFactory.java @@ -21,14 +21,26 @@ import android.content.Context; public class InferenceModuleFactory { public static InferenceHandler createHandler(InferenceMode inferenceMode) { + if (inferenceMode == null) { + throw new IllegalArgumentException("inferenceMode is null."); + } + return new InferenceHandler(inferenceMode); } public static Peer createPeerServer(Context context, String peerType) { + if (context == null || peerType == null) { + throw new IllegalArgumentException("Arguments are not correct."); + } + return new Peer(context, peerType, NodeType.SERVER); } public static Peer createPeerClient(Context context, String peerType) { + if (context == null || peerType == null) { + throw new IllegalArgumentException("Arguments are not correct."); + } + return new Peer(context, peerType, NodeType.CLIENT); } } diff --git a/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/Peer.java b/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/Peer.java index 9452c32..3859dfc 100644 --- a/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/Peer.java +++ b/subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/Peer.java @@ -22,6 +22,8 @@ import android.util.Log; import com.samsung.android.beyond.NativeInstance; import static com.samsung.android.beyond.inference.Option.TAG; +import java.util.regex.Pattern; + public class Peer extends NativeInstance { static { initialize(); @@ -34,9 +36,16 @@ public class Peer extends NativeInstance { // TODO: // List of runtimes + + public Info() {} + + public Info(String host, int port) { + this.host = host; + this.port = port; + } } - public Peer(Context context, String peerType, NodeType nodeType) { + Peer(Context context, String peerType, NodeType nodeType) { switch (nodeType) { case SERVER: String[] serverConfiguration = new String[4]; @@ -67,6 +76,25 @@ public class Peer extends NativeInstance { registerNativeInstance(instance, (Long instance) -> destroy(instance)); } + public boolean setIPPort(String IP, int port) { + if (validateIPv4(IP) == false || port < 0) { + Log.e(TAG, "Arguments are invalid."); + return false; + } + + return setInfo(instance, new Info(IP, port)); + } + + private boolean validateIPv4(String IP) { + if (IP == null) { + Log.e(TAG, "IP is null."); + } + + String regexIPv4 = "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"; + Pattern pattern = Pattern.compile(regexIPv4); + return pattern.matcher(IP).matches(); + } + public boolean setInfo() { Info info = new Info(); info.host = "0.0.0.0"; @@ -88,6 +116,11 @@ public class Peer extends NativeInstance { } public boolean configure(char type, Object obj) { + if (Character.isDefined(type) || obj == null) { + Log.e(TAG, "Arguments are invalid."); + return false; + } + return configure(instance, type, obj); } diff --git a/subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/InferenceModuleFactoryUnitTest.java b/subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/InferenceModuleFactoryUnitTest.java new file mode 100644 index 0000000..62f4e44 --- /dev/null +++ b/subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/InferenceModuleFactoryUnitTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.samsung.android.beyond; + +import android.content.Context; + +import com.samsung.android.beyond.inference.InferenceModuleFactory; +import com.samsung.android.beyond.module.peer.NN.NNModule; + +import org.junit.Test; + +public class InferenceModuleFactoryUnitTest { + + private static Context context; + + @Test(expected = IllegalArgumentException.class) + public void testCreateHandlerWithNullArgument() { + InferenceModuleFactory.createHandler(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreatePeerServerWithNullArguments() { + InferenceModuleFactory.createPeerServer(context, null); + + InferenceModuleFactory.createPeerServer(null, NNModule.NAME); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreatePeerClientWithNullArgument() { + InferenceModuleFactory.createPeerClient(context, null); + + InferenceModuleFactory.createPeerClient(null, NNModule.NAME); + } +} diff --git a/subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/TensorUnitTest.java b/subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/TensorUnitTest.java index 699c657..d60e019 100644 --- a/subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/TensorUnitTest.java +++ b/subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/TensorUnitTest.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.samsung.android.beyond; import static org.junit.Assert.assertArrayEquals; @@ -156,7 +172,7 @@ public class TensorUnitTest { ByteBuffer byteBuffer = (ByteBuffer) buffer; } - private class TestTensor extends Tensor { + private static class TestTensor extends Tensor { TestTensor(Class classType, TensorInfo tensorInfo, ByteBuffer buffer) { super(classType, tensorInfo); super.setBuffer(buffer);