Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Dev/1.8/enhance tests #226

Open
wants to merge 4 commits into
base: rel/1.8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions fido/char_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@

# \a\b\n\r\t\v
# MdR: took out '<' and '>' out of _ordinary because they were converted to entities &lt;&gt;
# MdR: moved '!' from _ordinary to _special because it means "NOT" in the regex world. At this time no regex in any sig has a negate set, did this to be on the safe side
ORDINARY = frozenset(' "#%&\',-/0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~')
SPECIAL = '$()*+.?![]^\\{|}' # Before: '$*+.?![]^\\{|}'
HEX = '0123456789abcdef'
# MdR: moved '!' from _ordinary to _special because it means "NOT" in the regex
# world. At this time no regex in any sig has a negate set, did this to be on
# the safe side
ORDINARY = frozenset(
" \"#%&',-/0123456789:;=@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~"
)
SPECIAL = "$()*+.?![]^\\{|}" # Before: '$*+.?![]^\\{|}'
HEX = "0123456789abcdef"


def escape_char(c):
"""Add appropriate escape sequence to passed character c."""
if c in '\n':
return '\\n'
if c == '\r':
return '\\r'
if c in "\n":
return "\\n"
if c == "\r":
return "\\r"
if c in SPECIAL:
return '\\' + c
return "\\" + c
(high, low) = divmod(ord(c), 16)
return '\\x' + HEX[high] + HEX[low]
return "\\x" + HEX[high] + HEX[low]


def escape(string):
"""Escape characters in pattern that are non-printable, non-ascii, or special for regexes."""
return ''.join(c if c in ORDINARY else escape_char(c) for c in string)
return "".join(c if c in ORDINARY else escape_char(c) for c in string)
35 changes: 28 additions & 7 deletions fido/cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,24 @@ def parse_cli_args(argv: List[str], defaults: Dict[str, Any]) -> argparse.Namesp
fromfile_prefix_chars="@",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument("-v", default=False, action="store_true", help="show version information")
parser.add_argument("-q", default=False, action="store_true", help="run (more) quietly")
parser.add_argument("-recurse", default=False, action="store_true", help="recurse into subdirectories")
parser.add_argument("-zip", default=False, action="store_true", help="recurse into zip and tar files")
parser.add_argument(
"-v", default=False, action="store_true", help="show version information"
)
parser.add_argument(
"-q", default=False, action="store_true", help="run (more) quietly"
)
parser.add_argument(
"-recurse",
default=False,
action="store_true",
help="recurse into subdirectories",
)
parser.add_argument(
"-zip",
default=False,
action="store_true",
help="recurse into zip and tar files",
)
parser.add_argument(
"-noextension",
default=False,
Expand All @@ -44,7 +58,9 @@ def parse_cli_args(argv: List[str], defaults: Dict[str, Any]) -> argparse.Namesp

group = parser.add_mutually_exclusive_group()
group.add_argument(
"-input", default=False, help="file containing a list of files to check, one per line. - means stdin"
"-input",
default=False,
help="file containing a list of files to check, one per line. - means stdin",
)
group.add_argument(
"files",
Expand All @@ -54,7 +70,9 @@ def parse_cli_args(argv: List[str], defaults: Dict[str, Any]) -> argparse.Namesp
help="files to check. If the file is -, then read content from stdin. In this case, python must be invoked with -u or it may convert the line terminators.",
)

parser.add_argument("-filename", default=None, help="filename if file contents passed through STDIN")
parser.add_argument(
"-filename", default=None, help="filename if file contents passed through STDIN"
)
parser.add_argument(
"-useformats",
metavar="INCLUDEPUIDS",
Expand Down Expand Up @@ -98,7 +116,10 @@ def parse_cli_args(argv: List[str], defaults: Dict[str, Any]) -> argparse.Namesp
help=f"size (in bytes) of the buffer to match against (default={defaults['container_bufsize']}).",
)
parser.add_argument(
"-loadformats", default=None, metavar="XML1,...,XMLn", help="comma separated string of XML format files to add."
"-loadformats",
default=None,
metavar="XML1,...,XMLn",
help="comma separated string of XML format files to add.",
)
parser.add_argument(
"-confdir",
Expand Down
10 changes: 7 additions & 3 deletions fido/fido.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,16 +541,20 @@ def can_recurse_into_container(self, container_type):
"""
return container_type in ("zip", "tar")

# This is updated following PR #191: FIX: Develop out FIDO tests with pytest
# It should fix a problem that streams (not files) would hang.
# Needs thorough testing, though.
def blocking_read(self, file, bytes_to_read):
"""Perform a blocking read and return the buffer."""
bytes_read = 0
buffer = b""
while bytes_read < bytes_to_read:
readbuffer = file.read(bytes_to_read - bytes_read)
last_read_len = len(readbuffer)
buffer += readbuffer
bytes_read = len(buffer)
# break out if EOF is reached.
if readbuffer == "":
bytes_read += last_read_len
# break out if EOF is reached, that is zero bytes read.
if last_read_len < 1:
break
return buffer

Expand Down
Loading
Loading