Skip to content

Commit

Permalink
Add POSIX optionflag
Browse files Browse the repository at this point in the history
Run test on other UNIX systems too if they don't use Linux specifics.

Add a TODO optionflag too to mark platform restrictions that might be too strict and should be looked at.
  • Loading branch information
peace-maker committed Dec 22, 2024
1 parent d80b096 commit c92fe20
Show file tree
Hide file tree
Showing 29 changed files with 52 additions and 29 deletions.
3 changes: 2 additions & 1 deletion docs/source/asm.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import subprocess
from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.asm` --- Assembler functions
=========================================
Expand Down
6 changes: 6 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ class _DummyClass(object): pass
# they are skipped on other platforms
WINDOWS = doctest.register_optionflag('WINDOWS')
LINUX = doctest.register_optionflag('LINUX')
POSIX = doctest.register_optionflag('POSIX')

# doctest optionflag for tests that haven't been looked at yet
TODO = doctest.register_optionflag('TODO')

class Py2OutputChecker(_DummyClass, doctest.OutputChecker):
def check_output(self, want, got, optionflags):
Expand Down Expand Up @@ -448,6 +452,8 @@ def filter_platform(example):
return False
if (optionflags & LINUX) == LINUX and sys.platform != 'linux':
return False
if (optionflags & POSIX) == POSIX and os.name != 'posix':
return False
return True

test.examples[:] = [example for example in test.examples if filter_platform(example)]
Expand Down
2 changes: 1 addition & 1 deletion docs/source/elf/corefile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
os.environ.setdefault('SHELL', '/bin/sh')

import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']


:mod:`pwnlib.elf.corefile` --- Core Files
Expand Down
3 changes: 2 additions & 1 deletion docs/source/elf/elf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from pwnlib.elf.maps import CAT_PROC_MAPS_EXIT
import shutil

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.elf.elf` --- ELF Files
===========================================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/encoders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.encoders` --- Encoding Shellcode
===============================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/filesystem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from pwnlib.tubes.ssh import ssh
from pwnlib.filesystem import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.filesystem` --- Manipulating Files Locally and Over SSH
====================================================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/gdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
context.arch = 'amd64'
context.terminal = [os.path.join(os.path.dirname(pwnlib.__file__), 'gdb_faketerminal.py')]

# TODO: Test on cygwin too
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.gdb` --- Working with GDB
======================================
Expand Down
2 changes: 1 addition & 1 deletion docs/source/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pwn import *

import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

Getting Started
========================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/libcdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from pwn import *
from pwnlib.libcdb import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.libcdb` --- Libc Database
===========================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/qemu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']


:mod:`pwnlib.qemu` --- QEMU Utilities
Expand Down
1 change: 1 addition & 0 deletions docs/source/rop/rop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

context.clear()

# TODO: Remove global LINUX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']

Expand Down
3 changes: 2 additions & 1 deletion docs/source/runner.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from pwnlib.runner import *
from pwnlib.asm import asm

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.runner` --- Running Shellcode
===========================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/shellcraft.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwnlib import shellcraft

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.shellcraft` --- Shellcode generation
=================================================
Expand Down
1 change: 1 addition & 0 deletions docs/source/shellcraft/amd64.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pwn import *
context.clear(arch='amd64')

# TODO: POSIX/WINDOWS shellcode test
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']

Expand Down
2 changes: 1 addition & 1 deletion docs/source/shellcraft/i386.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
context.clear(arch='i386')

import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.shellcraft.i386` --- Shellcode for Intel 80386
===========================================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tubes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes` --- Talking to the World!
=============================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tubes/buffer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwnlib.tubes.buffer import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes.buffer` --- buffer implementation for tubes
==============================================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tubes/processes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes.process` --- Processes
===========================================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tubes/serial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes.serialtube` --- Serial Ports
===========================================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tubes/sockets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from pwn import *
from pwnlib.tubes.server import server

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes.sock` --- Sockets
===========================================================
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tubes/ssh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from pwn import *

# TODO: Remove global POSIX flag
import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.tubes.ssh` --- SSH
===========================================================
Expand Down
2 changes: 1 addition & 1 deletion docs/source/ui.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io

import doctest
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['LINUX']
doctest_additional_flags = doctest.OPTIONFLAGS_BY_NAME['POSIX']

:mod:`pwnlib.ui` --- Functions for user interaction
===================================================
Expand Down
6 changes: 3 additions & 3 deletions pwnlib/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class ContextType(object):
32
.. doctest::
:options: +LINUX
:options: +POSIX +TODO
>>> def nop():
... print(enhex(pwnlib.asm.asm('nop')))
Expand Down Expand Up @@ -875,7 +875,7 @@ def binary(self, binary):
Examples:
.. doctest::
:options: +LINUX
:options: +POSIX +TODO
>>> context.clear()
>>> context.arch, context.bits
Expand Down Expand Up @@ -1412,7 +1412,7 @@ def cache_dir(self):
True
>>> os.chmod(cache_dir, 0o000)
>>> context.cache_dir = True
>>> context.cache_dir is None # doctest: +LINUX
>>> context.cache_dir is None # doctest: +POSIX +TODO
True
>>> os.chmod(cache_dir, 0o755)
>>> cache_dir == context.cache_dir
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/fmtstr.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
We can automate the exploitation of the process like so:
.. doctest::
:options: +LINUX
:options: +POSIX +TODO
>>> program = pwnlib.data.elf.fmtstr.get('i386')
>>> def exec_fmt(payload):
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def some_leaker(addr):
Example:
.. doctest::
:options: +LINUX
:options: +POSIX +TODO
>>> import pwnlib
>>> binsh = pwnlib.util.misc.read('/bin/sh')
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/util/iters.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ def mbruteforce(func, alphabet, length, method = 'upto', start = None, threads =
Example:
.. doctest::
:options: +LINUX
:options: +POSIX +TODO
>>> mbruteforce(lambda x: x == 'hello', string.ascii_lowercase, length = 10)
'hello'
Expand Down
4 changes: 2 additions & 2 deletions pwnlib/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def read(path, count=-1, skip=0):
Examples:
>>> read('/proc/self/exe')[:4] # doctest: +LINUX
>>> read('/proc/self/exe')[:4] # doctest: +LINUX +TODO
b'\x7fELF'
"""
path = os.path.expanduser(os.path.expandvars(path))
Expand Down Expand Up @@ -163,7 +163,7 @@ def which(name, all = False, path=None):
Example:
>>> which('sh') # doctest: +ELLIPSIS +LINUX
>>> which('sh') # doctest: +ELLIPSIS +POSIX +TODO
'.../bin/sh'
"""
# If name is a path, do not attempt to resolve it.
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/util/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ def dd(dst, src, count = 0, skip = 0, seek = 0, truncate = False):
['H', 'e', 'l', 'l', 'o', b'?']
.. doctest::
:options: +LINUX
:options: +POSIX +TODO
>>> _ = open('/tmp/foo', 'w').write('A' * 10)
>>> dd(open('/tmp/foo'), open('/dev/zero'), skip = 3, count = 4).read()
Expand Down
2 changes: 1 addition & 1 deletion pwnlib/util/sh_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def test(original):
r"""Tests the output provided by a shell interpreting a string
.. doctest::
:options: +LINUX
:options: +POSIX
>>> test(b'foobar')
>>> test(b'foo bar')
Expand Down

0 comments on commit c92fe20

Please sign in to comment.