Erik Grinaker is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

erikg / Revelation

Revelation is a password manager for the GNOME desktop, released under the GNU GPL license. It stores all your accounts and passwords in a single, secure place, and gives you access to it through a user-friendly graphical interface.

Clone this repository (size: 2.0 MB): HTTPS / SSH
hg clone https://bitbucket.org/erikg/revelation
hg clone ssh://hg@bitbucket.org/erikg/revelation

Revelation / test / io.py

  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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/usr/bin/env python

#
# Revelation 0.4.0 - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# Unit tests for IO module
#
#
# Copyright (c) 2003-2005 Erik Grinaker
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

import unittest, os, time

from revelation import data, datahandler, io



class DataFile__init(unittest.TestCase):
        "DataFile()"

        def test_default_file(self):
                "DataFile() file defaults to None"

                f = io.DataFile(datahandler.Revelation)
                self.assertEqual(f.get_file(), None)


        def test_default_password(self):
                "DataFile() password defaults to None"

                f = io.DataFile(datahandler.Revelation)
                self.assertEqual(f.get_password(), None)


        def test_handler(self):
                "DataFile() handles handler arg correctly"

                f = io.DataFile(datahandler.Revelation)
                self.assertEqual(type(f.get_handler()), datahandler.Revelation)


        def test_noargs(self):
                "DataFile() with no args"

                io.DataFile(datahandler.Revelation)



class DataFile__str(unittest.TestCase):
        "DataFile.__str__()"


        def test_get_file(self):
                "DataFile.__str__() gives same results as DataFile.get_file()"

                files = [
                        "/bin/ls",
                        "file:///bin/ls",
                        "file:///home/../bin/ls",
                        "http://www.google.com/index.html",
                        "ftp://test:test123@ftp.testftp.com/test/123"
                ]

                for file in files:
                        f = io.DataFile(datahandler.Revelation)
                        f.set_file(file)
                        self.assertEqual(str(f), f.get_file())


        def test_none(self):
                "DataFile.__str__() with no file returns ''"

                f = io.DataFile(datahandler.Revelation)
                self.assertEqual(str(f), "")



class DataFile_close(unittest.TestCase):
        "DataFile.close()"

        def test_file(self):
                "DataFile.close() resets file uri"

                f = io.DataFile(datahandler.Revelation)
                f.set_file("/bin/ls")
                f.close()
                self.assertEqual(f.get_file(), None)


        def test_handler(self):
                "DataFile.close() doesn't reset handler"

                f = io.DataFile(datahandler.Revelation)
                f.set_handler(datahandler.Revelation)
                f.close()
                self.assertEqual(type(f.get_handler()), datahandler.Revelation)


        def test_password(self):
                "DataFile.close() resets password"

                f = io.DataFile(datahandler.Revelation)
                f.set_password("test123")
                f.close()
                self.assertEqual(f.get_password(), None)



class DataFile_get_file(unittest.TestCase):
        "DataFile.get_file()"

        def test_normpath(self):
                "DataFile.get_file() uses file_normpath()"

                tests = (
                        ( None                                  , None ),
                        ( "/bin/ls"                             , "/bin/ls" ),
                        ( "/home/../bin/ls"                     , "/bin/ls" ),
                        ( "http://www.google.com/index.html"    , "http://www.google.com/index.html" ),
                        ( "../../../../../../../../../../home/../bin/ls", "/bin/ls"),
                        ( "file:///bin/ls"                      , "/bin/ls" ),
                        ( "file:../../../../../../../../../../home/../bin/ls", "/bin/ls")
                )

                for input, output in tests:
                        self.assertEqual(output, io.file_normpath(input))



class DataFile_get_password(unittest.TestCase):
        "Datafile.get_password()"

        def test_none(self):
                "DataFile.get_password() returns None on no password"

                f = io.DataFile(datahandler.Revelation)
                f.set_password(None)
                self.assertEqual(f.get_password(), None)


        def test_password(self):
                "DataFile.get_password() gets password"

                f = io.DataFile(datahandler.Revelation)
                f.set_password("test123")
                self.assertEqual(f.get_password(), "test123")



class DataFile_get_handler(unittest.TestCase):
        "DataFile.get_handler()"

        def test_none(self):
                "DataFile.get_handler() returns None when no handler is set"

                f = io.DataFile(None)
                self.assertEqual(f.get_handler(), None)


        def test_normal(self):
                "DataFile.get_handler() returns the handler"

                f = io.DataFile(datahandler.Revelation)
                self.assertEqual(type(f.get_handler()), datahandler.Revelation)



