Snap for 6618402 from 39963aef4174292b22fec78b89667eec267cf34c to mainline-release

Change-Id: I570c7b47d74fab12bd3eee14339321d451805abf
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/media/omx/1.0/vts/functional/common/Android.bp b/media/omx/1.0/vts/functional/common/Android.bp
index 2c024a0..720ea9f 100644
--- a/media/omx/1.0/vts/functional/common/Android.bp
+++ b/media/omx/1.0/vts/functional/common/Android.bp
@@ -74,5 +74,6 @@
     // TODO(b/64437680): Assume these libs are always available on the device.
     shared_libs: [
         "libstagefright_foundation",
+        "libstagefright_omx_utils",
     ],
 }
diff --git a/media/omx/1.0/vts/functional/master/VtsHalMediaOmxV1_0TargetMasterTest.cpp b/media/omx/1.0/vts/functional/master/VtsHalMediaOmxV1_0TargetMasterTest.cpp
index 9b4722e..4e25313 100644
--- a/media/omx/1.0/vts/functional/master/VtsHalMediaOmxV1_0TargetMasterTest.cpp
+++ b/media/omx/1.0/vts/functional/master/VtsHalMediaOmxV1_0TargetMasterTest.cpp
@@ -33,6 +33,7 @@
 #include <gtest/gtest.h>
 #include <hidl/GtestPrinter.h>
 #include <hidl/ServiceManagement.h>
+#include <media/stagefright/omx/OMXUtils.h>
 
 using ::android::sp;
 using ::android::base::Join;
@@ -87,71 +88,6 @@
     }
 }
 
-/*
- * Returns the role based on is_encoder and mime.
- *
- * The mapping from a pair (is_encoder, mime) to a role string is
- * defined in frameworks/av/media/libmedia/MediaDefs.cpp and
- * frameworks/av/media/libstagefright/omx/OMXUtils.cpp. This function
- * does essentially the same work as GetComponentRole() in
- * OMXUtils.cpp.
- *
- * Args:
- *   is_encoder: A boolean indicating whether the role is for an
- *       encoder or a decoder.
- *   mime: A string of the desired mime type.
- *
- * Returns:
- *   A const string for the requested role name, empty if mime is not
- *   recognized.
- */
-const std::string getComponentRole(bool isEncoder, const std::string mime) {
-    // Mapping from mime types to roles.
-    // These values come from MediaDefs.cpp and OMXUtils.cpp
-    const std::map<const std::string, const std::string> audioMimeToRole = {
-            {"3gpp", "amrnb"},         {"ac3", "ac3"},     {"amr-wb", "amrwb"},
-            {"eac3", "eac3"},          {"flac", "flac"},   {"g711-alaw", "g711alaw"},
-            {"g711-mlaw", "g711mlaw"}, {"gsm", "gsm"},     {"mp4a-latm", "aac"},
-            {"mpeg", "mp3"},           {"mpeg-L1", "mp1"}, {"mpeg-L2", "mp2"},
-            {"opus", "opus"},          {"raw", "raw"},     {"vorbis", "vorbis"},
-    };
-    const std::map<const std::string, const std::string> videoMimeToRole = {
-            {"3gpp", "h263"},         {"avc", "avc"},           {"dolby-vision", "dolby-vision"},
-            {"hevc", "hevc"},         {"mp4v-es", "mpeg4"},     {"mpeg2", "mpeg2"},
-            {"x-vnd.on2.vp8", "vp8"}, {"x-vnd.on2.vp9", "vp9"},
-    };
-    const std::map<const std::string, const std::string> imageMimeToRole = {
-            {"vnd.android.heic", "heic"},
-    };
-
-    // Suffix begins after the mime prefix.
-    const size_t prefixEnd = mime.find("/");
-    if (prefixEnd == std::string::npos || prefixEnd == mime.size()) return "";
-    const std::string mime_suffix = mime.substr(prefixEnd + 1, mime.size() - 1);
-    const std::string middle = isEncoder ? "encoder." : "decoder.";
-    std::string prefix;
-    std::string suffix;
-    if (mime.rfind("audio/", 0) != std::string::npos) {
-        const auto it = audioMimeToRole.find(mime_suffix);
-        if (it == audioMimeToRole.end()) return "";
-        prefix = "audio_";
-        suffix = it->second;
-    } else if (mime.rfind("video/", 0) != std::string::npos) {
-        const auto it = videoMimeToRole.find(mime_suffix);
-        if (it == videoMimeToRole.end()) return "";
-        prefix = "video_";
-        suffix = it->second;
-    } else if (mime.rfind("image/", 0) != std::string::npos) {
-        const auto it = imageMimeToRole.find(mime_suffix);
-        if (it == imageMimeToRole.end()) return "";
-        prefix = "image_";
-        suffix = it->second;
-    } else {
-        return "";
-    }
-    return prefix + middle + suffix;
-}
-
 void validateAttributes(
         const std::map<const std::string, const testing::internal::RE>& knownPatterns,
         const std::vector<const struct AttributePattern>& unknownPatterns,
@@ -328,7 +264,8 @@
 
         // Make sure role name follows expected format based on type and
         // isEncoder
-        const std::string role_name = getComponentRole(role.isEncoder, role.type);
+        const std::string role_name(
+                ::android::GetComponentRole(role.isEncoder, role.type.c_str()));
         EXPECT_EQ(role_name, role.role) << "Role \"" << role.role << "\" does not match "
                                         << (role.isEncoder ? "an encoder " : "a decoder ")
                                         << "for mime type \"" << role.type << ".";
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.