Fix code to search for MMC device
The findDevice() function is broken currently because it's looking for a
device in sysfs with the name of mmcblk*. The problem is that the boot
partitions are also showing up in sysfs (mmcblk0boot0 and mmcblk0boot1),
and the findDevice() function is getting confused.
Instead of relying on the name of the device to find the MMC device, we
now look at the following entry in sysfs, to make sure we found an MMC
device:
/sys/block/<dev_name>/device/type
The contents of that file should be MMC.
Tested:
Ran eStoraged on a machine to confirm that it created a D-Bus object
with the mmcblk0 device, instead of mmcblk0boot0.
$ busctl tree xyz.openbmc_project.eStoraged
`-/xyz
`-/xyz/openbmc_project
`-/xyz/openbmc_project/inventory
`-/xyz/openbmc_project/inventory/storage
`-/xyz/openbmc_project/inventory/storage/mmcblk0
Signed-off-by: John Wedig <johnwedig@google.com>
Change-Id: I786934fcdc950b55c62bc7e3784e29d5ba73099f
diff --git a/src/test/util_test.cpp b/src/test/util_test.cpp
index a4b796c..807eb20 100644
--- a/src/test/util_test.cpp
+++ b/src/test/util_test.cpp
@@ -1,7 +1,5 @@
#include "getConfig.hpp"
-#include <unistd.h>
-
#include <boost/container/flat_map.hpp>
#include <util.hpp>
@@ -65,18 +63,23 @@
estoraged::BasicVariantType("EmmcDevice"));
data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
- /* Create a dummy device file. */
- const std::string testDevName("mmcblk0");
- std::ofstream testFile;
- testFile.open(testDevName,
- std::ios::out | std::ios::binary | std::ios::trunc);
- testFile.close();
+ /* Create a dummy device. */
+ std::filesystem::create_directories("abc/device");
+ const std::string dummyTypeFileName("abc/device/type");
+ std::ofstream dummyTypeFile(dummyTypeFileName,
+ std::ios::out | std::ios::trunc);
+ dummyTypeFile << "SSD";
+ dummyTypeFile.close();
- /* Create another dummy file. */
- const std::string testDummyFilename("abc");
- testFile.open(testDummyFilename,
- std::ios::out | std::ios::binary | std::ios::trunc);
- testFile.close();
+ /* Another device. */
+ std::filesystem::create_directories("def/device");
+
+ /* Create a dummy eMMC device. */
+ std::filesystem::create_directories("mmcblk0/device");
+ const std::string typeFileName("mmcblk0/device/type");
+ std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
+ typeFile << "MMC";
+ typeFile.close();
/* Look for the device file. */
std::filesystem::path deviceFile, sysfsDir;
@@ -90,8 +93,9 @@
EXPECT_EQ("luks-mmcblk0", luksName);
/* Delete the dummy files. */
- EXPECT_EQ(0, unlink(testDevName.c_str()));
- EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
+ EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0"));
+ EXPECT_EQ(3, std::filesystem::remove_all("abc"));
+ EXPECT_EQ(2, std::filesystem::remove_all("def"));
}
/* Test case where the "Type" property doesn't exist. */
@@ -102,18 +106,23 @@
/* Set up the map of properties (with the "Type" property missing). */
data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
- /* Create a dummy device file. */
- const std::string testDevName("mmcblk0");
- std::ofstream testFile;
- testFile.open(testDevName,
- std::ios::out | std::ios::binary | std::ios::trunc);
- testFile.close();
+ /* Create a dummy device. */
+ std::filesystem::create_directories("abc/device");
+ const std::string dummyTypeFileName("abc/device/type");
+ std::ofstream dummyTypeFile(dummyTypeFileName,
+ std::ios::out | std::ios::trunc);
+ dummyTypeFile << "SSD";
+ dummyTypeFile.close();
- /* Create another dummy file. */
- const std::string testDummyFilename("abc");
- testFile.open(testDummyFilename,
- std::ios::out | std::ios::binary | std::ios::trunc);
- testFile.close();
+ /* Another device. */
+ std::filesystem::create_directories("def/device");
+
+ /* Create a dummy eMMC device. */
+ std::filesystem::create_directories("mmcblk0/device");
+ const std::string typeFileName("mmcblk0/device/type");
+ std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
+ typeFile << "MMC";
+ typeFile.close();
/* Look for the device file. */
std::filesystem::path deviceFile, sysfsDir;
@@ -122,8 +131,9 @@
deviceFile, sysfsDir, luksName));
/* Delete the dummy files. */
- EXPECT_EQ(0, unlink(testDevName.c_str()));
- EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
+ EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0"));
+ EXPECT_EQ(3, std::filesystem::remove_all("abc"));
+ EXPECT_EQ(2, std::filesystem::remove_all("def"));
}
/* Test case where the device type is not supported. */
@@ -136,18 +146,23 @@
data.emplace(std::string("Name"),
estoraged::BasicVariantType("some_drive"));
- /* Create a dummy device file. */
- const std::string testDevName("mmcblk0");
- std::ofstream testFile;
- testFile.open(testDevName,
- std::ios::out | std::ios::binary | std::ios::trunc);
- testFile.close();
+ /* Create a dummy device. */
+ std::filesystem::create_directories("abc/device");
+ const std::string dummyTypeFileName("abc/device/type");
+ std::ofstream dummyTypeFile(dummyTypeFileName,
+ std::ios::out | std::ios::trunc);
+ dummyTypeFile << "SSD";
+ dummyTypeFile.close();
- /* Create another dummy file. */
- const std::string testDummyFilename("abc");
- testFile.open(testDummyFilename,
- std::ios::out | std::ios::binary | std::ios::trunc);
- testFile.close();
+ /* Another device. */
+ std::filesystem::create_directories("def/device");
+
+ /* Create a dummy eMMC device. */
+ std::filesystem::create_directories("mmcblk0/device");
+ const std::string typeFileName("mmcblk0/device/type");
+ std::ofstream typeFile(typeFileName, std::ios::out | std::ios::trunc);
+ typeFile << "MMC";
+ typeFile.close();
/* Look for the device file. */
std::filesystem::path deviceFile, sysfsDir;
@@ -156,8 +171,9 @@
deviceFile, sysfsDir, luksName));
/* Delete the dummy files. */
- EXPECT_EQ(0, unlink(testDevName.c_str()));
- EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
+ EXPECT_EQ(3, std::filesystem::remove_all("mmcblk0"));
+ EXPECT_EQ(3, std::filesystem::remove_all("abc"));
+ EXPECT_EQ(2, std::filesystem::remove_all("def"));
}
/* Test case where we can't find the device file. */
@@ -170,12 +186,16 @@
estoraged::BasicVariantType("EmmcDevice"));
data.emplace(std::string("Name"), estoraged::BasicVariantType("emmc"));
- /* Create a dummy file. */
- const std::string testDummyFilename("abc");
- std::ofstream testFile;
- testFile.open(testDummyFilename,
- std::ios::out | std::ios::binary | std::ios::trunc);
- testFile.close();
+ /* Create a dummy device. */
+ std::filesystem::create_directories("abc/device");
+ const std::string dummyTypeFileName("abc/device/type");
+ std::ofstream dummyTypeFile(dummyTypeFileName,
+ std::ios::out | std::ios::trunc);
+ dummyTypeFile << "SSD";
+ dummyTypeFile.close();
+
+ /* Another device. */
+ std::filesystem::create_directories("def/device");
/* Look for the device file. */
std::filesystem::path deviceFile, sysfsDir;
@@ -183,8 +203,9 @@
EXPECT_FALSE(estoraged::util::findDevice(data, std::filesystem::path("./"),
deviceFile, sysfsDir, luksName));
- /* Delete the dummy file. */
- EXPECT_EQ(0, unlink(testDummyFilename.c_str()));
+ /* Delete the dummy files. */
+ EXPECT_EQ(3, std::filesystem::remove_all("abc"));
+ EXPECT_EQ(2, std::filesystem::remove_all("def"));
}
} // namespace estoraged_test