Пример #1
0
def test_rules_supported_options():
    rules = bucket.JBlockBuckets(["adv", "@@advice.$~script"])
    assert not rules.should_block("http://example.com/advice.html",
                                  {"script": False})

    # exception rule should be discarded if "script" option is not supported
    rules2 = bucket.JBlockBuckets(["adv", "@@advice.$~script"],
                                  supported_options=[])
    assert rules2.should_block("http://example.com/advice.html",
                               {"script": False})
Пример #2
0
def jblock_reload(quiet=False):
    """Reload jblock rules from disk."""
    global init_time
    global jblock_buckets
    global psl
    global whitelist_urls
    init_time = time.perf_counter()
    lines = []
    if JBLOCK_RULES.exists():
        with open(JBLOCK_RULES, "r", encoding="utf-8") as f:
            lines = f.readlines()

    freq = None
    if JBLOCK_FREQ.exists():
        with open(JBLOCK_FREQ, "rb") as f:
            freq = pickle.load(f)

    jblock_buckets = bucket.JBlockBuckets(lines, token_frequency=freq)
    init_time = time.perf_counter() - init_time

    if not quiet:
        message.info("Loaded {} rules from lists.".format(len(jblock_buckets)))

    # Init whitelist (handled entirely in integration)
    if JBLOCK_WHITELIST.exists():
        with open(JBLOCK_WHITELIST, 'r') as f:
            whitelist_urls = frozenset(
                filter(None, map(operator.methodcaller('strip'), f)))

    # initialize PSL
    psl = fpdomain.PSL(PSL_FILE)
Пример #3
0
def test_rule_with_options(rule_text, results):
    rule = parser.JBlockRule(rule_text)
    rules = bucket.JBlockBuckets([rule_text])

    for url, params, match in results:
        assert rule.match_url(url, params) == match
        assert rules.should_block(url, params) == match
Пример #4
0
 def easylist_buckets(self, easylist, ubo_urls):
     "A standard, optimized easylist buckets"
     b = bucket.JBlockBuckets(easylist)
     for url in ubo_urls:
         b.should_block(url)
     b.regen_buckets(easylist)
     return b
Пример #5
0
def test_rule_exceptions(rules, results):
    rules = bucket.JBlockBuckets(rules)

    for url in results["blocks"]:
        assert rules.should_block(url)

    for url in results["doesn't block"]:
        assert not rules.should_block(url)
Пример #6
0
def test_documented_examples(rule_text, results):
    rule = parser.JBlockRule(rule_text)
    rules = bucket.JBlockBuckets([rule_text])

    for url in results["blocks"]:
        assert rule.match_url(url)
        assert rules.should_block(url)

    for url in results["doesn't block"]:
        assert not rule.match_url(url)
        assert not rules.should_block(url)
Пример #7
0
    def easylist_buckets_nofreq(self, easylist):
        tracemalloc.start()
        initial_snapshot = tracemalloc.take_snapshot()

        b = bucket.JBlockBuckets(easylist)

        new_snapshot = tracemalloc.take_snapshot()
        tracemalloc.stop()
        top_stats = new_snapshot.compare_to(initial_snapshot, "lineno")
        top_stats = top_stats[:100]
        top_stats = "\n".join(map(str, top_stats))
        # Remove if you don't like memory stats
        print(top_stats)
        return b
Пример #8
0
def test_empty_regexp_rules():
    with pytest.raises(tools.JBlockParseError):
        bucket.JBlockBuckets(["adv", "/", "//"])
Пример #9
0
def test_empty_rules():
    rules = bucket.JBlockBuckets(["adv", "", " \t", parser.JBlockRule("adv2")])
    assert len(rules) == 2
Пример #10
0
def test_rules_instantiation():
    rule = parser.JBlockRule("adv")
    rules = bucket.JBlockBuckets([rule])
    assert rule.match_url("http://example.com/adv")
    assert rules.should_block("http://example.com/adv")
Пример #11
0
def test_regex_rules():
    rules = bucket.JBlockBuckets([r"/banner\d+/"])
    assert rules.should_block("banner123")
    assert not rules.should_block("banners")
Пример #12
0
def test_rules_with_options(rules, results):
    rules = bucket.JBlockBuckets(rules)
    for url, params, should_block in results:
        assert rules.should_block(url, params) == should_block
Пример #13
0
def test_empty_regexp_rules():
    with pytest.raises(tools.JBlockParseError):
        bucket.JBlockBuckets(['adv', '/', '//'])