summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/tests/kopetepasswordtest_program.cpp
blob: 3a80524cb0b1aef50d1d0cb830c8534370121d61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
    Tests for the Kopete::Password class

    Copyright (c) 2003      by Richard Smith          <kde@metafoo.co.uk>
    Kopete    (c) 2002-2003 by the Kopete developers  <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#include "kopetepasswordtest_program.h"
#include "kopetepassword.h"

#include <tqtextstream.h>
#include <tqpixmap.h>
#include <tqtimer.h>

#include <tdeaboutdata.h>
#include <tdeapplication.h>
#include <tdecmdlineargs.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>

static TQTextStream _out( stdout, IO_WriteOnly );

static TDECmdLineOptions opts[] =
{
 { "id <id>", I18N_NOOP("Config group to store password in"), "TestAccount" },
 { "set <new>", I18N_NOOP("Set password to new"), 0 },
 { "error", I18N_NOOP("Claim password was erroneous"), 0 },
 { "prompt <prompt>", I18N_NOOP("Password prompt"), "Enter a password" },
 { "image <filename>", I18N_NOOP("Image to display in password dialog"), 0 },
 TDECmdLineLastOption
};

using namespace Kopete;

TQString retrieve( Password &pwd, const TQPixmap &image, const TQString &prompt )
{
	PasswordRetriever r;
	pwd.request( &r, TQT_SLOT( gotPassword( const TQString & ) ), image, prompt );
	TQTimer tmr;
	r.connect( &tmr, TQT_SIGNAL( timeout() ), TQT_SLOT( timer() ) );
	tmr.start( 1000 );
	tqApp->exec();
	return r.password;
}

void PasswordRetriever::gotPassword( const TQString &pass )
{
	password = pass;
	tqApp->quit();
}

void PasswordRetriever::timer()
{
	_out << "." << flush;
}

int main( int argc, char *argv[] )
{
	TDEAboutData aboutData( "kopetepasswordtest", "kopetepasswordtest", "version" );
	TDECmdLineArgs::init( argc, argv, &aboutData );
	TDECmdLineArgs::addCmdLineOptions( opts );
	TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

	TDEApplication app( "kopetepasswordtest" );

	bool setPassword = args->isSet("set");
	TQString newPwd = args->getOption("set");
	TQString passwordId = args->getOption("id");
	bool error = args->isSet("error");
	TQString prompt = args->getOption("prompt");
	TQPixmap image = TQString(args->getOption("image"));

	_out << (image.isNull() ? "image is null" : "image is valid") << endl;

	Password pwd( passwordId, 0, false );
	pwd.setWrong( error );

	_out << "Cached value is null: " << pwd.cachedValue().isNull() << endl;

	TQString pass = retrieve( pwd, image, prompt );

	if ( !pass.isNull() )
		_out << "Read password: " << pass << endl;
	else
		_out << "Could not read a password" << endl;

	_out << "Cached value: " << (pwd.cachedValue().isNull() ? "null" : pwd.cachedValue()) << endl;

	if ( setPassword )
	{
		if ( newPwd.isEmpty() )
		{
			_out << "Clearing password" << endl;
			newPwd = TQString();
		}
		else
		{
			_out << "Setting password to " << newPwd << endl;
		}
		pwd.set( newPwd );
	}

	// without this, setting passwords will fail since they're
	// set asynchronously.
	TQTimer::singleShot( 0, &app, TQT_SLOT( deref() ) );
	app.exec();

	if ( setPassword )
	{
		pass = retrieve( pwd, image, i18n("Hopefully this popped up because you set the password to the empty string.") );
		if( pass == newPwd )
			_out << "Password successfully set." << endl;
		else
			_out << "Failed: password ended up as " << pass << endl;
	}

	return 0;
}

#include "kopetepasswordtest_program.moc"

// vim: set noet ts=4 sts=4 sw=4: