def test_name_setter(self): """ verify that WhiteList.name setter works """ whitelist = WhiteList([], "foo") whitelist.name = "bar" self.assertEqual(whitelist.name, "bar")
def test_name_from_filename(self): """ verify how name_from_filename() works """ self.assertEqual( WhiteList.name_from_filename("some/path/foo.whitelist"), "foo") self.assertEqual(WhiteList.name_from_filename("foo.whitelist"), "foo") self.assertEqual(WhiteList.name_from_filename("foo."), "foo") self.assertEqual(WhiteList.name_from_filename("foo"), "foo") self.assertEqual(WhiteList.name_from_filename("foo.notawhitelist"), "foo")
def test_name_from_filename(self): """ verify how name_from_filename() works """ self.assertEqual( WhiteList.name_from_filename("some/path/foo.whitelist"), "foo") self.assertEqual(WhiteList.name_from_filename("foo.whitelist"), "foo") self.assertEqual(WhiteList.name_from_filename("foo."), "foo") self.assertEqual(WhiteList.name_from_filename("foo"), "foo") self.assertEqual( WhiteList.name_from_filename("foo.notawhitelist"), "foo")
def __init__(self, provider_list, config, settings, ns, display=None): super().__init__(provider_list, config) self.settings = settings self.display = display self.ns = ns self.whitelists = [] self._local_only = False # Only run local jobs if self.ns.whitelist: for whitelist in self.ns.whitelist: self.whitelists.append(WhiteList.from_file(whitelist.name)) elif self.config.whitelist is not Unset: self.whitelists.append(WhiteList.from_file(self.config.whitelist)) elif self.ns.include_pattern_list: self.whitelists.append(WhiteList(self.ns.include_pattern_list))
def test_designates(self): """ verify that WhiteList.designates() works """ self.assertTrue( WhiteList.from_string("foo").designates(make_job('foo'))) self.assertTrue( WhiteList.from_string("foo\nbar\n").designates(make_job('foo'))) self.assertTrue( WhiteList.from_string("foo\nbar\n").designates(make_job('bar'))) # Note, it's not matching either! self.assertFalse( WhiteList.from_string("foo").designates(make_job('foobar'))) self.assertFalse( WhiteList.from_string("bar").designates(make_job('foobar')))
def __init__(self, provider_list, config, settings, ns): super().__init__(provider_list) self.provider_list = provider_list self.config = config self.settings = settings self.ns = ns self.whitelists = [] if self.ns.whitelist: for whitelist in self.ns.whitelist: self.whitelists.append(WhiteList.from_file(whitelist.name)) elif self.config.whitelist is not Unset: self.whitelists.append(WhiteList.from_file(self.config.whitelist)) elif self.ns.include_pattern_list: self.whitelists.append(WhiteList(self.ns.include_pattern_list)) if self.is_interactive: if self.settings['welcome_text']: try: curses.wrapper(show_welcome, self.settings['welcome_text']) except curses.error: raise SystemExit('Terminal size must be at least 80x24') if not self.whitelists: whitelists = [] for p in self.provider_list: if p.name in self.settings['default_providers']: whitelists.extend( [w.name for w in p.get_builtin_whitelists()]) try: selection = curses.wrapper(show_menu, "Suite selection", whitelists) except curses.error: raise SystemExit('Terminal size must be at least 80x24') if not selection: raise SystemExit('No whitelists selected, aborting...') for s in selection: self.whitelists.append( get_whitelist_by_name(provider_list, whitelists[s])) else: self.whitelists.append( get_whitelist_by_name( provider_list, self.settings['default_whitelist'])) print("[ Analyzing Jobs ]".center(80, '=')) self.session = None self.runner = None
def __init__(self, filename, text): """ Initialize the plug-in with the specified name text """ try: self._whitelist = WhiteList.from_string(text, filename=filename) except Exception as exc: raise PlugInError( "Cannot load whitelist {!r}: {}".format(filename, exc))
def get_whitelist_from_file(self, filename, stream=None): """ Load a whitelist from a file, with special behavior. :param filename: name of the file to load :param stream: (optional) pre-opened stream pointing at the whitelist :returns: The loaded whitelist or None if loading fails for any reason This function implements special loading behavior for whitelists that makes them inherit the implicit namespace of the provider they may be a part of. Before loading the whitelist directly from the file, all known providers are interrogated to see if any of them has a whitelist that was loaded from the same file (as indicated by os.path.realpath()) The stream argument can be provided if the caller already has an open file object, which is typically the case when working with argparse. """ # Look up a whitelist with the same name in any of the providers wanted_realpath = os.path.realpath(filename) for provider in self.provider_list: for whitelist in provider.get_builtin_whitelists(): if (whitelist.origin is not None and whitelist.origin.source is not None and isinstance(whitelist.origin.source, FileTextSource) and os.path.realpath( whitelist.origin.source.filename) == wanted_realpath): logger.debug( _("Using whitelist %r obtained from provider %r"), whitelist.name, provider) return whitelist # Or load it directly try: if stream is not None: return WhiteList.from_string(stream.read(), filename=filename) else: return WhiteList.from_file(filename) except Exception as exc: logger.warning( _("Unable to load whitelist %r: %s"), filename, exc)
def __init__(self, filename, text, implicit_namespace=None): """ Initialize the plug-in with the specified name text """ try: self._whitelist = WhiteList.from_string( text, filename=filename, implicit_namespace=implicit_namespace) except Exception as exc: raise PlugInError( _("Cannot load whitelist {!r}: {}").format(filename, exc))
def get_whitelist_from_file(self, filename, stream=None): """ Load a whitelist from a file, with special behavior. :param filename: name of the file to load :param stream: (optional) pre-opened stream pointing at the whitelist :returns: The loaded whitelist or None if loading fails for any reason This function implements special loading behavior for whitelists that makes them inherit the implicit namespace of the provider they may be a part of. Before loading the whitelist directly from the file, all known providers are interrogated to see if any of them has a whitelist that was loaded from the same file (as indicated by os.path.realpath()) The stream argument can be provided if the caller already has an open file object, which is typically the case when working with argparse. """ # Look up a whitelist with the same name in any of the providers wanted_realpath = os.path.realpath(filename) for provider in self.provider_list: for whitelist in provider.get_builtin_whitelists(): if (whitelist.origin is not None and whitelist.origin.source is not None and isinstance(whitelist.origin.source, FileTextSource) and os.path.realpath(whitelist.origin.source.filename) == wanted_realpath): logger.debug( _("Using whitelist %r obtained from provider %r"), whitelist.name, provider) return whitelist # Or load it directly try: if stream is not None: return WhiteList.from_string(stream.read(), filename=filename) else: return WhiteList.from_file(filename) except Exception as exc: logger.warning(_("Unable to load whitelist %r: %s"), filename, exc)
def test_from_string(self): """ verify that WhiteList.from_string() works """ whitelist = WhiteList.from_string("\n".join(self._content)) # verify that the patterns are okay self.assertEqual(repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name is the empty default self.assertEqual(whitelist.name, None) # verify that the origin got set to the default constructed value self.assertEqual(whitelist.origin, Origin(UnknownTextSource(), 1, 3))
def test_from_string(self): """ verify that WhiteList.from_string() works """ whitelist = WhiteList.from_string("\n".join(self._content)) # verify that the patterns are okay self.assertEqual( repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name is the empty default self.assertEqual(whitelist.name, None) # verify that the origin got set to the default constructed value self.assertEqual(whitelist.origin, Origin(UnknownTextSource(), 1, 3))
def test_from_file(self): """ verify that WhiteList.from_file() works """ with self.mocked_file(self._name, self._content): whitelist = WhiteList.from_file(self._name) # verify that the patterns are okay self.assertEqual(repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name got set self.assertEqual(whitelist.name, "whitelist") # verify that the origin got set self.assertEqual(whitelist.origin, Origin(FileTextSource("whitelist.txt"), 1, 3))
def test_from_file(self): """ verify that WhiteList.from_file() works """ with self.mocked_file(self._name, self._content): whitelist = WhiteList.from_file(self._name) # verify that the patterns are okay self.assertEqual( repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name got set self.assertEqual(whitelist.name, "whitelist") # verify that the origin got set self.assertEqual( whitelist.origin, Origin(FileTextSource("whitelist.txt"), 1, 3))
def test_from_string__with_filename(self): """ verify that WhiteList.from_string() works when passing filename """ # construct a whitelist with some dummy data, the names, pathnames and # line ranges are arbitrary whitelist = WhiteList.from_string("\n".join(self._content), filename="somefile.txt") # verify that the patterns are okay self.assertEqual(repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name is derived from the filename self.assertEqual(whitelist.name, "somefile") # verify that the origin is properly derived from the filename self.assertEqual(whitelist.origin, Origin(FileTextSource("somefile.txt"), 1, 3))
def test_from_string__with_filename(self): """ verify that WhiteList.from_string() works when passing filename """ # construct a whitelist with some dummy data, the names, pathnames and # line ranges are arbitrary whitelist = WhiteList.from_string( "\n".join(self._content), filename="somefile.txt") # verify that the patterns are okay self.assertEqual( repr(whitelist.qualifier_list[0]), "RegExpJobQualifier('^foo$', inclusive=True)") # verify that whitelist name is derived from the filename self.assertEqual(whitelist.name, "somefile") # verify that the origin is properly derived from the filename self.assertEqual( whitelist.origin, Origin(FileTextSource("somefile.txt"), 1, 3))
def test_namespace_behavior(self): """ verify that WhiteList() correctly respects namespace declarations and uses implict_namespace to fully qualifiy all patterns """ whitelist = WhiteList.from_string( "foo\n" "2014\\.example\\.org::bar\n", implicit_namespace="2014.other.example.org") # verify that the implicit namespace was recorded self.assertEqual(whitelist.implicit_namespace, "2014.other.example.org") # verify that the patterns are okay self.assertEqual(whitelist.qualifier_list[0].pattern_text, "^2014\\.other\\.example\\.org::foo$") self.assertEqual(whitelist.qualifier_list[1].pattern_text, "^2014\\.example\\.org::bar$")
def test_namespace_behavior(self): """ verify that WhiteList() correctly respects namespace declarations and uses implict_namespace to fully qualifiy all patterns """ whitelist = WhiteList.from_string( "foo\n" "2014\\.example\\.org::bar\n", implicit_namespace="2014.other.example.org") # verify that the implicit namespace was recorded self.assertEqual( whitelist.implicit_namespace, "2014.other.example.org") # verify that the patterns are okay self.assertEqual( whitelist.qualifier_list[0].pattern_text, "^2014\\.other\\.example\\.org::foo$") self.assertEqual( whitelist.qualifier_list[1].pattern_text, "^2014\\.example\\.org::bar$")
def test_from_empty_string(self): """ verify that WhiteList.from_string("") works """ WhiteList.from_string("")
def test_load_patterns(self): with self.mocked_file(self._name, self._content): pattern_list, max_lineno = WhiteList._load_patterns(self._name) self.assertEqual(pattern_list, ['^foo$', '^bar$']) self.assertEqual(max_lineno, 3)
def test_name_getter(self): """ verify that WhiteList.name getter works """ self.assertEqual(WhiteList([], "foo").name, "foo")
def test_repr(self): """ verify that custom repr works """ whitelist = WhiteList([], name="test") self.assertEqual(repr(whitelist), "<WhiteList name:'test'>")