aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/ssh
diff options
context:
space:
mode:
authorChristian Stenger <[email protected]>2022-03-04 13:44:20 +0100
committerJarek Kobus <[email protected]>2022-03-16 16:44:27 +0000
commit66c5b1e11e1f80c4113bea940302ea79926a0c13 (patch)
tree179f44f03c0f2f69d2aa474be733383248a6eebc /src/libs/ssh
parente9074792d2c0bcb1c3e46cf0cd05113b532d7ba7 (diff)
RemoteLinux: Avoid unexpected password dialog in tests
Make the test depend on environment variables and give some hint how to run the test correctly. Beside this make the test work on Windows and share the setup with ssh unit test. Change-Id: I6bbf1ec863449512646ca2c51d13fec537beedbc Reviewed-by: hjk <[email protected]>
Diffstat (limited to 'src/libs/ssh')
-rw-r--r--src/libs/ssh/ssh.qbs8
-rw-r--r--src/libs/ssh/sshconnection.cpp91
-rw-r--r--src/libs/ssh/sshconnection.h13
3 files changed, 111 insertions, 1 deletions
diff --git a/src/libs/ssh/ssh.qbs b/src/libs/ssh/ssh.qbs
index 36c69c7ee72..cd306d0c4a3 100644
--- a/src/libs/ssh/ssh.qbs
+++ b/src/libs/ssh/ssh.qbs
@@ -4,7 +4,13 @@ Project {
name: "QtcSsh"
QtcLibrary {
- cpp.defines: base.concat("QTCSSH_LIBRARY")
+ cpp.defines: {
+ var defines = base;
+ defines.push("QTCSSH_LIBRARY");
+ if (project.withAutotests && !defines.contains("WITH_TESTS"))
+ defines.push("WITH_TESTS");
+ return defines;
+ }
cpp.enableExceptions: true
Depends { name: "Qt"; submodules: ["widgets", "network" ] }
diff --git a/src/libs/ssh/sshconnection.cpp b/src/libs/ssh/sshconnection.cpp
index 5f8134e1c9a..839ee2d5431 100644
--- a/src/libs/ssh/sshconnection.cpp
+++ b/src/libs/ssh/sshconnection.cpp
@@ -403,4 +403,95 @@ SftpTransferPtr SshConnection::setupTransfer(
d->connectionArgs(SshSettings::sftpFilePath())));
}
+#ifdef WITH_TESTS
+namespace SshTest {
+const QString getHostFromEnvironment()
+{
+ const QString host = QString::fromLocal8Bit(qgetenv("QTC_SSH_TEST_HOST"));
+ if (host.isEmpty() && qEnvironmentVariableIsSet("QTC_SSH_TEST_DEFAULTS"))
+ return QString("127.0.0.1");
+ return host;
+}
+
+quint16 getPortFromEnvironment()
+{
+ const int port = qEnvironmentVariableIntValue("QTC_SSH_TEST_PORT");
+ return port != 0 ? quint16(port) : 22;
+}
+
+const QString getUserFromEnvironment()
+{
+ return QString::fromLocal8Bit(qgetenv("QTC_SSH_TEST_USER"));
+}
+
+const QString getKeyFileFromEnvironment()
+{
+ const FilePath defaultKeyFile = FileUtils::homePath() / ".ssh/id_rsa";
+ const QString keyFile = QString::fromLocal8Bit(qgetenv("QTC_SSH_TEST_KEYFILE"));
+ if (keyFile.isEmpty()) {
+ if (qEnvironmentVariableIsSet("QTC_SSH_TEST_DEFAULTS"))
+ return defaultKeyFile.toString();
+ }
+ return keyFile;
+}
+
+const QString userAtHost()
+{
+ QString userMidFix = getUserFromEnvironment();
+ if (!userMidFix.isEmpty())
+ userMidFix.append('@');
+ return userMidFix + getHostFromEnvironment();
+}
+
+SshConnectionParameters getParameters()
+{
+ SshConnectionParameters params;
+ if (!qEnvironmentVariableIsSet("QTC_SSH_TEST_DEFAULTS")) {
+ params.setUserName(getUserFromEnvironment());
+ params.privateKeyFile = Utils::FilePath::fromUserInput(getKeyFileFromEnvironment());
+ }
+ params.setHost(getHostFromEnvironment());
+ params.setPort(getPortFromEnvironment());
+ params.timeout = 10;
+ params.authenticationType = !params.privateKeyFile.isEmpty()
+ ? QSsh::SshConnectionParameters::AuthenticationTypeSpecificKey
+ : QSsh::SshConnectionParameters::AuthenticationTypeAll;
+ return params;
+}
+
+bool checkParameters(const QSsh::SshConnectionParameters &params)
+{
+ if (qEnvironmentVariableIsSet("QTC_SSH_TEST_DEFAULTS"))
+ return true;
+ if (params.host().isEmpty()) {
+ qWarning("No hostname provided. Set QTC_SSH_TEST_HOST.");
+ return false;
+ }
+ if (params.userName().isEmpty())
+ qWarning("No user name provided - test may fail with empty default. Set QTC_SSH_TEST_USER.");
+ if (params.privateKeyFile.isEmpty()) {
+ qWarning("No key file provided. Set QTC_SSH_TEST_KEYFILE.");
+ return false;
+ }
+ return true;
+}
+
+void printSetupHelp()
+{
+ qInfo() << "In order to run this test properly it requires some setup (example for fedora):\n"
+ "1. Run a server on the host to connect to:\n"
+ " systemctl start sshd\n"
+ "2. Create your own ssh key (needed only once). For fedora it needs ecdsa type:\n"
+ " ssh-keygen -t ecdsa\n"
+ "3. Make your public key known to the server (needed only once):\n"
+ " ssh-copy-id -i [full path to your public key] [user@host]\n"
+ "4. Set the env variables before executing test:\n"
+ " QTC_SSH_TEST_HOST=127.0.0.1\n"
+ " QTC_SSH_TEST_KEYFILE=[full path to your private key]\n"
+ " QTC_SSH_TEST_USER=[your user name]\n";
+}
+
+} // namespace SshTest
+#endif
+
} // namespace QSsh
diff --git a/src/libs/ssh/sshconnection.h b/src/libs/ssh/sshconnection.h
index 381bd463c91..a559fc13163 100644
--- a/src/libs/ssh/sshconnection.h
+++ b/src/libs/ssh/sshconnection.h
@@ -142,6 +142,19 @@ private:
SshConnectionPrivate * const d;
};
+#ifdef WITH_TESTS
+namespace SshTest {
+const QString QSSH_EXPORT getHostFromEnvironment();
+quint16 QSSH_EXPORT getPortFromEnvironment();
+const QString QSSH_EXPORT getUserFromEnvironment();
+const QString QSSH_EXPORT getKeyFileFromEnvironment();
+const QSSH_EXPORT QString userAtHost();
+SshConnectionParameters QSSH_EXPORT getParameters();
+bool QSSH_EXPORT checkParameters(const SshConnectionParameters &params);
+void QSSH_EXPORT printSetupHelp();
+} // namespace SshTest
+#endif
+
} // namespace QSsh
Q_DECLARE_METATYPE(QSsh::SshConnectionParameters::AuthenticationType)