Пример #1
0
 def audit(self, file_path):
     self.auditor = PluginsManager(config=self.config)
     self.parser = NginxParser(file_path,
                               allow_includes=self.config.allow_includes)
     self.root = self.parser.parse(file_path)
     push_context(self.root)
     self._audit_recursive(self.root.children)
Пример #2
0
def test_context_depend_variables():
    push_context(Root())
    assert_equals(len(get_context().variables['index']), 0)
    assert_equals(len(get_context().variables['name']), 0)

    get_context().add_var(1, Variable(1, value='one'))
    get_context().add_var('some', Variable('some', value='some'))

    assert_equals(get_context().get_var(1).value, 'one')
    assert_equals(get_context().get_var('some').value, 'some')

    # Checks top context variables are still exists
    push_context(Root())
    assert_equals(get_context().get_var(1).value, 'one')
    assert_equals(get_context().get_var('some').value, 'some')

    # Checks variable overriding
    get_context().add_var('some', Variable('some', value='some_new'))
    get_context().add_var('foo', Variable('foo', value='foo'))
    assert_not_equals(get_context().get_var('some').value, 'some')
    assert_equals(get_context().get_var('some').value, 'some_new')
    assert_equals(get_context().get_var('foo').value, 'foo')
    assert_equals(get_context().get_var(1).value, 'one')

    # Checks variables after restore previous context
    pop_context()
    assert_not_equals(get_context().get_var('some').value, 'some_new')
    assert_equals(get_context().get_var('some').value, 'some')
    assert_equals(get_context().get_var('foo'), None)
    assert_equals(get_context().get_var(1).value, 'one')
Пример #3
0
    def audit(self, file_path, file_data, is_stdin=False):
        LOG.debug("Audit config file: {fname}".format(fname=file_path))
        parser = NginxParser(
            cwd=os.path.dirname(file_path) if not is_stdin else '',
            allow_includes=self.config.allow_includes)
        self.root = parser.parse(content=file_data.read(), path_info=file_path)

        push_context(self.root)
        self._audit_recursive(self.root.children)
Пример #4
0
 def _audit_recursive(self, tree):
     for directive in tree:
         self._update_variables(directive)
         self.auditor.audit(directive)
         if directive.is_block:
             if directive.self_context:
                 push_context(directive)
             self._audit_recursive(directive.children)
             if directive.self_context:
                 pop_context()
Пример #5
0
def test_push_failed_with_regexp_py35_gixy_10():
    push_context(Root())
    assert_equals(len(get_context().variables['index']), 0)
    assert_equals(len(get_context().variables['name']), 0)

    regexp = Regexp('^/some/(.*?)')
    for name, group in regexp.groups.items():
        get_context().add_var(name, Variable(name=name, value=group))

    push_context(Root())
Пример #6
0
def test_push_get_purge_context():
    root = Root()
    push_context(root)
    assert_equals(len(CONTEXTS), 1)
    assert_equals(get_context().block, root)
    root = Root()
    push_context(root)
    assert_equals(len(CONTEXTS), 2)
    assert_equals(get_context().block, root)

    purge_context()
    assert_equals(len(CONTEXTS), 0)
Пример #7
0
def test_push_pop_context():
    root_a = Root()
    push_context(root_a)
    assert_equals(len(CONTEXTS), 1)
    root_b = Root()
    push_context(root_b)
    assert_equals(len(CONTEXTS), 2)

    poped = pop_context()
    assert_equals(len(CONTEXTS), 1)
    assert_equals(poped.block, root_b)
    poped = pop_context()
    assert_equals(len(CONTEXTS), 0)
    assert_equals(poped.block, root_a)
Пример #8
0
def test_get_variables():
    context = push_context(Root())
    assert_equals(len(context.variables['index']), 0)
    assert_equals(len(context.variables['name']), 0)

    one_var = Variable(1)
    context.add_var(1, one_var)
    some_var = Variable('some')
    context.add_var('some', some_var)

    assert_equals(context.get_var(1), one_var)
    assert_equals(context.get_var('some'), some_var)
    # Checks not existed variables, for now context may return None
    assert_equals(context.get_var(0), None)
    assert_equals(context.get_var('not_existed'), None)
    # Checks builtins variables
    assert_true(context.get_var('uri'))
    assert_true(context.get_var('document_uri'))
    assert_true(context.get_var('arg_asdsadasd'))
    assert_true(context.get_var('args'))
Пример #9
0
def test_add_variables():
    context = push_context(Root())
    assert_equals(len(context.variables['index']), 0)
    assert_equals(len(context.variables['name']), 0)

    one_str_var = Variable('1')
    context.add_var('1', one_str_var)
    one_int_var = Variable(1)
    context.add_var(1, one_int_var)
    some_var = Variable('some')
    context.add_var('some', some_var)

    assert_equals(len(context.variables['index']), 1)
    assert_equals(context.variables['index'][1], one_int_var)
    assert_equals(len(context.variables['name']), 1)
    assert_equals(context.variables['name']['some'], some_var)
    context.clear_index_vars()
    assert_equals(len(context.variables['index']), 0)
    assert_equals(len(context.variables['name']), 1)
    assert_equals(context.variables['name']['some'], some_var)
Пример #10
0
def setup():
    push_context(Root())