-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathcheck_sample_filenames.py
121 lines (100 loc) · 3.3 KB
/
check_sample_filenames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright 2023 Google LLC
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
"""
Check testfiles data directory for consistent naming.
"""
import os
import sys
import string
import hashlib
import logging
import os.path
import argparse
from pathlib import Path
logger = logging.getLogger("capa.tests.data")
IGNORED_EXTS = (".md", ".txt", ".git", ".gitattributes", ".gitignore", ".gitmodules", ".json")
VALID_EXTS = (
".exe_",
".dll_",
".elf_",
".sys_",
".raw32",
".raw64",
".aspx_",
".cs_",
".py_",
".json.gz",
".log.gz",
".BinExport",
".zip",
".bndb",
)
IGNORED_DIRS = (".git", ".github", "sigs")
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser()
parser.add_argument("testfiles", type=str, help="Path to tests/data")
args = parser.parse_args(args=argv)
test_failed = test_data_filenames(args)
if test_failed:
return 1
else:
logger.info("test files look good!")
return 0
def test_data_filenames(args):
test_failed = False
for root, _, files in os.walk(args.testfiles):
root = Path(root)
# Skip ignored directories
if any((ignored_dir in root.parts) for ignored_dir in IGNORED_DIRS):
continue
for filename in files:
if filename.endswith(IGNORED_EXTS):
continue
path: Path = root / filename
if not filename.endswith(VALID_EXTS):
logger.error("invalid file extension: %s", path)
test_failed = True
continue
name = path.stem
if all(c in string.hexdigits for c in name):
try:
hashes = get_file_hashes(path)
except IOError:
continue
# MD5 file name
if len(name) == 32:
if hashes["md5"] != name:
logger.error("invalid file name: %s, MD5 hash: %s", path, hashes["md5"])
test_failed = True
# SHA256 file name
elif len(name) == 64:
if hashes["sha256"] != name:
logger.error("invalid file name: %s, SHA256 hash: %s", path, hashes["sha256"])
test_failed = True
else:
logger.error("invalid file name: %s, should be MD5 or SHA256 hash", path)
test_failed = True
return test_failed
def get_file_hashes(path: Path):
buf = path.read_bytes()
md5 = hashlib.md5()
md5.update(buf)
sha256 = hashlib.sha256()
sha256.update(buf)
return {"md5": md5.hexdigest().lower(), "sha256": sha256.hexdigest().lower()}
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
sys.exit(main())