| 1 | // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "diskapitestobj.h" |
| 6 | |
| 7 | #include <DiskUtil/DiskUtil.h> |
| 8 | #include <QDebug> |
| 9 | |
| 10 | QString getDiskKey(QString targetDev) |
| 11 | { |
| 12 | int iIndex = targetDev.lastIndexOf("/"); |
| 13 | |
| 14 | if (iIndex < 0) { |
| 15 | return targetDev; |
| 16 | } |
| 17 | |
| 18 | QString strPath = targetDev.left(iIndex + 1); |
| 19 | QString strKey = targetDev.right(targetDev.length() - iIndex - 1); |
| 20 | return strKey; |
| 21 | } |
| 22 | |
| 23 | // Sets up the test fixture. |
| 24 | void DiskApiTestObj::SetUp() |
| 25 | { |
| 26 | m_pBootMaker = new BootMaker; |
| 27 | } |
| 28 | |
| 29 | // Tears down the test fixture. |
| 30 | void DiskApiTestObj::TearDown() |
| 31 | { |
| 32 | delete m_pBootMaker; |
| 33 | m_pBootMaker = nullptr; |
| 34 | } |
| 35 | |
| 36 | TEST_F(DiskApiTestObj, TestName) |
| 37 | { |
| 38 | qInfo() << "Mount Point Test"; |
| 39 | QList<DeviceInfo> listDeviceInfo = m_pBootMaker->deviceList(); |
| 40 | |
| 41 | if (listDeviceInfo.size() > 0) { |
| 42 | DeviceInfo devInfo = listDeviceInfo.at(0); |
| 43 | QString strDisk = XSys::DiskUtil::GetPartitionDisk(devInfo.path); |
| 44 | qInfo() << "Disk Name" << strDisk; |
| 45 | ASSERT_TRUE(!strDisk.isEmpty()); |
| 46 | QString strDiskKey = getDiskKey(strDisk); |
| 47 | QStringList listPartions = XSys::DiskUtil::GetPartionOfDisk(strDiskKey); |
| 48 | |
| 49 | foreach (QString strPation, listPartions) { |
| 50 | qDebug() << "Partion:" << strPation; |
| 51 | } |
| 52 | |
| 53 | QString strMountPt = XSys::DiskUtil::MountPoint(devInfo.path); |
| 54 | qInfo() << "Mount Point:" << strMountPt; |
| 55 | |
| 56 | if (!strMountPt.isEmpty()) { |
| 57 | bool bRet = XSys::DiskUtil::UmountPartion(devInfo.path); |
| 58 | ASSERT_TRUE(bRet); |
| 59 | strMountPt = XSys::DiskUtil::MountPoint(devInfo.path); |
| 60 | qInfo() << "Mount Point:" << strMountPt; |
| 61 | ASSERT_TRUE(strMountPt.isEmpty()); |
| 62 | } |
| 63 | |
| 64 | bool bRet = XSys::DiskUtil::Mount(devInfo.path); |
| 65 | ASSERT_TRUE(bRet); |
| 66 | strMountPt = XSys::DiskUtil::MountPoint(devInfo.path); |
| 67 | qInfo() << "Mount Point:" << strMountPt; |
| 68 | ASSERT_FALSE(strMountPt.isEmpty()); |
| 69 | |
| 70 | listPartions = XSys::DiskUtil::GetPartionOfDisk(strDiskKey); |
| 71 | ASSERT_GE(listPartions.size(), 1); |
| 72 | |
| 73 | bRet = XSys::DiskUtil::UmountDisk(strDisk); |
| 74 | listPartions.clear(); |
| 75 | strMountPt = XSys::DiskUtil::MountPoint(devInfo.path); |
| 76 | ASSERT_TRUE(!strMountPt.isNull()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | TEST_F(DiskApiTestObj, TestName2) |
| 81 | { |
| 82 | qDebug() << "Format Partion Test"; |
| 83 | QList<DeviceInfo> listDeviceInfo = m_pBootMaker->deviceList(); |
| 84 | |
| 85 | if (listDeviceInfo.size() > 0) { |
| 86 | DeviceInfo devInfo = listDeviceInfo.at(0); |
| 87 | QString strMountPt = XSys::DiskUtil::MountPoint(devInfo.path); |
| 88 | bool bRet = false; |
| 89 | |
| 90 | if (!strMountPt.isEmpty()) { |
| 91 | bRet = XSys::DiskUtil::UmountPartion(devInfo.path); |
| 92 | } |
| 93 | else { |
| 94 | bRet = true; |
| 95 | } |
| 96 | |
| 97 | if (bRet) { |
| 98 | //此处理需要root权限来进行操作,因而也会失败,报告日志为权限不足。 |
| 99 | bRet = XSys::DiskUtil::FormatPartion(devInfo.path); |
| 100 | ASSERT_TRUE(bRet); |
| 101 | } |
| 102 | else { |
| 103 | bRet = false; |
| 104 | ASSERT_FALSE(bRet); |
| 105 | } |
| 106 | |
| 107 | if (bRet) { |
| 108 | QString strDisk = XSys::DiskUtil::GetPartitionDisk(devInfo.path); |
| 109 | ASSERT_TRUE(!strDisk.isEmpty()); |
| 110 | |
| 111 | //此处理需要root权限来进行操作,因而也会失败,报告日志为权限不足。 |
| 112 | bRet = XSys::DiskUtil::SetActivePartion(strDisk, devInfo.path); |
| 113 | ASSERT_FALSE(bRet); |
| 114 | } |
| 115 | } |
| 116 | } |