class DataFile_load(unittest.TestCase):
        "DataFile.load()"

        def test_file(self):
                "DataFile.load() updates file property"

                xml =   "<?xml version=\"1.0\" ?>"              \
                        "<revelationdata dataversion=\"1\">"    \
                        "</revelationdata>"

                io.file_write("/tmp/iotest-xmldata", xml)

                f = io.DataFile(datahandler.RevelationXML)
                entrystore = f.load("/tmp/iotest-xmldata")
                self.assertEqual(f.get_file(), "/tmp/iotest-xmldata")


        def test_load(self):
                "DataFile.load() returns an EntryStore on success"

                xml =   "<?xml version=\"1.0\" ?>"              \
                        "<revelationdata dataversion=\"1\">"    \
                        "</revelationdata>"

                io.file_write("/tmp/iotest-xmldata", xml)

                f = io.DataFile(datahandler.RevelationXML)
                entrystore = f.load("/tmp/iotest-xmldata")
                self.assertEqual(type(entrystore), data.EntryStore)


        def test_none(self):
                "DataFile.load() raises IOError when passed None"

                self.assertRaises(IOError, io.DataFile(datahandler.Revelation).load, None)


        def test_pwgetter(self):
                "DataFile.load() calls pwgetter() when password isn't passed"

                global pwflag
                pwflag = False

                def pwgetter():
                        global pwflag
                        pwflag = True
                        return "dummy"

                handler = datahandler.Revelation()
                encdata = handler.export_data(data.EntryStore(), "jeje")

                io.file_write("/tmp/iotest-testfile", encdata)

                try:
                        f = io.DataFile(datahandler.Revelation)
                        f.load("/tmp/iotest-testfile", None, pwgetter)

                except datahandler.PasswordError:
                        pass

                self.assertEqual(pwflag, True)



class DataFile_set_file(unittest.TestCase):
        "DataFile.set_file()"

        def test_file(self):
                "DataFile.set_file() sets the file"

                f = io.DataFile(datahandler.Revelation)
                f.set_file("/bin/ls")
                self.assertEqual(f.get_file(), "/bin/ls")


        def test_none(self):
                "DataFile.set_file() handles None correctly"

                f = io.DataFile(datahandler.Revelation)
                f.set_file("/bin/ls")
                f.set_file(None)
                self.assertEqual(f.get_file(), None)



class DataFile_set_handler(unittest.TestCase):
        "DataFile.set_handler()"

        def test_handler(self):
                "DataFile.set_handler() sets handler correctly"

                f = io.DataFile(None)
                f.set_handler(datahandler.Revelation)
                self.assertEqual(type(f.get_handler()), datahandler.Revelation)

        def test_init(self):
                "DataFile.set_handler() works like init"

                for handler in ( datahandler.Revelation, None ):
                        a = io.DataFile(handler)
                        b = io.DataFile(None)
                        b.set_handler(handler)

                        self.assertEqual(type(a.get_handler()), type(b.get_handler()))


        def test_none(self):
                "DataFile.set_handler() handles None correctly"

                f = io.DataFile(datahandler.Revelation)
                f.set_handler(None)
                self.assertEqual(f.get_handler(), None)



class DataFile_set_password(unittest.TestCase):
        "Datafile.set_password()"

        def test_none(self):
                "DataFile.set_password() handles None correctly"

                f = io.DataFile(datahandler.Revelation)
                f.set_password(None)
                self.assertEqual(f.get_password(), None)


        def test_password(self):
                "DataFile.set_password() sets password"

                f = io.DataFile(datahandler.Revelation)
                f.set_password("test123")
                self.assertEqual(f.get_password(), "test123")



class file_exists(unittest.TestCase):
        "file_exists()"

        def test_local(self):
                "file_exists() with local file"

                self.assertEqual(io.file_exists("/bin/ls"), True)


        def test_none(self):
                "file_exists() passed None"

                self.assertEqual(io.file_exists(None), False)


        def test_nonexist(self):
                "file_exists() with non-existant file"

                self.assertEqual(io.file_exists("/fjkdlsafjo8942389"), False)


        def test_remote(self):
                "file_exists() with remote file"

                self.assertEqual(io.file_exists("http://www.google.com/index.html"), True)



