ShortcutManagerTest4.java revision ee6b6e4a1861336f8db52d8c93dbf32ee9615cfa
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.pm;
17
18import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundlesEqual;
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
20import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makePersistableBundle;
23
24import android.content.Intent;
25import android.os.Bundle;
26import android.os.PersistableBundle;
27import android.test.suitebuilder.annotation.SmallTest;
28
29@SmallTest
30public class ShortcutManagerTest4 extends BaseShortcutManagerTest {
31
32    private static Bundle sIntentExtras = makeBundle(
33            "key{\u0000}", "value{\u0000}",
34            "key{\u0001}", "value{\u0001}",
35            "key{\u001f}", "value{\u001f}",
36            "key{\u007f}", "value{\u007f}",
37
38            "key{\ud800\udc00}", "value{\ud800\udc00}",
39            "key{\ud801\udc01}", "value{\ud801\udc01}",
40            "key{\udbff\udfff}", "value{\udbff\udfff}",
41
42            "key{\ud801}x", 1, // broken surrogate pair
43            "key{\uDC01}\"x", 2, // broken surrogate pair
44
45            "x1", "value{\ud801}x", // broken surrogate pair
46            "x2", "value{\uDC01}\"x" // broken surrogate pair
47    );
48
49    // Same as above, except broken surrogate pairs are replaced with '?'s.
50    private static Bundle sIntentExtrasDecoded = makeBundle(
51            "key{\u0000}", "value{\u0000}",
52            "key{\u0001}", "value{\u0001}",
53            "key{\u001f}", "value{\u001f}",
54            "key{\u007f}", "value{\u007f}",
55
56            "key{\ud800\udc00}", "value{\ud800\udc00}",
57            "key{\ud801\udc01}", "value{\ud801\udc01}",
58            "key{\udbff\udfff}", "value{\udbff\udfff}",
59
60            "key{?}x", 1,
61            "key{?}\"x", 2,
62
63            "x1", "value{?}x",
64            "x2", "value{?}\"x"
65    );
66
67    private static PersistableBundle sShortcutExtras = makePersistableBundle(
68            "key{\u0000}", "value{\u0000}",
69            "key{\u0001}", "value{\u0001}",
70            "key{\u001f}", "value{\u001f}",
71            "key{\u007f}", "value{\u007f}",
72
73            "key{\ud800\udc00}", "value{\ud800\udc00}",
74            "key{\ud801\udc01}", "value{\ud801\udc01}",
75            "key{\udbff\udfff}", "value{\udbff\udfff}",
76
77            "key{\ud801}", 1, // broken surrogate pair
78            "key{\uDC01}", 2, // broken surrogate pair
79
80            "x1", "value{\ud801}", // broken surrogate pair
81            "x2", "value{\uDC01}" // broken surrogate pair
82    );
83
84    // Same as above, except broken surrogate pairs are replaced with '?'s.
85    private static PersistableBundle sShortcutExtrasDecoded = makePersistableBundle(
86            "key{\u0000}", "value{\u0000}",
87            "key{\u0001}", "value{\u0001}",
88            "key{\u001f}", "value{\u001f}",
89            "key{\u007f}", "value{\u007f}",
90
91            "key{\ud800\udc00}", "value{\ud800\udc00}",
92            "key{\ud801\udc01}", "value{\ud801\udc01}",
93            "key{\udbff\udfff}", "value{\udbff\udfff}",
94
95            "key{?}", 1,
96            "key{?}", 2,
97
98            "x1", "value{?}",
99            "x2", "value{?}"
100    );
101
102    public void testPersistingWeirdCharacters() {
103        final Intent intent = new Intent(Intent.ACTION_VIEW)
104                .putExtras(sIntentExtras);
105
106        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
107            assertTrue(mManager.setDynamicShortcuts(list(
108                    makeShortcutWithExtras("s1", intent, sShortcutExtras),
109                    makeShortcut("s{\u0000}{\u0001}{\uD800\uDC00}x[\uD801][\uDC01]")
110            )));
111        });
112
113        // Make sure save & load works fine. (i.e. shouldn't crash even with invalid characters.)
114        initService();
115        mService.handleUnlockUser(USER_0);
116
117        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
118            assertWith(getCallerShortcuts())
119                    .haveIds("s1", "s{\u0000}{\u0001}{\uD800\uDC00}x[?][?]")
120                    .forShortcutWithId("s1", si -> {
121                        assertBundlesEqual(si.getIntent().getExtras(), sIntentExtrasDecoded);
122                        assertBundlesEqual(si.getExtras(), sShortcutExtrasDecoded);
123                    });
124        });
125    }
126}