2006-11-30 14:50:28 +00:00
|
|
|
#
|
|
|
|
# Copyright 2006 sense.lab e.V.
|
|
|
|
#
|
|
|
|
# This file is part of the CryptoBox.
|
|
|
|
#
|
|
|
|
# The CryptoBox is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# The CryptoBox is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with the CryptoBox; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
2006-11-06 16:05:00 +00:00
|
|
|
"""
|
|
|
|
super class of all web interface unittests for the cryptobox
|
|
|
|
|
|
|
|
just inherit this class and add some test functions
|
|
|
|
"""
|
|
|
|
|
2006-12-05 12:24:47 +00:00
|
|
|
__revision__ = "$Id"
|
|
|
|
|
2006-11-06 16:05:00 +00:00
|
|
|
import unittest
|
|
|
|
import twill
|
|
|
|
import cherrypy
|
2006-11-27 19:25:26 +00:00
|
|
|
import cryptobox.web.sites
|
2006-11-30 14:50:28 +00:00
|
|
|
import os
|
|
|
|
|
2006-11-06 16:05:00 +00:00
|
|
|
|
|
|
|
## commands api: http://twill.idyll.org/commands.html
|
2006-12-05 12:24:47 +00:00
|
|
|
CBXHOST = "localhost"
|
|
|
|
CBXPORT = 8081
|
|
|
|
CBX_URL = "http://%s:%d/" % (CBXHOST, CBXPORT)
|
|
|
|
LOG_FILE = "/tmp/cryptobox-twill.log"
|
|
|
|
WEBLOG_FILE = "/tmp/cryptobox-cherrypy.log"
|
|
|
|
|
2006-11-06 16:05:00 +00:00
|
|
|
|
|
|
|
class WebInterfaceTestClass(unittest.TestCase):
|
|
|
|
'''this class checks the webserver, using "twill"
|
|
|
|
|
|
|
|
the tests in this class are from the browsers point of view, so not
|
|
|
|
really unittests.
|
|
|
|
fetch twill from: http://twill.idyll.org
|
|
|
|
one way to manually run twill code is through the python
|
|
|
|
interpreter commandline e.g.:
|
|
|
|
|
|
|
|
import twill
|
|
|
|
twill.shell.main()
|
|
|
|
go http://localhost:8080
|
|
|
|
find "my very special html content"
|
|
|
|
help
|
|
|
|
'''
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
'''configures the cherrypy server that it works nice with twill
|
|
|
|
'''
|
|
|
|
cherrypy.config.update({
|
|
|
|
'server.logToScreen' : False,
|
|
|
|
'autoreload.on': False,
|
|
|
|
'server.threadPool': 1,
|
2006-12-05 12:24:47 +00:00
|
|
|
'server.environment': 'development',
|
|
|
|
'server.log_tracebacks': True,
|
|
|
|
'server.log_file': WEBLOG_FILE,
|
2006-11-06 16:05:00 +00:00
|
|
|
})
|
2006-12-05 12:24:47 +00:00
|
|
|
cherrypy.root = cryptobox.web.sites.WebInterfaceSites(
|
|
|
|
"cryptobox-unittests.conf")
|
2006-11-06 16:05:00 +00:00
|
|
|
cherrypy.server.start(initOnly=True, serverClass=None)
|
|
|
|
|
|
|
|
from cherrypy._cpwsgi import wsgiApp
|
|
|
|
twill.add_wsgi_intercept(CBXHOST, CBXPORT, lambda: wsgiApp)
|
|
|
|
|
|
|
|
# grab the output of twill commands
|
|
|
|
self.output = open(LOG_FILE,"a")
|
|
|
|
twill.set_output(self.output)
|
|
|
|
self.cmd = twill.commands
|
2006-12-05 12:24:47 +00:00
|
|
|
self.url = CBX_URL
|
2006-11-06 16:05:00 +00:00
|
|
|
self.cbox = cherrypy.root.cbox
|
|
|
|
self.globals, self.locals = twill.namespaces.get_twill_glocals()
|
2006-11-30 14:50:28 +00:00
|
|
|
## search for a usable block device
|
|
|
|
## use /dev/ubd? if possible - otherwise /dev/hd?
|
|
|
|
## so it will be possible to use these tests inside of an uml
|
2006-12-05 12:24:47 +00:00
|
|
|
self.blockdevice, self.device = self.__find_test_device()
|
2006-12-12 13:34:05 +00:00
|
|
|
## umount the device (just to be sure)
|
|
|
|
url = self.url + "volume_mount?weblang=en&device=%2Fdev%2F" + self.device
|
|
|
|
self.register_auth(url)
|
|
|
|
self.cmd.go(url + "&action=umount")
|
2006-11-30 14:50:28 +00:00
|
|
|
|
2006-11-06 16:05:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
'''clean up the room when leaving'''
|
2006-12-12 13:34:05 +00:00
|
|
|
## remove intercept.
|
2006-11-06 16:05:00 +00:00
|
|
|
twill.remove_wsgi_intercept(CBXHOST, CBXPORT)
|
2006-12-12 13:34:05 +00:00
|
|
|
## stop the cryptobox
|
|
|
|
cherrypy.root.cleanup()
|
|
|
|
## shut down the cherrypy server.
|
2006-11-06 16:05:00 +00:00
|
|
|
cherrypy.server.stop()
|
|
|
|
self.output.close()
|
|
|
|
|
|
|
|
|
|
|
|
def __get_soup():
|
|
|
|
browser = twill.commands.get_browser()
|
|
|
|
soup = BeautifulSoup(browser.get_html())
|
|
|
|
return soup
|
2006-12-05 12:24:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __find_test_device(self):
|
|
|
|
"""Search for a valid test device - the data will get lost ...
|
|
|
|
"""
|
|
|
|
for dev in ["ubdb", "loop", "ubda", "udbc", "ubdd"]:
|
|
|
|
if os.path.exists("/dev/%s1" % dev) \
|
|
|
|
and not self.__is_config_partition("/dev/%s1" % dev):
|
|
|
|
return (dev, dev + "1")
|
|
|
|
if os.path.exists("/dev/%s2" % dev) \
|
|
|
|
and not self.__is_config_partition("/dev/%s2" % dev):
|
|
|
|
return (dev, dev + "2")
|
|
|
|
else:
|
|
|
|
raise Exception, "no valid device for testing found"
|
|
|
|
|
|
|
|
|
|
|
|
def __is_config_partition(self, device):
|
|
|
|
"""Check if the device is a configuration partition.
|
|
|
|
"""
|
|
|
|
import subprocess
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
shell = False,
|
|
|
|
stdout = subprocess.PIPE,
|
|
|
|
stderr = subprocess.PIPE,
|
|
|
|
args = [ '/sbin/e2label',
|
|
|
|
device ])
|
|
|
|
(stdout, stderr) = proc.communicate()
|
|
|
|
return stdout.strip() == "cbox_config"
|
2006-11-06 16:05:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register_auth(self, url, user="admin", password="admin"):
|
|
|
|
self.cmd.add_auth("CryptoBox", url, user, password)
|
|
|
|
|