tools/test-harness/jssh-driver/jssh_driver.py

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rwxr-xr-x

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 #!/usr/bin/env python
michael@0 2 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 5 import re
michael@0 6 import sys
michael@0 7 import telnetlib
michael@0 8
michael@0 9 class JsshDriver:
michael@0 10
michael@0 11 COMMAND_PROMPT = "\n> "
michael@0 12
michael@0 13 def __init__(self, host=None, port=9997, telnetklass=telnetlib.Telnet):
michael@0 14 """Ctor
michael@0 15 Ctor
michael@0 16 """
michael@0 17 self.host = host
michael@0 18 self.port = port
michael@0 19 self.tn = telnetklass(host,port)
michael@0 20 self.init()
michael@0 21
michael@0 22 def open(self,host,port=9997):
michael@0 23 self.tn.close()
michael@0 24 self.host = host
michael@0 25 self.port = port
michael@0 26 self.tn.open(host,port)
michael@0 27 self.init()
michael@0 28
michael@0 29 def init(self):
michael@0 30 if not self.tn.get_socket():
michael@0 31 return
michael@0 32
michael@0 33 self.tn.read_until(JsshDriver.COMMAND_PROMPT)
michael@0 34 self.send_command("setProtocol('synchronous')")
michael@0 35
michael@0 36 def send_command(self,command):
michael@0 37 self.tn.write(command + "\n")
michael@0 38 return self.tn.read_until(JsshDriver.COMMAND_PROMPT)
michael@0 39
michael@0 40 def send_quit(self):
michael@0 41 self.tn.write("quit()\n")
michael@0 42 return self.tn.read_all()
michael@0 43
michael@0 44 class JsshTester:
michael@0 45
michael@0 46 def __init__(self, host, port=9997, telnetklass=telnetlib.Telnet):
michael@0 47 self.browser = JsshDriver(host,port,telnetklass)
michael@0 48
michael@0 49 def __del__(self):
michael@0 50 self.browser.send_quit()
michael@0 51
michael@0 52 def get_innerHTML_from_URL(self,url):
michael@0 53 self.browser.send_command ('var browser = getWindows()[0].getBrowser()')
michael@0 54
michael@0 55 if url:
michael@0 56 self.browser.send_command ('browser.loadURI("' + url + '")')
michael@0 57
michael@0 58 self.browser.send_command ('var document = browser.contentDocument')
michael@0 59 self.browser.send_command ('var window = browser.contentWindow')
michael@0 60 jssh_response = self.browser.send_command ('print(document.documentElement.innerHTML)')
michael@0 61
michael@0 62 m = re.compile(r"\[(?P<len>\d+)](?P<rest>.*)", re.DOTALL).search(jssh_response)
michael@0 63
michael@0 64 return m.group('rest')[0:int(m.group('len'))]

mercurial