Improve accuracy of 'Locked' property

The 'Locked' property in the volume interface is supposed to indicate
whether the LUKS volume is currently activated, but this property is
often inaccurate because it always defaults to false upon startup
(i.e. unlocked). However, the LUKS volume is usually locked at startup.
So, client daemons can get confused when looking at the Locked
property.

This commit reworks the functionality for the 'Locked' property, so that
it checks whether the mapped virtual crypt device exists, e.g. whether
/dev/mapper/<luks_device> exists. This way, the Locked property should
better reflect the actual state.

The one caveat to keep in mind is that 'Locked' will be True even if the
device isn't formatted as a LUKS volume. If client daemons need to know
whether it's already formatted, we may want to add another property to
the Volume interface for that purpose. But in the meantime, eStoraged
already exports an EncryptionStatus property as part of the Drive
interface. So, the information is already available, if needed.

Tested:
Checked 'Locked' property at startup
$ busctl get-property xyz.openbmc_project.eStoraged \
  /xyz/openbmc_project/inventory/storage/mmcblk0 \
  xyz.openbmc_project.Inventory.Item.Volume Locked
b true
Formatted the LUKS volume, then checked 'Locked' property again
$ busctl call xyz.openbmc_project.eStoraged \
      /xyz/openbmc_project/inventory/storage/mmcblk0 \
      xyz.openbmc_project.Inventory.Item.Volume FormatLuks ays 3 1 2 3 \
      xyz.openbmc_project.Inventory.Item.Volume.FilesystemType.ext4 \
      --timeout=60
$ busctl get-property xyz.openbmc_project.eStoraged \
  /xyz/openbmc_project/inventory/storage/mmcblk0 \
  xyz.openbmc_project.Inventory.Item.Volume Locked
b false
Restarted eStoraged and checked 'Locked' again.
$ systemctl restart xyz.openbmc_project.eStoraged
$ busctl get-property xyz.openbmc_project.eStoraged \
  /xyz/openbmc_project/inventory/storage/mmcblk0 \
  xyz.openbmc_project.Inventory.Item.Volume Locked
b false
Locked the LUKS volume, and checked 'Locked' again.
$ busctl call xyz.openbmc_project.eStoraged \
      /xyz/openbmc_project/inventory/storage/mmcblk0 \
      xyz.openbmc_project.Inventory.Item.Volume Lock
$ busctl get-property xyz.openbmc_project.eStoraged \
  /xyz/openbmc_project/inventory/storage/mmcblk0 \
  xyz.openbmc_project.Inventory.Item.Volume Locked
b true
Restarted eStoraged, and checked 'Locked' again.
$ systemctl restart xyz.openbmc_project.eStoraged
$ busctl get-property xyz.openbmc_project.eStoraged \
  /xyz/openbmc_project/inventory/storage/mmcblk0 \
  xyz.openbmc_project.Inventory.Item.Volume Locked
b true

Signed-off-by: John Wedig <johnwedig@google.com>
Change-Id: I5cd6bac4b4426c0e2579c3fc8cf7a27b4f2ccc08
diff --git a/src/test/estoraged_test.cpp b/src/test/estoraged_test.cpp
index 9fde533..a0c56fe 100644
--- a/src/test/estoraged_test.cpp
+++ b/src/test/estoraged_test.cpp
@@ -29,7 +29,6 @@
 using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume;
 using std::filesystem::path;
 using ::testing::_;
-using ::testing::ContainsRegex;
 using ::testing::Return;
 using ::testing::StrEq;
 
@@ -38,6 +37,7 @@
   public:
     const char* testFileName = "testfile";
     const char* testLuksDevName = "testfile_luksDev";
+    const char* testCryptDir = "/tmp";
     const std::string testConfigPath =
         "/xyz/openbmc_project/inventory/system/board/test_board/test_emmc";
     const uint64_t testSize = 24;
@@ -77,6 +77,9 @@
             std::make_unique<MockFilesystemInterface>();
         mockFsIface = fsIface.get();
 
+        /* Set up location of dummy mapped crypt file. */
+        EXPECT_CALL(*cryptIface, cryptGetDir).WillOnce(Return(testCryptDir));
+
         conn = std::make_shared<sdbusplus::asio::connection>(io);
         // request D-Bus server name.
         conn->request_name("xyz.openbmc_project.eStoraged.test");
