Commit Graph

4000 Commits

Author SHA1 Message Date
Damian Johnson
efe28987ff Expanding contorller exception types
Exception types pretty much mirror what TorCtl has (protocol error, socket
error, and controller closed) with a base type users can catch instead. One
difference though is that if stem functions raise a socket.error (without
documenting that they do) then that's a bug - those errors should cuase a
stem.types.SocketError instead.
2011-11-12 15:55:09 -08:00
Damian Johnson
d1c3c8db72 Standardizing on suppress_exc args for sys util
The system functions are mostly best-effort, platform specific attempts to
retrieve data and usually don't have meaningful information to provide back
to the caller via exceptions. I'm defaulting to have them return None, with
an optional arg for having them raise IOError exceptions on failure instead.

I've gone back and forth on the fencepost here a few times, and I'm still not
positive if this is the right choice. Might change this later if it doesn't
work out.
2011-11-12 15:46:13 -08:00
Damian Johnson
fab872725d Function for querying a process' pwd
Implementing and testing a stem.util.system.get_pwd(pid) function. This will be
needed for correcting the relative cookie paths mentioned in...
https://trac.torproject.org/projects/tor/ticket/1101
2011-11-10 09:30:06 -08:00
Damian Johnson
7c7bd6adec Fully qualifying util paths
Changing the conventional usage of util imports from "from stem.util import X"
to simple imports (not pulling them into our namespace). There's some
exceptions to fully qualified util usage where it hurts readability (often the
case with stem.util.term), but explite paths will be the more common case.
2011-11-09 10:23:07 -08:00
Damian Johnson
713eec7b70 Replacing custom logging util with python builtin
Finally abandoning my custom logging implementation for the python builtin
logging module. It's far more customizable, standardized, and what users would
expect from a library like this. The only disadvantage is that it doesn't
buffer past events so we lose anything prior to adding a handler.
2011-11-09 10:13:55 -08:00
Damian Johnson
78de4e4cf1 Util for reading proc contents
Rewrite of arm's utility for parsing proc contents, changes including better
error handling and refacotring for stem's coding conventions and commenting.

While writing the PROTOCOLINFO response handler I needed a system util for
querying tor's pwd, which works best with proc information, so pulling it all
in. For now most of this isn't being exercised, but will be later.
2011-11-08 09:04:16 -08:00
Damian Johnson
eb5b522430 Adding optional key arg to is_next_mapping checks
Option to simplify testing for the next key being parsed.
2011-11-06 11:24:21 -08:00
Damian Johnson
40b7ab1b36 Unit testing for stem.types.ControlLine
General unit tests to exercise the ControlLine class with PROTOCOLINFO output.
This also has a minor fix so we throw an IndexError rather than ValueError when
pop_mapping() is called while empty.
2011-11-05 17:18:10 -07:00
Damian Johnson
c0525d58a7 Unit testing stem.types.ControlLine.pop() examples
Adding unit tests and correcting the pydocs for the pop method examples.
2011-11-05 15:49:48 -07:00
Damian Johnson
78d44b0995 Moving testrc.sample into testing module
Tired of having 'test' autocompletion trip up on the sample rc file, so moving
it into the test directory. It's probably not of much interest to general
library users anyway.
2011-11-05 14:15:33 -07:00
Damian Johnson
c4c998f620 Better organizing unit tests for stem.types 2011-11-05 14:14:50 -07:00
Damian Johnson
aba675c3f5 Header documentation for ControlLine class 2011-11-05 13:56:13 -07:00
Damian Johnson
12064b7b9f Replacing get_entry with ControlLine class
Making a string subclass to help with the parsing of controller output. Most
entries are space separated lists of elements, which this class has functions
to easily parse. For controller messages that don't follow this pattern we can
still treat it as a normal string.

Next is to add header documentation and tests.
2011-11-04 10:16:28 -07:00
Damian Johnson
45d3c819b2 Unit tests for types.get_entry examples
Unit tests for the examples in the pydocs of the function and fixes for the
issues they uncovered.
2011-11-03 07:07:11 -07:00
Damian Johnson
a090373e53 Utility function for parsing control messages
Trying out a utility function to pop the first item off a control response,
with the caller providing attributes it should have.
2011-11-01 18:49:55 -07:00
Damian Johnson
263dfd53ce Config argument for the test runner
Integration tests can have custom behavior via a testrc, which is simple for
now, and will grow as tests become more complex. Previously I was loading a
static settings.cfg but that was stupid. The user's config file should neither
be hardcoded nor under version control. This change also includes the config
loading in the status output to stdout.
2011-10-30 00:51:47 -07:00
Damian Johnson
63a9a5fa52 Merging version constructor and parser
The version constructor wasn't really useful so merging it with the get_version
function to make its usage more intuitive.
2011-10-27 20:31:43 -07:00
Damian Johnson
52be689448 Putting stem under the LGPL v3 2011-10-27 19:43:45 -07:00
Damian Johnson
f07c6cc77f Expanding header documentation
Adding a summary of a files functions/classes/methods at the top of the file.
This makes using a library much nicer.
2011-10-27 09:53:44 -07:00
Damian Johnson
11b2a71d3a Caching for stem.process.get_version()
Caching results of the stem.process.get_version() function to avoid unnecessary
lookups.
2011-10-27 09:17:26 -07:00
Damian Johnson
7766cb79ff Requirement check for the config-text test
An integration test requires 'GETINFO config-text' requires tor version 0.2.2.7
so adding a check for it.
2011-10-27 07:16:06 -07:00
Damian Johnson
fbfa73a099 Rewrite of integration test runner
Several imporvements for the integration tests, most notably including...
- Test configurability via a 'test/settings.cfg' file

