def test_hash2(self): """Test the hash.""" imapset = imaputils.ImapSettings() sec = secrets.SecretIsbg(filename="", imapset=imapset) sec.hashlen == len(sec.hash) sec = secrets.SecretIsbg(filename="", imapset=imapset, hashlen=16) sec.hashlen == len(sec.hash) sec.hashlen == 16
def test_get_and_set(self): """Test the get and set funcionts.""" imapset = imaputils.ImapSettings() # Test with a erroneous filename. sec = secrets.SecretIsbg(filename="", imapset=imapset) assert sec.get("foo") is None with pytest.raises(EnvironmentError, match="\[Errno ", message="A EnvironmentError should be raised."): sec.set("foo", "boo") # Test with a ok filename: sec = secrets.SecretIsbg(filename="tmp.txt", imapset=imapset) assert sec.get("foo") is None sec.set("foo1", "boo1") sec.set("foo2", "boo2") sec.set("foo3", "boo3") sec.set("foo3", "boo4") assert sec.get("foo3") == "boo4" with pytest.raises(ValueError, match="Key 'foo3' exists.", message="It should raise a ValueError"): sec.set("foo3", "boo5", overwrite=False) assert sec.get("foo3") == "boo4" with pytest.raises(TypeError, message="A TypeError should be raised."): sec.set("foo4", 4) assert sec.get("foo2") == "boo2" # Remove the created keys: sec.delete("foo1") assert sec.get("foo1") is None sec.delete("foo2") assert sec.get("foo2") is None # Remove non existant key: with pytest.raises(ValueError, match="Key 'foo4' not", message="It should raise a ValueError"): sec.delete("foo4") # Remove last key, it should delete the file: sec.delete("foo3") assert sec.get("foo3") is None with pytest.raises(EnvironmentError, match="\[Errno 2\]", message="A EnvironmentError should be raised."): os.remove("tmp.txt") # Remove non existant key in non existant file: with pytest.raises(ValueError, match="Key 'foo4' not", message="It should raise a ValueError"): sec.delete("foo4")
def _do_save_password(self): """Save password to the password file.""" try: sec = secrets.SecretIsbg(filename=self.passwdfilename, imapset=self.imapsets) sec.set("password", self.imapsets.passwd) del sec except Exception: # pylint: disable=broad-except self.logger.exception('Error saving pw!')
def test__deobfuscate(self): """Test _deobfuscate.""" sec = secrets.SecretIsbg(filename="", imapset=imaputils.ImapSettings()) pas = """QVMVEGQ2ODYxMzdjODY0NWQ0NDAyNDA5NmEwZWQ0NDEwNjdlYmQxMTY0ZGUyMDliMWQ1ZjgzODMw YzBjMDBlYWE3OWI1NzU1MzEzZmUzNmU3M2YzMGM5MmU1NmE2YjFlMDM0NTIxZTg1MWFlNzM0MTgy NDQ5NDNlYWU1N2YwMzI0M2VhYTI0MTAyYTgwOWZkYjA5ZTBmZjkzM2UwYzIwZWI4YzhiZjZiMTRh NTZlOTUwYjUyNjM5MzdhNTNjMWNmOWFjNGY3ODQyZDE4MWMxNWNkMDA0MjRkODZiNmQ4NzZjM2Ez NTk2YTEyMDIyYTM4ZDc3YjM3Mzk2OGNlMzc1Yg== """ pas = base64.b64decode(pas).decode("utf-8") ret = sec._deobfuscate(pas) assert ret == u"test"
def test__obfuscate(self): """Test _obfuscate.""" sec = secrets.SecretIsbg(filename="", imapset=imaputils.ImapSettings()) # We construct a password: pas = sec._obfuscate(u"test") try: # pylint: disable=no-member pas = base64.encodebytes(bytes(pas, "utf-8")) # py3 pas = pas.decode("utf-8") except (TypeError, AttributeError): pas = base64.encodestring(pas) # py2 res = """QVMVEGQ2ODYxMzdjODY0NWQ0NDAyNDA5NmEwZWQ0NDEwNjdlYmQxMTY0ZGUyMDliMWQ1ZjgzODMw YzBjMDBlYWE3OWI1NzU1MzEzZmUzNmU3M2YzMGM5MmU1NmE2YjFlMDM0NTIxZTg1MWFlNzM0MTgy NDQ5NDNlYWU1N2YwMzI0M2VhYTI0MTAyYTgwOWZkYjA5ZTBmZjkzM2UwYzIwZWI4YzhiZjZiMTRh NTZlOTUwYjUyNjM5MzdhNTNjMWNmOWFjNGY3ODQyZDE4MWMxNWNkMDA0MjRkODZiNmQ4NzZjM2Ez NTk2YTEyMDIyYTM4ZDc3YjM3Mzk2OGNlMzc1Yg== """ assert pas == res, "Unexpected password encoded"
def _do_get_password(self): """Get the password from file or prompt for it.""" if (self.savepw is False and os.path.exists(self.passwdfilename) is True): try: sec = secrets.SecretIsbg(filename=self.passwdfilename, imapset=self.imapsets) self.imapsets.passwd = sec.get("password") del sec self.logger.debug("Successfully read password file") except Exception: # pylint: disable=broad-except self.logger.exception('Error reading pw!') # do we have to prompt? if self.imapsets.passwd is None: if not self.interactive: raise ISBGError(__exitcodes__['ok'], "You need to specify your imap password " + "and save it with the --savepw switch") self.imapsets.passwd = getpass.getpass( "IMAP password for %s@%s: " % ( self.imapsets.user, self.imapsets.host))