hwid: Don't use default components if mismatched components are allowed.

Normally, the existance of a default component is a workaround so
that a bad probed values will not block the factory from generating
the HWID strings.  However, when we run hwid-updater, we consider the
probed values as a golden sample and encountering mismatched probed
values means that we need to record those values in the HWID database
so at this step, we should always ignore the default components.

BUG=chromium:845835
TEST=make test

Change-Id: I06918ba60aae310cd59b158e0324072f53bf792a
Reviewed-on: https://chromium-review.googlesource.com/1070831
Commit-Ready: Yong Hong <yhong@google.com>
Tested-by: Yong Hong <yhong@google.com>
Reviewed-by: Cheng-Han Yang <chenghan@chromium.org>
diff --git a/py/hwid/v3/probe.py b/py/hwid/v3/probe.py
index 99efb5d..4f85f89 100644
--- a/py/hwid/v3/probe.py
+++ b/py/hwid/v3/probe.py
@@ -57,6 +57,20 @@
         return False
     return True
 
+  def _GetDefaultComponent(comp_cls):
+    if allow_mismatched_components:
+      return None
+
+    default_comp = database.GetDefaultComponent(comp_cls)
+
+    # Always ignore the unsupported default components.
+    if default_comp is not None:
+      default_comp_info = database.GetComponents(comp_cls)[default_comp]
+      if default_comp_info.status == common.COMPONENT_STATUS.unsupported:
+        default_comp = None
+
+    return default_comp
+
   # Construct a dict of component classes to list of component names.
   matched_components = {comp_cls: []
                         for comp_cls in database.GetComponentClasses()}
@@ -65,13 +79,7 @@
                            if comp_cls not in matched_components}
 
   for comp_cls in database.GetComponentClasses():
-    default_comp = database.GetDefaultComponent(comp_cls)
-
-    # Always ignore the unsupported default components.
-    if default_comp is not None:
-      default_comp_info = database.GetComponents(comp_cls)[default_comp]
-      if default_comp_info.status == common.COMPONENT_STATUS.unsupported:
-        default_comp = None
+    default_comp = _GetDefaultComponent(comp_cls)
 
     for probed_comp in probed_results.get(comp_cls, []):
       for comp_name, comp_info in database.GetComponents(
diff --git a/py/hwid/v3/probe_unittest.py b/py/hwid/v3/probe_unittest.py
index af1006d..9403467 100755
--- a/py/hwid/v3/probe_unittest.py
+++ b/py/hwid/v3/probe_unittest.py
@@ -39,6 +39,12 @@
                        'comp_cls_2': ['comp_2_default'],
                        'comp_cls_3': []})
 
+    # When allow_mismatched_components=True, don't use the default components
+    bom = probe.GenerateBOMFromProbedResults(
+        self.database, {}, {}, {}, common.OPERATION_MODE.normal, True)[0]
+    self.assertEquals(
+        bom.components, {'comp_cls_1': [], 'comp_cls_2': [], 'comp_cls_3': []})
+
   def testSomeComponentMismatched(self):
     probed_results = {
         'comp_cls_1': [