blob: 99dbe6445662350fcd24060bf317e81383f7b092 [file] [log] [blame]
Olivier Gaillarde4ff3972018-08-16 14:01:58 +01001/*
2 * Copyright (C) 2018 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 android.os;
18
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090019import androidx.test.filters.SmallTest;
Olivier Gaillarde4ff3972018-08-16 14:01:58 +010020
21import junit.framework.TestCase;
22
23public class BinderTest extends TestCase {
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000024 private static final int UID = 100;
Olivier Gaillarde4ff3972018-08-16 14:01:58 +010025
26 @SmallTest
27 public void testSetWorkSource() throws Exception {
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000028 Binder.setCallingWorkSourceUid(UID);
29 assertEquals(UID, Binder.getCallingWorkSourceUid());
Olivier Gaillarde4ff3972018-08-16 14:01:58 +010030 }
31
32 @SmallTest
33 public void testClearWorkSource() throws Exception {
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000034 Binder.setCallingWorkSourceUid(UID);
35 Binder.clearCallingWorkSource();
36 assertEquals(-1, Binder.getCallingWorkSourceUid());
37 }
38
39 @SmallTest
40 public void testRestoreWorkSource() throws Exception {
41 Binder.setCallingWorkSourceUid(UID);
42 long token = Binder.clearCallingWorkSource();
43 Binder.restoreCallingWorkSource(token);
44 assertEquals(UID, Binder.getCallingWorkSourceUid());
Olivier Gaillarde4ff3972018-08-16 14:01:58 +010045 }
Nikita Ioffea929cf02019-01-03 13:35:22 +000046
47 @SmallTest
48 public void testGetCallingUidOrThrow() throws Exception {
49 try {
50 Binder.getCallingUidOrThrow();
51 throw new AssertionError("IllegalStateException expected");
52 } catch (IllegalStateException expected) {
53 }
54 }
Yo Chiangb0316852020-01-21 16:10:20 +080055
56 @SmallTest
57 public void testGetExtension() throws Exception {
58 Binder binder = new Binder();
59 assertNull(binder.getExtension());
60
61 IBinder extension = new Binder();
62 binder.setExtension(extension);
63 assertNotNull(binder.getExtension());
64 assertSame(binder.getExtension(), extension);
65
66 binder.setExtension(null);
67 assertNull(binder.getExtension());
68 }
Olivier Gaillarde4ff3972018-08-16 14:01:58 +010069}