@@ -94,6 +97,29 @@
     }
 };
 
+const char* mappedDevicePath = "/tmp/testfile_luksDev";
+std::ofstream mappedDevice;
+
+int createMappedDev()
+{
+    mappedDevice.open(mappedDevicePath,
+                      std::ios::out | std::ios::binary | std::ios::trunc);
+    mappedDevice.close();
+    if (mappedDevice.fail())
+    {
+        throw std::runtime_error("Failed to open test mapped device");
+    }
+
+    return 0;
+}
+
+int removeMappedDev()
+{
+    EXPECT_EQ(0, unlink(mappedDevicePath));
+
+    return 0;
+}
+
 /* Test case to format and then lock the LUKS device. */
 TEST_F(EStoragedTest, FormatPass)
 {
@@ -105,9 +131,10 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(0));
 
     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
         .WillOnce(Return(false));
@@ -116,7 +143,7 @@
         .WillOnce(Return(true));
 
     EXPECT_CALL(*mockFsIface,
-                doMount(ContainsRegex("/dev/mapper/"),
+                doMount(StrEq(esObject->getCryptDevicePath()),
                         StrEq(esObject->getMountPoint()), _, _, _))
         .WillOnce(Return(0));
 
@@ -126,7 +153,8 @@
     EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
         .WillOnce(Return(true));
 
-    EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
+    EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
+        .WillOnce(&removeMappedDev);
 
     /* Format the encrypted device. */
     esObject->formatLuks(password, Volume::FilesystemType::ext4);
@@ -150,9 +178,10 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(0));
 
     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
         .WillOnce(Return(true));
@@ -161,7 +190,7 @@
         .Times(0);
 
     EXPECT_CALL(*mockFsIface,
-                doMount(ContainsRegex("/dev/mapper/"),
+                doMount(StrEq(esObject->getCryptDevicePath()),
                         StrEq(esObject->getMountPoint()), _, _, _))
         .WillOnce(Return(0));
 
@@ -171,7 +200,8 @@
     EXPECT_CALL(*mockFsIface, removeDirectory(path(esObject->getMountPoint())))
         .WillOnce(Return(true));
 
-    EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _)).Times(1);
+    EXPECT_CALL(*mockCryptIface, cryptDeactivate(_, _))
+        .WillOnce(&removeMappedDev);
 
     /* Format the encrypted device. */
     esObject->formatLuks(password, Volume::FilesystemType::ext4);
@@ -189,7 +219,7 @@
 
     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
                  ResourceNotFound);
-    EXPECT_FALSE(esObject->isLocked());
+    EXPECT_TRUE(esObject->isLocked());
 
     /* Create the test file again, so that the TearDown function works. */
     testFile.open(testFileName,
@@ -205,7 +235,7 @@
 
     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
                  InternalFailure);
-    EXPECT_FALSE(esObject->isLocked());
+    EXPECT_TRUE(esObject->isLocked());
 }
 
 /* Test case where we fail to set the password for the LUKS device. */
@@ -265,13 +295,16 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(-1));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(-1));
 
     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
                  InternalFailure);
     EXPECT_FALSE(esObject->isLocked());
+
+    EXPECT_EQ(0, removeMappedDev());
 }
 
 /* Test case where we fail to create the mount point. */
@@ -285,9 +318,10 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(0));
 
     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
         .WillOnce(Return(false));
@@ -298,6 +332,8 @@
     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
                  InternalFailure);
     EXPECT_FALSE(esObject->isLocked());
+
+    EXPECT_EQ(0, removeMappedDev());
 }
 
 /* Test case where we fail to mount the filesystem. */
@@ -311,9 +347,10 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(0));
 
     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
         .WillOnce(Return(false));
@@ -322,7 +359,7 @@
         .WillOnce(Return(true));
 
     EXPECT_CALL(*mockFsIface,
-                doMount(ContainsRegex("/dev/mapper/"),
+                doMount(StrEq(esObject->getCryptDevicePath()),
                         StrEq(esObject->getMountPoint()), _, _, _))
         .WillOnce(Return(-1));
 
@@ -332,6 +369,8 @@
     EXPECT_THROW(esObject->formatLuks(password, Volume::FilesystemType::ext4),
                  InternalFailure);
     EXPECT_FALSE(esObject->isLocked());
+
+    EXPECT_EQ(0, removeMappedDev());
 }
 
 /* Test case where we fail to unmount the filesystem. */
