def test_write_file(self): # valid password and plain keyfile, compressed kdb with keepass.open(absfile1, password="******") as kdb: self.assertEquals(kdb.opened, True) self.assertEquals(kdb.read(32), '<?xml version="1.0" encoding="ut') kdb.set_compression(0) #kdb.set_comment("this is pretty cool!") kdb.clear_credentials() kdb.add_credentials(password="******") with open(output1, 'w') as outfile: kdb.write_to(outfile) with keepass.open(output1, password="******") as kdb: self.assertEquals(kdb.read(32), "<?xml version='1.0' encoding='ut") with keepass.open(absfile4, password="******", keyfile=keyfile4) as kdb: self.assertEquals(kdb.opened, True) self.assertEquals(kdb.read(32), '<?xml version="1.0" encoding="ut') with open(output4, 'w') as outfile: kdb.write_to(outfile) with keepass.open(output4, password="******", keyfile=keyfile4) as kdb: self.assertEquals(kdb.read(32), "<?xml version='1.0' encoding='ut")
def create_db(args): db_file = None key_file = None if args.db_file is not None: db_file = os.path.expanduser(args.db_file) elif 'KP_DB_FILE' in os.environ: db_file = os.path.expanduser(os.environ['KP_DB_FILE']) else: sys.stderr.write("Must supply a db filename.\n") sys.exit(1) if args.key_file is not None: key_file = os.path.expanduser(args.key_file) elif 'KP_KEY_FILE' in os.environ: key_file = os.path.expanduser(os.environ['KP_KEY_FILE']) password = getpass.getpass('Password: ') with keepass.open(db_file, password=password, keyfile=key_file) as kdb: yield kdb
def test_open_file(self): # file not found, proper exception gets re-raised with self.assertRaisesRegexp(IOError, "No such file or directory"): with keepass.open(filename1, password="******"): pass # invalid password with self.assertRaisesRegexp(IndexError, "No credentials found."): with keepass.open(absfile1): pass # invalid password with self.assertRaisesRegexp(IOError, "Master key invalid."): with keepass.open(absfile1, password="******"): pass # invalid keyfile with self.assertRaisesRegexp(IOError, "Master key invalid."): with keepass.open(absfile1, password="******", keyfile="invalid"): pass # old kdb file with keepass.open(absfile2, password="******") as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) # valid password with keepass.open(absfile1, password="******") as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) self.assertIsInstance(kdb, keepass.kdb4.KDB4Reader) # valid password and xml keyfile with keepass.open(absfile3, password="******", keyfile=keyfile3) as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) self.assertIsInstance(kdb, keepass.kdb4.KDB4Reader) # valid password and plain keyfile, compressed kdb with keepass.open(absfile4, password="******", keyfile=keyfile4) as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) self.assertIsInstance(kdb, keepass.kdb4.KDB4Reader) # read raw data tmp1 = kdb.read(32) tmp2 = kdb.read(32) self.assertIsNotNone(tmp1) self.assertEquals(tmp1, '<?xml version="1.0" encoding="ut') self.assertIsNotNone(tmp2) self.assertEquals(tmp2, 'f-8" standalone="yes"?>\n<KeePass') self.assertNotEquals(tmp1, tmp2) self.assertEquals(kdb.tell(), 64) kdb.seek(0) tmp3 = kdb.read(32) self.assertEquals(tmp1, tmp3) self.assertNotEquals(tmp2, tmp3) # read xml xml1 = kdb.obj_root.Root.Group.Entry.String[1].Value self.assertEquals(xml1, "Password") xml2 = kdb.obj_root.Root.Group.Entry.String[1].Value.get('ProtectedValue') kdb.protect() # re-encrypt protected values again xml3 = kdb.obj_root.Root.Group.Entry.String[1].Value self.assertEquals(xml2, xml3) kdb.unprotect() # and make passwords clear again xml4 = kdb.obj_root.Root.Group.Entry.String[1].Value self.assertEquals(xml1, xml4) self.assertIsNotNone(kdb.pretty_print())
def test_open_file(self): # file not found, proper exception gets re-raised with self.assertRaisesRegexp(IOError, "No such file or directory"): with keepass.open(filename1, password="******"): pass # invalid password with self.assertRaisesRegexp(IndexError, "No credentials found."): with keepass.open(absfile1): pass # invalid password with self.assertRaisesRegexp(IOError, "Master key invalid."): with keepass.open(absfile1, password="******"): pass # invalid keyfile with self.assertRaisesRegexp(IOError, "Master key invalid."): with keepass.open(absfile1, password="******", keyfile="invalid"): pass # old kdb file with keepass.open(absfile2, password="******") as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) # valid password with keepass.open(absfile1, password="******") as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) self.assertIsInstance(kdb, keepass.kdb4.KDB4Reader) # valid password and xml keyfile with keepass.open(absfile3, password="******", keyfile=keyfile3) as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) self.assertIsInstance(kdb, keepass.kdb4.KDB4Reader) # valid password and plain keyfile, compressed kdb with keepass.open(absfile4, password="******", keyfile=keyfile4) as kdb: self.assertIsNotNone(kdb) self.assertEquals(kdb.opened, True) self.assertIsInstance(kdb, keepass.kdb4.KDB4Reader) # read raw data tmp1 = kdb.read(32) tmp2 = kdb.read(32) self.assertIsNotNone(tmp1) self.assertEquals(tmp1, '<?xml version="1.0" encoding="ut') self.assertIsNotNone(tmp2) self.assertEquals(tmp2, 'f-8" standalone="yes"?>\n<KeePass') self.assertNotEquals(tmp1, tmp2) self.assertEquals(kdb.tell(), 64) kdb.seek(0) tmp3 = kdb.read(32) self.assertEquals(tmp1, tmp3) self.assertNotEquals(tmp2, tmp3) # read xml xml1 = kdb.obj_root.Root.Group.Entry.String[1].Value self.assertEquals(xml1, "Password") xml2 = kdb.obj_root.Root.Group.Entry.String[1].Value.get( 'ProtectedValue') kdb.protect() # re-encrypt protected values again xml3 = kdb.obj_root.Root.Group.Entry.String[1].Value self.assertEquals(xml2, xml3) kdb.unprotect() # and make passwords clear again xml4 = kdb.obj_root.Root.Group.Entry.String[1].Value self.assertEquals(xml1, xml4) self.assertIsNotNone(kdb.pretty_print())