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

Commit 8b544885 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I7368f9b4,I7a6bec4f,I3eb9f7bc

* changes:
  p2p: unit tests for WifiP2pWfdInfo
  p2p: unit tests for WifiP2pInfo
  p2p: unit tests for WifiP2pProvDiscEvent
parents d9439bf4 573b2b0c
Loading
Loading
Loading
Loading
+83 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.wifi.p2p;

import static org.junit.Assert.assertEquals;

import android.os.Parcel;

import androidx.test.filters.SmallTest;

import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;

/**
 * Unit test harness for {@link android.net.wifi.p2p.WifiP2pInfo}
 */
@SmallTest
public class WifiP2pInfoTest {

    private InetAddress mGroupOnwerIpv4Address;

    @Before
    public void setUp() throws Exception {
        byte[] ipv4 = {(byte) 192, (byte) 168, (byte) 49, (byte) 1};
        mGroupOnwerIpv4Address = InetAddress.getByAddress(ipv4);
    }

    /**
     * Verifies copy constructor.
     */
    @Test
    public void testCopyOperator() throws Exception {
        WifiP2pInfo info = new WifiP2pInfo();
        info.groupFormed = true;
        info.isGroupOwner = true;
        info.groupOwnerAddress = mGroupOnwerIpv4Address;

        WifiP2pInfo copiedInfo = new WifiP2pInfo(info);

        // no equals operator, use toString for data comparison.
        assertEquals(info.toString(), copiedInfo.toString());
    }

    /**
     * Verifies parcel serialization/deserialization.
     */
    @Test
    public void testParcelOperation() throws Exception {
        WifiP2pInfo info = new WifiP2pInfo();
        info.groupFormed = true;
        info.isGroupOwner = true;
        info.groupOwnerAddress = mGroupOnwerIpv4Address;

        Parcel parcelW = Parcel.obtain();
        info.writeToParcel(parcelW, 0);
        byte[] bytes = parcelW.marshall();
        parcelW.recycle();

        Parcel parcelR = Parcel.obtain();
        parcelR.unmarshall(bytes, 0, bytes.length);
        parcelR.setDataPosition(0);
        WifiP2pInfo fromParcel = WifiP2pInfo.CREATOR.createFromParcel(parcelR);

        // no equals operator, use toString for data comparison.
        assertEquals(info.toString(), fromParcel.toString());
    }
}
+111 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.wifi.p2p;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import androidx.test.filters.SmallTest;

import org.junit.Test;

/**
 * Unit test harness for {@link android.net.wifi.p2p.WifiP2pProvDiscEvent}
 */
@SmallTest
public class WifiP2pProvDiscEventTest {

    private static final String DEVICE_ADDRESS = "aa:bb:cc:dd:ee:ff";
    private static final String EVENT_PBC_REQ_STRING = "P2P-PROV-DISC-PBC-REQ";
    private static final String EVENT_PBC_RSP_STRING = "P2P-PROV-DISC-PBC-RESP";
    private static final String EVENT_ENTER_PIN_STRING = "P2P-PROV-DISC-ENTER-PIN";
    private static final String EVENT_SHOW_PIN_STRING = "P2P-PROV-DISC-SHOW-PIN";
    private static final String TEST_PIN = "44490607";

    /**
     * Test parsing PBC request event.
     */
    @Test
    public void testPbcReqEvent() throws Exception {
        WifiP2pProvDiscEvent event =
                new WifiP2pProvDiscEvent(EVENT_PBC_REQ_STRING + " " + DEVICE_ADDRESS);
        assertEquals(WifiP2pProvDiscEvent.PBC_REQ, event.event);
        assertEquals(DEVICE_ADDRESS, event.device.deviceAddress);
    }


    /**
     * Test parsing PBC response event.
     */
    @Test
    public void testPbcRespEvent() throws Exception {
        WifiP2pProvDiscEvent event =
                new WifiP2pProvDiscEvent(EVENT_PBC_RSP_STRING + " " + DEVICE_ADDRESS);
        assertEquals(WifiP2pProvDiscEvent.PBC_RSP, event.event);
        assertEquals(DEVICE_ADDRESS, event.device.deviceAddress);
    }

    /**
     * Test parsing ENTER-PIN event.
     */
    @Test
    public void testEnterPinEvent() throws Exception {
        WifiP2pProvDiscEvent event =
                new WifiP2pProvDiscEvent(EVENT_ENTER_PIN_STRING + " " + DEVICE_ADDRESS);
        assertEquals(WifiP2pProvDiscEvent.ENTER_PIN, event.event);
        assertEquals(DEVICE_ADDRESS, event.device.deviceAddress);
    }

