Keun Soo Yim | 0840037 | 2016-03-03 13:28:51 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 30 | namespace android { |
| 31 | namespace vts { |
| 32 | |
| 33 | SpecificationBuilder::SpecificationBuilder(const string dir_path) |
| 34 | : dir_path_(dir_path) {} |
| 35 | |
| 36 | |
| 37 | vts::InterfaceSpecificationMessage* |
| 38 | SpecificationBuilder::FindInterfaceSpecification( |
| 39 | const int target_class, |
| 40 | const int target_type, |
| 41 | const float target_version) { |
| 42 | DIR* dir; |
| 43 | struct dirent* ent; |
| 44 | |
| 45 | if (!(dir = opendir(dir_path_.c_str()))) { |
| 46 | cerr << __FUNCTION__ << ": Can't opendir " << dir_path_ << endl; |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | while ((ent = readdir(dir))) { |
| 51 | if (string(ent->d_name).find(SPEC_FILE_EXT) != std::string::npos) { |
| 52 | cout << __FUNCTION__ << ": Checking a file " << ent->d_name << endl; |
| 53 | const string file_path = string(dir_path_) + "/" + string(ent->d_name); |
| 54 | vts::InterfaceSpecificationMessage* message = |
| 55 | new vts::InterfaceSpecificationMessage(); |
| 56 | if (InterfaceSpecificationParser::parse(file_path.c_str(), message)) { |
| 57 | if (message->component_class() == target_class |
| 58 | && message->component_type() == target_type |
| 59 | && message->component_type_version() == target_version) { |
| 60 | closedir(dir); |
| 61 | return message; |
| 62 | } |
| 63 | } |
| 64 | delete message; |
| 65 | } |
| 66 | } |
| 67 | closedir(dir); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | bool SpecificationBuilder::Process( |
| 73 | const char* dll_file_name, |
| 74 | const char* spec_lib_file_path, |
| 75 | int target_class, |
| 76 | int target_type, |
| 77 | float target_version) { |
| 78 | vts::InterfaceSpecificationMessage* interface_specification_message = |
| 79 | FindInterfaceSpecification(target_class, target_type, target_version); |
| 80 | if (!interface_specification_message) { |
| 81 | cerr << __FUNCTION__ << |
| 82 | ": no interface specification file found for " |
| 83 | << "class " << target_class |
| 84 | << " type " << target_type |
| 85 | << " version " << target_version << endl; |
| 86 | return false; |
| 87 | } |
| 88 | FuzzerWrapper wrapper = FuzzerWrapper(); |
| 89 | |
| 90 | if (!wrapper.LoadInterfaceSpecificationLibrary(spec_lib_file_path)) { |
| 91 | return false; |
| 92 | } |
| 93 | FuzzerBase* fuzzer = wrapper.GetFuzzer(*interface_specification_message); |
| 94 | if (!fuzzer) { |
| 95 | cerr << __FUNCTION__ << ": coult't get a fuzzer base class" << endl; |
| 96 | return false; |
| 97 | } |
| 98 | if (!fuzzer->LoadTargetComponent(dll_file_name)) return -1; |
| 99 | // TODO: make 10 configurable. |
| 100 | for (int i = 0; i < 10; i++) { |
| 101 | fuzzer->Fuzz(*interface_specification_message); |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | } // namespace vts |
| 108 | } // namespace android |