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

Commit 9c0ff1b0 authored by Chalard Jean's avatar Chalard Jean
Browse files

Factorize custom asserts.

Also a few utilities that were in the way, and some opportunistic
cleanups.

Test: FrameworksNetTest NetworkStackTest
Change-Id: I385070e2044fd967cb18f1ffea9a86a4627b742e
parent e7418497
Loading
Loading
Loading
Loading
+21 −20
Original line number Original line Diff line number Diff line
@@ -15,6 +15,7 @@
 */
 */
package android.net;
package android.net;


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


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


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


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


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


package android.net;
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.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.fail;


import android.os.Parcel;

import androidx.test.filters.SmallTest;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
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
    @Test
    public void testEquals() {
    public void testEquals() {
        IpPrefix p1, p2;
        IpPrefix p1, p2;


        p1 = new IpPrefix("192.0.2.251/23");
        p1 = new IpPrefix("192.0.2.251/23");
        p2 = new IpPrefix(new byte[]{(byte) 192, (byte) 0, (byte) 2, (byte) 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");
        p1 = new IpPrefix("192.0.2.5/23");
        assertAreEqual(p1, p2);
        assertEqualBothWays(p1, p2);


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


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




        p1 = new IpPrefix("2001:db8:dead:beef:f00::80/122");
        p1 = new IpPrefix("2001:db8:dead:beef:f00::80/122");
        p2 = new IpPrefix(IPV6_BYTES, 122);
        p2 = new IpPrefix(IPV6_BYTES, 122);
        assertEquals("2001:db8:dead:beef:f00::80/122", p2.toString());
        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");
        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");
        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");
        p1 = new IpPrefix("2001:db8:dead:beef::/122");
        assertAreNotEqual(p1, p2);
        assertNotEqualEitherWay(p1, p2);


        // 192.0.2.4/32 != c000:0204::/32.
        // 192.0.2.4/32 != c000:0204::/32.
        byte[] ipv6bytes = new byte[16];
        byte[] ipv6bytes = new byte[16];
        System.arraycopy(IPV4_BYTES, 0, ipv6bytes, 0, IPV4_BYTES.length);
        System.arraycopy(IPV4_BYTES, 0, ipv6bytes, 0, IPV4_BYTES.length);
        p1 = new IpPrefix(ipv6bytes, 32);
        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);
        p2 = new IpPrefix(IPV4_BYTES, 32);
        assertAreNotEqual(p1, p2);
        assertNotEqualEitherWay(p1, p2);
    }
    }


    @Test
    @Test
@@ -356,25 +348,6 @@ public class IpPrefixTest {
        assertEquals(InetAddress.parseNumericAddress("192.0.2.0"), p.getAddress());
        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
    @Test
    public void testParceling() {
    public void testParceling() {
        IpPrefix p;
        IpPrefix p;
@@ -386,5 +359,7 @@ public class IpPrefixTest {
        p = new IpPrefix("192.0.2.0/25");
        p = new IpPrefix("192.0.2.0/25");
        assertParcelingIsLossless(p);
        assertParcelingIsLossless(p);
        assertTrue(p.isIPv4());
        assertTrue(p.isIPv4());

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