1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "apk_validator.h"
16
17 namespace tf = tuningfork;
18
19 namespace {
20
validateApk(const char * apk_path,const std::string asset_path,bool enforce_enums_in_annotations,bool check_dev_fidelityparams,bool debug)21 int validateApk(const char *apk_path, const std::string asset_path,
22 bool enforce_enums_in_annotations,
23 bool check_dev_fidelityparams, bool debug) {
24 #undef open
25 std::shared_ptr<android::ZipFileRO> zipFile(
26 android::ZipFileRO::open(apk_path));
27 #define open ___xxx_unix_open
28 if (zipFile == nullptr) {
29 ERROR(ERROR_CANT_FIND_FILE, "Could not open " << apk_path);
30 }
31 if (debug) {
32 std::cout << "Opened " << apk_path << std::endl;
33 std::cout << "Searching under " << asset_path << std::endl;
34 }
35 tf::ApkValidator validator(zipFile, asset_path, debug);
36 return validator.Validate(enforce_enums_in_annotations,
37 check_dev_fidelityparams);
38 }
39
40 } // anonymous namespace
41
main(int argc,char ** argv)42 int main(int argc, char **argv) {
43 std::string asset_path = "assets/tuningfork/";
44 bool enforce_enums_in_annotations = true;
45 bool check_dev_fidelityparams = true;
46 bool debug = false;
47 constexpr char usage_string[] =
48 "Usage: tfvalidate [-a <asset_path_in_apk>] [-N] [-F] [-d] <apk_path>";
49 int opt;
50 optind = 1;
51 while ((opt=getopt(argc, argv, "+a:NFd")) != -1){
52 switch(opt) {
53 case 'a':
54 asset_path = optarg;
55 if ( asset_path.size() > 0 && asset_path.back() != '/' )
56 asset_path += '/';
57 break;
58 case 'N':
59 enforce_enums_in_annotations = false;
60 break;
61 case 'F':
62 check_dev_fidelityparams = false;
63 break;
64 case 'd':
65 debug = true;
66 break;
67 default:
68 ERROR(ERROR_BAD_USAGE, usage_string);
69 }
70 }
71 if (optind < argc) {
72 for (int i=optind; i<argc; ++i) {
73 int ret = validateApk(argv[i], asset_path, enforce_enums_in_annotations,
74 check_dev_fidelityparams, debug);
75 if (ret != NO_ERROR) {
76 ERROR(ret, "Error validating " << argv[i]);
77 } else {
78 std::cout << "Validated " << argv[i] << std::endl;
79 }
80 }
81 }
82 else
83 ERROR(ERROR_BAD_USAGE, usage_string);
84
85 return NO_ERROR;
86 }
87