Skip to content
Permalink
Browse files

Improve doctest coverage (#1275)

* Improve util.proc doctest coverage

* Improve util.crc coverage

* Triggering doctests for getdents and buffer

* Processes behave dynamically
  • Loading branch information...
Arusekk committed Aug 27, 2019
1 parent c15afc5 commit 977f3f7314b998639ec52ca61ff2447538e4ad9d
Showing with 61 additions and 2 deletions.
  1. +9 −0 docs/source/tubes/buffer.rst
  2. +10 −0 docs/source/util/getdents.rst
  3. +5 −0 pwnlib/util/crc/__init__.py
  4. +1 −1 pwnlib/util/crc/known.py
  5. +36 −1 pwnlib/util/proc.py
@@ -0,0 +1,9 @@
.. testsetup:: *

from pwnlib.tubes.buffer import *

:mod:`pwnlib.tubes.buffer` --- buffer implementation for tubes
==============================================================

.. automodule:: pwnlib.tubes.buffer
:members:
@@ -0,0 +1,10 @@
.. testsetup:: *

from pwnlib.util.getdents import *


:mod:`pwnlib.util.getdents` --- Linux binary directory listing
==============================================================

.. automodule:: pwnlib.util.getdents
:members:
@@ -10,6 +10,11 @@
the crc32-sum of ``'A'*40000``.
An obvious optimization would be to actually generate some lookup-tables.
This doctest is to ensure that the known data are accurate:
>>> known = sys.modules['pwnlib.util.crc.known']
>>> known.all_crcs == known.generate()
True
"""
from __future__ import absolute_import
from __future__ import division
@@ -4,7 +4,7 @@
import re


def generate(self):
def generate():
"""Generates a dictionary of all the known CRC formats from:
http://reveng.sourceforge.net/crc-catalogue/all.htm
@@ -30,6 +30,12 @@ def pidof(target):
Returns:
A list of found PIDs.
Example:
>>> l = tubes.listen.listen()
>>> p = process(['curl', '-s', 'http://127.0.0.1:%d'%l.lport])
>>> pidof(p) == pidof(l) == pidof(('127.0.0.1', l.lport))
True
"""
if isinstance(target, tubes.ssh.ssh_channel):
return [target.pid]
@@ -88,7 +94,7 @@ def match(p):

processes = sorted(processes, key=lambda p: p.create_time())

return list(reversed([p.pid for p in processes]))
return reversed([p.pid for p in processes])

def name(pid):
"""name(pid) -> str
@@ -140,6 +146,10 @@ def ancestors(pid):
Returns:
List of PIDs of whose parent process is `pid` or an ancestor of `pid`.
Example:
>>> ancestors(os.getpid()) # doctest: +ELLIPSIS
[..., 1]
"""
pids = []
while pid != 0:
@@ -155,6 +165,11 @@ def descendants(pid):
Returns:
Dictionary mapping the PID of each child of `pid` to it's descendants.
Example:
>>> d = descendants(os.getppid())
>>> os.getpid() in d.keys()
True
"""
this_pid = pid
allpids = all_pids()
@@ -177,6 +192,10 @@ def exe(pid):
Returns:
The path of the binary of the process. I.e. what ``/proc/<pid>/exe`` points to.
Example:
>>> exe(os.getpid()) == os.path.realpath(sys.executable)
True
"""
return psutil.Process(pid).exe()

@@ -189,6 +208,10 @@ def cwd(pid):
Returns:
The path of the process's current working directory. I.e. what
``/proc/<pid>/cwd`` points to.
Example:
>>> cwd(os.getpid()) == os.getcwd()
True
"""
return psutil.Process(pid).cwd()

@@ -200,6 +223,10 @@ def cmdline(pid):
Returns:
A list of the fields in ``/proc/<pid>/cmdline``.
Example:
>>> 'py' in ''.join(cmdline(os.getpid()))
True
"""
return psutil.Process(pid).cmdline()

@@ -211,6 +238,10 @@ def stat(pid):
Returns:
A list of the values in ``/proc/<pid>/stat``, with the exception that ``(`` and ``)`` has been removed from around the process name.
Example:
>>> stat(os.getpid())[2]
'R'
"""
with open('/proc/%d/stat' % pid) as fd:
s = fd.read()
@@ -228,6 +259,10 @@ def starttime(pid):
Returns:
The time (in seconds) the process started after system boot
Example:
>>> starttime(os.getppid()) < starttime(os.getpid())
True
"""
return psutil.Process(pid).create_time() - psutil.boot_time()

0 comments on commit 977f3f7

Please sign in to comment.
You can’t perform that action at this time.