aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowBinderTest.java
blob: b02e066918a8ce19ad42bba966ffad0347e1e567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
import static android.os.Build.VERSION_CODES.Q;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.os.Binder;
import android.os.Parcel;
import android.os.UserHandle;
import android.os.UserManager;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(AndroidJUnit4.class)
public class ShadowBinderTest {

  private UserManager userManager;

  @Before
  public void setUp() {
    Context context = ApplicationProvider.getApplicationContext();
    userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
  }

  @Test
  public void transactCallsOnTransact() throws Exception {
    TestBinder testBinder = new TestBinder();
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    data.writeString("Hello Robolectric");
    assertTrue(testBinder.transact(2, data, reply, 3));
    assertThat(testBinder.code).isEqualTo(2);
    assertThat(testBinder.data).isSameInstanceAs(data);
    assertThat(testBinder.reply).isSameInstanceAs(reply);
    assertThat(testBinder.flags).isEqualTo(3);
    reply.readException();
    assertThat(reply.readString()).isEqualTo("Hello Robolectric");
  }

  static class TestBinder extends Binder {
    int code;
    Parcel data;
    Parcel reply;
    int flags;

    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
      this.code = code;
      this.data = data;
      this.reply = reply;
      this.flags = flags;
      String string = data.readString();
      reply.writeNoException();
      reply.writeString(string);
      return true;
    }
  }

  @Test
  public void thrownExceptionIsParceled() throws Exception {
    TestThrowingBinder testThrowingBinder = new TestThrowingBinder();
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    testThrowingBinder.transact(2, data, reply, 3);
    try {
      reply.readException();
      fail(); // Expect thrown
    } catch (SecurityException e) {
      assertThat(e.getMessage()).isEqualTo("Halt! Who goes there?");
    }
  }

  static class TestThrowingBinder extends Binder {

    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
      throw new SecurityException("Halt! Who goes there?");
    }
  }

  @Test
  public void testSetCallingUid() {
    ShadowBinder.setCallingUid(37);
    assertThat(Binder.getCallingUid()).isEqualTo(37);
  }

  @Test
  public void testSetCallingPid() {
    ShadowBinder.setCallingPid(25);
    assertThat(Binder.getCallingPid()).isEqualTo(25);
  }

  @Test
  @Config(minSdk = JELLY_BEAN_MR1)
  public void testSetCallingUserHandle() {
    UserHandle newUser = shadowOf(userManager).addUser(10, "secondary_user", 0);
    ShadowBinder.setCallingUserHandle(newUser);
    assertThat(Binder.getCallingUserHandle()).isEqualTo(newUser);
  }

  @Test
  public void testGetCallingUidShouldUseProcessUidByDefault() {
    assertThat(Binder.getCallingUid()).isEqualTo(android.os.Process.myUid());
  }

  @Test
  public void testGetCallingPidShouldUseProcessPidByDefault() {
    assertThat(Binder.getCallingPid()).isEqualTo(android.os.Process.myPid());
  }

  @Test
  @Config(minSdk = Q)
  public void testGetCallingUidOrThrowWithValueSet() {
    ShadowBinder.setCallingUid(123);
    assertThat(Binder.getCallingUidOrThrow()).isEqualTo(123);
  }

  @Test
  @Config(minSdk = Q)
  public void testGetCallingUidOrThrowWithValueNotSet() {
    ShadowBinder.reset();
    IllegalStateException ex =
        assertThrows(IllegalStateException.class, () -> Binder.getCallingUidOrThrow());

    // Typo in "transaction" is intentional to match platform
    assertThat(ex).hasMessageThat().isEqualTo("Thread is not in a binder transcation");
  }

  @Test
  @Config(minSdk = JELLY_BEAN_MR1)
  public void testGetCallingUserHandleShouldUseThatOfProcessByDefault() {
    assertThat(Binder.getCallingUserHandle()).isEqualTo(android.os.Process.myUserHandle());
  }

  @Test
  public void testResetUpdatesCallingUidAndPid() {
    ShadowBinder.setCallingPid(48);
    ShadowBinder.setCallingUid(49);
    ShadowBinder.reset();
    assertThat(Binder.getCallingPid()).isEqualTo(android.os.Process.myPid());
    assertThat(Binder.getCallingUid()).isEqualTo(android.os.Process.myUid());
  }

  @Test
  @Config(minSdk = Q)
  public void testResetUpdatesGetCallingUidOrThrow() {
    ShadowBinder.setCallingUid(123);
    ShadowBinder.reset();

    assertThrows(IllegalStateException.class, () -> Binder.getCallingUidOrThrow());
  }

  @Test
  @Config(minSdk = JELLY_BEAN_MR1)
  public void testResetUpdatesCallingUserHandle() {
    UserHandle newUser = shadowOf(userManager).addUser(10, "secondary_user", 0);
    ShadowBinder.setCallingUserHandle(newUser);
    ShadowBinder.reset();
    assertThat(Binder.getCallingUserHandle()).isEqualTo(android.os.Process.myUserHandle());
  }
}