summaryrefslogtreecommitdiff
path: root/hostsidetests/scopedstorage/device/src/android/scopedstorage/cts/device/RedactUriDeviceTest.java
blob: 8e653a1f14f56115fd48d738d67e42947f77e794 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * Copyright (C) 2021 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 android.scopedstorage.cts.device;


import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
import static android.database.Cursor.FIELD_TYPE_BLOB;
import static android.scopedstorage.cts.lib.TestUtils.assertThrows;
import static android.scopedstorage.cts.lib.TestUtils.canOpenRedactedUriForWrite;
import static android.scopedstorage.cts.lib.TestUtils.canQueryOnUri;
import static android.scopedstorage.cts.lib.TestUtils.checkPermission;
import static android.scopedstorage.cts.lib.TestUtils.forceStopApp;
import static android.scopedstorage.cts.lib.TestUtils.getContentResolver;
import static android.scopedstorage.cts.lib.TestUtils.grantPermission;
import static android.scopedstorage.cts.lib.TestUtils.installAppWithStoragePermissions;
import static android.scopedstorage.cts.lib.TestUtils.isFileDescriptorRedacted;
import static android.scopedstorage.cts.lib.TestUtils.isFileOpenRedacted;
import static android.scopedstorage.cts.lib.TestUtils.setShouldForceStopTestApp;
import static android.scopedstorage.cts.lib.TestUtils.uninstallAppNoThrow;

import static androidx.test.InstrumentationRegistry.getContext;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileUtils;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.system.Os;

import androidx.test.filters.SdkSuppress;

import com.android.cts.install.lib.TestApp;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Device-side test suite to verify redacted URI operations.
 */
@RunWith(Parameterized.class)
@SdkSuppress(minSdkVersion = 31, codeName = "S")
public class RedactUriDeviceTest extends ScopedStorageBaseDeviceTest {

    /**
     * To help avoid flaky tests, give ourselves a unique nonce to be used for
     * all filesystem paths, so that we don't risk conflicting with previous
     * test runs.
     */
    static final String NONCE = String.valueOf(System.nanoTime());

    static final String IMAGE_FILE_NAME = "ScopedStorageDeviceTest_file_" + NONCE + ".jpg";

    static final String FUZZER_HEIC_FILE_NAME =
            "ScopedStorageDeviceTest_file_fuzzer_" + NONCE + ".heic";

    // An app with no permissions
    private static final TestApp APP_B_NO_PERMS = new TestApp("TestAppB",
            "android.scopedstorage.cts.testapp.B.noperms", 1, false,
            "CtsScopedStorageTestAppB.apk");

    // The following apps are not installed at test startup - please install before using.
    private static final TestApp APP_C = new TestApp("TestAppC",
            "android.scopedstorage.cts.testapp.C", 1, false, "CtsScopedStorageTestAppC.apk");

    @Parameterized.Parameter(0)
    public String mVolumeName;

    /** Parameters data. */
    @Parameterized.Parameters(name = "volume={0}")
    public static Iterable<? extends Object> data() {
        return getTestParameters();
    }

    @BeforeClass
    public static void setupApps() {
        // Installed by target preparer
        assertThat(checkPermission(APP_B_NO_PERMS,
                Manifest.permission.READ_EXTERNAL_STORAGE)).isFalse();
        setShouldForceStopTestApp(false);
    }

    @AfterClass
    public static void destroy() {
        setShouldForceStopTestApp(true);
    }

