summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/selftest.cpp
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/selftest.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/selftest.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/selftest.cpp b/pwmanager/pwmanager/selftest.cpp
new file mode 100644
index 0000000..45179d8
--- a/dev/null
+++ b/pwmanager/pwmanager/selftest.cpp
@@ -0,0 +1,105 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License version 2 *
8 * as published by the Free Software Foundation. *
9 * *
10 ***************************************************************************/
11
12#include "selftest.h"
13#include "pwmexception.h"
14#include "sha1.h"
15#include "blowfish.h"
16#include "base64.h"
17
18#include <pthread.h>
19
20
21static SelfTest *st = 0;
22static pthread_mutex_t st_mutex = PTHREAD_MUTEX_INITIALIZER;
23
24
25SelfTest::SelfTest()
26{
27 connect(&schedTimer, SIGNAL(timeout()),
28 this, SLOT(doSelfTest()));
29}
30
31SelfTest::~SelfTest()
32{
33}
34
35void SelfTest::run()
36{
37 //printDebug("running self-test...");
38 if (unlikely(!Sha1::selfTest())) {
39 failed("SHA1");
40 return;
41 }
42 if (unlikely(!Blowfish::selfTest())) {
43 failed("BLOWFISH");
44 return;
45 }
46 if (unlikely(!Base64::selfTest())) {
47 failed("BASE64");
48 return;
49 }
50 //printDebug("self-test done.");
51 if (!pthread_mutex_trylock(&st_mutex)) {
52 /* only cancel (delete) this thread, if there's currently
53 * noone else doing this job. Otherwise we will deadlock.
54 * we use a timer here, to do some kind of context-switch.
55 */
56 QTimer::singleShot(0, st, SLOT(doCancel()));
57 }
58}
59
60void SelfTest::failed(const char *algo)
61{
62 string msg("\n\n\n"
63 "PwManager FATAL ERROR:\n"
64 "Self-test for algorithm \"");
65 msg += algo;
66 msg += "\" failed!\n\n";
67 msg += "It's likely to be either a PwManager BUG or "
68 "bad hardware in your machine. Please contact the "
69 "maintainer of PwManager for this issue.";
70 std::cerr << msg << std::endl;
71 ::exit(1);
72}
73
74void SelfTest::schedule()
75{
76 if (pthread_mutex_lock(&st_mutex))
77 return;
78 if (st)
79 goto out;
80 st = new SelfTest;
81 st->startTimer();
82out:
83 pthread_mutex_unlock(&st_mutex);
84}
85
86void SelfTest::cancel()
87{
88 if (pthread_mutex_lock(&st_mutex))
89 return;
90 if (!st) {
91 pthread_mutex_unlock(&st_mutex);
92 return;
93 }
94 st->doCancel();
95}
96
97void SelfTest::doCancel()
98{
99 st->stopTimer();
100 st->wait();
101 delete_and_null(st);
102 pthread_mutex_unlock(&st_mutex);
103}
104
105#include "selftest.moc"