1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "neuralnetworks_hidl_hal_test"
18 
19 #include "1.0/Callbacks.h"
20 #include "1.0/Utils.h"
21 #include "GeneratedTestHarness.h"
22 #include "VtsHalNeuralnetworks.h"
23 
24 namespace android::hardware::neuralnetworks::V1_1::vts::functional {
25 
26 using V1_0::ErrorStatus;
27 using V1_0::IPreparedModel;
28 using V1_0::Request;
29 using V1_0::implementation::ExecutionCallback;
30 
31 using ExecutionMutation = std::function<void(Request*)>;
32 
33 ///////////////////////// UTILITY FUNCTIONS /////////////////////////
34 
35 // Primary validation function. This function will take a valid request, apply a
36 // mutation to it to invalidate the request, then pass it to interface calls
37 // that use the request.
validate(const sp<IPreparedModel> & preparedModel,const std::string & message,const Request & originalRequest,const ExecutionMutation & mutate)38 static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
39                      const Request& originalRequest, const ExecutionMutation& mutate) {
40     Request request = originalRequest;
41     mutate(&request);
42     SCOPED_TRACE(message + " [execute]");
43 
44     sp<ExecutionCallback> executionCallback = new ExecutionCallback();
45     Return<ErrorStatus> executeLaunchStatus = preparedModel->execute(request, executionCallback);
46     ASSERT_TRUE(executeLaunchStatus.isOk());
47     ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
48 
49     executionCallback->wait();
50     ErrorStatus executionReturnStatus = executionCallback->getStatus();
51     ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
52 }
53 
54 ///////////////////////// REMOVE INPUT ////////////////////////////////////
55 
removeInputTest(const sp<IPreparedModel> & preparedModel,const Request & request)56 static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
57     for (size_t input = 0; input < request.inputs.size(); ++input) {
58         const std::string message = "removeInput: removed input " + std::to_string(input);
59         validate(preparedModel, message, request,
60                  [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
61     }
62 }
63 
64 ///////////////////////// REMOVE OUTPUT ////////////////////////////////////
65 
removeOutputTest(const sp<IPreparedModel> & preparedModel,const Request & request)66 static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
67     for (size_t output = 0; output < request.outputs.size(); ++output) {
68         const std::string message = "removeOutput: removed Output " + std::to_string(output);
69         validate(preparedModel, message, request,
70                  [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
71     }
72 }
73 
74 ///////////////////////////// ENTRY POINT //////////////////////////////////
75 
validateRequest(const sp<IPreparedModel> & preparedModel,const Request & request)76 void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
77     removeInputTest(preparedModel, request);
78     removeOutputTest(preparedModel, request);
79 }
80 
81 }  // namespace android::hardware::neuralnetworks::V1_1::vts::functional
82