1 /**
2  * Copyright (C) 2020 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 
17 package android.scopedstorage.cts.lib;
18 
19 import static androidx.test.InstrumentationRegistry.getContext;
20 
21 import static org.junit.Assert.fail;
22 
23 import android.media.ExifInterface;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.RawRes;
27 
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.HashMap;
32 import java.util.Objects;
33 
34 /**
35  * Helper functions and utils for redactions tests
36  */
37 public class RedactionTestHelper {
38     private static final String TAG = "RedactionTestHelper";
39 
40     private static final String[] EXIF_GPS_TAGS = {
41             ExifInterface.TAG_GPS_ALTITUDE,
42             ExifInterface.TAG_GPS_DOP,
43             ExifInterface.TAG_GPS_DATESTAMP,
44             ExifInterface.TAG_GPS_LATITUDE,
45             ExifInterface.TAG_GPS_LATITUDE_REF,
46             ExifInterface.TAG_GPS_LONGITUDE,
47             ExifInterface.TAG_GPS_LONGITUDE_REF,
48             ExifInterface.TAG_GPS_PROCESSING_METHOD,
49             ExifInterface.TAG_GPS_TIMESTAMP,
50             ExifInterface.TAG_GPS_VERSION_ID,
51     };
52 
53     public static final String EXIF_METADATA_QUERY = "android.scopedstorage.cts.exif";
54 
55     /**
56      * Retrieve the EXIF metadata from the given file.
57      */
58     @NonNull
getExifMetadata(@onNull File file)59     public static HashMap<String, String> getExifMetadata(@NonNull File file) throws IOException {
60         final ExifInterface exif = new ExifInterface(file);
61         return dumpExifGpsTagsToMap(exif);
62     }
63 
64     /**
65      * Retrieve the EXIF metadata from the given resource.
66      */
67     @NonNull
getExifMetadataFromRawResource(@awRes int resId)68     public static HashMap<String, String> getExifMetadataFromRawResource(@RawRes int resId)
69             throws IOException {
70         final ExifInterface exif;
71         try (InputStream in = getContext().getResources().openRawResource(resId)) {
72             exif = new ExifInterface(in);
73         }
74         return dumpExifGpsTagsToMap(exif);
75     }
76 
77     /**
78      * Asserts the 2 given EXIF maps have the same content.
79      */
assertExifMetadataMatch( @onNull HashMap<String, String> actual, @NonNull HashMap<String, String> expected)80     public static void assertExifMetadataMatch(
81             @NonNull HashMap<String, String> actual, @NonNull HashMap<String, String> expected) {
82         for (String tag : EXIF_GPS_TAGS) {
83             assertMetadataEntryMatch(tag, actual.get(tag), expected.get(tag));
84         }
85     }
86 
87     /**
88      * Asserts the 2 given EXIF maps don't have the same content.
89      */
assertExifMetadataMismatch( @onNull HashMap<String, String> actual, @NonNull HashMap<String, String> expected)90     public static void assertExifMetadataMismatch(
91             @NonNull HashMap<String, String> actual, @NonNull HashMap<String, String> expected) {
92         for (String tag : EXIF_GPS_TAGS) {
93             assertMetadataEntryMismatch(tag, actual.get(tag), expected.get(tag));
94         }
95     }
96 
assertMetadataEntryMatch(String tag, String actual, String expected)97     private static void assertMetadataEntryMatch(String tag, String actual, String expected) {
98         if (!Objects.equals(actual, expected)) {
99             fail("Unexpected metadata mismatch for tag: " + tag + "\n"
100                     + "expected:" + expected + "\n"
101                     + "but was: " + actual);
102         }
103     }
104 
assertMetadataEntryMismatch(String tag, String actual, String expected)105     private static void assertMetadataEntryMismatch(String tag, String actual, String expected) {
106         if (Objects.equals(actual, expected)) {
107             fail("Unexpected metadata match for tag: " + tag + "\n"
108                     + "expected not to be:" + expected);
109         }
110     }
111 
dumpExifGpsTagsToMap(ExifInterface exif)112     private static HashMap<String, String> dumpExifGpsTagsToMap(ExifInterface exif) {
113         final HashMap<String, String> res = new HashMap<>();
114         for (String tag : EXIF_GPS_TAGS) {
115             res.put(tag, exif.getAttribute(tag));
116         }
117         return res;
118     }
119 }
120