OpenSecurity/bin/test_vmmanager.pyw
author BarthaM@N3SIM1218.D03.arc.local
Thu, 02 Oct 2014 13:08:09 +0100
changeset 234 216da9017f8f
parent 221 853af9cfab6a
child 240 d7ef04254e9c
permissions -rw-r--r--
- changed opensecurity to always hold at least 2 SDVM sessions to be ready when needed
- added automatic wpad proxy detection
BarthaM@212
     1
#!/bin/env python
BarthaM@212
     2
# -*- coding: utf-8 -*-
BarthaM@212
     3
BarthaM@212
     4
# ------------------------------------------------------------
BarthaM@212
     5
# opensecurityd
BarthaM@212
     6
#   
BarthaM@212
     7
# the opensecurityd as RESTful server
BarthaM@212
     8
#
BarthaM@212
     9
# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
BarthaM@212
    10
#
BarthaM@212
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
BarthaM@212
    12
# AIT Austrian Institute of Technology GmbH
BarthaM@212
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
BarthaM@212
    14
# http://www.ait.ac.at
BarthaM@212
    15
#
BarthaM@212
    16
# This program is free software; you can redistribute it and/or
BarthaM@212
    17
# modify it under the terms of the GNU General Public License
BarthaM@212
    18
# as published by the Free Software Foundation version 2.
BarthaM@212
    19
# 
BarthaM@212
    20
# This program is distributed in the hope that it will be useful,
BarthaM@212
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
BarthaM@212
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
BarthaM@212
    23
# GNU General Public License for more details.
BarthaM@212
    24
# 
BarthaM@212
    25
# You should have received a copy of the GNU General Public License
BarthaM@212
    26
# along with this program; if not, write to the Free Software
BarthaM@212
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
BarthaM@212
    28
# Boston, MA  02110-1301, USA.
BarthaM@212
    29
# ------------------------------------------------------------
BarthaM@212
    30
BarthaM@212
    31
BarthaM@212
    32
# ------------------------------------------------------------
BarthaM@212
    33
# imports
BarthaM@212
    34
BarthaM@212
    35
import unittest
BarthaM@212
    36
BarthaM@212
    37
import os
BarthaM@213
    38
import re
BarthaM@212
    39
import os.path
BarthaM@212
    40
import sys
BarthaM@212
    41
import cygwin
BarthaM@212
    42
import vmmanager
BarthaM@213
    43
import _winreg
BarthaM@234
    44
import time
BarthaM@212
    45
gvm_mgr = None
BarthaM@212
    46
BarthaM@212
    47
class TestVMManager(unittest.TestCase):
BarthaM@212
    48
    
BarthaM@212
    49
    def setUp(self):
BarthaM@212
    50
        pass
BarthaM@212
    51
    
BarthaM@212
    52
    @classmethod
BarthaM@212
    53
    def setUpClass(self):
BarthaM@212
    54
        global gvm_mgr
BarthaM@212
    55
        gvm_mgr = vmmanager.VMManager.getInstance()
BarthaM@212
    56
        pass
BarthaM@212
    57
    
BarthaM@234
    58
    #@classmethod
BarthaM@234
    59
    #def tearOffClass(self):
BarthaM@234
    60
    #    gvm_mgr.stop()
BarthaM@234
    61
    #    gvm_mgr.cleanup()
BarthaM@234
    62
    
BarthaM@212
    63
    @unittest.skip("skipping")
BarthaM@212
    64
    def testGetTemplateUUID(self):
BarthaM@221
    65
        template = vmmanager.VMManager.getVDiskUUID(gvm_mgr.templateImage)
BarthaM@212
    66
        self.assertIsNotNone(template,  "returned no UUID for template")
BarthaM@212
    67
    
BarthaM@213
    68
    @unittest.skip("skipping")    
BarthaM@212
    69
    def testUpdateTemplate(self):
BarthaM@212
    70
        gvm_mgr.updateTemplate()
BarthaM@212
    71
        pass
BarthaM@212
    72
    
BarthaM@213
    73
    def setKey(self, key, name, value):
BarthaM@213
    74
        _, reg_type = _winreg.QueryValueEx(key, name)
BarthaM@213
    75
        _winreg.SetValueEx(key, name, 0, reg_type, value)
BarthaM@213
    76
    
BarthaM@213
    77
    @unittest.skip("skipping")    
BarthaM@213
    78
    def testGetProxySettings(self):
BarthaM@213
    79
        #sudo echo "http_proxy=http://80.122.169.38:8080/" >> /etc/environment
BarthaM@213
    80
        aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
