From e7c481452ecf22e59e983759c3f7e9d932738334 Mon Sep 17 00:00:00 2001 From: sewookseo Date: Thu, 10 Mar 2022 03:06:43 +0000 Subject: [PATCH 1/2] Expose constructor of Exceptions. Exposing constructor of Exceptions for test purpose. CTS-Coverage-Bug: 224059548 BUG: 215240597 BUG: 216368595 Test: build Change-Id: I20af104a891e94873715ab46ecabc3e775638c8c --- framework/api/system-current.txt | 5 +++++ .../src/android/net/NetworkReleasedException.java | 4 +++- framework/src/android/net/QosCallbackException.java | 10 ++++------ .../net/SocketLocalAddressChangedException.java | 4 +++- framework/src/android/net/SocketNotBoundException.java | 4 +++- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/framework/api/system-current.txt b/framework/api/system-current.txt index 764cffa92a..015d2e58c2 100644 --- a/framework/api/system-current.txt +++ b/framework/api/system-current.txt @@ -360,6 +360,7 @@ package android.net { } public class NetworkReleasedException extends java.lang.Exception { + ctor public NetworkReleasedException(); } public class NetworkRequest implements android.os.Parcelable { @@ -424,6 +425,8 @@ package android.net { } public final class QosCallbackException extends java.lang.Exception { + ctor public QosCallbackException(@NonNull String); + ctor public QosCallbackException(@NonNull Throwable); } public abstract class QosFilter { @@ -469,9 +472,11 @@ package android.net { } public class SocketLocalAddressChangedException extends java.lang.Exception { + ctor public SocketLocalAddressChangedException(); } public class SocketNotBoundException extends java.lang.Exception { + ctor public SocketNotBoundException(); } public final class StaticIpConfiguration implements android.os.Parcelable { diff --git a/framework/src/android/net/NetworkReleasedException.java b/framework/src/android/net/NetworkReleasedException.java index 0629b7563a..cdfb6a1785 100644 --- a/framework/src/android/net/NetworkReleasedException.java +++ b/framework/src/android/net/NetworkReleasedException.java @@ -18,6 +18,8 @@ package android.net; import android.annotation.SystemApi; +import com.android.internal.annotations.VisibleForTesting; + /** * Indicates that the {@link Network} was released and is no longer available. * @@ -25,7 +27,7 @@ import android.annotation.SystemApi; */ @SystemApi public class NetworkReleasedException extends Exception { - /** @hide */ + @VisibleForTesting public NetworkReleasedException() { super("The network was released and is no longer available"); } diff --git a/framework/src/android/net/QosCallbackException.java b/framework/src/android/net/QosCallbackException.java index 7fd9a527e2..ed6eb15939 100644 --- a/framework/src/android/net/QosCallbackException.java +++ b/framework/src/android/net/QosCallbackException.java @@ -21,6 +21,8 @@ import android.annotation.NonNull; import android.annotation.SystemApi; import android.util.Log; +import com.android.internal.annotations.VisibleForTesting; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -94,16 +96,12 @@ public final class QosCallbackException extends Exception { } } - /** - * @hide - */ + @VisibleForTesting public QosCallbackException(@NonNull final String message) { super(message); } - /** - * @hide - */ + @VisibleForTesting public QosCallbackException(@NonNull final Throwable cause) { super(cause); } diff --git a/framework/src/android/net/SocketLocalAddressChangedException.java b/framework/src/android/net/SocketLocalAddressChangedException.java index 9daad83fd1..7be37935ae 100644 --- a/framework/src/android/net/SocketLocalAddressChangedException.java +++ b/framework/src/android/net/SocketLocalAddressChangedException.java @@ -18,6 +18,8 @@ package android.net; import android.annotation.SystemApi; +import com.android.internal.annotations.VisibleForTesting; + /** * Thrown when the local address of the socket has changed. * @@ -25,7 +27,7 @@ import android.annotation.SystemApi; */ @SystemApi public class SocketLocalAddressChangedException extends Exception { - /** @hide */ + @VisibleForTesting public SocketLocalAddressChangedException() { super("The local address of the socket changed"); } diff --git a/framework/src/android/net/SocketNotBoundException.java b/framework/src/android/net/SocketNotBoundException.java index b1d7026ac9..59f34a3d65 100644 --- a/framework/src/android/net/SocketNotBoundException.java +++ b/framework/src/android/net/SocketNotBoundException.java @@ -18,6 +18,8 @@ package android.net; import android.annotation.SystemApi; +import com.android.internal.annotations.VisibleForTesting; + /** * Thrown when a previously bound socket becomes unbound. * @@ -25,7 +27,7 @@ import android.annotation.SystemApi; */ @SystemApi public class SocketNotBoundException extends Exception { - /** @hide */ + @VisibleForTesting public SocketNotBoundException() { super("The socket is unbound"); } From 27447c526a3a29236581812dcbcf6610db3cf32c Mon Sep 17 00:00:00 2001 From: sewookseo Date: Thu, 17 Mar 2022 04:25:49 +0000 Subject: [PATCH 2/2] CTS test for QosCallbackException Adding CTS test coverage for Exceptions realted to QosCallback Bug: 224059548 Test: atest CtsNetTestCases Change-Id: Iccd1d3259961a6e4a841395175e215724895786e --- .../net/cts/QosCallbackExceptionTest.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java diff --git a/tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java b/tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java new file mode 100644 index 0000000000..cd43a34b5e --- /dev/null +++ b/tests/cts/net/src/android/net/cts/QosCallbackExceptionTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * 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 android.net.cts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import android.net.NetworkReleasedException; +import android.net.QosCallbackException; +import android.net.SocketLocalAddressChangedException; +import android.net.SocketNotBoundException; +import android.os.Build; + +import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; +import com.android.testutils.DevSdkIgnoreRunner; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(DevSdkIgnoreRunner.class) +@IgnoreUpTo(Build.VERSION_CODES.R) +public class QosCallbackExceptionTest { + private static final String ERROR_MESSAGE = "Test Error Message"; + private static final String ERROR_MSG_SOCK_NOT_BOUND = "The socket is unbound"; + private static final String ERROR_MSG_NET_RELEASED = + "The network was released and is no longer available"; + private static final String ERROR_MSG_SOCK_ADDR_CHANGED = + "The local address of the socket changed"; + + + @Test + public void testQosCallbackException() throws Exception { + final Throwable testcause = new Throwable(ERROR_MESSAGE); + final QosCallbackException exception = new QosCallbackException(testcause); + assertEquals(testcause, exception.getCause()); + + final QosCallbackException exceptionMsg = new QosCallbackException(ERROR_MESSAGE); + assertEquals(ERROR_MESSAGE, exceptionMsg.getMessage()); + } + + @Test + public void testNetworkReleasedExceptions() throws Exception { + final Throwable netReleasedException = new NetworkReleasedException(); + final QosCallbackException exception = new QosCallbackException(netReleasedException); + + assertTrue(exception.getCause() instanceof NetworkReleasedException); + assertEquals(netReleasedException, exception.getCause()); + assertTrue(exception.getMessage().contains(ERROR_MSG_NET_RELEASED)); + assertThrowableMessageContains(exception, ERROR_MSG_NET_RELEASED); + } + + @Test + public void testSocketNotBoundExceptions() throws Exception { + final Throwable sockNotBoundException = new SocketNotBoundException(); + final QosCallbackException exception = new QosCallbackException(sockNotBoundException); + + assertTrue(exception.getCause() instanceof SocketNotBoundException); + assertEquals(sockNotBoundException, exception.getCause()); + assertTrue(exception.getMessage().contains(ERROR_MSG_SOCK_NOT_BOUND)); + assertThrowableMessageContains(exception, ERROR_MSG_SOCK_NOT_BOUND); + } + + @Test + public void testSocketLocalAddressChangedExceptions() throws Exception { + final Throwable localAddrChangedException = new SocketLocalAddressChangedException(); + final QosCallbackException exception = new QosCallbackException(localAddrChangedException); + + assertTrue(exception.getCause() instanceof SocketLocalAddressChangedException); + assertEquals(localAddrChangedException, exception.getCause()); + assertTrue(exception.getMessage().contains(ERROR_MSG_SOCK_ADDR_CHANGED)); + assertThrowableMessageContains(exception, ERROR_MSG_SOCK_ADDR_CHANGED); + } + + private void assertThrowableMessageContains(QosCallbackException exception, String errorMsg) + throws Exception { + try { + triggerException(exception); + fail("Expect exception"); + } catch (QosCallbackException e) { + assertTrue(e.getMessage().contains(errorMsg)); + } + } + + private void triggerException(QosCallbackException exception) throws Exception { + throw new QosCallbackException(exception.getCause()); + } +}