cli: Fix DLC LoadPin digests

.. as LoadPin header is getting parsed as part of the digest set.

BUG=b:278277742
TEST=deploy loadpin DLC

Change-Id: Ib7a7d8c16ea0d03409a13291b65f59adb100799e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/4428209
Tested-by: Jae Hoon Kim <kimjae@chromium.org>
Commit-Queue: Jae Hoon Kim <kimjae@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/cli/deploy_unittest.py b/cli/deploy_unittest.py
index 3408ee4..b462e41 100644
--- a/cli/deploy_unittest.py
+++ b/cli/deploy_unittest.py
@@ -15,6 +15,7 @@
 from chromite.lib import build_target_lib
 from chromite.lib import cros_build_lib
 from chromite.lib import cros_test_lib
+from chromite.lib import dlc_lib
 from chromite.lib import osutils
 from chromite.lib import remote_access
 from chromite.lib import sysroot_lib
@@ -32,6 +33,17 @@
 # pylint: disable=protected-access
 
 
+# Example DLC LoadPin digests to test with.
+LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS = """# LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS
+75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431
+"""
+
+# An example LoadPin digest.
+DLC_LOADPIN_DIGEST = (
+    "feeddeadc0de0000000000000000000000000000000000000000000000000000"
+)
+
+
 class ChromiumOSDeviceFake(object):
     """Fake for device."""
 
@@ -44,6 +56,8 @@
         self.cmds = []
         self.work_dir = "/testdir/"
         self.selinux_available = False
+        self.copy_store = None
+        self.cat_file_output = ""
 
     def MountRootfsReadWrite(self):
         return True
@@ -58,8 +72,13 @@
         self.cmds.append(cmd)
 
     def CopyToDevice(self, _src, _dest, _mode="rsync", **_kwargs):
+        if os.path.exists(_src):
+            self.copy_store = osutils.ReadFile(_src)
         return True
 
+    def CatFile(self, _src):
+        return self.cat_file_output
+
 
 class ChromiumOSDeviceHandlerFake(object):
     """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler."""
@@ -429,6 +448,47 @@
         # Check that dlcservice is restarted (DLC modules are deployed).
         self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds)
 
+    def testDeployDLCLoadPinMissingDeviceDigests(self):
+        """Test that _DeployDLCLoadPin works with missing device digests."""
+        osutils.WriteFile(
+            self.tempdir
+            / dlc_lib.DLC_META_DIR
+            / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
+            LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
+            makedirs=True,
+        )
+        with self.device as d:
+            deploy._DeployDLCLoadPin(self.tempdir, d)
+        self.assertEqual(
+            d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
+        )
+        self.assertFalse(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
+        self.assertTrue(
+            "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
+            in d.copy_store.splitlines()
+        )
+
+    def testDeployDLCLoadPinFeedNewDigests(self):
+        """Test that _DeployDLCLoadPin works with digest format file."""
+        osutils.WriteFile(
+            self.tempdir
+            / dlc_lib.DLC_META_DIR
+            / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
+            LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
+            makedirs=True,
+        )
+        with self.device as d:
+            d.cat_file_output = DLC_LOADPIN_DIGEST
+            deploy._DeployDLCLoadPin(self.tempdir, d)
+        self.assertEqual(
+            d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
+        )
+        self.assertTrue(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
+        self.assertTrue(
+            "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
+            in d.copy_store.splitlines()
+        )
+
     def testDeployEmergeSELinux(self):
         """Test deploy progress when the device has SELinux"""