    @Test
    public void testRedactedUri_single() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);

        try {
            final Uri uri = MediaStore.scanFile(getContentResolver(), img);
            final Uri redactedUri = MediaStore.getRedactedUri(getContentResolver(), uri);
            testRedactedUriCommon(uri, redactedUri);
        } finally {
            img.delete();
        }
    }

    @Test
    public void testRedactedUri_list() throws Exception {
        List<Uri> uris = new ArrayList<>();
        List<File> files = new ArrayList<>();

        try {
            for (int i = 0; i < 10; i++) {
                File file = stageImageFileWithMetadata("img_metadata" + String.valueOf(
                        System.nanoTime()) + i + ".jpg");
                files.add(file);
                uris.add(MediaStore.scanFile(getContentResolver(), file));
            }

            final Collection<Uri> redactedUris = MediaStore.getRedactedUri(getContentResolver(),
                    uris);
            int i = 0;
            for (Uri redactedUri : redactedUris) {
                Uri uri = uris.get(i++);
                testRedactedUriCommon(uri, redactedUri);
            }
        } finally {
            files.forEach(file -> file.delete());
        }
    }

    @Test
    public void testQueryOnRedactionUri() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        final Uri uri = MediaStore.scanFile(getContentResolver(), img);
        final Uri redactedUri = MediaStore.getRedactedUri(getContentResolver(), uri);
        final Cursor uriCursor = getContentResolver().query(uri, null, null, null);
        final String redactedUriDir = ".transforms/synthetic/redacted";
        final String redactedUriDirAbsolutePath =
                Environment.getExternalStorageDirectory() + "/" + redactedUriDir;
        try {
            assertNotNull(uriCursor);
            assertThat(uriCursor.moveToFirst()).isTrue();

            final Cursor redactedUriCursor = getContentResolver().query(redactedUri, null, null,
                    null);
            assertNotNull(redactedUriCursor);
            assertThat(redactedUriCursor.moveToFirst()).isTrue();

            assertEquals(redactedUriCursor.getColumnCount(), uriCursor.getColumnCount());

            final String data = getStringFromCursor(redactedUriCursor,
                    MediaStore.MediaColumns.DATA);
            final String redactedUriDisplayName = redactedUri.getLastPathSegment() + ".jpg";
            assertEquals(redactedUriDirAbsolutePath + "/" + redactedUriDisplayName, data);

            final String name = getStringFromCursor(redactedUriCursor,
                    MediaStore.MediaColumns.DISPLAY_NAME);
            assertEquals(redactedUriDisplayName, name);

            final String relativePath = getStringFromCursor(redactedUriCursor,
                    MediaStore.MediaColumns.RELATIVE_PATH);
            assertEquals(redactedUriDir, relativePath);

            final String bucketDisplayName = getStringFromCursor(redactedUriCursor,
                    MediaStore.MediaColumns.BUCKET_DISPLAY_NAME);
            assertEquals(redactedUriDir, bucketDisplayName);

            final String docId = getStringFromCursor(redactedUriCursor,
                    MediaStore.MediaColumns.DOCUMENT_ID);
            assertNull(docId);

            final String insId = getStringFromCursor(redactedUriCursor,
                    MediaStore.MediaColumns.INSTANCE_ID);
            assertNull(insId);

            final String bucId = getStringFromCursor(redactedUriCursor,
                    MediaStore.MediaColumns.BUCKET_ID);
            assertNull(bucId);

            final Collection<String> updatedCols = Arrays.asList(MediaStore.MediaColumns._ID,
                    MediaStore.MediaColumns.DISPLAY_NAME,
                    MediaStore.MediaColumns.RELATIVE_PATH,
                    MediaStore.MediaColumns.BUCKET_DISPLAY_NAME,
                    MediaStore.MediaColumns.DATA,
                    MediaStore.MediaColumns.DOCUMENT_ID,
                    MediaStore.MediaColumns.INSTANCE_ID,
                    MediaStore.MediaColumns.BUCKET_ID);
            for (String colName : uriCursor.getColumnNames()) {
                if (!updatedCols.contains(colName)) {
                    if (uriCursor.getType(uriCursor.getColumnIndex(colName)) == FIELD_TYPE_BLOB) {
                        assertThat(
                                Arrays.equals(uriCursor.getBlob(uriCursor.getColumnIndex(colName)),
                                        redactedUriCursor.getBlob(redactedUriCursor.getColumnIndex(
                                                colName)))).isTrue();
                    } else {
                        assertEquals(getStringFromCursor(uriCursor, colName),
                                getStringFromCursor(redactedUriCursor, colName));
                    }
                }
            }
        } finally {
            img.delete();
        }
    }

    /*
     * Verify that app can't open the shared redacted URI for write.
     **/
    @Test
    public void testSharedRedactedUri_openFdForWrite() throws Exception {
        forceStopApp(APP_B_NO_PERMS.getPackageName());
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            Uri redactedUri = shareAndGetRedactedUri(img, APP_B_NO_PERMS);
            assertThrows(UnsupportedOperationException.class,
                    () -> canOpenRedactedUriForWrite(APP_B_NO_PERMS, redactedUri));
        } finally {
            img.delete();
        }
    }

    /*
     * Verify that app with correct permission can open the shared redacted URI for read in
     * redacted mode.
     **/
    @Test
    public void testSharedRedactedUri_openFdForRead() throws Exception {
        forceStopApp(APP_B_NO_PERMS.getPackageName());
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            final Uri redactedUri = shareAndGetRedactedUri(img, APP_B_NO_PERMS);
            assertThat(isFileDescriptorRedacted(APP_B_NO_PERMS, redactedUri)).isTrue();
        } finally {
            img.delete();
        }
    }

    /*
     * Verify that app with correct permission can open the shared redacted URI for read in
     * redacted mode.
     **/
    @Test
    public void testSharedRedactedUri_openFileForRead() throws Exception {
        forceStopApp(APP_B_NO_PERMS.getPackageName());
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            Uri redactedUri = shareAndGetRedactedUri(img, APP_B_NO_PERMS);
            assertThat(isFileOpenRedacted(APP_B_NO_PERMS, redactedUri)).isTrue();
        } finally {
            img.delete();
        }
    }

    /*
     * Verify that the app with redacted URI granted can query it.
     **/
    @Test
    public void testSharedRedactedUri_query() throws Exception {
        forceStopApp(APP_B_NO_PERMS.getPackageName());
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            Uri redactedUri = shareAndGetRedactedUri(img, APP_B_NO_PERMS);
            assertThat(canQueryOnUri(APP_B_NO_PERMS, redactedUri)).isTrue();
        } finally {
            img.delete();
        }
    }

    /*
     * Verify that for app with AML permission shared redacted URI opens for read in redacted mode.
     **/
    @Test
    public void testSharedRedactedUri_openFileForRead_withLocationPerm() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            // Install test app
            installAppWithStoragePermissions(APP_C);
            grantPermission(APP_C.getPackageName(), Manifest.permission.ACCESS_MEDIA_LOCATION);

            Uri redactedUri = shareAndGetRedactedUri(img, APP_C);
            assertThat(isFileOpenRedacted(APP_C, redactedUri)).isTrue();
        } finally {
            img.delete();
            uninstallAppNoThrow(APP_C);
        }
    }

    /*
     * Verify that for app with AML permission shared redacted URI opens for read in redacted mode.
     **/
    @Test
    public void testSharedRedactedUri_openFdForRead_withLocationPerm() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            // Install test app
            installAppWithStoragePermissions(APP_C);
            grantPermission(APP_C.getPackageName(), Manifest.permission.ACCESS_MEDIA_LOCATION);

            Uri redactedUri = shareAndGetRedactedUri(img, APP_C);
            assertThat(isFileDescriptorRedacted(APP_C, redactedUri)).isTrue();
        } finally {
            img.delete();
            uninstallAppNoThrow(APP_C);
        }
    }

    /*
     * Verify that the test app can't access unshared redacted uri via file descriptor
     **/
    @Test
    public void testUnsharedRedactedUri_openFdForRead() throws Exception {
        forceStopApp(APP_B_NO_PERMS.getPackageName());
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            // Install test app
            installAppWithStoragePermissions(APP_C);

            final Uri redactedUri = getRedactedUri(img);
            // APP_C has R_E_S, so should have access to redactedUri
            assertThat(isFileDescriptorRedacted(APP_C, redactedUri)).isTrue();
            assertThrows(SecurityException.class,
                    () -> isFileDescriptorRedacted(APP_B_NO_PERMS, redactedUri));
        } finally {
            img.delete();
            uninstallAppNoThrow(APP_C);
        }
    }

    /*
     * Verify that the test app can't access unshared redacted uri via file path
     **/
    @Test
    public void testUnsharedRedactedUri_openFileForRead() throws Exception {
        forceStopApp(APP_B_NO_PERMS.getPackageName());
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        try {
            // Install test app
            installAppWithStoragePermissions(APP_C);

            final Uri redactedUri = getRedactedUri(img);
            // APP_C has R_E_S
            assertThat(isFileOpenRedacted(APP_C, redactedUri)).isTrue();
            assertThrows(IOException.class, () -> isFileOpenRedacted(APP_B_NO_PERMS, redactedUri));
        } finally {
            img.delete();
            uninstallAppNoThrow(APP_C);
        }
    }

    @Test
    public void testGrantUriPermissionsForRedactedUri() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        final Uri redactedUri = getRedactedUri(img);
        try {
            getContext().grantUriPermission(APP_B_NO_PERMS.getPackageName(), redactedUri,
                    FLAG_GRANT_READ_URI_PERMISSION);
            assertThrows(SecurityException.class, () ->
                    getContext().grantUriPermission(APP_B_NO_PERMS.getPackageName(), redactedUri,
                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
        } finally {
            img.delete();
        }
    }

    @Test
    public void testDisallowedOperationsOnRedactedUri() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        final Uri redactedUri = getRedactedUri(img);
        try {
            ContentValues cv = new ContentValues();
            cv.put(MediaStore.MediaColumns.DATE_ADDED, 1);
            assertEquals(0, getContentResolver().update(redactedUri, new ContentValues(),
                    new Bundle()));
            assertEquals(0, getContentResolver().delete(redactedUri, new Bundle()));
        } finally {
            img.delete();
        }
    }

    @Test
    public void testOpenOnRedactedUri_file() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        final Uri redactedUri = getRedactedUri(img);
        try {
            assertUriIsUnredacted(img);

            final Cursor redactedUriCursor = getRedactedCursor(redactedUri);
            File file = new File(
                    getStringFromCursor(redactedUriCursor, MediaStore.MediaColumns.DATA));
            ExifInterface redactedExifInf = new ExifInterface(file);
            assertUriIsRedacted(redactedExifInf);

            assertThrows(FileNotFoundException.class, () -> new FileOutputStream(file));
        } finally {
            img.delete();
        }
    }

    @Test
    public void testOpenOnRedactedUri_write() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        final Uri redactedUri = getRedactedUri(img);
        try {
            assertThrows(UnsupportedOperationException.class,
                    () -> getContentResolver().openFileDescriptor(redactedUri,
                            "w"));
        } finally {
            img.delete();
        }
    }

    @Test
    public void testOpenOnRedactedUri_inputstream() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        final Uri redactedUri = getRedactedUri(img);
        try {
            assertUriIsUnredacted(img);

            try (InputStream is = getContentResolver().openInputStream(redactedUri)) {
                ExifInterface redactedExifInf = new ExifInterface(is);
                assertUriIsRedacted(redactedExifInf);
            }
        } finally {
            img.delete();
        }
    }

    @Test
    public void testOpenOnRedactedUri_read() throws Exception {
        final File img = stageImageFileWithMetadata(IMAGE_FILE_NAME);
        final Uri redactedUri = getRedactedUri(img);
        try {
            assertUriIsUnredacted(img);

            try (ParcelFileDescriptor pfd =
                         getContentResolver().openFileDescriptor(redactedUri, "r")) {
                FileDescriptor fd = pfd.getFileDescriptor();
                ExifInterface redactedExifInf = new ExifInterface(fd);
                assertUriIsRedacted(redactedExifInf);
            }
        } finally {
            img.delete();
        }
    }

    @Test
    public void testOpenOnRedactedUri_readFuzzer() throws Exception {
        final File img = stageFuzzerImageFileWithMetadata(FUZZER_HEIC_FILE_NAME);
        final Uri redactedUri = getRedactedUri(img);
        try {
            assertUriIsUnredacted(img);

            try (ParcelFileDescriptor pfd =
                         getContentResolver().openFileDescriptor(redactedUri, "r")) {
                FileDescriptor fd = pfd.getFileDescriptor();
                int bufSize = 0x1000000;
                byte[] data = new byte[bufSize];
                int fileSize = Os.read(fd, data, 0, bufSize);
                assertUriIsRedacted(data, fileSize);
            }
        } finally {
            img.delete();
        }
    }

    private void testRedactedUriCommon(Uri uri, Uri redactedUri) {
        assertEquals(redactedUri.getAuthority(), uri.getAuthority());
        assertEquals(redactedUri.getScheme(), uri.getScheme());
        assertNotEquals(redactedUri.getPath(), uri.getPath());
        assertNotEquals(redactedUri.getPathSegments(), uri.getPathSegments());

        final String uriId = redactedUri.getLastPathSegment();
        assertThat(uriId.startsWith("RUID")).isTrue();
        assertEquals(uriId.length(), 36);
    }

    private Uri shareAndGetRedactedUri(File file, TestApp testApp) {
        final Uri redactedUri = getRedactedUri(file);
        getContext().grantUriPermission(testApp.getPackageName(), redactedUri,
                FLAG_GRANT_READ_URI_PERMISSION);

        return redactedUri;
    }

    private Uri getRedactedUri(File file) {
        final Uri uri = MediaStore.scanFile(getContentResolver(), file);
        return MediaStore.getRedactedUri(getContentResolver(), uri);
    }

    private void assertUriIsUnredacted(File img) throws Exception {
        final ExifInterface exifInterface = new ExifInterface(img);
        assertNotEquals(exifInterface.getGpsDateTime(), -1);

        float[] latLong = new float[]{0, 0};
        exifInterface.getLatLong(latLong);
        assertNotEquals(latLong[0], 0);
        assertNotEquals(latLong[1], 0);
    }

    private void assertUriIsRedacted(ExifInterface redactedExifInf) {
        assertEquals(redactedExifInf.getGpsDateTime(), -1);
        float[] latLong = new float[]{0, 0};
        redactedExifInf.getLatLong(latLong);
        assertEquals(latLong[0], 0.0, 0.0);
        assertEquals(latLong[1], 0.0, 0.0);
    }

    private void assertUriIsRedacted(byte[] data, int fileSize) {
        // Data in redaction ranges should be zero.
        int[] start = new int[]{50538, 712941, 712965, 712989, 713033, 713101};
        int[] end = new int[]{711958, 712943, 712967, 712990, 713100, 713125};

        assertTrue(fileSize == 4407744);
        for (int index = 0; index < start.length && index < end.length; index++) {
            for (int c = start[index]; c < end[index]; c++) {
                assertTrue("It should be zero!", data[c] == (byte) 0);
            }
        }
    }

    private Cursor getRedactedCursor(Uri redactedUri) {
        Cursor redactedUriCursor = getContentResolver().query(redactedUri, null, null, null);
        assertNotNull(redactedUriCursor);
        assertThat(redactedUriCursor.moveToFirst()).isTrue();

        return redactedUriCursor;
    }

    private String getStringFromCursor(Cursor c, String colName) {
        return c.getString(c.getColumnIndex(colName));
    }

    private File stageImageFileWithMetadata(String name) throws Exception {
        return stageImageFileWithMetadata(name, R.raw.img_with_metadata);
    }

    private File stageFuzzerImageFileWithMetadata(String name) throws Exception {
        return stageImageFileWithMetadata(name, R.raw.fuzzer);
    }

    private File stageImageFileWithMetadata(String name, int sourceId) throws Exception {
        final File img = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), name);

        try (InputStream in =
                     getContext().getResources().openRawResource(sourceId);
             OutputStream out = new FileOutputStream(img)) {
            // Dump the image we have to external storage
            FileUtils.copy(in, out);
        }

        return img;
    }
}