1 /*
2  * Copyright (C) 2019 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 com.android.preload.check;
18 
19 import dalvik.system.DexFile;
20 
21 import java.util.Collection;
22 import java.util.Enumeration;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 
26 /**
27  * Test boot classpath classes that satisfy a given regular expression to be not initialized.
28  * Optionally check that at least one class was matched.
29  */
30 public class NotInitializedRegex {
31     /**
32      * First arg (mandatory): regular exception. Second arg (optional): boolean to denote a
33      * required match.
34      */
main(String[] args)35     public static void main(String[] args) throws Exception {
36         Matcher m = Pattern.compile(args[0]).matcher("");
37         boolean requiresMatch = args.length > 1 ? Boolean.parseBoolean(args[1]) : false;
38 
39         Collection<DexFile> dexFiles = Util.getBootDexFiles();
40         int matched = 0, notMatched = 0;
41         for (DexFile dexFile : dexFiles) {
42             Enumeration<String> entries = dexFile.entries();
43             while (entries.hasMoreElements()) {
44                 String entry = entries.nextElement();
45                 m.reset(entry);
46                 if (m.matches()) {
47                     System.out.println(entry + ": match");
48                     matched++;
49                     check(entry);
50                 } else {
51                     System.out.println(entry + ": no match");
52                     notMatched++;
53                 }
54             }
55         }
56         System.out.println("Matched: " + matched + " Not-Matched: " + notMatched);
57         if (requiresMatch && matched == 0) {
58             throw new RuntimeException("Did not find match");
59         }
60         System.out.println("OK");
61     }
62 
check(String name)63     private static void check(String name) {
64         Util.assertNotInitialized(name, null);
65     }
66 }
67