commit 5333b1b650667409a7dc52cb646b2a822e3a2c80 Author: George Kadianakis desnacked@riseup.net Date: Mon Apr 14 15:01:01 2014 +0300
Add scramblesuit unittests for the state module. --- obfsproxy/test/transports/test_scramblesuit.py | 47 ++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
diff --git a/obfsproxy/test/transports/test_scramblesuit.py b/obfsproxy/test/transports/test_scramblesuit.py index 541d92e..df7f69b 100644 --- a/obfsproxy/test/transports/test_scramblesuit.py +++ b/obfsproxy/test/transports/test_scramblesuit.py @@ -19,6 +19,8 @@ import obfsproxy.transports.scramblesuit.mycrypto as mycrypto import obfsproxy.transports.scramblesuit.uniformdh as uniformdh import obfsproxy.transports.scramblesuit.scramblesuit as scramblesuit import obfsproxy.transports.scramblesuit.message as message +import obfsproxy.transports.scramblesuit.state as state +
# Disable all logging as it would yield plenty of warning and error # messages. @@ -226,6 +228,51 @@ class UtilTest( unittest.TestCase ): # Read file where we (hopefully) don't have permissions. self.failUnless(util.readFromFile("/etc/shadow") == None)
+class StateTest( unittest.TestCase ): + + def setUp( self ): + const.STATE_LOCATION = "/tmp/" + self.stateFile = const.STATE_LOCATION + const.SERVER_STATE_FILE + self.state = state.State() + + def tearDown( self ): + try: + os.unlink(self.stateFile) + except OSError: + pass + + def test1_genState( self ): + self.state.genState() + self.failUnless(os.path.exists(self.stateFile)) + + def test2_loadState( self ): + # load() should create the state file if it doesn't exist yet. + self.failIf(os.path.exists(self.stateFile)) + self.failUnless(isinstance(state.load(), state.State)) + self.failUnless(os.path.exists(self.stateFile)) + + def test3_replay( self ): + key = "A" * const.HMAC_SHA256_128_LENGTH + self.state.genState() + self.state.registerKey(key) + self.failUnless(self.state.isReplayed(key)) + self.failIf(self.state.isReplayed("B" * const.HMAC_SHA256_128_LENGTH)) + + def test4_ioerrorFail( self ): + def fake_open(name, mode): + raise IOError() + self.state.genState() + + import __builtin__ + real_open = __builtin__.open + __builtin__.open = fake_open + + # Make state.load() fail + self.assertRaises(SystemExit, state.load) + # Make State.writeState() fail. + self.assertRaises(SystemExit, self.state.genState) + + __builtin__.open = real_open
class MockArgs( object ): uniformDHSecret = sharedSecret = ext_cookie_file = dest = None