aboutsummaryrefslogtreecommitdiffstats
path: root/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/SecurityDetectorTest.java
blob: 1c293c3dcaa515615c4841b969bd94e9fd8de8ff (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tools.lint.checks;

import com.android.tools.lint.detector.api.Detector;

@SuppressWarnings("javadoc")
public class SecurityDetectorTest extends AbstractCheckTest {
    @Override
    protected Detector getDetector() {
        return new SecurityDetector();
    }

    public void testBroken() throws Exception {
        assertEquals(
            "AndroidManifest.xml:12: Warning: Exported service does not require permission [ExportedService]\n" +
            "        <service\n" +
            "        ^\n" +
            "0 errors, 1 warnings\n" +
            "",
            lintProject(
                    "exportservice1.xml=>AndroidManifest.xml",
                    "res/values/strings.xml"));
    }

    public void testBroken2() throws Exception {
        assertEquals(
            "AndroidManifest.xml:12: Warning: Exported service does not require permission [ExportedService]\n" +
            "        <service\n" +
            "        ^\n" +
            "0 errors, 1 warnings\n" +
            "",
            lintProject(
                    "exportservice2.xml=>AndroidManifest.xml",
                    "res/values/strings.xml"));
    }

    public void testBroken3() throws Exception {
        // Not defining exported, but have intent-filters
        assertEquals(
            "AndroidManifest.xml:12: Warning: Exported service does not require permission [ExportedService]\n" +
            "        <service\n" +
            "        ^\n" +
            "0 errors, 1 warnings\n" +
            "",
            lintProject(
                    "exportservice5.xml=>AndroidManifest.xml",
                    "res/values/strings.xml"));
    }

    public void testOk1() throws Exception {
        // Defines a permission on the <service> element
        assertEquals(
            "No warnings.",
            lintProject(
                    "exportservice3.xml=>AndroidManifest.xml",
                    "res/values/strings.xml"));
    }

    public void testOk2() throws Exception {
        // Defines a permission on the parent <application> element
        assertEquals(
            "No warnings.",
            lintProject(
                    "exportservice4.xml=>AndroidManifest.xml",
                    "res/values/strings.xml"));
    }

    public void testUri() throws Exception {
        assertEquals(
            "AndroidManifest.xml:25: Warning: Content provider shares everything; this is potentially dangerous. [GrantAllUris]\n" +
            "        <grant-uri-permission android:path=\"/\"/>\n" +
            "                              ~~~~~~~~~~~~~~~~\n" +
            "AndroidManifest.xml:26: Warning: Content provider shares everything; this is potentially dangerous. [GrantAllUris]\n" +
            "        <grant-uri-permission android:pathPrefix=\"/\"/>\n" +
            "                              ~~~~~~~~~~~~~~~~~~~~~~\n" +
            "0 errors, 2 warnings\n" +
            "",

            lintProject(
                    "grantpermission.xml=>AndroidManifest.xml",
                    "res/values/strings.xml"));
    }

    // exportprovider1.xml has two exported content providers with no permissions
    public void testContentProvider1() throws Exception {
        assertEquals(
            "AndroidManifest.xml:14: Warning: Exported content providers can provide access to potentially sensitive data [ExportedContentProvider]\n" +
            "        <provider\n" +
            "        ^\n" +
            "AndroidManifest.xml:20: Warning: Exported content providers can provide access to potentially sensitive data [ExportedContentProvider]\n" +
            "        <provider\n" +
            "        ^\n" +
            "0 errors, 2 warnings\n" +
            "",
             lintProject(
                    "exportprovider1.xml=>AndroidManifest.xml",
                    "res/values/strings.xml"));
    }

    // exportprovider2.xml has no un-permissioned exported content providers
    public void testContentProvider2() throws Exception {
        assertEquals(
                "No warnings.",
                lintProject(
                        "exportprovider2.xml=>AndroidManifest.xml",
                        "res/values/strings.xml"));
    }

    public void testWorldWriteable() throws Exception {
        assertEquals(
            "src/test/pkg/WorldWriteableFile.java:26: Warning: Using MODE_WORLD_READABLE when creating files can be risky, review carefully [WorldReadableFiles]\n" +
            "            out = openFileOutput(mFile.getName(), MODE_WORLD_READABLE);\n" +
            "                                                  ~~~~~~~~~~~~~~~~~~~\n" +
            "src/test/pkg/WorldWriteableFile.java:31: Warning: Using MODE_WORLD_READABLE when creating files can be risky, review carefully [WorldReadableFiles]\n" +
            "            prefs = getSharedPreferences(mContext, MODE_WORLD_READABLE);\n" +
            "                                                   ~~~~~~~~~~~~~~~~~~~\n" +
            "src/test/pkg/WorldWriteableFile.java:25: Warning: Using MODE_WORLD_WRITEABLE when creating files can be risky, review carefully [WorldWriteableFiles]\n" +
            "            out = openFileOutput(mFile.getName(), MODE_WORLD_WRITEABLE);\n" +
            "                                                  ~~~~~~~~~~~~~~~~~~~~\n" +
            "src/test/pkg/WorldWriteableFile.java:30: Warning: Using MODE_WORLD_WRITEABLE when creating files can be risky, review carefully [WorldWriteableFiles]\n" +
            "            prefs = getSharedPreferences(mContext, MODE_WORLD_WRITEABLE);\n" +
            "                                                   ~~~~~~~~~~~~~~~~~~~~\n" +
            "0 errors, 4 warnings\n" +
            "",

            lintProject(
                // Java files must be renamed in source tree
                "src/test/pkg/WorldWriteableFile.java.txt=>src/test/pkg/WorldWriteableFile.java"));
    }

    public void testActivity0() throws Exception {
        // Activities that do not have intent-filters do not need warnings
        assertEquals(
            "No warnings.",
            lintProject(
                "exportactivity0.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testActivity1() throws Exception {
        assertEquals(
            "AndroidManifest.xml:12: Warning: Exported activity does not require permission [ExportedActivity]\n" +
            "        <activity\n" +
            "        ^\n" +
            "0 errors, 1 warnings\n" +
            "",
            lintProject(
                "exportactivity1.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testActivity2() throws Exception {
        // Defines a permission on the <activity> element
        assertEquals(
            "No warnings.",
            lintProject(
                "exportactivity2.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testActivity3() throws Exception {
        // Defines a permission on the parent <application> element
        assertEquals(
            "No warnings.",
            lintProject(
                "exportactivity3.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testActivity4() throws Exception {
        // Not defining exported, but have intent-filters
        assertEquals(
            "AndroidManifest.xml:12: Warning: Exported activity does not require permission [ExportedActivity]\n" +
            "        <activity\n" +
            "        ^\n" +
            "0 errors, 1 warnings\n" +
            "",
            lintProject(
                "exportactivity4.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testReceiver0() throws Exception {
        // Activities that do not have intent-filters do not need warnings
        assertEquals(
            "No warnings.",
            lintProject(
                "exportreceiver0.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testReceiver1() throws Exception {
        assertEquals(
            "AndroidManifest.xml:12: Warning: Exported receiver does not require permission [ExportedReceiver]\n" +
            "        <receiver\n" +
            "        ^\n" +
            "0 errors, 1 warnings\n" +
            "",
            lintProject(
                "exportreceiver1.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testReceiver2() throws Exception {
        // Defines a permission on the <activity> element
        assertEquals(
            "No warnings.",
            lintProject(
                "exportreceiver2.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testReceiver3() throws Exception {
        // Defines a permission on the parent <application> element
        assertEquals(
            "No warnings.",
            lintProject(
                "exportreceiver3.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testReceiver4() throws Exception {
        // Not defining exported, but have intent-filters
        assertEquals(
            "AndroidManifest.xml:12: Warning: Exported receiver does not require permission [ExportedReceiver]\n" +
            "        <receiver\n" +
            "        ^\n" +
            "0 errors, 1 warnings\n" +
            "",
            lintProject(
                "exportreceiver4.xml=>AndroidManifest.xml",
                "res/values/strings.xml"));
    }

    public void testReceiver5() throws Exception {
      // Intent filter for standard Android action
      assertEquals(
          "No warnings.",
          lintProject(
              "exportreceiver5.xml=>AndroidManifest.xml",
              "res/values/strings.xml"));
    }

    public void testStandard() throws Exception {
        // Various regression tests for http://code.google.com/p/android/issues/detail?id=33976
        assertEquals(
            "No warnings.",
            lintProject("exportreceiver6.xml=>AndroidManifest.xml"));
    }
}