blob: cc323d6acfdbc615711e8fe9c075afd8bc27baa3 [file] [log] [blame]
Keun Soo Yim08400372016-03-03 13:28:51 -08001/*
2 * Copyright (C) 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#include "specification_parser/SpecificationBuilder.h"
18
19#include <dirent.h>
20
21#include <iostream>
22#include <string>
23
24#include "fuzz_tester/FuzzerBase.h"
25#include "fuzz_tester/FuzzerWrapper.h"
26#include "specification_parser/InterfaceSpecificationParser.h"
27
28#include "test/vts/sysfuzzer/common/proto/InterfaceSpecificationMessage.pb.h"
29
30namespace android {
31namespace vts {
32
Keun Soo Yim8103c912016-04-22 20:07:13 -070033SpecificationBuilder::SpecificationBuilder(
34 const string dir_path, int epoch_count)
35 : dir_path_(dir_path),
36 epoch_count_(epoch_count) {}
Keun Soo Yim08400372016-03-03 13:28:51 -080037
38
39vts::InterfaceSpecificationMessage*
40SpecificationBuilder::FindInterfaceSpecification(
41 const int target_class,
42 const int target_type,
43 const float target_version) {
44 DIR* dir;
45 struct dirent* ent;
46
47 if (!(dir = opendir(dir_path_.c_str()))) {
48 cerr << __FUNCTION__ << ": Can't opendir " << dir_path_ << endl;
49 return NULL;
50 }
51
52 while ((ent = readdir(dir))) {
53 if (string(ent->d_name).find(SPEC_FILE_EXT) != std::string::npos) {
54 cout << __FUNCTION__ << ": Checking a file " << ent->d_name << endl;
55 const string file_path = string(dir_path_) + "/" + string(ent->d_name);
56 vts::InterfaceSpecificationMessage* message =
57 new vts::InterfaceSpecificationMessage();
58 if (InterfaceSpecificationParser::parse(file_path.c_str(), message)) {
59 if (message->component_class() == target_class
60 && message->component_type() == target_type
61 && message->component_type_version() == target_version) {
62 closedir(dir);
63 return message;
64 }
65 }
66 delete message;
67 }
68 }
69 closedir(dir);
70 return NULL;
71}
72
73
74bool SpecificationBuilder::Process(
75 const char* dll_file_name,
76 const char* spec_lib_file_path,
77 int target_class,
78 int target_type,
79 float target_version) {
80 vts::InterfaceSpecificationMessage* interface_specification_message =
81 FindInterfaceSpecification(target_class, target_type, target_version);
82 if (!interface_specification_message) {
83 cerr << __FUNCTION__ <<
84 ": no interface specification file found for "
85 << "class " << target_class
86 << " type " << target_type
87 << " version " << target_version << endl;
88 return false;
89 }
90 FuzzerWrapper wrapper = FuzzerWrapper();
91
92 if (!wrapper.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) {
93 return false;
94 }
95 FuzzerBase* fuzzer = wrapper.GetFuzzer(*interface_specification_message);
96 if (!fuzzer) {
97 cerr << __FUNCTION__ << ": coult't get a fuzzer base class" << endl;
98 return false;
99 }
100 if (!fuzzer->LoadTargetComponent(dll_file_name)) return -1;
Keun Soo Yim8103c912016-04-22 20:07:13 -0700101 for (int i = 0; i < epoch_count_; i++) {
Keun Soo Yimc5d092a2016-04-28 16:51:56 +0000102 void* result;
103 fuzzer->Fuzz(*interface_specification_message, result);
Keun Soo Yim08400372016-03-03 13:28:51 -0800104 }
105 return true;
106}
107
108
109} // namespace vts
110} // namespace android