BarthaM@213
    81
        aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
BarthaM@213
    82
        subCount, valueCount, lastModified = _winreg.QueryInfoKey(aKey)
BarthaM@213
    83
        proxy = dict()
BarthaM@213
    84
        for i in range(valueCount):                                           
BarthaM@213
    85
            try:
BarthaM@213
    86
                n,v,t = _winreg.EnumValue(aKey,i)
BarthaM@213
    87
                proxy[n] = v
BarthaM@213
    88
            except EnvironmentError:                                               
BarthaM@213
    89
                break
BarthaM@213
    90
        _winreg.CloseKey(aKey)
BarthaM@213
    91
        print proxy
BarthaM@213
    92
        if 'ProxyEnable' in proxy.keys() and proxy['ProxyEnable'] == 1:
BarthaM@213
    93
            print proxy['ProxyServer']
BarthaM@213
    94
            return proxy['ProxyServer']
BarthaM@213
    95
        else: 
BarthaM@213
    96
            return ""
BarthaM@217
    97
    
BarthaM@217
    98
    @unittest.skip("skipping")      
BarthaM@213
    99
    def testMatchProxy(self):
BarthaM@213
   100
        #http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080
BarthaM@213
   101
        #212.17.86.109:8080
BarthaM@213
   102
        text = 'http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080'
BarthaM@213
   103
        print re.search(r"(?<=http=)(?P<HttpProxy>.*?)(?=;)", text).groupdict()
BarthaM@213
   104
        print re.search(r"(?<=http=)(.*?)(?=;)", text)
BarthaM@213
   105
        
BarthaM@217
   106
    
BarthaM@217
   107
    
BarthaM@234
   108
    @unittest.skip("skipping")
BarthaM@221
   109
    def testImportTemplate(self):
BarthaM@221
   110
        gvm_mgr.cleanup()
BarthaM@221
   111
        if 'SecurityDVM' in gvm_mgr.listVMS():
BarthaM@221
   112
            gvm_mgr.removeVM('SecurityDVM')
BarthaM@221
   113
        
BarthaM@221
   114
        uuid = gvm_mgr.getVDiskUUID(gvm_mgr.templateImage)
BarthaM@221
   115
        if uuid:
BarthaM@221
   116
            gvm_mgr.removeImage(uuid)
BarthaM@221
   117
        
BarthaM@221
   118
        gvm_mgr.removeVMFolder('SecurityDVM')
BarthaM@221
   119
        gvm_mgr.importTemplate('C:\Windows\System32\config\systemprofile\VirtualBox VMs\OsecVM.ova')
BarthaM@221
   120
        gvm_mgr.updateTemplate()
BarthaM@234
   121
   
BarthaM@234
   122
    @unittest.skip("skipping")   
BarthaM@217
   123
    def testHostOnlyDHCP(self):
BarthaM@217
   124
        #list hostonlyifs
BarthaM@217
   125
        #Cygwin.vboxExecute("list hostonlyifs")
BarthaM@212
   126
        
BarthaM@217
   127
        hostonlyifs = gvm_mgr.getHostOnlyIFs()
BarthaM@217
   128
        print hostonlyifs
BarthaM@217
   129
        
BarthaM@217
   130
        dhcpservers = gvm_mgr.getDHCPServers()
BarthaM@217
   131
        print dhcpservers
BarthaM@217
   132
        
BarthaM@234
   133
    def testBrowsingRequest(self):
BarthaM@234
   134
        gvm_mgr.start()
BarthaM@234
   135
        gvm_mgr.handleBrowsingRequest()
BarthaM@234
   136
        time.sleep(3000)
BarthaM@234
   137
        pass
BarthaM@234
   138
        
BarthaM@212
   139
if __name__ == '__main__':
BarthaM@212
   140
    TestVMManager.setUpClass()
BarthaM@212
   141
    
BarthaM@212
   142
    suite = unittest.TestLoader().loadTestsFromTestCase(TestVMManager)
BarthaM@212
   143
    unittest.TextTestRunner().run(suite)
BarthaM@212
   144
    
BarthaM@234
   145
    #VBoxManage list hostonlyifs
BarthaM@234
   146
    #VBoxManage list dhcpservers
BarthaM@234
   147
    #VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter"
BarthaM@234
   148
    #VBoxManage dhcpserver add --ifname "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.254 --enable
BarthaM@234
   149
    #VBoxManage dhcpserver modify --ifname "VirtualBox Host-Only Ethernet Adapter" --enable
BarthaM@234
   150
    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --dhcp
