Home | History | Annotate | Download | only in fuzz_tester
      1 /*
      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 #include "fuzz_tester/FuzzerCallbackBase.h"
     18 
     19 #include <dirent.h>
     20 #include <netdb.h>
     21 #include <netinet/in.h>
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <string.h>
     25 #include <sys/mman.h>
     26 #include <sys/socket.h>
     27 #include <sys/stat.h>
     28 #include <sys/types.h>
     29 #include <sys/un.h>
     30 #include <unistd.h>
     31 
     32 #include <iostream>
     33 #include <string>
     34 #include <vector>
     35 
     36 #include <VtsDriverCommUtil.h>
     37 #include "test/vts/proto/ComponentSpecificationMessage.pb.h"
     38 #include "test/vts/proto/AndroidSystemControlMessage.pb.h"
     39 
     40 #include "component_loader/DllLoader.h"
     41 #include "utils/InterfaceSpecUtil.h"
     42 
     43 using namespace std;
     44 
     45 namespace android {
     46 namespace vts {
     47 
     48 static std::map<string, string> id_map_;
     49 
     50 FuzzerCallbackBase::FuzzerCallbackBase() {}
     51 
     52 FuzzerCallbackBase::~FuzzerCallbackBase() {}
     53 
     54 bool FuzzerCallbackBase::Register(const VariableSpecificationMessage& message) {
     55   cout << __func__ << " type = " << message.type() << endl;
     56   if (!message.is_callback()) {
     57     cerr << __func__ << " ERROR: argument is not a callback." << endl;
     58     return false;
     59   }
     60 
     61   if (!message.has_type() || message.type() != TYPE_FUNCTION_POINTER) {
     62     cerr << __func__ << " ERROR: inconsistent message." << endl;
     63     return false;
     64   }
     65 
     66   for (const auto& func_pt : message.function_pointer()) {
     67     cout << __func__ << " map[" << func_pt.function_name()
     68          << "] = " << func_pt.id() << endl;
     69     id_map_[func_pt.function_name()] = func_pt.id();
     70   }
     71   return true;
     72 }
     73 
     74 const char* FuzzerCallbackBase::GetCallbackID(const string& name) {
     75   // TODO: handle when not found.
     76   cout << __func__ << ":" << __LINE__ << " " << name << endl;
     77   cout << __func__ << ":" << __LINE__ << " returns '" << id_map_[name].c_str()
     78        << "'" << endl;
     79   return id_map_[name].c_str();
     80 }
     81 
     82 void FuzzerCallbackBase::RpcCallToAgent(
     83     const AndroidSystemCallbackRequestMessage& message,
     84     const string& callback_socket_name) {
     85   cout << __func__ << ":" << __LINE__ << " id = '" << message.id() << "'"
     86        << endl;
     87   if (message.id().empty() || callback_socket_name.empty()) {
     88     cout << "Message ID: " << message.id() << endl
     89          << "Callback socket name: " << callback_socket_name << endl
     90          << "Abort callback forwarding." << endl;
     91     return;
     92   }
     93   VtsDriverCommUtil util;
     94   if (!util.Connect(callback_socket_name)) exit(-1);
     95   util.VtsSocketSendMessage(message);
     96   util.Close();
     97 }
     98 
     99 }  // namespace vts
    100 }  // namespace android
    101