Skip to content

Commit

Permalink
test: [vault] add vault UT
Browse files Browse the repository at this point in the history
add vault UT
  • Loading branch information
GongHeng2017 authored and deepin-bot[bot] committed Jul 25, 2023
1 parent 8975346 commit bc12e2d
Show file tree
Hide file tree
Showing 11 changed files with 916 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ bool OperatorCenter::createDirAndFile()
if (!configDir.exists()) {
bool ok = configDir.mkpath(strConfigDir);
if (!ok) {
qDebug() << "create config dir failure!";
qCritical() << "Vault: create config dir failed!";
return false;
}
}
Expand All @@ -218,15 +218,15 @@ bool OperatorCenter::createDirAndFile()
configFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup);
configFile.close();
} else {
qInfo() << "保险箱:创建配置文件失败!";
qCritical() << "Vault:create config file failed!";
}
}

// 创建存放rsa公钥的文件,并设置文件权限
QString strPriKeyFile = makeVaultLocalPath(kRSAPUBKeyFileName);
QFile prikeyFile(strPriKeyFile);
if (!prikeyFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
qDebug() << "create rsa private key file failure!";
qCritical() << "Vault: create rsa private key file failed!";
return false;
}
prikeyFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup);
Expand All @@ -236,7 +236,7 @@ bool OperatorCenter::createDirAndFile()
QString strRsaCiphertext = makeVaultLocalPath(kRSACiphertextFileName);
QFile rsaCiphertextFile(strRsaCiphertext);
if (!rsaCiphertextFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
qDebug() << "create rsa ciphertext file failure!";
qCritical() << "Vault: create rsa ciphertext file failed!";
return false;
}
rsaCiphertextFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup);
Expand All @@ -246,7 +246,7 @@ bool OperatorCenter::createDirAndFile()
QString strPasswordHintFilePath = makeVaultLocalPath(kPasswordHintFileName);
QFile passwordHintFile(strPasswordHintFilePath);
if (!passwordHintFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
qDebug() << "create password hint file failure!";
qCritical() << "Vault: create password hint file failed!";
return false;
}
passwordHintFile.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ReadGroup);
Expand All @@ -271,7 +271,7 @@ bool OperatorCenter::savePasswordAndPasswordHint(const QString &password, const
const QString &strPasswordHintFilePath = makeVaultLocalPath(kPasswordHintFileName);
QFile passwordHintFile(strPasswordHintFilePath);
if (!passwordHintFile.open(QIODevice::Text | QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "write password hint failure";
qCritical() << "Vault: open password hint file failed!";
return false;
}
QTextStream out2(&passwordHintFile);
Expand Down Expand Up @@ -305,7 +305,7 @@ bool OperatorCenter::createKey(const QString &password, int bytes)
// 将公钥分成两部分(一部分用于保存到本地,一部分生成二维码,提供给用户)
QString strSaveToLocal("");
if (strPubKey.length() < 2 * kUserKeyInterceptIndex + bytes) {
qDebug() << "USER_KEY_LENGTH is to long!";
qCritical() << "Vault: USER_KEY_LENGTH is too long!";
return false;
}
QString strPart1 = strPubKey.mid(0, kUserKeyInterceptIndex);
Expand All @@ -318,7 +318,7 @@ bool OperatorCenter::createKey(const QString &password, int bytes)
QString publicFilePath = makeVaultLocalPath(kRSAPUBKeyFileName);
QFile publicFile(publicFilePath);
if (!publicFile.open(QIODevice::Text | QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "open public key file failure!";
qCritical() << "Vault: open public key file failure!";
return false;
}
QTextStream out(&publicFile);
Expand All @@ -329,7 +329,7 @@ bool OperatorCenter::createKey(const QString &password, int bytes)
QString strCipherFilePath = makeVaultLocalPath(kRSACiphertextFileName);
QFile cipherFile(strCipherFilePath);
if (!cipherFile.open(QIODevice::Text | QIODevice::WriteOnly | QIODevice::Truncate)) {
qDebug() << "open rsa cipher file failure!";
qCritical() << "Vault: open rsa cipher file failure!";
return false;
}
QTextStream out2(&cipherFile);
Expand Down Expand Up @@ -358,7 +358,7 @@ bool OperatorCenter::checkPassword(const QString &password, QString &cipher)
QString strNewCipher2 = pbkdf2::pbkdf2EncrypyPassword(strNewSaltAndCipher, strSalt, kIterationTwo, kPasswordCipherLength);

if (strCipher != strNewCipher2) {
qDebug() << "password error!";
qCritical() << "Vault: password error!";
return false;
}

Expand All @@ -373,7 +373,7 @@ bool OperatorCenter::checkPassword(const QString &password, QString &cipher)
QString strfilePath = makeVaultLocalPath(kPasswordFileName);
QFile file(strfilePath);
if (!file.open(QIODevice::Text | QIODevice::ReadOnly)) {
qDebug() << "open pbkdf2cipher file failure!";
qCritical() << "Vault: open pbkdf2cipher file failed!";
return false;
}
QString strSaltAndCipher = QString(file.readAll());
Expand All @@ -385,15 +385,15 @@ bool OperatorCenter::checkPassword(const QString &password, QString &cipher)
QString strNewCipher = pbkdf2::pbkdf2EncrypyPassword(password, strSalt, kIteration, kPasswordCipherLength);
QString strNewSaltAndCipher = strSalt + strNewCipher;
if (strNewSaltAndCipher != strSaltAndCipher) {
qDebug() << "password error!";
qCritical() << "Vault: password error!";
return false;
}

cipher = strNewSaltAndCipher;

// 保存第二次加密后的密文,并更新保险箱版本信息
if (!secondSaveSaltAndCiphertext(strNewSaltAndCipher, strSalt, kConfigVaultVersion)) {
qDebug() << "第二次加密密文失败!";
qCritical() << "Vault: the second encrypt failed!";
return false;
}

Expand All @@ -406,15 +406,15 @@ bool OperatorCenter::checkPassword(const QString &password, QString &cipher)
bool OperatorCenter::checkUserKey(const QString &userKey, QString &cipher)
{
if (userKey.length() != kUserKeyLength) {
qDebug() << "user key length error!";
qCritical() << "Vault: user key length error!";
return false;
}

// 结合本地公钥和用户密钥,还原完整公钥
QString strLocalPubKeyFilePath = makeVaultLocalPath(kRSAPUBKeyFileName);
QFile localPubKeyfile(strLocalPubKeyFilePath);
if (!localPubKeyfile.open(QIODevice::Text | QIODevice::ReadOnly)) {
qDebug() << "cant't open local public key file!";
qCritical() << "Vault: cant't open local public key file!";
return false;
}
QString strLocalPubKey(localPubKeyfile.readAll());
Expand All @@ -426,7 +426,7 @@ bool OperatorCenter::checkUserKey(const QString &userKey, QString &cipher)
QString strRSACipherFilePath = makeVaultLocalPath(kRSACiphertextFileName);
QFile rsaCipherfile(strRSACipherFilePath);
if (!rsaCipherfile.open(QIODevice::Text | QIODevice::ReadOnly)) {
qDebug() << "cant't open rsa cipher file!";
qCritical() << "Vault: cant't open rsa cipher file!";
return false;
}
QString strRsaCipher(rsaCipherfile.readAll());
Expand All @@ -436,7 +436,7 @@ bool OperatorCenter::checkUserKey(const QString &userKey, QString &cipher)

// 判断密码的正确性,如果密码正确,则用户密钥正确,否则用户密钥错误
if (!checkPassword(strNewPassword, cipher)) {
qDebug() << "user key error!";
qCritical() << "Vault: user key error!";
return false;
}

Expand All @@ -453,7 +453,7 @@ bool OperatorCenter::getPasswordHint(QString &passwordHint)
QString strPasswordHintFilePath = makeVaultLocalPath(kPasswordHintFileName);
QFile passwordHintFile(strPasswordHintFilePath);
if (!passwordHintFile.open(QIODevice::Text | QIODevice::ReadOnly)) {
qDebug() << "open password hint file failure";
qCritical() << "open password hint file failure";
return false;
}
passwordHint = QString(passwordHintFile.readAll());
Expand Down Expand Up @@ -542,13 +542,13 @@ int OperatorCenter::executionShellCommand(const QString &strCmd, QStringList &ls

// 命令为空
if (strCmd.isEmpty()) {
qDebug() << "cmd is empty!";
qCritical() << "Vault: the shell cmd is empty!";
return -1;
}

if ((fp = popen(cmd, "r")) == nullptr) {
perror("popen");
qDebug() << QString("popen error: %s").arg(strerror(errno));
qCritical() << QString("Vault: popen error: %s").arg(strerror(errno));
return -1;
} else {
char buf[kBuffterMaxLine] = { '\0' };
Expand All @@ -561,12 +561,12 @@ int OperatorCenter::executionShellCommand(const QString &strCmd, QStringList &ls

int res;
if ((res = pclose(fp)) == -1) {
qDebug() << "close popen file pointer fp error!";
qCritical() << "Vault: close popen file pointer fp failed!";
return res;
} else if (res == 0) {
return res;
} else {
qDebug() << QString("popen res is : %1").arg(res);
qCritical() << QString("Vault: popen res is : %1").arg(res);
return res;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ QString pbkdf2::createRandomSalt(int byte)
QString pbkdf2::pbkdf2EncrypyPassword(const QString &password, const QString &randSalt, int iteration, int cipherByteNum)
{
if (cipherByteNum < 0 || cipherByteNum % 2 != 0) {
qDebug() << "cipherByteNum can't less than zero and must be even!";
qCritical() << "Vault: cipherByteNum can't less than zero and must be even!";
return "";
}
// 字节长度
Expand Down Expand Up @@ -90,7 +90,7 @@ QString pbkdf2::pbkdf2EncrypyPassword(const QString &password, const QString &ra
if (pstr)
free(pstr);
} else {
qDebug() << "PKCS5_PBKDF2_HMAC_SHA1 failed";
qCritical() << "Vault: the function of PKCS5_PBKDF2_HMAC_SHA1 failed";
}
free(out);
return strCipherText;
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/filemanager/dfmplugin-vault/utils/operator/rsam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bool rsam::createPublicAndPrivateKey(QString &publicKey, QString &privateKey)
BN_set_word(pNum, RSA_F4);
int nRet = RSA_generate_key_ex(pRsa, kKeyLength, pNum, nullptr);
if (nRet != 1) {
qDebug() << "RSA_generate_key_ex 失败!";
qCritical() << "Vault: the function of RSA_generate_key_ex run failed!";
return false;
}

Expand Down Expand Up @@ -66,15 +66,15 @@ QString rsam::privateKeyEncrypt(const QString &password, const QString &privateK
uchar *pPrivateKey = reinterpret_cast<uchar *>(privateKeyArry.data());
BIO *pPrivateKeyBio = BIO_new_mem_buf(pPrivateKey, privateKey.length());
if (pPrivateKeyBio == nullptr) {
qDebug() << "BIO_new_mem_buf 失败!";
qCritical() << "Vault: the function of BIO_new_mem_buf run failed!";
return "";
}

RSA *pRsa = RSA_new();
pRsa = PEM_read_bio_RSAPrivateKey(pPrivateKeyBio, &pRsa, nullptr, nullptr);
if (pRsa == nullptr) {
BIO_free_all(pPrivateKeyBio);
qDebug() << "PEM_read_bio_RSAPrivateKey 失败!";
qCritical() << "Vault: the function of PEM_read_bio_RSAPrivateKey run failed!";
return "";
}

Expand Down Expand Up @@ -111,7 +111,7 @@ QString rsam::publicKeyDecrypt(const QString &ciphertext, const QString &publicK
uchar *pPublicKey = reinterpret_cast<uchar *>(publickKeyArry.data());
BIO *pPublicKeyBio = BIO_new_mem_buf(pPublicKey, publicKey.length());
if (pPublicKeyBio == nullptr) {
qDebug() << "BIO_new_mem_buf 失败!";
qCritical() << "Vault: the function of BIO_new_mem_buf run failed!";
return "";
}

Expand All @@ -123,7 +123,7 @@ QString rsam::publicKeyDecrypt(const QString &ciphertext, const QString &publicK
}

if (!pRsa) {
qDebug() << "PEM_read_bio_RSAPublicKey 失败!";
qCritical() << "Vault: the function of PEM_read_bio_RSAPublicKey run failed!";
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ TEST(UT_WorkspacePluginBugTest, bug_169379_NotInit)


FileView view(QUrl("/home"));
view.d->delegates.insert(static_cast<int>(Global::ViewMode::kListMode), nullptr);
view.setViewMode(Global::ViewMode::kListMode);
EXPECT_TRUE(isInit);
}

TEST(UT_WorkspacePluginBugTest, bug_140799_desktopFilter)
{
bool isExcuDesktopFileCheck { false };
stub_ext::StubExt stub;
typedef QVariant(*FuncType)(Application::ApplicationAttribute);
stub.set_lamda(static_cast<FuncType>(&Application::appAttribute), [] {return QVariant(false); });
stub.set_lamda(&FileSortWorker::checkNameFilters, [ &isExcuDesktopFileCheck ] { isExcuDesktopFileCheck = true; });
stub.set_lamda(&FileSortWorker::checkNameFilters, []{});

FileViewModel model;
FileInfoPointer info(new FileInfo(QUrl("file:///home")));
FileSortWorker *work = new FileSortWorker(QUrl("file:///home"), "1234");
FileSortWorker *work = new FileSortWorker(QUrl("file:///home"), "1234", nullptr, {"*.desktop"});
model.nameFilters = QStringList { "*.desktop" };
model.filterSortWorker.reset(work);
EXPECT_TRUE(isExcuDesktopFileCheck);
EXPECT_TRUE(model.nameFilters == work->nameFilters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <QStandardPaths>
#include <QProcess>
#include <QDBusConnection>

DPVAULT_USE_NAMESPACE
DWIDGET_USE_NAMESPACE
Expand All @@ -42,6 +43,14 @@ TEST(UT_VaultPluginBugTest, bug_178305_NotRegisterService)
bool isRegisterService { false };
stub_ext::StubExt stub;
stub.set_lamda(&VaultVisibleManager::pluginServiceRegister, [ &isRegisterService ] { __DBG_STUB_INVOKE__ isRegisterService = true; });
typedef bool(QDBusConnection::*FuncType)(const QString &, const QString &, const QString &, const QString &, QObject *, const char *);
stub.set_lamda(static_cast<FuncType>(&QDBusConnection::connect), []{
return true;
});
typedef bool(QDBusConnection::*FuncType2)(const QString &, const QString &, const QString &, const QString &, const QString &, QObject *, const char *);
stub.set_lamda(static_cast<FuncType2>(&QDBusConnection::connect), []{
return true;
});

Vault plugin;
plugin.initialize();
Expand Down
Loading

0 comments on commit bc12e2d

Please sign in to comment.