release-request-b613f8ce-05b5-465e-b783-c1b87f3c1e95-for-git_oc-mr1-release-4332123 snap-temp-L59300000101925107

Change-Id: I677a2fdc1151095a7a7c6912027396fe1d5cc0f7
diff --git a/RuntimeInfo.cpp b/RuntimeInfo.cpp
index 0bcb363..b343138 100644
--- a/RuntimeInfo.cpp
+++ b/RuntimeInfo.cpp
@@ -101,8 +101,8 @@
            minLts.minorRev <= mKernelVersion.minorRev;
 }
 
-bool RuntimeInfo::checkCompatibility(const CompatibilityMatrix &mat,
-            std::string *error) const {
+bool RuntimeInfo::checkCompatibility(const CompatibilityMatrix& mat, std::string* error,
+                                     DisabledChecks disabledChecks) const {
     if (mat.mType != SchemaType::FRAMEWORK) {
         if (error != nullptr) {
             *error = "Should not check runtime info against " + to_string(mat.mType)
@@ -160,25 +160,28 @@
         error->clear();
     }
 
-    const Version &matAvb = mat.framework.mAvbMetaVersion;
-    if (mBootAvbVersion.majorVer != matAvb.majorVer || mBootAvbVersion.minorVer < matAvb.minorVer) {
-        if (error != nullptr) {
-            std::stringstream ss;
-            ss << "AVB version " << mBootAvbVersion << " does not match framework matrix "
-               << matAvb;
-            *error = ss.str();
+    if ((disabledChecks & DISABLE_AVB_CHECK) == 0) {
+        const Version& matAvb = mat.framework.mAvbMetaVersion;
+        if (mBootAvbVersion.majorVer != matAvb.majorVer ||
+            mBootAvbVersion.minorVer < matAvb.minorVer) {
+            if (error != nullptr) {
+                std::stringstream ss;
+                ss << "AVB version " << mBootAvbVersion << " does not match framework matrix "
+                   << matAvb;
+                *error = ss.str();
+            }
+            return false;
         }
-        return false;
-    }
-    if (mBootVbmetaAvbVersion.majorVer != matAvb.majorVer ||
-        mBootVbmetaAvbVersion.minorVer < matAvb.minorVer) {
-        if (error != nullptr) {
-            std::stringstream ss;
-            ss << "Vbmeta version " << mBootVbmetaAvbVersion << " does not match framework matrix "
-               << matAvb;
-            *error = ss.str();
+        if (mBootVbmetaAvbVersion.majorVer != matAvb.majorVer ||
+            mBootVbmetaAvbVersion.minorVer < matAvb.minorVer) {
+            if (error != nullptr) {
+                std::stringstream ss;
+                ss << "Vbmeta version " << mBootVbmetaAvbVersion
+                   << " does not match framework matrix " << matAvb;
+                *error = ss.str();
+            }
+            return false;
         }
-        return false;
     }
 
     return true;
diff --git a/VintfObject.cpp b/VintfObject.cpp
index f785e6b..d441245 100644
--- a/VintfObject.cpp
+++ b/VintfObject.cpp
@@ -172,9 +172,9 @@
 // Checks given compatibility info against info on the device. If no
 // compatability info is given then the device info will be checked against
 // itself.
-int32_t checkCompatibility(const std::vector<std::string> &xmls, bool mount,
-        const PartitionMounter &mounter, std::string *error) {
-
+int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount,
+                           const PartitionMounter& mounter, std::string* error,
+                           DisabledChecks disabledChecks) {
     status_t status;
     ParseStatus parseStatus;
     PackageInfo pkg; // All information from package.
@@ -277,7 +277,7 @@
         }
     }
     if (updated.runtimeInfo && updated.fwk.matrix) {
-        if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error)) {
+        if (!updated.runtimeInfo->checkCompatibility(*updated.fwk.matrix, error, disabledChecks)) {
             if (error)
                 error->insert(0, "Runtime info and framework compatibility matrix "
                                  "are incompatible: ");
@@ -291,11 +291,10 @@
 } // namespace details
 
 // static
-int32_t VintfObject::CheckCompatibility(
-        const std::vector<std::string> &xmls, std::string *error) {
-    return details::checkCompatibility(xmls, false /* mount */,
-            *details::gPartitionMounter,
-            error);
+int32_t VintfObject::CheckCompatibility(const std::vector<std::string>& xmls, std::string* error,
+                                        DisabledChecks disabledChecks) {
+    return details::checkCompatibility(xmls, false /* mount */, *details::gPartitionMounter, error,
+                                       disabledChecks);
 }
 
 
diff --git a/include/vintf/DisabledChecks.h b/include/vintf/DisabledChecks.h
new file mode 100644
index 0000000..09f49a3
--- /dev/null
+++ b/include/vintf/DisabledChecks.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_VINTF_DISABLED_CHECKS_H_
+#define ANDROID_VINTF_DISABLED_CHECKS_H_
+
+namespace android {
+namespace vintf {
+
+// Flags for *::checkCompatibility functions.
+enum DisabledChecks : int32_t {
+    ENABLE_ALL_CHECKS = 0,
+    // Disable AVB version check in RuntimeInfo::checkCompatibility
+    DISABLE_AVB_CHECK = 1 << 0,
+};
+
+}  // namespace vintf
+}  // namespace android
+
+#endif  // ANDROID_VINTF_DISABLED_CHECKS_H_
diff --git a/include/vintf/RuntimeInfo.h b/include/vintf/RuntimeInfo.h
index 6b510fb..1505c15 100644
--- a/include/vintf/RuntimeInfo.h
+++ b/include/vintf/RuntimeInfo.h
@@ -25,6 +25,7 @@
 
 #include <utils/Errors.h>
 
+#include "DisabledChecks.h"
 #include "MatrixKernel.h"
 #include "Version.h"
 
@@ -70,11 +71,10 @@
     //   RuntimeInfo object is created (the first time VintfObject::GetRuntimeInfo is called),
     //   not when RuntimeInfo::checkCompatibility is called.
     // - avb-vbmetaversion matches related sysprops
-    bool checkCompatibility(const CompatibilityMatrix &mat,
-            std::string *error = nullptr) const;
+    bool checkCompatibility(const CompatibilityMatrix& mat, std::string* error = nullptr,
+                            DisabledChecks disabledChecks = ENABLE_ALL_CHECKS) const;
 
-private:
-
+   private:
     friend struct RuntimeInfoFetcher;
     friend class VintfObject;
     friend struct LibVintfTest;
diff --git a/include/vintf/VintfObject.h b/include/vintf/VintfObject.h
index 9d5cc6f..e4dbe7c 100644
--- a/include/vintf/VintfObject.h
+++ b/include/vintf/VintfObject.h
@@ -18,6 +18,7 @@
 #define ANDROID_VINTF_VINTF_OBJECT_H_
 
 #include "CompatibilityMatrix.h"
+#include "DisabledChecks.h"
 #include "HalManifest.h"
 #include "RuntimeInfo.h"
 
@@ -84,14 +85,16 @@
      *
      * @param packageInfo a list of XMLs of HalManifest /
      * CompatibilityMatrix objects.
+     * @param error error message
+     * @param disabledChecks flags to disable certain checks. See DisabledChecks.
      *
      * @return = 0 if success (compatible)
      *         > 0 if incompatible
      *         < 0 if any error (mount partition fails, illformed XML, etc.)
      */
-    static int32_t CheckCompatibility(
-            const std::vector<std::string> &packageInfo,
-            std::string *error = nullptr);
+    static int32_t CheckCompatibility(const std::vector<std::string>& packageInfo,
+                                      std::string* error = nullptr,
+                                      DisabledChecks disabledChecks = ENABLE_ALL_CHECKS);
 };
 
 enum : int32_t {
@@ -102,9 +105,9 @@
 // exposed for testing and VintfObjectRecovery.
 namespace details {
 class PartitionMounter;
-int32_t checkCompatibility(const std::vector<std::string> &xmls, bool mount,
-        const PartitionMounter& partitionMounter,
-        std::string *error);
+int32_t checkCompatibility(const std::vector<std::string>& xmls, bool mount,
+                           const PartitionMounter& partitionMounter, std::string* error,
+                           DisabledChecks disabledChecks = ENABLE_ALL_CHECKS);
 } // namespace details
 
 } // namespace vintf
diff --git a/test/main.cpp b/test/main.cpp
index 94a4d8e..495b35c 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -771,8 +771,7 @@
         CompatibilityMatrix cm = testMatrix(std::move(kernel));
         EXPECT_FALSE(ki.checkCompatibility(cm)) << "Value shouldn't match for integer";
     }
-// TODO(b/38325029) enable avb check when avb version is injected to fwk matrix.
-#if 0
+
     RuntimeInfo badAvb = testRuntimeInfo();
     CompatibilityMatrix cm = testMatrix(MatrixKernel(KernelVersion{3, 18, 31}, {}));
     {
@@ -792,7 +791,6 @@
         setAvb(badAvb, {2, 3}, {2, 1});
         EXPECT_TRUE(badAvb.checkCompatibility(cm, &error));
     }
-#endif
 }
 
 TEST_F(LibVintfTest, MissingAvb) {
@@ -809,6 +807,27 @@
     EXPECT_EQ(getAvb(cm), Version(0, 0));
 }
 
+TEST_F(LibVintfTest, DisableAvb) {
+    std::string xml =
+        "<compatibility-matrix version=\"1.0\" type=\"framework\">\n"
+        "    <kernel version=\"3.18.31\"></kernel>"
+        "    <sepolicy>\n"
+        "        <kernel-sepolicy-version>30</kernel-sepolicy-version>\n"
+        "        <sepolicy-version>25.5</sepolicy-version>\n"
+        "    </sepolicy>\n"
+        "    <avb>\n"
+        "        <vbmeta-version>1.0</vbmeta-version>\n"
+        "    </avb>\n"
+        "</compatibility-matrix>\n";
+    CompatibilityMatrix cm;
+    EXPECT_TRUE(gCompatibilityMatrixConverter(&cm, xml));
+    RuntimeInfo ki = testRuntimeInfo();
+    std::string error;
+    EXPECT_FALSE(ki.checkCompatibility(cm, &error));
+    EXPECT_STREQ(error.c_str(), "AVB version 2.1 does not match framework matrix 1.0");
+    EXPECT_TRUE(ki.checkCompatibility(cm, &error, DISABLE_AVB_CHECK)) << error;
+}
+
 // This is the test extracted from VINTF Object doc
 TEST_F(LibVintfTest, HalCompat) {
     CompatibilityMatrix matrix;