blob: 7c95e791ab43f3357fd2c7642c4f8a1d6dae6366 [file] [log] [blame]
Keun Soo Yimff902242016-03-03 16:23:38 -08001/*
2 * Copyright 2016 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/*
18 * Example usage:
19 * $ fuzzer --class=hal --type=light --version=1.0 /system/lib/hw/lights.gce_x86.so
20 * $ fuzzer --class=hal --type=gps --version=1.0 /system/lib/hw/gps.gce_x86.so
21 * $ fuzzer --class=hal --type=camera --version=1.0 /system/lib/hw/camera.gce_x86.so
22 */
23
24#include <getopt.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#include <algorithm>
31#include <string>
32#include <iostream>
33
34#include "specification_parser/InterfaceSpecificationParser.h"
35#include "specification_parser/SpecificationBuilder.h"
36
37using namespace std;
38using namespace android;
39
40#define INTERFACE_SPEC_LIB_FILENAME "libvts_interfacespecification.so"
41
42
43// Dumps usage on stderr.
44static void usage() {
45 fprintf(
46 stderr,
47 "Usage: fuzzer [options] <target HAL file path>\n"
48 "\n"
49 "Android fuzzer v0.1. To fuzz Android system.\n"
50 "\n"
51 "Options:\n"
52 "--help\n"
53 " Show this message.\n"
54 "\n"
55 "Recording continues until Ctrl-C is hit or the time limit is reached.\n"
56 "\n");
57}
58
59
60// Parses command args and kicks things off.
61int main(int argc, char* const argv[]) {
62 static const struct option longOptions[] = {
63 {"help", no_argument, NULL, 'h'},
64 {"class", required_argument, NULL, 'c'},
65 {"type", required_argument, NULL, 't'},
66 {"version", required_argument, NULL, 'v'},
67 {"spec_dir", required_argument, NULL, 's'},
68 {NULL, 0, NULL, 0}};
69 int target_class;
70 int target_type;
71 float target_version = 1.0;
72 string spec_dir_path(DEFAULT_SPEC_DIR_PATH);
73
74 while (true) {
75 int optionIndex = 0;
76 int ic = getopt_long(argc, argv, "", longOptions, &optionIndex);
77 if (ic == -1) {
78 break;
79 }
80
81 switch (ic) {
82 case 'h':
83 usage();
84 return 0;
85 case 'c': {
86 string target_class_str = string(optarg);
87 transform(target_class_str.begin(), target_class_str.end(),
88 target_class_str.begin(), ::tolower);
89 if (!strcmp(target_class_str.c_str(), "hal")) {
90 target_class = vts::HAL;
91 } else {
92 target_class = 0;
93 }
94 break;
95 }
96 case 't': {
97 string target_type_str = string(optarg);
98 transform(target_type_str.begin(), target_type_str.end(),
99 target_type_str.begin(), ::tolower);
100 if (!strcmp(target_type_str.c_str(), "camera")) {
101 target_type = vts::CAMERA;
102 } else if (!strcmp(target_type_str.c_str(), "gps")) {
103 target_type = vts::GPS;
104 } else if (!strcmp(target_type_str.c_str(), "audio")) {
105 target_type = vts::AUDIO;
106 } else if (!strcmp(target_type_str.c_str(), "light")) {
107 target_type = vts::LIGHT;
108 } else {
109 target_type = 0;
110 }
111 break;
112 }
113 case 'v':
114 target_version = atof(optarg);
115 break;
116 case 's':
117 spec_dir_path = string(optarg);
118 break;
119 default:
120 if (ic != '?') {
121 fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
122 }
123 return 2;
124 }
125 }
126
127 if (optind != argc - 1) {
128 fprintf(stderr, "Must specify output file (see --help).\n");
129 return 2;
130 }
131
132 android::vts::SpecificationBuilder spec_builder(spec_dir_path);
133 cout << "Result: "
134 << spec_builder.Process(argv[optind],
135 INTERFACE_SPEC_LIB_FILENAME,
136 target_class,
137 target_type,
138 target_version) << endl;
139 return 0;
140}