Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c176efa authored by Chalard Jean's avatar Chalard Jean Committed by Gerrit Code Review
Browse files

Merge "Factorize custom asserts."

parents ff32698f 9c0ff1b0
Loading
Loading
Loading
Loading
+21 −20
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package android.net;

import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;

@@ -333,25 +334,25 @@ public final class IpSecConfig implements Parcelable {
                }
            };

    @VisibleForTesting
    /** Equals method used for testing */
    public static boolean equals(IpSecConfig lhs, IpSecConfig rhs) {
        if (lhs == null || rhs == null) return (lhs == rhs);
        return (lhs.mMode == rhs.mMode
                && lhs.mSourceAddress.equals(rhs.mSourceAddress)
                && lhs.mDestinationAddress.equals(rhs.mDestinationAddress)
                && ((lhs.mNetwork != null && lhs.mNetwork.equals(rhs.mNetwork))
                        || (lhs.mNetwork == rhs.mNetwork))
                && lhs.mEncapType == rhs.mEncapType
                && lhs.mEncapSocketResourceId == rhs.mEncapSocketResourceId
                && lhs.mEncapRemotePort == rhs.mEncapRemotePort
                && lhs.mNattKeepaliveInterval == rhs.mNattKeepaliveInterval
                && lhs.mSpiResourceId == rhs.mSpiResourceId
                && IpSecAlgorithm.equals(lhs.mEncryption, rhs.mEncryption)
                && IpSecAlgorithm.equals(lhs.mAuthenticatedEncryption, rhs.mAuthenticatedEncryption)
                && IpSecAlgorithm.equals(lhs.mAuthentication, rhs.mAuthentication)
                && lhs.mMarkValue == rhs.mMarkValue
                && lhs.mMarkMask == rhs.mMarkMask
                && lhs.mXfrmInterfaceId == rhs.mXfrmInterfaceId);
    @Override
    public boolean equals(@Nullable Object other) {
        if (!(other instanceof IpSecConfig)) return false;
        final IpSecConfig rhs = (IpSecConfig) other;
        return (mMode == rhs.mMode
                && mSourceAddress.equals(rhs.mSourceAddress)
                && mDestinationAddress.equals(rhs.mDestinationAddress)
                && ((mNetwork != null && mNetwork.equals(rhs.mNetwork))
                        || (mNetwork == rhs.mNetwork))
                && mEncapType == rhs.mEncapType
                && mEncapSocketResourceId == rhs.mEncapSocketResourceId
                && mEncapRemotePort == rhs.mEncapRemotePort
                && mNattKeepaliveInterval == rhs.mNattKeepaliveInterval
                && mSpiResourceId == rhs.mSpiResourceId
                && IpSecAlgorithm.equals(mEncryption, rhs.mEncryption)
                && IpSecAlgorithm.equals(mAuthenticatedEncryption, rhs.mAuthenticatedEncryption)
                && IpSecAlgorithm.equals(mAuthentication, rhs.mAuthentication)
                && mMarkValue == rhs.mMarkValue
                && mMarkMask == rhs.mMarkMask
                && mXfrmInterfaceId == rhs.mXfrmInterfaceId);
    }
}
+6 −8
Original line number Diff line number Diff line
@@ -148,15 +148,13 @@ public final class IpSecTransform implements AutoCloseable {
    }

    /**
     * Equals method used for testing
     *
     * @hide
     * Standard equals.
     */
    @VisibleForTesting
    public static boolean equals(IpSecTransform lhs, IpSecTransform rhs) {
        if (lhs == null || rhs == null) return (lhs == rhs);
        return IpSecConfig.equals(lhs.getConfig(), rhs.getConfig())
                && lhs.mResourceId == rhs.mResourceId;
    public boolean equals(Object other) {
        if (this == other) return true;
        if (!(other instanceof IpSecTransform)) return false;
        final IpSecTransform rhs = (IpSecTransform) other;
        return getConfig().equals(rhs.getConfig()) && mResourceId == rhs.mResourceId;
    }

    /**
+0 −1
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@ java_defaults {
    static_libs: [
        "FrameworksNetCommonTests",
        "frameworks-base-testutils",
        "frameworks-net-testutils",
        "framework-protos",
        "androidx.test.rules",
        "mockito-target-minus-junit4",
+2 −2
Original line number Diff line number Diff line
@@ -21,9 +21,9 @@ java_library {
    srcs: ["java/**/*.java", "java/**/*.kt"],
    static_libs: [
        "androidx.test.rules",
        "frameworks-net-testutils",
        "junit",
        "mockito-target-minus-junit4",
        "net-tests-utils",
        "platform-test-annotations",
    ],
    libs: [
+17 −42
Original line number Diff line number Diff line
@@ -16,16 +16,18 @@

package android.net;

import static com.android.testutils.MiscAssertsKt.assertEqualBothWays;
import static com.android.testutils.MiscAssertsKt.assertFieldCountEquals;
import static com.android.testutils.MiscAssertsKt.assertNotEqualEitherWay;
import static com.android.testutils.ParcelUtilsKt.assertParcelingIsLossless;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import android.os.Parcel;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

@@ -171,56 +173,46 @@ public class IpPrefixTest {

    }

    private void assertAreEqual(Object o1, Object o2) {
        assertTrue(o1.equals(o2));
        assertTrue(o2.equals(o1));
    }

    private void assertAreNotEqual(Object o1, Object o2) {
        assertFalse(o1.equals(o2));
        assertFalse(o2.equals(o1));
    }

    @Test
    public void testEquals() {
        IpPrefix p1, p2;

        p1 = new IpPrefix("192.0.2.251/23");
        p2 = new IpPrefix(new byte[]{(byte) 192, (byte) 0, (byte) 2, (byte) 251}, 23);
        assertAreEqual(p1, p2);
        assertEqualBothWays(p1, p2);

        p1 = new IpPrefix("192.0.2.5/23");
        assertAreEqual(p1, p2);
        assertEqualBothWays(p1, p2);

        p1 = new IpPrefix("192.0.2.5/24");
        assertAreNotEqual(p1, p2);
        assertNotEqualEitherWay(p1, p2);

        p1 = new IpPrefix("192.0.4.5/23");
        assertAreNotEqual(p1, p2);
        assertNotEqualEitherWay(p1, p2);


        p1 = new IpPrefix("2001:db8:dead:beef:f00::80/122");
        p2 = new IpPrefix(IPV6_BYTES, 122);
        assertEquals("2001:db8:dead:beef:f00::80/122", p2.toString());
        assertAreEqual(p1, p2);
        assertEqualBothWays(p1, p2);

        p1 = new IpPrefix("2001:db8:dead:beef:f00::bf/122");
        assertAreEqual(p1, p2);
        assertEqualBothWays(p1, p2);

        p1 = new IpPrefix("2001:db8:dead:beef:f00::8:0/123");
        assertAreNotEqual(p1, p2);
        assertNotEqualEitherWay(p1, p2);

        p1 = new IpPrefix("2001:db8:dead:beef::/122");
        assertAreNotEqual(p1, p2);
        assertNotEqualEitherWay(p1, p2);

        // 192.0.2.4/32 != c000:0204::/32.
        byte[] ipv6bytes = new byte[16];
        System.arraycopy(IPV4_BYTES, 0, ipv6bytes, 0, IPV4_BYTES.length);
        p1 = new IpPrefix(ipv6bytes, 32);
        assertAreEqual(p1, new IpPrefix("c000:0204::/32"));
        assertEqualBothWays(p1, new IpPrefix("c000:0204::/32"));

        p2 = new IpPrefix(IPV4_BYTES, 32);
        assertAreNotEqual(p1, p2);
        assertNotEqualEitherWay(p1, p2);
    }

    @Test
@@ -356,25 +348,6 @@ public class IpPrefixTest {
        assertEquals(InetAddress.parseNumericAddress("192.0.2.0"), p.getAddress());
    }

    public IpPrefix passThroughParcel(IpPrefix p) {
        Parcel parcel = Parcel.obtain();
        IpPrefix p2 = null;
        try {
            p.writeToParcel(parcel, 0);
            parcel.setDataPosition(0);
            p2 = IpPrefix.CREATOR.createFromParcel(parcel);
        } finally {
            parcel.recycle();
        }
        assertNotNull(p2);
        return p2;
    }

    public void assertParcelingIsLossless(IpPrefix p) {
        IpPrefix p2 = passThroughParcel(p);
        assertEquals(p, p2);
    }

    @Test
    public void testParceling() {
        IpPrefix p;
@@ -386,5 +359,7 @@ public class IpPrefixTest {
        p = new IpPrefix("192.0.2.0/25");
        assertParcelingIsLossless(p);
        assertTrue(p.isIPv4());

        assertFieldCountEquals(2, IpPrefix.class);
    }
}
Loading