BarthaM@234
   151
    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.1 --netmask 255.255.255.0    
BarthaM@212
   152
#    logger = setupLogger('Cygwin')
BarthaM@212
   153
#    c = Cygwin()
BarthaM@212
   154
#    unittest.main()
BarthaM@212
   155
    #man = VMManager.getInstance()
BarthaM@212
   156
    #man.listVM()
BarthaM@212
   157
    #print man.getConnectedRSDs()
BarthaM@212
   158
    #print man.getNetworkDrives()
BarthaM@212
   159
    #man.genNetworkDrive()
BarthaM@212
   160
    #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
BarthaM@212
   161
    #print list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
BarthaM@212
   162
    #print list(map(chr, range(68, 91))) 
BarthaM@212
   163
    #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
BarthaM@212
   164
    #devices = VMManager.getConnectedRSDS()
BarthaM@212
   165
    #print devices
BarthaM@212
   166
    
BarthaM@212
   167
    #drives = VMManager.getLogicalDrives()
BarthaM@212
   168
    #print drives
BarthaM@212
   169
    #print VMManager.getDriveType("E")
BarthaM@212
   170
    #print VMManager.getVolumeInfo("E")
BarthaM@212
   171
    #print VMManager.getNetworkPath("E")
BarthaM@212
   172
    
BarthaM@212
   173
    #vmm.backupFile()
BarthaM@212
   174
    #for device in devices.values():
BarthaM@212
   175
    #    #print device
BarthaM@212
   176
    #    if VMManager.isMassStorageDevice(device):
BarthaM@212
   177
    #        print device
BarthaM@212
   178
        
BarthaM@212
   179
    
BarthaM@212
   180
    
BarthaM@212
   181
    #time.sleep(-1)
BarthaM@212
   182
    #man.listVM()
BarthaM@212
   183
    #man.listVM()
BarthaM@212
   184
    #man.listVM()
BarthaM@212
   185
    #man.listVM()
BarthaM@212
   186
    #man.genCertificateISO('SecurityDVM0')
BarthaM@212
   187
    #man.guestExecute('SecurityDVM0', '/bin/ls -la')
BarthaM@212
   188
    #logger = setupLogger('VMManager')
BarthaM@212
   189
    #c = Cygwin()
BarthaM@212
   190
    
BarthaM@212
   191
    #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
BarthaM@212
   192
    #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
BarthaM@212
   193
    #man.removeVM('SecurityDVM0')
BarthaM@212
   194
    #man.netUse('192.168.56.134', 'USB\\')
BarthaM@212
   195
    #ip = '192.168.56.139'
BarthaM@212
   196
    
BarthaM@212
   197
    #man.cygwin_path = 'c:\\cygwin64\\bin\\'
BarthaM@212
   198
    #man.handleDeviceChange()
BarthaM@212
   199
    #print man.listSDVM()
BarthaM@212
   200
    #man.configureHostNetworking()
BarthaM@212
   201
    #new_vm = man.generateSDVMName()
BarthaM@212
   202
    #man.createVM(new_vm)
BarthaM@212
   203
    
BarthaM@212
   204
    #print Cygwin.cmd()
BarthaM@212
   205
    #man.isAvailable('c:')
BarthaM@212
   206
    #ip = man.getHostOnlyIP('SecurityDVM0')
BarthaM@212
   207
    #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
BarthaM@212
   208
    
BarthaM@212
   209
    #man.genCertificateISO(new_vm)
BarthaM@212
   210
    #man.attachCertificateISO(new_vm)
BarthaM@212
   211
    
BarthaM@212
   212
    #man.attachCertificateISO(vm_name)
BarthaM@212
   213
    #man.guestExecute(vm_name, "ls")
BarthaM@212
   214
    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
BarthaM@212
   215
    #time.sleep(60)
BarthaM@212
   216
    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
BarthaM@212
   217
    #man.genCertificateISO('SecurityDVM')
BarthaM@212
   218
    #man.attachCertificateISO('SecurityDVM')
BarthaM@212
   219
    #man.isStorageAttached('SecurityDVM')
BarthaM@212
   220
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
BarthaM@212
   221
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
BarthaM@212
   222
    
BarthaM@212
   223
    #man.stopVM('SecurityDVM')
BarthaM@219
   224
    #man.detachStorage('SecurityDVM')
BarthaM@212
   225
    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
BarthaM@212
   226
    #man.storageAttach('SecurityDVM')
BarthaM@212
   227
    
BarthaM@212
   228
    
BarthaM@212
   229
    #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
BarthaM@212
   230
    #man.execute(cmd)