Add null checks and non-instrumented TCs for inference package.
authorChanhee Lee <ch2102.lee@samsung.com>
Tue, 12 Oct 2021 01:04:17 +0000 (10:04 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 28 Oct 2021 10:32:49 +0000 (19:32 +0900)
* Change null returns into exceptions.

Change-Id: I537cfed6aabc63f922a5a7100eaff26073b1bbf3

subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceHandler.java
subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceModuleFactory.java
subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/Peer.java
subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/InferenceModuleFactoryUnitTest.java [new file with mode: 0644]
subprojects/libbeyond-android/src/test/java/com/samsung/android/beyond/TensorUnitTest.java

index 6294f62d1f6b62e5f3d3a202037f68d73ac184a6..2fb786f0323f8a4f9489f6b1221f4b43c1c9ec1a 100644 (file)
@@ -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));
     }
 
index ebe592c645a3fa383675cf3ddc2e46036f3accfb..0d6da4183c77e71ba6affb101d52b24f31912bb1 100644 (file)
@@ -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);
     }
 }
index 9452c3294a49766e07b8aef2ed0aa790cd6603cf..3859dfcceecb4626bc9222b4f06bff198e8ed0bc 100644 (file)
@@ -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 (file)
index 0000000..62f4e44
--- /dev/null
@@ -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);
+    }
+}
index 699c657e307a1b724d73896555e0fe948518e39b..d60e01943ee2edeb2558aa2b64c620b032e44b90 100644 (file)
@@ -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);