@@ -345,9 +384,10 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(0));
 
     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
         .WillOnce(Return(false));
@@ -356,7 +396,7 @@
         .WillOnce(Return(true));
 
     EXPECT_CALL(*mockFsIface,
-                doMount(ContainsRegex("/dev/mapper/"),
+                doMount(StrEq(esObject->getCryptDevicePath()),
                         StrEq(esObject->getMountPoint()), _, _, _))
         .WillOnce(Return(0));
 
@@ -368,6 +408,8 @@
 
     EXPECT_THROW(esObject->lock(), InternalFailure);
     EXPECT_FALSE(esObject->isLocked());
+
+    EXPECT_EQ(0, removeMappedDev());
 }
 
 /* Test case where we fail to remove the mount point. */
@@ -381,9 +423,10 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(0));
 
     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
         .WillOnce(Return(false));
@@ -392,7 +435,7 @@
         .WillOnce(Return(true));
 
     EXPECT_CALL(*mockFsIface,
-                doMount(ContainsRegex("/dev/mapper/"),
+                doMount(StrEq(esObject->getCryptDevicePath()),
                         StrEq(esObject->getMountPoint()), _, _, _))
         .WillOnce(Return(0));
 
@@ -408,6 +451,8 @@
     /* This will fail to remove the mount point. */
     EXPECT_THROW(esObject->lock(), InternalFailure);
     EXPECT_FALSE(esObject->isLocked());
+
+    EXPECT_EQ(0, removeMappedDev());
 }
 
 /* Test case where we fail to deactivate the LUKS device. */
@@ -421,9 +466,10 @@
     EXPECT_CALL(*mockCryptIface, cryptLoad(_, _, _)).Times(1);
 
     EXPECT_CALL(*mockCryptIface, cryptActivateByPassphrase(_, _, _, _, _, _))
-        .Times(1);
+        .WillOnce(&createMappedDev);
 
-    EXPECT_CALL(*mockFsIface, runMkfs(testLuksDevName)).WillOnce(Return(0));
+    EXPECT_CALL(*mockFsIface, runMkfs(StrEq(esObject->getCryptDevicePath())))
+        .WillOnce(Return(0));
 
     EXPECT_CALL(*mockFsIface, directoryExists(path(esObject->getMountPoint())))
         .WillOnce(Return(false));
@@ -432,7 +478,7 @@
         .WillOnce(Return(true));
 
     EXPECT_CALL(*mockFsIface,
-                doMount(ContainsRegex("/dev/mapper/"),
+                doMount(StrEq(esObject->getCryptDevicePath()),
                         StrEq(esObject->getMountPoint()), _, _, _))
         .WillOnce(Return(0));
 
@@ -450,6 +496,8 @@
 
     EXPECT_THROW(esObject->lock(), InternalFailure);
     EXPECT_FALSE(esObject->isLocked());
+
+    EXPECT_EQ(0, removeMappedDev());
 }
 
 /* Test case where we successfully change the password. */
diff --git a/src/test/include/estoraged_test.hpp b/src/test/include/estoraged_test.hpp
index 6809c37..a9dfbc0 100644
--- a/src/test/include/estoraged_test.hpp
+++ b/src/test/include/estoraged_test.hpp
@@ -19,7 +19,8 @@
 class MockFilesystemInterface : public estoraged::FilesystemInterface
 {
   public:
-    MOCK_METHOD(int, runMkfs, (const std::string& logicalVolume), (override));
+    MOCK_METHOD(int, runMkfs, (const std::string& logicalVolumePath),
+                (override));
 
     MOCK_METHOD(int, doMount,
                 (const char* source, const char* target,
@@ -81,6 +82,8 @@
 
     MOCK_METHOD(crypt_keyslot_info, cryptKeySlotStatus,
                 (struct crypt_device * cd, int keyslot), (override));
+
+    MOCK_METHOD(std::string, cryptGetDir, (), (override));
 };
 
 } // namespace estoraged_test