- Thread safety for runner usage

- Vastly better startup time for how integration tests run by default...
  - Reusing data directory so we don't need to request as much from authorities
    when starting (faster startup and less burden on them). Users can opt for a
    fresh temporary directory instead by setting 'test.integ.test_directory' to
    a blank value.

  - Starting tests when bootstraping reaches 5%. This is enough for tests that
    don't require network activity to run, and we can explicitly run those
    tests by setting the 'test.integ.run.online' option. This change also means
    that we can now run integration tests while offline.
2011-10-26 20:13:34 -07:00
Damian Johnson
51d2ded44f Adding function to query tor's version
Command to issue a 'tor --version' query with a stem.types.Version response.
2011-10-21 12:56:28 -07:00
Damian Johnson
52768d36f8 Quietly stopping integ launch on ctrl+c
Keyboard interrupts while launching tor would result in a stacktrace. Quietly
exiting instead since this isn't really an issue.
2011-10-21 07:33:40 -07:00
Damian Johnson
8293c518d0 Making the tor launching timeout an arg
Moving the timeout for launching a tor process from being a constant to being
an argument of the function.
2011-10-21 07:28:27 -07:00
Damian Johnson
0a083c8b73 Accidently suppressing integ asserts
A previous commit to suppress errors from the close() method in python 2.7 also
suppressed the following asserts. These asserts still work with 2.7 so removing
them from the try block.
2011-10-20 10:09:36 -07:00
Damian Johnson
3a913b6b92 Moving tor launcher into stem function
In writing the stem integ tests I needed a function for starting tor then
blocking for its bootstraping to complete. Yesterday Jake mentioned that he
could use a launch_tor() function in TorCtl so I'm generalizing this code and
moving it into the stem lib.
2011-10-20 09:58:37 -07:00
Damian Johnson
26374bafd5 Message integ tests compatability issues
The message integration tests had compatability issues with...
- Newer python versions (2.6 -> 2.7) due to an unexpected failure when calling
  close() on the socket's file object. We're operating with a closed socket at
  that time so all bets are off about if this should/shouldn't raise so the
  difference in behavior just warranted a testing workaround.

- Older tor versions don't support 'GETINFO config-text'. I've left this as a
  TODO note for now and will add a version check for that test later.
2011-10-20 09:49:45 -07:00
Damian Johnson
1b44b967e7 Integration tests / fixes for types.ControlMessage
Adding integration tests for basic control port communication, exercising...
- connection failure
- bad commands
- bad getinfo queries
- general getinfo queries
- setevent/basic event parsing