    /**
     * Test parsing SHOW-PIN event.
     */
    @Test
    public void testShowPinEvent() throws Exception {
        WifiP2pProvDiscEvent event =
                new WifiP2pProvDiscEvent(
                        EVENT_SHOW_PIN_STRING + " " + DEVICE_ADDRESS + " " + TEST_PIN);
        assertEquals(WifiP2pProvDiscEvent.SHOW_PIN, event.event);
        assertEquals(DEVICE_ADDRESS, event.device.deviceAddress);
        assertEquals(TEST_PIN, event.pin);
    }

    /**
     * Test parsing malformed input.
     */
    @Test
    public void testMalformedInput() throws Exception {
        try {
            WifiP2pProvDiscEvent event = new WifiP2pProvDiscEvent("OneToken");
            fail("Should throw IllegalArgumentException exception.");
        } catch (IllegalArgumentException ex) {
            // expected exception.
        }
    }

    /**
     * Test parsing malformed event.
     */
    @Test
    public void testMalformedEvent() throws Exception {
        try {
            WifiP2pProvDiscEvent event = new WifiP2pProvDiscEvent("XXX " + DEVICE_ADDRESS);
            fail("Should throw IllegalArgumentException exception.");
        } catch (IllegalArgumentException ex) {
            // expected exception.
        }
    }
}
+110 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.wifi.p2p;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import android.os.Parcel;

import androidx.test.filters.SmallTest;

import org.junit.Before;
import org.junit.Test;

/**
 * Unit test harness for {@link android.net.wifi.p2p.WifiP2pWfdInfo}
 */
@SmallTest
public class WifiP2pWfdInfoTest {

    private static final int TEST_CTRL_PORT = 9999;
    private static final int TEST_MAX_TPUT = 1024;

    private WifiP2pWfdInfo mSourceInfo = new WifiP2pWfdInfo(
            0,
            TEST_CTRL_PORT,
            TEST_MAX_TPUT);

    @Before
    public void setUp() {
        // initialize device info flags.
        mSourceInfo.setDeviceType(WifiP2pWfdInfo.WFD_SOURCE);
        mSourceInfo.setSessionAvailable(true);
    }

    /**
     * Verifies setters/getters.
     */
    @Test
    public void testSettersGetters() throws Exception {
        WifiP2pWfdInfo info = new WifiP2pWfdInfo();

        info.setWfdEnabled(true);
        assertTrue(info.isWfdEnabled());

        info.setDeviceType(WifiP2pWfdInfo.WFD_SOURCE);
        assertEquals(WifiP2pWfdInfo.WFD_SOURCE, info.getDeviceType());

        info.setCoupledSinkSupportAtSource(true);
        assertTrue(info.isCoupledSinkSupportedAtSource());

        info.setCoupledSinkSupportAtSink(true);
        assertTrue(info.isCoupledSinkSupportedAtSink());

        info.setSessionAvailable(true);
        assertTrue(info.isSessionAvailable());

        info.setControlPort(TEST_CTRL_PORT);
        assertEquals(TEST_CTRL_PORT, info.getControlPort());

        info.setMaxThroughput(TEST_MAX_TPUT);
        assertEquals(TEST_MAX_TPUT, info.getMaxThroughput());

        assertEquals("0018270f0400", info.getDeviceInfoHex());
    }

    /**
     * Verifies copy constructor.
     */
    @Test
    public void testCopyOperator() throws Exception {
        WifiP2pWfdInfo copiedInfo = new WifiP2pWfdInfo(mSourceInfo);

        // no equals operator, use toString for data comparison.
        assertEquals(mSourceInfo.toString(), copiedInfo.toString());
    }

    /**
     * Verifies parcel serialization/deserialization.
     */
    @Test
    public void testParcelOperation() throws Exception {
        Parcel parcelW = Parcel.obtain();
        mSourceInfo.writeToParcel(parcelW, 0);
        byte[] bytes = parcelW.marshall();
        parcelW.recycle();

        Parcel parcelR = Parcel.obtain();
        parcelR.unmarshall(bytes, 0, bytes.length);
        parcelR.setDataPosition(0);
        WifiP2pWfdInfo fromParcel = WifiP2pWfdInfo.CREATOR.createFromParcel(parcelR);

        // no equals operator, use toString for data comparison.
        assertEquals(mSourceInfo.toString(), fromParcel.toString());
    }
}