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 */
16
17package com.android.server.wifi;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21
22import android.os.Binder;
23import android.test.suitebuilder.annotation.SmallTest;
24
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28
29/**
30 * Unit tests for {@link com.android.server.wifi.BinderUtil}.
31 */
32@SmallTest
33public class BinderUtilTest {
34    static final int FAKE_UID = 30000000;
35
36    private long mToken;
37
38    /**
39     * Sets up the test harness before running a test.
40     */
41    @Before
42    public void setUp() {
43        mToken = Binder.clearCallingIdentity();
44    }
45
46    /**
47     * Cleans up the test harness after running a test.
48     */
49    @After
50    public void cleanUp() {
51        Binder.restoreCallingIdentity(mToken);
52    }
53
54    /**
55     * Test using {@link BinderUtil.setUid} to set and restore the Binder uid.
56     */
57    @Test
58    public void setUid() {
59        final int pid = Binder.getCallingPid();
60        final int uid = Binder.getCallingUid();
61        assertFalse(uid == FAKE_UID);
62
63        // Verify that setUid() can be used to fake the Binder uid without affecting the pid.
64        BinderUtil.setUid(FAKE_UID);
65        assertEquals(pid, Binder.getCallingPid());
66        assertEquals(FAKE_UID, Binder.getCallingUid());
67
68        // Verify that setUid() can be used to restore the original Binder uid without affecting the
69        // pid.
70        BinderUtil.setUid(uid);
71        assertEquals(pid, Binder.getCallingPid());
72        assertEquals(uid, Binder.getCallingUid());
73    }
74
75    /**
76     * Test using {@link BinderUtil.setUid} to set the Binder uid and
77     * {@link Binder.restoreCallingIdentity} to restore it.
78     */
79    @Test
80    public void setUidAndRestoreCallingIdentity() {
81        final int pid = Binder.getCallingPid();
82        final int uid = Binder.getCallingUid();
83        assertFalse(uid == FAKE_UID);
84
85        // Verify that setUid() can be used to fake the Binder uid without affecting the pid.
86        BinderUtil.setUid(FAKE_UID);
87        assertEquals(pid, Binder.getCallingPid());
88        assertEquals(FAKE_UID, Binder.getCallingUid());
89
90        // Verify that the setUid() calls above did not break Binder.restoreCallingIdentity().
91        Binder.restoreCallingIdentity(mToken);
92        assertEquals(pid, Binder.getCallingPid());
93        assertEquals(uid, Binder.getCallingUid());
94    }
95}
96