NetworkEventTest.java revision f77ee4f1b79929a77f603e5e879f3616ae464e3e
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.devicepolicy;
17
18import android.app.admin.ConnectEvent;
19import android.app.admin.DnsEvent;
20import android.os.Parcel;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import static junit.framework.Assert.assertEquals;
24
25@SmallTest
26public class NetworkEventTest extends DpmTestBase {
27
28    /**
29     * Test parceling and unparceling of a ConnectEvent.
30     */
31    public void testConnectEventParceling() {
32        ConnectEvent event = new ConnectEvent("127.0.0.1", 80, "com.android.whateverdude", 100000);
33        Parcel p = Parcel.obtain();
34        p.writeParcelable(event, 0);
35        p.setDataPosition(0);
36        ConnectEvent unparceledEvent = p.readParcelable(NetworkEventTest.class.getClassLoader());
37        p.recycle();
38        assertEquals(event.getIpAddress(), unparceledEvent.getIpAddress());
39        assertEquals(event.getPort(), unparceledEvent.getPort());
40        assertEquals(event.getPackageName(), unparceledEvent.getPackageName());
41        assertEquals(event.getTimestamp(), unparceledEvent.getTimestamp());
42    }
43
44    /**
45     * Test parceling and unparceling of a DnsEvent.
46     */
47    public void testDnsEventParceling() {
48        DnsEvent event = new DnsEvent("d.android.com", new String[]{"192.168.0.1", "127.0.0.1"}, 2,
49                "com.android.whateverdude", 100000);
50        Parcel p = Parcel.obtain();
51        p.writeParcelable(event, 0);
52        p.setDataPosition(0);
53        DnsEvent unparceledEvent = p.readParcelable(NetworkEventTest.class.getClassLoader());
54        p.recycle();
55        assertEquals(event.getHostname(), unparceledEvent.getHostname());
56        assertEquals(event.getIpAddresses()[0], unparceledEvent.getIpAddresses()[0]);
57        assertEquals(event.getIpAddresses()[1], unparceledEvent.getIpAddresses()[1]);
58        assertEquals(event.getIpAddressesCount(), unparceledEvent.getIpAddressesCount());
59        assertEquals(event.getPackageName(), unparceledEvent.getPackageName());
60        assertEquals(event.getTimestamp(), unparceledEvent.getTimestamp());
61    }
62}
63