class file_normpath(unittest.TestCase):
        "file_normpath()"

        def test_empty(self):
                "file_normpath handles ''"

                self.assertEqual(io.file_normpath(""), None)

        def test_local(self):
                "file_normpath handles local files"

                self.assertEqual(io.file_normpath("/bin/ls"), "/bin/ls")


        def test_none(self):
                "file_normpath returns None when no file"

                self.assertEqual(io.file_normpath(None), None)


        def test_normalize(self):
                "file_normpath normalizes paths"

                self.assertEqual(io.file_normpath("/home/../bin/ls"), "/bin/ls")


        def test_remote(self):  
                "file_normpath works for remote files"

                self.assertEqual(io.file_normpath("http://www.google.com/index.html"), "http://www.google.com/index.html")


        def test_resolve(self):
                "file_normpath resolves relative paths"

                self.assertEqual(io.file_normpath("../../../../../../../../../../home/../bin/ls"), "/bin/ls")


        def test_striplocal(self):
                "file_normpath strips file:// for local files"

                self.assertEqual(io.file_normpath("file:///bin/ls"), "/bin/ls")


        def test_striplocal_relative(self):
                "file_normpath strips file: for local, relative paths"

                self.assertEqual(io.file_normpath("file:../../../../../../../../home/../bin/ls"), "/bin/ls")



class file_read(unittest.TestCase):
        "file_read()"

        def test_invperm(self):
                "file_read() without perms"

                if io.file_exists("/tmp/iotest-perms") == True:
                        os.unlink("/tmp/iotest-perms")

                io.file_write("/tmp/iotest-perms", "")
                os.chmod("/tmp/iotest-perms", 0000)
                self.assertRaises(IOError, io.file_read, "/tmp/iotest-perms")


        def test_local(self):
                "file_read() on local file"

                self.assertNotEqual(io.file_read("/bin/ls"), "")
                self.assertNotEqual(io.file_read("/bin/ls"), None)


        def test_none(self):
                "file_read() passed None"

                self.assertRaises(IOError, io.file_read, None)


        def test_nonexist(self):
                "file_read() on non-existant file"

                self.assertRaises(IOError, io.file_read, "/jfejiof0312e390rjw")


        def test_remote(self):
                "file_read() on remote file"

                self.assertNotEqual(io.file_read("http://www.google.com/index.html"), "")
                self.assertNotEqual(io.file_read("http://www.google.com/index.html"), None)




class file_is_local(unittest.TestCase):
        "file_is_local()"

        def test_local(self):
                "file_is_local() for local file"

                self.assertEqual(io.file_is_local("/bin/ls"), True)


        def test_none(self):
                "file_is_local() for None"

                self.assertEqual(io.file_is_local(None), False)


        def test_remote(self):
                "file_is_local() for remote file"

                self.assertEqual(io.file_is_local("http://www.google.com/index.html"), False)



class file_write(unittest.TestCase):
        "file_write()"

        def test_create(self):
                "file_write() creates file"

                file = "/tmp/iotest-tempfile"
                data = "test123"

                if os.access(file, os.F_OK):
                        os.unlink(file)

                io.file_write(file, "test123")
                self.assertEqual(os.access(file, os.F_OK), True)


        def test_invperm(self):
                "file_write() without perms"

                if io.file_exists("/tmp/iotest-perms"):
                        os.unlink("/tmp/iotest-perms")

                io.file_write("/tmp/iotest-perms", "a")
                os.chmod("/tmp/iotest-perms", 0000)
                self.assertRaises(IOError, io.file_write, "/tmp/iotest-perms", "test123")


        def test_nofile(self):
                "file_write() passed None as file"

                self.assertRaises(IOError, io.file_write, None, "test123")


        def test_nodata(self):
                "file_write() handles None as data"

                io.file_write("/tmp/iotest-testfile", None)

                f = open("/tmp/iotest-testfile")
                self.assertEquals(f.read(), "")
                f.close()


        def test_overwrite(self):
                "file_write() overwrites file"

                file = "/tmp/iotest-tempfile"
                data = "testjejejeje"

                f = open(file, "w")
                f.write("dummydata")
                f.close()

                io.file_write(file, data)

                f = open(file)
                self.assertEqual(f.read(), data)
                f.close()



if __name__ == "__main__":
        unittest.main()