This also includes fixes for a variety of issues found while testing.
2011-10-17 09:45:43 -07:00
Damian Johnson
24d4881025 Moving integ runner into testing base
Moving the integ runner module from 'test/integ/runner.py' to 'test/runner.py'.
The 'test/unit/*' and 'test/integ/*' are for test cases, and will later be
kinda crowded. The runner is special since it doesn't test, but rather provides
the runtime context for the integ tests so probably best to keep this separate
from the rest.
2011-10-16 19:30:57 -07:00
Damian Johnson
aa01c07989 Adding get_pid and get_bsd_jail_id util functoins
Stealing arm's getTorPid and getBsdJailId from the torTools util, generalizing
both functions to be for arbitrary processes rather than just tor. This also
adds unit tests for get_pid and a simple exercise of get_bsd_jail_id (I can't
really test the later since I'm not on BSD).
2011-10-16 19:28:13 -07:00
Damian Johnson
3731bba464 Adding BSD compatability to system.is_running
Copying over an arm fix I made earlier this week from
util.torTools.isTorRunning so that it would work on OSX/FreeBSD/OpenBSD.
2011-10-16 17:27:57 -07:00
Damian Johnson
1a30fbd652 Integration tests for stem.util.system functions
Very, very basic tests that the tor process is running and available.
2011-10-16 17:19:01 -07:00
Damian Johnson
0c03d4eb2a Moving integration test init/shutdown into class
Making a Runner class which will be available to integration tests. This both
better modularizes the code and will allow for us to give runtime context to
the tests.
2011-10-16 15:19:03 -07:00
Damian Johnson
bdbf352ad7 Running tor instances for integ tests
First draft for the startup/shutdown of integration tests. This...
- makes a test directory
- generates a torrc
- starts a tor instance, waiting until its bootstrap completes (timing out if
  it gets stuck)
- runs tests (not done yet - those are next)
- shuts down the tor instance

This'll certainly go through some refactoring to better modularize, but it's
functional as-is.
2011-10-16 01:17:38 -07:00
Damian Johnson
a2fde9374e Adding util for handling configuration files
Integration tests and other things will be stored via configuration files
(hardcoding data in source is a horrible thing and you have no excuse unless
you're writing in LISP). This is being stolen from the arm codebase, with the
code refactored for this project's conventions, some fixes to better generalize
the util, and vastly improved documentation.

Tom has been using the config util a bit for his torperf rewrite and having
difficulties so hopefully this will make its usage easier.
2011-10-14 20:33:12 -07:00
Damian Johnson
c74ac0f7f0 Tesing and fix for disconnected socket reads
Adding a unit test and fix for when the input file is derived from a socket
that's never been connected. I'm suspicious that this won't catch disconnects
that accur a little while into the socket's use, but I'll need to implement
integration tests for that. Guess that's next...
2011-10-12 09:37:49 -07:00
Damian Johnson
05b106bb57 Protocol error tests and fix
Expanding the ControlMessage unit tests to cover causes of protocol errors, and
fixing an issue that this revealed where it was possable for a malformed line
prefix to go undetected (if, say, a dropped character caused a valid line
divider to fall into that place).
2011-10-12 09:23:39 -07:00
Damian Johnson
5b1a7e8f2d Basic unit tests for ControlMessage
Just some sanity checks that ControlMessage parses common GETINFO responses.
I'll expand on these including some error cases next.
2011-10-10 21:24:58 -07:00
Damian Johnson
92f78d4594 Moving ControlMessage and parsing into types
The ControlMessage is a standalone class so moving it into types. This is both
appropriate for the types module and will make testing easier.
2011-10-10 19:54:30 -07:00
Damian Johnson
5d7688e723 First draft controller connection
This is a functional rewrite of the sendAndRecv TorCtl functionality. It
follows a similar pattern, having a couple threads to continually pull the
socket and provide event notifications. This still needs testing, better
exception handling, and some more thought about the ControlMessage api.
2011-10-10 10:05:14 -07:00
Damian Johnson
1aff6b8752 General system utilities
Adding some simple utility functions that I'll need later. These are rewrites
of their arm counterparts, which I wrote before discovering the subprocess
module.
2011-10-09 03:24:44 -07:00
Damian Johnson
00739ee4eb Adding logging utility
Borrowing the logger from arm, with some refactoring to remove unneeded
functions and make it conform with this project's coding conventions.
2011-10-08 23:24:37 -07:00
Damian Johnson
88921da126 Adding gitignore for pyc and vim swap files 2011-10-08 16:15:48 -07:00
Damian Johnson
3dd9ea0d92 Running tests based on input arguments
Making the test runner accept arguments for the type of tests to be ran. The
integration tests especially will take a while when they're implemented so
letting the user specify the use cases for those.

This included copying and refactoring some basic utilities from arm for
enumerations and terminal text attributes.
2011-10-08 16:04:08 -07:00
Damian Johnson
39e10eed90 Using Pascal Case styling (unscores) for variables
According to PEP8 [1] both functions and variable names should use the
underscore naming convention (ie, 'my_var') rather than camel case ('myVar').
This is a little weird for me and python standard libraries use both, but I see
a readability advantage to this for functions and my previous approach of using
camel case just for variables is kinda weird. Hence switching to the 'right'
convention while the codebase is still tiny.

As an added bonus this conforms with torctl...

[1] http://www.python.org/dev/peps/pep-0008/
2011-10-08 14:03:08 -07:00
Damian Johnson
d38b883080 Subdirectories for integ and unit tests
Unit and integration tests will be separate subdirectories of test.
2011-10-08 13:10:38 -07:00
Damian Johnson
e1c31c060c Removing checked in binaries
Forgot to establish a gitignore before the prior commit. An extra copy of
run_tests.py also snuck in (haven't a clue how).
2011-10-06 09:57:24 -07:00
Damian Johnson
69083da95c Class and function for Tor Versions
Starting with a simple (but non-trivial) class that will be needed for handling
PROTOCOLINFO responses. This is partly to establish conventions for
documentation and unit tests.
2011-10-06 09:52:04 -07:00
Damian Johnson
211c52e6f2 Initial commit
Simply filling in an empty file to initialize a root for the repo.
2011-10-06 09:33:34 -07:00