def test_get_and_set(self): """Test the get and set funcionts.""" imapset = imaputils.ImapSettings() # Test: sec = secrets.SecretKeyring(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."): sec.set("foo3", "boo5", overwrite=False) pytest.fail("It should raise a ValueError") # 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"): sec.delete("foo4") pytest.fail("It should raise a ValueError") sec.delete("foo3") assert sec.get("foo3") is None # Remove non existant key: with pytest.raises(ValueError, match="Key 'foo4' not"): sec.delete("foo4") pytest.fail("It should raise a ValueError")
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 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_login_imap(): """Test login_imap.""" with pytest.raises(TypeError, match="ImapSettings"): imaputils.login_imap(None) pytest.fail("spected a ImapSettings") imapsets = imaputils.ImapSettings() imapsets.host = '' # don't try to connect to internet imapsets.nossl = True with pytest.raises(Exception, match="[Errno -5]"): imaputils.login_imap(imapsets, logger=logging.getLogger(__name__)) pytest.fail("No address associated with hostname")
def __init__(self): """Initialize a ISBG object.""" self.imapsets = imaputils.ImapSettings() self.imap = None # FIXME: This could be used when ran non-interactively, maybe with # the --noninteractive argument (instead of the addHandler: # logging.basicConfig( # format=('%(asctime)s %(levelname)-8s [%(filename)s' # + '%(lineno)d] %(message)s'), # datefmt='%Y%m%d %H:%M:%S %Z') # see https://docs.python.org/2/howto/logging-cookbook.html self.logger = logging.getLogger(__name__) #: a logger self.logger.addHandler(logging.StreamHandler()) # We create the dir for store cached information (if needed) if not os.path.isdir(os.path.join(xdg_cache_home, "isbg")): os.makedirs(os.path.join(xdg_cache_home, "isbg")) self.imaplist, self.nostats = (False, False) self.noreport, self.exitcodes = (False, True) self.verbose_mails, self._verbose = (False, False) self._set_loglevel(logging.INFO) # Processing options: self.dryrun, self.maxsize, self.teachonly = (False, 120000, False) self.spamc, self.gmail = (False, False) # spamassassin options: self.movehamto, self.delete = (None, False) self.deletehigherthan, self.flag, self.expunge = (None, False, False) # Learning options: self.learnflagged, self.learnunflagged = (False, False) self.learnthendestroy, self.learnthenflag = (False, False) # Lockfile options: self.ignorelockfile = False self.lockfilename = os.path.join(xdg_cache_home, "isbg", "lock") self.lockfilegrace = 240.0 # Password options (a vague level of obfuscation): self.passwdfilename, self.savepw = (None, False) # Trackfile options: self.trackfile, self.partialrun = (None, 50) try: self.interactive = sys.stdin.isatty() except AttributeError: self.logger.warning("Can't get info about if stdin is a tty") self.interactive = False # what we use to set flags on the original spam in imapbox self.spamflagscmd = "+FLAGS.SILENT" # and the flags we set them to (none by default) self.spamflags = []
def test(self): """Test the object.""" imapset = imaputils.ImapSettings() imaphash = imapset.hash assert imapset.hash == imaphash assert imapset.hash.hexdigest() == '56fdd686137c8645d44024096a0ed441' assert imapset.hash.hexdigest() == '56fdd686137c8645d44024096a0ed441' assert imapset.hash.hexdigest() == '56fdd686137c8645d44024096a0ed441' imapset.host = '127.0.0.1' assert imapset.hash.hexdigest() == 'ca057ebec07690c05f64959fff011c8d' assert imapset.hash.hexdigest() == 'ca057ebec07690c05f64959fff011c8d' assert imapset.hash.hexdigest() == 'ca057ebec07690c05f64959fff011c8d' imapset.host = 'localhost' assert imapset.hash.hexdigest() == '56fdd686137c8645d44024096a0ed441' assert imapset.hash.hexdigest() == '56fdd686137c8645d44024096a0ed441' assert imapset.hash.hexdigest() == '56fdd686137c8645d44024096a0ed441'
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 __init__(self): """Initialize a ISBG object.""" self.imapsets = imaputils.ImapSettings() self.imap = None self.logger = logging.getLogger(__name__) #: a logger self.logger.addHandler(logging.StreamHandler()) # We create the dir for store cached information (if needed) if not os.path.isdir(os.path.join(xdg_cache_home, "isbg")): os.makedirs(os.path.join(xdg_cache_home, "isbg")) self.imaplist, self.nostats = (False, False) self.noreport, self.exitcodes = (False, True) self.verbose_mails, self._verbose = (False, False) self._set_loglevel(logging.INFO) # Processing options: self.dryrun, self.maxsize, self.teachonly = (False, 120000, False) self.spamc, self.gmail = (False, False) # spamassassin options: self.movehamto, self.delete = (None, False) self.deletehigherthan, self.flag, self.expunge = (None, False, False) # Learning options: self.learnflagged, self.learnunflagged = (False, False) self.learnthendestroy, self.learnthenflag = (False, False) # Lockfile options: self.ignorelockfile = False self.lockfilename = os.path.join(xdg_cache_home, "isbg", "lock") self.lockfilegrace = 240.0 # Password options (a vague level of obfuscation): self.passwdfilename, self.savepw = (None, False) # Trackfile options: self.trackfile, self.partialrun = (None, 50) try: self.interactive = sys.stdin.isatty() except AttributeError: self.logger.warning("Can't get info about if stdin is a tty") self.interactive = False # what we use to set flags on the original spam in imapbox self.spamflagscmd = "+FLAGS.SILENT" # and the flags we set them to (none by default) self.spamflags = []