Merge "RebootEscrow HAL: testing with secure implementations" into rvc-dev
diff --git a/automotive/evs/1.1/vts/functional/VtsHalEvsV1_1TargetTest.cpp b/automotive/evs/1.1/vts/functional/VtsHalEvsV1_1TargetTest.cpp
index 6a386c3..8b68fd6 100644
--- a/automotive/evs/1.1/vts/functional/VtsHalEvsV1_1TargetTest.cpp
+++ b/automotive/evs/1.1/vts/functional/VtsHalEvsV1_1TargetTest.cpp
@@ -2262,6 +2262,7 @@
 
     // Allocate buffers to use
     hidl_vec<BufferDesc> buffers;
+    buffers.resize(kBuffersToHold);
     for (auto i = 0; i < kBuffersToHold; ++i) {
         unsigned pixelsPerLine;
         buffer_handle_t memHandle = nullptr;
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp
index 2c2f23c..ea38cb3 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.cpp
@@ -71,35 +71,45 @@
 }
 
 android::base::Result<std::unique_ptr<VehiclePropValue>> EmulatedUserHal::onGetProperty(
-        int32_t prop) {
-    ALOGV("onGetProperty(%d)", prop);
-    switch (prop) {
+        const VehiclePropValue& value) {
+    ALOGV("onGetProperty(%s)", toString(value).c_str());
+    switch (value.prop) {
         case INITIAL_USER_INFO:
         case SWITCH_USER:
         case CREATE_USER:
         case REMOVE_USER:
-            ALOGE("onGetProperty(): %d is only supported on SET", prop);
+            ALOGE("onGetProperty(): %d is only supported on SET", value.prop);
             return android::base::Error(static_cast<int>(StatusCode::INVALID_ARG))
                    << "only supported on SET";
         case USER_IDENTIFICATION_ASSOCIATION:
-            if (mSetUserIdentificationAssociationResponseFromCmd != nullptr) {
-                ALOGI("onGetProperty(%d): returning %s", prop,
-                      toString(*mSetUserIdentificationAssociationResponseFromCmd).c_str());
-                auto value = std::unique_ptr<VehiclePropValue>(
-                        new VehiclePropValue(*mSetUserIdentificationAssociationResponseFromCmd));
-                return value;
-            }
-            ALOGE("onGetProperty(%d): USER_IDENTIFICATION_ASSOCIATION not set by lshal", prop);
-            return android::base::Error(static_cast<int>(StatusCode::NOT_AVAILABLE))
-                   << "not set by lshal";
+            return onGetUserIdentificationAssociation(value);
         default:
-            ALOGE("onGetProperty(): %d is not supported", prop);
+            ALOGE("onGetProperty(): %d is not supported", value.prop);
             return android::base::Error(static_cast<int>(StatusCode::INVALID_ARG))
                    << "not supported by User HAL";
     }
 }
 
 android::base::Result<std::unique_ptr<VehiclePropValue>>
+EmulatedUserHal::onGetUserIdentificationAssociation(const VehiclePropValue& value) {
+    if (mSetUserIdentificationAssociationResponseFromCmd != nullptr) {
+        ALOGI("get(USER_IDENTIFICATION_ASSOCIATION): returning %s",
+              toString(*mSetUserIdentificationAssociationResponseFromCmd).c_str());
+        auto newValue = std::unique_ptr<VehiclePropValue>(
+                new VehiclePropValue(*mSetUserIdentificationAssociationResponseFromCmd));
+        // Must use the same requestId
+        if (value.value.int32Values.size() > 0) {
+            newValue->value.int32Values[0] = value.value.int32Values[0];
+        } else {
+            ALOGE("get(USER_IDENTIFICATION_ASSOCIATION): no requestId on %s",
+                  toString(value).c_str());
+        }
+        return newValue;
+    }
+    return defaultUserIdentificationAssociation(value);
+}
+
+android::base::Result<std::unique_ptr<VehiclePropValue>>
 EmulatedUserHal::onSetInitialUserInfoResponse(const VehiclePropValue& value) {
     if (value.value.int32Values.size() == 0) {
         ALOGE("set(INITIAL_USER_INFO): no int32values, ignoring it: %s", toString(value).c_str());
@@ -250,16 +260,14 @@
     }
 
     // Returns default response
-    auto updatedValue = std::unique_ptr<VehiclePropValue>(new VehiclePropValue);
-    updatedValue->prop = USER_IDENTIFICATION_ASSOCIATION;
-    updatedValue->timestamp = elapsedRealtimeNano();
-    updatedValue->value.int32Values.resize(1);
-    updatedValue->value.int32Values[0] = requestId;
-    updatedValue->value.stringValue = "Response not set by LSHAL";
+    return defaultUserIdentificationAssociation(value);
+}
 
-    ALOGI("no lshal response; replying with an error message: %s", toString(*updatedValue).c_str());
-
-    return updatedValue;
+android::base::Result<std::unique_ptr<VehiclePropValue>>
+EmulatedUserHal::defaultUserIdentificationAssociation(const VehiclePropValue& request) {
+    // TODO(b/159498909): return a response with NOT_ASSOCIATED_ANY_USER for all requested types
+    ALOGE("no lshal response for %s; replying with NOT_AVAILABLE", toString(request).c_str());
+    return android::base::Error(static_cast<int>(StatusCode::NOT_AVAILABLE)) << "not set by lshal";
 }
 
 android::base::Result<std::unique_ptr<VehiclePropValue>> EmulatedUserHal::sendUserHalResponse(
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h
index 5243b96..db2f117 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedUserHal.h
@@ -58,7 +58,8 @@
      *
      * @return property value and StatusCode
      */
-    android::base::Result<std::unique_ptr<VehiclePropValue>> onGetProperty(int32_t prop);
+    android::base::Result<std::unique_ptr<VehiclePropValue>> onGetProperty(
+            const VehiclePropValue& value);
 
     /**
      * Shows the User HAL emulation help.
@@ -111,12 +112,25 @@
             const VehiclePropValue& value);
 
     /**
-     * Used to emulate USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
+     * Used to emulate set USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
      * usage.
      */
     android::base::Result<std::unique_ptr<VehiclePropValue>> onSetUserIdentificationAssociation(
             const VehiclePropValue& value);
 
+    /**
+     * Used to emulate get USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
+     * usage.
+     */
+    android::base::Result<std::unique_ptr<VehiclePropValue>> onGetUserIdentificationAssociation(
+            const VehiclePropValue& value);
+
+    /**
+     * Creates a default USER_IDENTIFICATION_ASSOCIATION when it was not set by lshal.
+     */
+    android::base::Result<std::unique_ptr<VehiclePropValue>> defaultUserIdentificationAssociation(
+            const VehiclePropValue& request);
+
     android::base::Result<std::unique_ptr<VehiclePropValue>> sendUserHalResponse(
             std::unique_ptr<VehiclePropValue> response, int32_t requestId);
 
diff --git a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp
index 9cfcc1c..a0b566d 100644
--- a/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp
+++ b/automotive/vehicle/2.0/default/impl/vhal_v2_0/EmulatedVehicleHal.cpp
@@ -153,7 +153,7 @@
         default:
             if (mEmulatedUserHal != nullptr && mEmulatedUserHal->isSupported(propId)) {
                 ALOGI("get(): getting value for prop %d from User HAL", propId);
-                const auto& ret = mEmulatedUserHal->onGetProperty(propId);
+                const auto& ret = mEmulatedUserHal->onGetProperty(requestedPropValue);
                 if (!ret.ok()) {
                     ALOGE("get(): User HAL returned error: %s", ret.error().message().c_str());
                     *outStatus = StatusCode(ret.error().code());
diff --git a/neuralnetworks/1.2/vts/functional/CompilationCachingTests.cpp b/neuralnetworks/1.2/vts/functional/CompilationCachingTests.cpp
index 16b313a..449b8f3 100644
--- a/neuralnetworks/1.2/vts/functional/CompilationCachingTests.cpp
+++ b/neuralnetworks/1.2/vts/functional/CompilationCachingTests.cpp
@@ -315,8 +315,7 @@
 
     void saveModelToCache(const Model& model, const hidl_vec<hidl_handle>& modelCache,
                           const hidl_vec<hidl_handle>& dataCache,
-                          sp<IPreparedModel>* preparedModel = nullptr,
-                          bool allowGeneralFailure = false) {
+                          sp<IPreparedModel>* preparedModel = nullptr) {
         if (preparedModel != nullptr) *preparedModel = nullptr;
 
         // Launch prepare model.
@@ -330,10 +329,7 @@
 
         // Retrieve prepared model.
         preparedModelCallback->wait();
-        const auto prepareCallbackStatus = preparedModelCallback->getStatus();
-        if (!allowGeneralFailure || prepareCallbackStatus != ErrorStatus::GENERAL_FAILURE) {
-            ASSERT_EQ(prepareCallbackStatus, ErrorStatus::NONE);
-        }
+        ASSERT_EQ(preparedModelCallback->getStatus(), ErrorStatus::NONE);
         if (preparedModel != nullptr) {
             *preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel())
                                      .withDefault(nullptr);
@@ -1026,8 +1022,7 @@
 
 // Number of operations in the large test model.
 constexpr uint32_t kLargeModelSize = 100;
-constexpr uint32_t kNumSuccessfulIterationsTOCTOU = 100;
-constexpr uint32_t kMaxNumFailedIterationsTOCTOU = 100;
+constexpr uint32_t kNumIterationsTOCTOU = 100;
 
 TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
     if (!mIsCachingSupported) return;
@@ -1055,30 +1050,18 @@
     // Use a different token for modelAdd.
     mToken[0]++;
 
-    // This test is probabilistic, so we run it multiple times. We allow the compilation to fail
-    // because it is not related to the security aspect of the TOCTOU test. However, we need to have
-    // enough successful iterations to ensure the test coverage.
-    uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
-    while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
+    // This test is probabilistic, so we run it multiple times.
+    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
         // Save the modelAdd compilation to cache.
         {
             hidl_vec<hidl_handle> modelCache, dataCache;
             createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
             createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
 
-            sp<IPreparedModel> preparedModel = nullptr;
             // Spawn a thread to copy the cache content concurrently while saving to cache.
             std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache));
-            saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
-                             /*allowGeneralFailure=*/true);
+            saveModelToCache(modelAdd, modelCache, dataCache);
             thread.join();
-
-            if (preparedModel == nullptr) {
-                numFailedIterations++;
-                ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
-            } else {
-                numSuccessfulIterations++;
-            }
         }
 
         // Retrieve preparedModel from cache.
@@ -1129,26 +1112,14 @@
     // Use a different token for modelAdd.
     mToken[0]++;
 
-    // This test is probabilistic, so we run it multiple times. We allow the compilation to fail
-    // because it is not related to the security aspect of the TOCTOU test. However, we need to have
-    // enough successful iterations to ensure the test coverage.
-    uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
-    while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
+    // This test is probabilistic, so we run it multiple times.
+    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
         // Save the modelAdd compilation to cache.
         {
             hidl_vec<hidl_handle> modelCache, dataCache;
             createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
             createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
-            sp<IPreparedModel> preparedModel = nullptr;
-            saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
-                             /*allowGeneralFailure=*/true);
-
-            if (preparedModel == nullptr) {
-                numFailedIterations++;
-                ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
-            } else {
-                numSuccessfulIterations++;
-            }
+            saveModelToCache(modelAdd, modelCache, dataCache);
         }
 
         // Retrieve preparedModel from cache.
diff --git a/neuralnetworks/1.3/vts/functional/CompilationCachingTests.cpp b/neuralnetworks/1.3/vts/functional/CompilationCachingTests.cpp
index 382fc76..ac18c8f 100644
--- a/neuralnetworks/1.3/vts/functional/CompilationCachingTests.cpp
+++ b/neuralnetworks/1.3/vts/functional/CompilationCachingTests.cpp
@@ -318,8 +318,7 @@
 
     void saveModelToCache(const Model& model, const hidl_vec<hidl_handle>& modelCache,
                           const hidl_vec<hidl_handle>& dataCache,
-                          sp<IPreparedModel>* preparedModel = nullptr,
-                          bool allowGeneralFailure = false) {
+                          sp<IPreparedModel>* preparedModel = nullptr) {
         if (preparedModel != nullptr) *preparedModel = nullptr;
 
         // Launch prepare model.
@@ -333,10 +332,7 @@
 
         // Retrieve prepared model.
         preparedModelCallback->wait();
-        const auto prepareCallbackStatus = preparedModelCallback->getStatus();
-        if (!allowGeneralFailure || prepareCallbackStatus != ErrorStatus::GENERAL_FAILURE) {
-            ASSERT_EQ(prepareCallbackStatus, ErrorStatus::NONE);
-        }
+        ASSERT_EQ(preparedModelCallback->getStatus(), ErrorStatus::NONE);
         if (preparedModel != nullptr) {
             *preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel())
                                      .withDefault(nullptr);
@@ -1017,8 +1013,7 @@
 
 // Number of operations in the large test model.
 constexpr uint32_t kLargeModelSize = 100;
-constexpr uint32_t kNumSuccessfulIterationsTOCTOU = 100;
-constexpr uint32_t kMaxNumFailedIterationsTOCTOU = 100;
+constexpr uint32_t kNumIterationsTOCTOU = 100;
 
 TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
     if (!mIsCachingSupported) return;
@@ -1046,30 +1041,18 @@
     // Use a different token for modelAdd.
     mToken[0]++;
 
-    // This test is probabilistic, so we run it multiple times. We allow the compilation to fail
-    // because it is not related to the security aspect of the TOCTOU test. However, we need to have
-    // enough successful iterations to ensure the test coverage.
-    uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
-    while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
+    // This test is probabilistic, so we run it multiple times.
+    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
         // Save the modelAdd compilation to cache.
         {
             hidl_vec<hidl_handle> modelCache, dataCache;
             createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
             createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
 
-            sp<IPreparedModel> preparedModel = nullptr;
             // Spawn a thread to copy the cache content concurrently while saving to cache.
             std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache));
-            saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
-                             /*allowGeneralFailure=*/true);
+            saveModelToCache(modelAdd, modelCache, dataCache);
             thread.join();
-
-            if (preparedModel == nullptr) {
-                numFailedIterations++;
-                ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
-            } else {
-                numSuccessfulIterations++;
-            }
         }
 
         // Retrieve preparedModel from cache.
@@ -1120,26 +1103,14 @@
     // Use a different token for modelAdd.
     mToken[0]++;
 
-    // This test is probabilistic, so we run it multiple times. We allow the compilation to fail
-    // because it is not related to the security aspect of the TOCTOU test. However, we need to have
-    // enough successful iterations to ensure the test coverage.
-    uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
-    while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
+    // This test is probabilistic, so we run it multiple times.
+    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
         // Save the modelAdd compilation to cache.
         {
             hidl_vec<hidl_handle> modelCache, dataCache;
             createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
             createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
-            sp<IPreparedModel> preparedModel = nullptr;
-            saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
-                             /*allowGeneralFailure=*/true);
-
-            if (preparedModel == nullptr) {
-                numFailedIterations++;
-                ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
-            } else {
-                numSuccessfulIterations++;
-            }
+            saveModelToCache(modelAdd, modelCache, dataCache);
         }
 
         // Retrieve preparedModel from cache.