Files
capa/tests/test_rule_cache.py
2023-01-20 15:20:26 +01:00

83 lines
2.2 KiB
Python

# Copyright (C) 2023 FireEye, Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: [package root]/LICENSE.txt
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
import os
import textwrap
import capa.rules
import capa.rules.cache
R1 = capa.rules.Rule.from_yaml(textwrap.dedent(
"""
rule:
meta:
name: test rule
authors:
- user@domain.com
scope: function
examples:
- foo1234
- bar5678
features:
- and:
- number: 1
- number: 2
"""
))
R2 = capa.rules.Rule.from_yaml(textwrap.dedent(
"""
rule:
meta:
name: test rule 2
authors:
- user@domain.com
scope: function
examples:
- foo1234
- bar5678
features:
- and:
- number: 3
- number: 4
"""
))
def test_ruleset_cache_ids():
rs = capa.rules.RuleSet([R1])
content = capa.rules.cache.get_ruleset_content(rs)
rs2 = capa.rules.RuleSet([R1, R2])
content2 = capa.rules.cache.get_ruleset_content(rs2)
id = capa.rules.cache.compute_cache_identifier(content)
id2 = capa.rules.cache.compute_cache_identifier(content2)
assert id != id2
def test_ruleset_cache_save_load():
rs = capa.rules.RuleSet([R1])
content = capa.rules.cache.get_ruleset_content(rs)
id = capa.rules.cache.compute_cache_identifier(content)
assert id is not None
path = capa.rules.cache.get_default_cache_path(id)
try:
os.remove(path)
except OSError:
pass
capa.rules.cache.cache_ruleset(rs)
assert os.path.exists(path)
rs = capa.rules.cache.load_cached_ruleset(content)
assert rs is not None