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