mirror of
https://github.com/chenx-dust/EZ4Connect.git
synced 2026-09-20 10:23:33 +08:00
feat: 配置向导中的凭据填写
This commit is contained in:
@@ -242,4 +242,23 @@ if(BUILD_TESTING)
|
||||
target_include_directories(profilemanager_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(profilemanager_test Qt::Core)
|
||||
add_test(NAME profilemanager_test COMMAND profilemanager_test)
|
||||
|
||||
add_executable(configurationguidedialog_test
|
||||
tests/configurationguidedialog_test.cpp
|
||||
presentation/dialogs/configurationguidedialog/configurationguidedialog.cpp
|
||||
presentation/dialogs/authinfowindow/authinfowindow.cpp
|
||||
infrastructure/coreprocess/consoleoutputdecoder.cpp
|
||||
infrastructure/coreprocess/coreexecutable.cpp
|
||||
)
|
||||
target_include_directories(configurationguidedialog_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(configurationguidedialog_test
|
||||
Qt::Core
|
||||
Qt::Gui
|
||||
Qt::Widgets
|
||||
Qt::Core5Compat
|
||||
)
|
||||
add_test(NAME configurationguidedialog_test COMMAND configurationguidedialog_test)
|
||||
set_tests_properties(configurationguidedialog_test PROPERTIES
|
||||
ENVIRONMENT "QT_QPA_PLATFORM=offscreen"
|
||||
)
|
||||
endif()
|
||||
|
||||
@@ -202,7 +202,7 @@ void ConnectionUiController::handleConnectClicked()
|
||||
parentWidget,
|
||||
"需要配置证书",
|
||||
"当前配置选择了证书认证。\n"
|
||||
"请先在“文件 → 设置 → 认证”中选择证书文件。"
|
||||
"请通过“文件 → 配置引导”选择证书文件。"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "configurationguidedialog.h"
|
||||
|
||||
#include <QCheckBox>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QFileDialog>
|
||||
#include <QFormLayout>
|
||||
#include <QFont>
|
||||
#include <QGroupBox>
|
||||
@@ -11,9 +13,11 @@
|
||||
#include <QPushButton>
|
||||
#include <QRadioButton>
|
||||
#include <QRegularExpression>
|
||||
#include <QRegularExpressionValidator>
|
||||
#include <QSettings>
|
||||
#include <QSpinBox>
|
||||
#include <QStackedWidget>
|
||||
#include <QStandardPaths>
|
||||
#include <QStringList>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
@@ -65,6 +69,7 @@ ConfigurationGuideDialog::ConfigurationGuideDialog(
|
||||
pages->addWidget(createProtocolPage());
|
||||
pages->addWidget(createServerPage(requestProfileName));
|
||||
pages->addWidget(createAuthenticationPage());
|
||||
pages->addWidget(createCredentialsPage());
|
||||
layout->addWidget(pages, 1);
|
||||
|
||||
auto *buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
|
||||
@@ -114,6 +119,29 @@ ConfigurationGuideDialog::ConfigurationGuideDialog(
|
||||
easyconnectAuthType != "certificate"
|
||||
);
|
||||
|
||||
usernameLineEdit->setText(
|
||||
sourceSettings->value("Credential/Username").toString()
|
||||
);
|
||||
passwordLineEdit->setText(QByteArray::fromBase64(
|
||||
sourceSettings->value("Credential/Password").toByteArray()
|
||||
));
|
||||
totpSecretLineEdit->setText(
|
||||
sourceSettings->value("Credential/TOTPSecret").toString()
|
||||
);
|
||||
certificateTotpSecretLineEdit->setText(totpSecretLineEdit->text());
|
||||
countryCodeLineEdit->setText(
|
||||
sourceSettings->value("ZJUConnect/PhoneCountryCode", "86").toString()
|
||||
);
|
||||
phoneNumberLineEdit->setText(
|
||||
sourceSettings->value("ZJUConnect/PhoneNumber").toString()
|
||||
);
|
||||
certificateFileLineEdit->setText(
|
||||
sourceSettings->value("Credential/CertFile").toString()
|
||||
);
|
||||
certificatePasswordLineEdit->setText(QByteArray::fromBase64(
|
||||
sourceSettings->value("Credential/CertPassword").toByteArray()
|
||||
));
|
||||
|
||||
updateProtocolPage();
|
||||
updateNavigation();
|
||||
}
|
||||
@@ -130,6 +158,34 @@ void ConfigurationGuideDialog::applyTo(QSettings &settings) const
|
||||
serverAddressLineEdit->text().trimmed()
|
||||
);
|
||||
settings.setValue("ZJUConnect/ServerPort", serverPortSpinBox->value());
|
||||
settings.setValue(
|
||||
"Credential/Username",
|
||||
usernameLineEdit->text().trimmed()
|
||||
);
|
||||
settings.setValue(
|
||||
"Credential/Password",
|
||||
QString(passwordLineEdit->text().toUtf8().toBase64())
|
||||
);
|
||||
settings.setValue(
|
||||
"Credential/TOTPSecret",
|
||||
totpSecretLineEdit->text().trimmed()
|
||||
);
|
||||
settings.setValue(
|
||||
"Credential/CertFile",
|
||||
certificateFileLineEdit->text().trimmed()
|
||||
);
|
||||
settings.setValue(
|
||||
"Credential/CertPassword",
|
||||
QString(certificatePasswordLineEdit->text().toUtf8().toBase64())
|
||||
);
|
||||
settings.setValue(
|
||||
"ZJUConnect/PhoneCountryCode",
|
||||
countryCodeLineEdit->text().trimmed()
|
||||
);
|
||||
settings.setValue(
|
||||
"ZJUConnect/PhoneNumber",
|
||||
phoneNumberLineEdit->text().trimmed()
|
||||
);
|
||||
|
||||
if (atrustRadioButton->isChecked())
|
||||
{
|
||||
@@ -258,7 +314,7 @@ QWidget *ConfigurationGuideDialog::createAuthenticationPage()
|
||||
easyconnectPage
|
||||
);
|
||||
auto *certificateHint = new QLabel(
|
||||
"选择证书认证后,可在“文件 → 设置 → 认证”中配置证书文件。",
|
||||
"选择证书认证后,下一步填写证书文件和密码。",
|
||||
easyconnectPage
|
||||
);
|
||||
certificateHint->setWordWrap(true);
|
||||
@@ -276,6 +332,214 @@ QWidget *ConfigurationGuideDialog::createAuthenticationPage()
|
||||
this,
|
||||
&ConfigurationGuideDialog::fetchAuthenticationMethods
|
||||
);
|
||||
connect(
|
||||
passwordAuthenticationRadioButton,
|
||||
&QRadioButton::toggled,
|
||||
this,
|
||||
&ConfigurationGuideDialog::updateCredentialsPage
|
||||
);
|
||||
return page;
|
||||
}
|
||||
|
||||
QWidget *ConfigurationGuideDialog::createCredentialsPage()
|
||||
{
|
||||
auto *page = new QWidget(this);
|
||||
auto *pageLayout = new QVBoxLayout(page);
|
||||
credentialPages = new QStackedWidget(page);
|
||||
credentialPages->setObjectName("credentialPages");
|
||||
|
||||
auto *passwordPage = new QWidget(credentialPages);
|
||||
auto *passwordPageLayout = new QVBoxLayout(passwordPage);
|
||||
auto *passwordGroup = new QGroupBox("账号凭据", passwordPage);
|
||||
auto *passwordForm = new QFormLayout(passwordGroup);
|
||||
passwordForm->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
|
||||
|
||||
usernameLineEdit = new QLineEdit(passwordGroup);
|
||||
usernameLineEdit->setObjectName("guideUsernameLineEdit");
|
||||
usernameLineEdit->setPlaceholderText("VPN 账号");
|
||||
passwordForm->addRow("账号", usernameLineEdit);
|
||||
|
||||
auto *passwordRow = new QWidget(passwordGroup);
|
||||
auto *passwordRowLayout = new QHBoxLayout(passwordRow);
|
||||
passwordRowLayout->setContentsMargins(0, 0, 0, 0);
|
||||
passwordLineEdit = new QLineEdit(passwordRow);
|
||||
passwordLineEdit->setObjectName("guidePasswordLineEdit");
|
||||
passwordLineEdit->setEchoMode(QLineEdit::Password);
|
||||
passwordLineEdit->setPlaceholderText("VPN 密码");
|
||||
auto *showPasswordCheckBox = new QCheckBox("显示", passwordRow);
|
||||
passwordRowLayout->addWidget(passwordLineEdit, 1);
|
||||
passwordRowLayout->addWidget(showPasswordCheckBox);
|
||||
passwordForm->addRow("密码", passwordRow);
|
||||
|
||||
auto *totpRow = new QWidget(passwordGroup);
|
||||
auto *totpRowLayout = new QHBoxLayout(totpRow);
|
||||
totpRowLayout->setContentsMargins(0, 0, 0, 0);
|
||||
totpSecretLineEdit = new QLineEdit(totpRow);
|
||||
totpSecretLineEdit->setObjectName("guideTotpSecretLineEdit");
|
||||
totpSecretLineEdit->setEchoMode(QLineEdit::Password);
|
||||
totpSecretLineEdit->setPlaceholderText("可选,TOTP 验证器密钥");
|
||||
auto *showTotpCheckBox = new QCheckBox("显示", totpRow);
|
||||
totpRowLayout->addWidget(totpSecretLineEdit, 1);
|
||||
totpRowLayout->addWidget(showTotpCheckBox);
|
||||
passwordForm->addRow("TOTP 密钥", totpRow);
|
||||
|
||||
passwordPageLayout->addWidget(passwordGroup);
|
||||
passwordPageLayout->addStretch();
|
||||
credentialPages->addWidget(passwordPage);
|
||||
|
||||
auto *phonePage = new QWidget(credentialPages);
|
||||
auto *phonePageLayout = new QVBoxLayout(phonePage);
|
||||
auto *phoneGroup = new QGroupBox("短信验证手机号", phonePage);
|
||||
auto *phoneForm = new QFormLayout(phoneGroup);
|
||||
phoneForm->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
|
||||
auto *phoneRow = new QWidget(phoneGroup);
|
||||
auto *phoneRowLayout = new QHBoxLayout(phoneRow);
|
||||
phoneRowLayout->setContentsMargins(0, 0, 0, 0);
|
||||
countryCodeLineEdit = new QLineEdit(phoneRow);
|
||||
countryCodeLineEdit->setObjectName("guideCountryCodeLineEdit");
|
||||
countryCodeLineEdit->setMaximumWidth(64);
|
||||
countryCodeLineEdit->setPlaceholderText("86");
|
||||
countryCodeLineEdit->setValidator(new QRegularExpressionValidator(
|
||||
QRegularExpression("[0-9]{1,4}"),
|
||||
countryCodeLineEdit
|
||||
));
|
||||
phoneNumberLineEdit = new QLineEdit(phoneRow);
|
||||
phoneNumberLineEdit->setObjectName("guidePhoneNumberLineEdit");
|
||||
phoneNumberLineEdit->setPlaceholderText("手机号码");
|
||||
phoneNumberLineEdit->setValidator(new QRegularExpressionValidator(
|
||||
QRegularExpression("[0-9]{1,20}"),
|
||||
phoneNumberLineEdit
|
||||
));
|
||||
phoneRowLayout->addWidget(new QLabel("+", phoneRow));
|
||||
phoneRowLayout->addWidget(countryCodeLineEdit);
|
||||
phoneRowLayout->addWidget(phoneNumberLineEdit, 1);
|
||||
phoneForm->addRow("手机号", phoneRow);
|
||||
phonePageLayout->addWidget(phoneGroup);
|
||||
phonePageLayout->addStretch();
|
||||
credentialPages->addWidget(phonePage);
|
||||
|
||||
auto *certificatePage = new QWidget(credentialPages);
|
||||
auto *certificatePageLayout = new QVBoxLayout(certificatePage);
|
||||
auto *certificateGroup = new QGroupBox("证书凭据", certificatePage);
|
||||
auto *certificateForm = new QFormLayout(certificateGroup);
|
||||
certificateForm->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
|
||||
|
||||
auto *certificateFileRow = new QWidget(certificateGroup);
|
||||
auto *certificateFileRowLayout = new QHBoxLayout(certificateFileRow);
|
||||
certificateFileRowLayout->setContentsMargins(0, 0, 0, 0);
|
||||
certificateFileLineEdit = new QLineEdit(certificateFileRow);
|
||||
certificateFileLineEdit->setObjectName("guideCertificateFileLineEdit");
|
||||
certificateFileLineEdit->setPlaceholderText("P12 或 PFX 证书文件");
|
||||
auto *browseCertificateButton = new QPushButton(
|
||||
"浏览...",
|
||||
certificateFileRow
|
||||
);
|
||||
certificateFileRowLayout->addWidget(certificateFileLineEdit, 1);
|
||||
certificateFileRowLayout->addWidget(browseCertificateButton);
|
||||
certificateForm->addRow("证书文件", certificateFileRow);
|
||||
|
||||
auto *certificatePasswordRow = new QWidget(certificateGroup);
|
||||
auto *certificatePasswordRowLayout = new QHBoxLayout(
|
||||
certificatePasswordRow
|
||||
);
|
||||
certificatePasswordRowLayout->setContentsMargins(0, 0, 0, 0);
|
||||
certificatePasswordLineEdit = new QLineEdit(certificatePasswordRow);
|
||||
certificatePasswordLineEdit->setObjectName(
|
||||
"guideCertificatePasswordLineEdit"
|
||||
);
|
||||
certificatePasswordLineEdit->setEchoMode(QLineEdit::Password);
|
||||
certificatePasswordLineEdit->setPlaceholderText("可选,证书密码");
|
||||
auto *showCertificatePasswordCheckBox = new QCheckBox(
|
||||
"显示",
|
||||
certificatePasswordRow
|
||||
);
|
||||
certificatePasswordRowLayout->addWidget(certificatePasswordLineEdit, 1);
|
||||
certificatePasswordRowLayout->addWidget(showCertificatePasswordCheckBox);
|
||||
certificateForm->addRow("证书密码", certificatePasswordRow);
|
||||
|
||||
auto *certificateTotpRow = new QWidget(certificateGroup);
|
||||
auto *certificateTotpRowLayout = new QHBoxLayout(certificateTotpRow);
|
||||
certificateTotpRowLayout->setContentsMargins(0, 0, 0, 0);
|
||||
certificateTotpSecretLineEdit = new QLineEdit(certificateTotpRow);
|
||||
certificateTotpSecretLineEdit->setObjectName(
|
||||
"guideCertificateTotpSecretLineEdit"
|
||||
);
|
||||
certificateTotpSecretLineEdit->setEchoMode(QLineEdit::Password);
|
||||
certificateTotpSecretLineEdit->setPlaceholderText(
|
||||
"可选,TOTP 验证器密钥"
|
||||
);
|
||||
auto *showCertificateTotpCheckBox = new QCheckBox(
|
||||
"显示",
|
||||
certificateTotpRow
|
||||
);
|
||||
certificateTotpRowLayout->addWidget(certificateTotpSecretLineEdit, 1);
|
||||
certificateTotpRowLayout->addWidget(showCertificateTotpCheckBox);
|
||||
certificateForm->addRow("TOTP 密钥", certificateTotpRow);
|
||||
certificatePageLayout->addWidget(certificateGroup);
|
||||
certificatePageLayout->addStretch();
|
||||
credentialPages->addWidget(certificatePage);
|
||||
|
||||
auto *ssoPage = new QWidget(credentialPages);
|
||||
auto *ssoPageLayout = new QVBoxLayout(ssoPage);
|
||||
auto *ssoLabel = new QLabel(
|
||||
"此认证方式将在连接时打开登录页面,无需提前填写登录凭据。",
|
||||
ssoPage
|
||||
);
|
||||
ssoLabel->setWordWrap(true);
|
||||
ssoPageLayout->addWidget(ssoLabel);
|
||||
ssoPageLayout->addStretch();
|
||||
credentialPages->addWidget(ssoPage);
|
||||
|
||||
pageLayout->addWidget(credentialPages);
|
||||
|
||||
connect(showPasswordCheckBox, &QCheckBox::toggled, this, [this](bool checked) {
|
||||
passwordLineEdit->setEchoMode(
|
||||
checked ? QLineEdit::Normal : QLineEdit::Password
|
||||
);
|
||||
});
|
||||
connect(showTotpCheckBox, &QCheckBox::toggled, this, [this](bool checked) {
|
||||
totpSecretLineEdit->setEchoMode(
|
||||
checked ? QLineEdit::Normal : QLineEdit::Password
|
||||
);
|
||||
});
|
||||
connect(
|
||||
showCertificatePasswordCheckBox,
|
||||
&QCheckBox::toggled,
|
||||
this,
|
||||
[this](bool checked) {
|
||||
certificatePasswordLineEdit->setEchoMode(
|
||||
checked ? QLineEdit::Normal : QLineEdit::Password
|
||||
);
|
||||
}
|
||||
);
|
||||
connect(
|
||||
showCertificateTotpCheckBox,
|
||||
&QCheckBox::toggled,
|
||||
this,
|
||||
[this](bool checked) {
|
||||
certificateTotpSecretLineEdit->setEchoMode(
|
||||
checked ? QLineEdit::Normal : QLineEdit::Password
|
||||
);
|
||||
}
|
||||
);
|
||||
connect(
|
||||
totpSecretLineEdit,
|
||||
&QLineEdit::textChanged,
|
||||
certificateTotpSecretLineEdit,
|
||||
&QLineEdit::setText
|
||||
);
|
||||
connect(
|
||||
certificateTotpSecretLineEdit,
|
||||
&QLineEdit::textChanged,
|
||||
totpSecretLineEdit,
|
||||
&QLineEdit::setText
|
||||
);
|
||||
connect(
|
||||
browseCertificateButton,
|
||||
&QPushButton::clicked,
|
||||
this,
|
||||
&ConfigurationGuideDialog::browseCertificateFile
|
||||
);
|
||||
return page;
|
||||
}
|
||||
|
||||
@@ -310,6 +574,50 @@ void ConfigurationGuideDialog::updateProtocolPage()
|
||||
atrustRadioButton->isChecked() ? 0 : 1
|
||||
);
|
||||
}
|
||||
updateCredentialsPage();
|
||||
}
|
||||
|
||||
void ConfigurationGuideDialog::updateCredentialsPage()
|
||||
{
|
||||
if (credentialPages == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!atrustRadioButton->isChecked())
|
||||
{
|
||||
credentialPages->setCurrentIndex(
|
||||
certificateAuthenticationRadioButton->isChecked() ? 2 : 0
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedAuthType == "smsCheckCode")
|
||||
{
|
||||
credentialPages->setCurrentIndex(1);
|
||||
}
|
||||
else if (selectedAuthType == "cas" || selectedAuthType == "httpsOauth2")
|
||||
{
|
||||
credentialPages->setCurrentIndex(3);
|
||||
}
|
||||
else
|
||||
{
|
||||
credentialPages->setCurrentIndex(0);
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigurationGuideDialog::browseCertificateFile()
|
||||
{
|
||||
const QString fileName = QFileDialog::getOpenFileName(
|
||||
this,
|
||||
"选择证书文件",
|
||||
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
|
||||
"P12 Certificate (*.p12 *.pfx);;All Files (*)"
|
||||
);
|
||||
if (!fileName.isEmpty())
|
||||
{
|
||||
certificateFileLineEdit->setText(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigurationGuideDialog::fetchAuthenticationMethods()
|
||||
@@ -365,6 +673,41 @@ bool ConfigurationGuideDialog::validateCurrentPage()
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (pages->currentIndex() == 3)
|
||||
{
|
||||
if (credentialPages->currentIndex() == 0
|
||||
&& (usernameLineEdit->text().trimmed().isEmpty()
|
||||
|| passwordLineEdit->text().isEmpty()))
|
||||
{
|
||||
QMessageBox::warning(
|
||||
this,
|
||||
"登录凭据不完整",
|
||||
"请填写 VPN 账号和密码。"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
if (credentialPages->currentIndex() == 1
|
||||
&& (countryCodeLineEdit->text().trimmed().isEmpty()
|
||||
|| phoneNumberLineEdit->text().trimmed().isEmpty()))
|
||||
{
|
||||
QMessageBox::warning(
|
||||
this,
|
||||
"手机号不完整",
|
||||
"请填写国家代码和手机号码。"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
if (credentialPages->currentIndex() == 2
|
||||
&& certificateFileLineEdit->text().trimmed().isEmpty())
|
||||
{
|
||||
QMessageBox::warning(
|
||||
this,
|
||||
"尚未选择证书",
|
||||
"请选择用于登录的 P12 或 PFX 证书文件。"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -373,12 +716,14 @@ void ConfigurationGuideDialog::updateNavigation()
|
||||
static const QStringList titles{
|
||||
"选择协议",
|
||||
"配置服务器",
|
||||
"选择认证方式"
|
||||
"选择认证方式",
|
||||
"配置登录凭据"
|
||||
};
|
||||
static const QStringList descriptions{
|
||||
"选择服务器实际使用的接入协议。",
|
||||
"填写 VPN 服务器提供方给出的地址和端口。",
|
||||
"只需确定认证方式;账号、密码和验证码将在连接时按需询问。"
|
||||
"选择服务器为你的账号提供的认证方式。",
|
||||
"填写当前认证方式连接时需要的登录信息。"
|
||||
};
|
||||
|
||||
const int pageIndex = pages->currentIndex();
|
||||
@@ -411,6 +756,7 @@ void ConfigurationGuideDialog::selectAuthenticationMethod(
|
||||
}
|
||||
selectedAuthenticationLabel->setText(details);
|
||||
fetchAuthenticationButton->setText("重新获取认证方式");
|
||||
updateCredentialsPage();
|
||||
}
|
||||
|
||||
QString ConfigurationGuideDialog::authenticationMethodName(
|
||||
|
||||
@@ -44,10 +44,16 @@ private:
|
||||
|
||||
QWidget *createAuthenticationPage();
|
||||
|
||||
QWidget *createCredentialsPage();
|
||||
|
||||
bool validateCurrentPage();
|
||||
|
||||
void updateNavigation();
|
||||
|
||||
void updateCredentialsPage();
|
||||
|
||||
void browseCertificateFile();
|
||||
|
||||
void selectAuthenticationMethod(
|
||||
const QString &authType,
|
||||
const QString &loginDomain,
|
||||
@@ -80,6 +86,16 @@ private:
|
||||
QRadioButton *passwordAuthenticationRadioButton;
|
||||
QRadioButton *certificateAuthenticationRadioButton;
|
||||
|
||||
QStackedWidget *credentialPages = nullptr;
|
||||
QLineEdit *usernameLineEdit;
|
||||
QLineEdit *passwordLineEdit;
|
||||
QLineEdit *totpSecretLineEdit;
|
||||
QLineEdit *countryCodeLineEdit;
|
||||
QLineEdit *phoneNumberLineEdit;
|
||||
QLineEdit *certificateFileLineEdit;
|
||||
QLineEdit *certificatePasswordLineEdit;
|
||||
QLineEdit *certificateTotpSecretLineEdit;
|
||||
|
||||
QString selectedAuthType;
|
||||
QString selectedLoginDomain;
|
||||
QString selectedLoginUrl;
|
||||
|
||||
@@ -682,7 +682,7 @@ void MainWindow::promptFirstLaunchGuide()
|
||||
messageBox.setWindowTitle("欢迎使用 EZ4Connect");
|
||||
messageBox.setText("检测到这是首次启动,是否现在配置 VPN 服务器?");
|
||||
messageBox.setInformativeText(
|
||||
"配置引导将协助你选择协议、填写服务器地址并选择认证方式。"
|
||||
"配置引导将协助你选择协议、填写服务器地址、认证方式和登录凭据。"
|
||||
);
|
||||
|
||||
QPushButton *startButton = messageBox.addButton(
|
||||
|
||||
208
tests/configurationguidedialog_test.cpp
Normal file
208
tests/configurationguidedialog_test.cpp
Normal file
@@ -0,0 +1,208 @@
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
#include <QLineEdit>
|
||||
#include <QSettings>
|
||||
#include <QStackedWidget>
|
||||
#include <QTemporaryDir>
|
||||
|
||||
#include "presentation/dialogs/configurationguidedialog/configurationguidedialog.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
QLineEdit *lineEdit(ConfigurationGuideDialog &dialog, const char *name)
|
||||
{
|
||||
QLineEdit *field = dialog.findChild<QLineEdit *>(name);
|
||||
if (field == nullptr)
|
||||
{
|
||||
qCritical() << "Missing guide field" << name;
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
QStackedWidget *credentialPages(ConfigurationGuideDialog &dialog)
|
||||
{
|
||||
QStackedWidget *pages = dialog.findChild<QStackedWidget *>(
|
||||
"credentialPages"
|
||||
);
|
||||
if (pages == nullptr)
|
||||
{
|
||||
qCritical() << "Missing credential pages";
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
bool savesPasswordCredentials(const QString &settingsPath)
|
||||
{
|
||||
QSettings settings(settingsPath, QSettings::IniFormat);
|
||||
settings.setValue("ZJUConnect/Protocol", "easyconnect");
|
||||
settings.setValue("ZJUConnect/EasyConnectAuthType", "password");
|
||||
settings.setValue("Credential/Username", "old-user");
|
||||
settings.setValue(
|
||||
"Credential/Password",
|
||||
QString(QStringLiteral("old-password").toUtf8().toBase64())
|
||||
);
|
||||
settings.setValue("Credential/TOTPSecret", "old-totp");
|
||||
|
||||
ConfigurationGuideDialog dialog(nullptr, &settings);
|
||||
QStackedWidget *pages = credentialPages(dialog);
|
||||
QLineEdit *username = lineEdit(dialog, "guideUsernameLineEdit");
|
||||
QLineEdit *password = lineEdit(dialog, "guidePasswordLineEdit");
|
||||
QLineEdit *totp = lineEdit(dialog, "guideTotpSecretLineEdit");
|
||||
if (pages == nullptr || username == nullptr || password == nullptr
|
||||
|| totp == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (pages->currentIndex() != 0 || username->text() != "old-user"
|
||||
|| password->text() != "old-password" || totp->text() != "old-totp")
|
||||
{
|
||||
qCritical() << "Password credentials were not loaded into the guide";
|
||||
return false;
|
||||
}
|
||||
|
||||
username->setText("alice");
|
||||
password->setText("secret");
|
||||
totp->setText("totp-key");
|
||||
dialog.applyTo(settings);
|
||||
|
||||
const QString savedPassword = QByteArray::fromBase64(
|
||||
settings.value("Credential/Password").toByteArray()
|
||||
);
|
||||
const bool passed = settings.value("Credential/Username") == "alice"
|
||||
&& savedPassword == "secret"
|
||||
&& settings.value("Credential/TOTPSecret") == "totp-key";
|
||||
if (!passed)
|
||||
{
|
||||
qCritical() << "Password credentials were not saved by the guide";
|
||||
}
|
||||
return passed;
|
||||
}
|
||||
|
||||
bool savesSmsCredentials(const QString &settingsPath)
|
||||
{
|
||||
QSettings settings(settingsPath, QSettings::IniFormat);
|
||||
settings.setValue("ZJUConnect/Protocol", "atrust");
|
||||
settings.setValue("ZJUConnect/AuthType", "smsCheckCode");
|
||||
settings.setValue("ZJUConnect/PhoneCountryCode", "86");
|
||||
settings.setValue("ZJUConnect/PhoneNumber", "123456");
|
||||
|
||||
ConfigurationGuideDialog dialog(nullptr, &settings);
|
||||
QStackedWidget *pages = credentialPages(dialog);
|
||||
QLineEdit *countryCode = lineEdit(dialog, "guideCountryCodeLineEdit");
|
||||
QLineEdit *phoneNumber = lineEdit(dialog, "guidePhoneNumberLineEdit");
|
||||
if (pages == nullptr || countryCode == nullptr || phoneNumber == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (pages->currentIndex() != 1 || countryCode->text() != "86"
|
||||
|| phoneNumber->text() != "123456")
|
||||
{
|
||||
qCritical() << "SMS credentials were not loaded into the guide";
|
||||
return false;
|
||||
}
|
||||
|
||||
countryCode->setText("852");
|
||||
phoneNumber->setText("98765432");
|
||||
dialog.applyTo(settings);
|
||||
|
||||
const bool passed = settings.value("ZJUConnect/PhoneCountryCode") == "852"
|
||||
&& settings.value("ZJUConnect/PhoneNumber") == "98765432";
|
||||
if (!passed)
|
||||
{
|
||||
qCritical() << "SMS credentials were not saved by the guide";
|
||||
}
|
||||
return passed;
|
||||
}
|
||||
|
||||
bool savesCertificateCredentials(const QString &settingsPath)
|
||||
{
|
||||
QSettings settings(settingsPath, QSettings::IniFormat);
|
||||
settings.setValue("ZJUConnect/Protocol", "easyconnect");
|
||||
settings.setValue("ZJUConnect/EasyConnectAuthType", "certificate");
|
||||
settings.setValue("Credential/CertFile", "/tmp/old.p12");
|
||||
settings.setValue(
|
||||
"Credential/CertPassword",
|
||||
QString(QStringLiteral("old-cert-password").toUtf8().toBase64())
|
||||
);
|
||||
settings.setValue("Credential/TOTPSecret", "old-cert-totp");
|
||||
|
||||
ConfigurationGuideDialog dialog(nullptr, &settings);
|
||||
QStackedWidget *pages = credentialPages(dialog);
|
||||
QLineEdit *certificateFile = lineEdit(
|
||||
dialog,
|
||||
"guideCertificateFileLineEdit"
|
||||
);
|
||||
QLineEdit *certificatePassword = lineEdit(
|
||||
dialog,
|
||||
"guideCertificatePasswordLineEdit"
|
||||
);
|
||||
QLineEdit *certificateTotp = lineEdit(
|
||||
dialog,
|
||||
"guideCertificateTotpSecretLineEdit"
|
||||
);
|
||||
if (pages == nullptr || certificateFile == nullptr
|
||||
|| certificatePassword == nullptr || certificateTotp == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (pages->currentIndex() != 2 || certificateFile->text() != "/tmp/old.p12"
|
||||
|| certificatePassword->text() != "old-cert-password"
|
||||
|| certificateTotp->text() != "old-cert-totp")
|
||||
{
|
||||
qCritical() << "Certificate credentials were not loaded into the guide";
|
||||
return false;
|
||||
}
|
||||
|
||||
certificateFile->setText("/tmp/client.pfx");
|
||||
certificatePassword->setText("cert-secret");
|
||||
certificateTotp->setText("new-cert-totp");
|
||||
dialog.applyTo(settings);
|
||||
|
||||
const QString savedPassword = QByteArray::fromBase64(
|
||||
settings.value("Credential/CertPassword").toByteArray()
|
||||
);
|
||||
const bool passed =
|
||||
settings.value("Credential/CertFile") == "/tmp/client.pfx"
|
||||
&& savedPassword == "cert-secret"
|
||||
&& settings.value("Credential/TOTPSecret") == "new-cert-totp";
|
||||
if (!passed)
|
||||
{
|
||||
qCritical() << "Certificate credentials were not saved by the guide";
|
||||
}
|
||||
return passed;
|
||||
}
|
||||
|
||||
bool showsSsoWithoutCredentialFields(const QString &settingsPath)
|
||||
{
|
||||
QSettings settings(settingsPath, QSettings::IniFormat);
|
||||
settings.setValue("ZJUConnect/Protocol", "atrust");
|
||||
settings.setValue("ZJUConnect/AuthType", "cas");
|
||||
|
||||
ConfigurationGuideDialog dialog(nullptr, &settings);
|
||||
QStackedWidget *pages = credentialPages(dialog);
|
||||
const bool passed = pages != nullptr && pages->currentIndex() == 3;
|
||||
if (!passed)
|
||||
{
|
||||
qCritical() << "CAS did not select the credential-free guide page";
|
||||
}
|
||||
return passed;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication application(argc, argv);
|
||||
QTemporaryDir directory;
|
||||
if (!directory.isValid())
|
||||
{
|
||||
qCritical() << "Unable to create temporary directory";
|
||||
return 1;
|
||||
}
|
||||
|
||||
const bool passed =
|
||||
savesPasswordCredentials(directory.filePath("password.ini"))
|
||||
&& savesSmsCredentials(directory.filePath("sms.ini"))
|
||||
&& savesCertificateCredentials(directory.filePath("certificate.ini"))
|
||||
&& showsSsoWithoutCredentialFields(directory.filePath("sso.ini"));
|
||||
return passed ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user