Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ut_urlresolver: add checkStoreAuthorizedKeys()
  • Loading branch information
martyone committed Apr 10, 2013
1 parent 33e4688 commit fa07e63
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/ut_urlresolver/urlresolvertest.cpp
Expand Up @@ -6,6 +6,7 @@
*/

#include "urlresolvertest.h"
#include "testutils/process.h"

void UrlResolverTest::initTestCase(){
#ifdef TARGET_ARCH
Expand Down Expand Up @@ -191,3 +192,60 @@ void UrlResolverTest::checkSetCredentials(){
QCOMPARE(ssu.credentials("utscope2").first, QString("john.doe2"));
QCOMPARE(ssu.credentials("utscope2").second, QString("SeCrEt2"));
}

void UrlResolverTest::checkStoreAuthorizedKeys(){
struct Cleanup {
~Cleanup(){
if (!tempHomePath.isEmpty()){
Process rmtemp;
rmtemp.execute("rm", QStringList() << "-rf" << tempHomePath);
if (rmtemp.hasError()){
qWarning("%s: Failed to remove temporary directory '%s': %s", Q_FUNC_INFO,
tempHomePath.constData(), qPrintable(rmtemp.fmtErrorMessage()));
}

if (!qputenv("HOME", originalHomePath)){
qFatal("%s: Failed to restore HOME environment variable", Q_FUNC_INFO);
}
}
}

QByteArray originalHomePath;
QByteArray tempHomePath;
} cleanup;

// Temporarily change HOME path so Ssu::storeAuthorizedKeys() does not touch
// real home directory
cleanup.originalHomePath = qgetenv("HOME");
QVERIFY(!cleanup.originalHomePath.isEmpty());

Process mktemp;
cleanup.tempHomePath = mktemp.execute("mktemp",
QStringList() << "-t" << "-d" << "ut_urlresolver.temp-home.XXX").trimmed().toLocal8Bit();
QVERIFY2(!mktemp.hasError(), qPrintable(mktemp.fmtErrorMessage()));

QVERIFY(qputenv("HOME", cleanup.tempHomePath));
QVERIFY2(QDir::homePath() == QString(cleanup.tempHomePath),
"QDir::homePath() does not change after qputenv(\"HOME\", \"...\")");

// Here starts the test itself
QByteArray testData("# test data\n");
ssu.storeAuthorizedKeys(testData);

QFile authorizedKeys(QDir::home().filePath(".ssh/authorized_keys"));
QVERIFY(authorizedKeys.open(QIODevice::ReadOnly));

QVERIFY(authorizedKeys.readAll().split('\n').contains(testData.trimmed()));

QByteArray testData2("# test data2\n");
ssu.storeAuthorizedKeys(testData2);

QEXPECT_FAIL("", "Ssu::storeAuthorizedKeys() does not modify existing authorized_keys", Continue);
authorizedKeys.seek(0);
QVERIFY(authorizedKeys.readAll().split('\n').contains(testData2.trimmed()));

const QFile::Permissions go_rwx =
QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
QFile::ReadOther | QFile::WriteOther | QFile::ExeOther;
QVERIFY((QFileInfo(QDir::home().filePath(".ssh")).permissions() & go_rwx) == 0);
}
1 change: 1 addition & 0 deletions tests/ut_urlresolver/urlresolvertest.h
Expand Up @@ -27,6 +27,7 @@ class UrlResolverTest: public QObject {
void simpleRepoUrlLookup();
void checkReleaseRepoUrls();
void checkSetCredentials();
void checkStoreAuthorizedKeys();

private:
Ssu ssu;
Expand Down
1 change: 1 addition & 0 deletions tests/ut_urlresolver/ut_urlresolver_dependencies.pri
@@ -1 +1,2 @@
include(../../libssu/libssu.pri)
include(../testutils/testutils.pri)

0 comments on commit fa07e63

Please sign in to comment.