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
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@212
    44
gvm_mgr = None
BarthaM@212
    45
BarthaM@212
    46
class TestVMManager(unittest.TestCase):
BarthaM@212
    47
    
BarthaM@212
    48
    def setUp(self):
BarthaM@212
    49
        pass
BarthaM@212
    50
    
BarthaM@212
    51
    @classmethod
BarthaM@212
    52
    def setUpClass(self):
BarthaM@212
    53
        global gvm_mgr
BarthaM@212
    54
        gvm_mgr = vmmanager.VMManager.getInstance()
BarthaM@212
    55
        pass
BarthaM@212
    56
    
BarthaM@212
    57
    @unittest.skip("skipping")
BarthaM@212
    58
    def testGetTemplateUUID(self):
BarthaM@221
    59
        template = vmmanager.VMManager.getVDiskUUID(gvm_mgr.templateImage)
BarthaM@212
    60
        self.assertIsNotNone(template,  "returned no UUID for template")
BarthaM@212
    61
    
BarthaM@213
    62
    @unittest.skip("skipping")    
BarthaM@212
    63
    def testUpdateTemplate(self):
BarthaM@212
    64
        gvm_mgr.updateTemplate()
BarthaM@212
    65
        pass
BarthaM@212
    66
    
BarthaM@213
    67
    def setKey(self, key, name, value):
BarthaM@213
    68
        _, reg_type = _winreg.QueryValueEx(key, name)
BarthaM@213
    69
        _winreg.SetValueEx(key, name, 0, reg_type, value)
BarthaM@213
    70
    
BarthaM@213
    71
    @unittest.skip("skipping")    
BarthaM@213
    72
    def testGetProxySettings(self):
BarthaM@213
    73
        #sudo echo "http_proxy=http://80.122.169.38:8080/" >> /etc/environment
BarthaM@213
    74
        aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
BarthaM@213
    75
        aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
BarthaM@213
    76
        subCount, valueCount, lastModified = _winreg.QueryInfoKey(aKey)
BarthaM@213
    77
        proxy = dict()
BarthaM@213
    78
        for i in range(valueCount):                                           
BarthaM@213
    79
            try:
BarthaM@213
    80
                n,v,t = _winreg.EnumValue(aKey,i)
BarthaM@213
    81
                proxy[n] = v
BarthaM@213
    82
            except EnvironmentError:                                               
BarthaM@213
    83
                break
BarthaM@213
    84
        _winreg.CloseKey(aKey)
BarthaM@213
    85
        print proxy
BarthaM@213
    86
        if 'ProxyEnable' in proxy.keys() and proxy['ProxyEnable'] == 1:
BarthaM@213
    87
            print proxy['ProxyServer']
BarthaM@213
    88
            return proxy['ProxyServer']
BarthaM@213
    89
        else: 
BarthaM@213
    90
            return ""
BarthaM@217
    91
    
BarthaM@217
    92
    @unittest.skip("skipping")      
BarthaM@213
    93
    def testMatchProxy(self):
BarthaM@213
    94
        #http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080
BarthaM@213
    95
        #212.17.86.109:8080
BarthaM@213
    96
        text = 'http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080'
BarthaM@213
    97
        print re.search(r"(?<=http=)(?P<HttpProxy>.*?)(?=;)", text).groupdict()
BarthaM@213
    98
        print re.search(r"(?<=http=)(.*?)(?=;)", text)
BarthaM@213
    99
        
BarthaM@212
   100
    #@classmethod
BarthaM@212
   101
    #def tearOffClass(self):
BarthaM@212
   102
    #    gvm_mgr.stop()
BarthaM@212
   103
    #    gvm_mgr.cleanup()
BarthaM@217
   104
    
BarthaM@217
   105
    
BarthaM@221
   106
    def testImportTemplate(self):
BarthaM@221
   107
        gvm_mgr.cleanup()
BarthaM@221
   108
        if 'SecurityDVM' in gvm_mgr.listVMS():
BarthaM@221
   109
            gvm_mgr.removeVM('SecurityDVM')
BarthaM@221
   110
        
BarthaM@221
   111
        uuid = gvm_mgr.getVDiskUUID(gvm_mgr.templateImage)
BarthaM@221
   112
        if uuid:
BarthaM@221
   113
            gvm_mgr.removeImage(uuid)
BarthaM@221
   114
        
BarthaM@221
   115
        gvm_mgr.removeVMFolder('SecurityDVM')
BarthaM@221
   116
        gvm_mgr.importTemplate('C:\Windows\System32\config\systemprofile\VirtualBox VMs\OsecVM.ova')
BarthaM@221
   117
        gvm_mgr.updateTemplate()
BarthaM@217
   118
    #VBoxManage list hostonlyifs
BarthaM@217
   119
    #VBoxManage list dhcpservers
BarthaM@217
   120
    #VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter"
BarthaM@217
   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
BarthaM@217
   122
    #VBoxManage dhcpserver modify --ifname "VirtualBox Host-Only Ethernet Adapter" --enable
BarthaM@217
   123
    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --dhcp
BarthaM@217
   124
    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.1 --netmask 255.255.255.0
BarthaM@217
   125
BarthaM@217
   126
    
BarthaM@217
   127
    
BarthaM@217
   128
    def testHostOnlyDHCP(self):
BarthaM@217
   129
        #list hostonlyifs
BarthaM@217
   130
        #Cygwin.vboxExecute("list hostonlyifs")
BarthaM@212
   131
        
BarthaM@217
   132
        hostonlyifs = gvm_mgr.getHostOnlyIFs()
BarthaM@217
   133
        print hostonlyifs
BarthaM@217
   134
        
BarthaM@217
   135
        dhcpservers = gvm_mgr.getDHCPServers()
BarthaM@217
   136
        print dhcpservers
BarthaM@217
   137
        
BarthaM@212
   138
if __name__ == '__main__':
BarthaM@212
   139
    TestVMManager.setUpClass()
BarthaM@212
   140
    
BarthaM@212
   141
    suite = unittest.TestLoader().loadTestsFromTestCase(TestVMManager)
BarthaM@212
   142
    unittest.TextTestRunner().run(suite)
BarthaM@212
   143
    
BarthaM@212
   144
#    logger = setupLogger('Cygwin')
BarthaM@212
   145
#    c = Cygwin()
BarthaM@212
   146
#    unittest.main()
BarthaM@212
   147
    #man = VMManager.getInstance()
BarthaM@212
   148
    #man.listVM()
BarthaM@212
   149
    #print man.getConnectedRSDs()
BarthaM@212
   150
    #print man.getNetworkDrives()
BarthaM@212
   151
    #man.genNetworkDrive()
BarthaM@212
   152
    #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
BarthaM@212
   153
    #print list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
BarthaM@212
   154
    #print list(map(chr, range(68, 91))) 
BarthaM@212
   155
    #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
BarthaM@212
   156
    #devices = VMManager.getConnectedRSDS()
BarthaM@212
   157
    #print devices
BarthaM@212
   158
    
BarthaM@212
   159
    #drives = VMManager.getLogicalDrives()
BarthaM@212
   160
    #print drives
BarthaM@212
   161
    #print VMManager.getDriveType("E")
BarthaM@212
   162
    #print VMManager.getVolumeInfo("E")
BarthaM@212
   163
    #print VMManager.getNetworkPath("E")
BarthaM@212
   164
    
BarthaM@212
   165
    #vmm.backupFile()
BarthaM@212
   166
    #for device in devices.values():
BarthaM@212
   167
    #    #print device
BarthaM@212
   168
    #    if VMManager.isMassStorageDevice(device):
BarthaM@212
   169
    #        print device
BarthaM@212
   170
        
BarthaM@212
   171
    
BarthaM@212
   172
    
BarthaM@212
   173
    #time.sleep(-1)
BarthaM@212
   174
    #man.listVM()
BarthaM@212
   175
    #man.listVM()
BarthaM@212
   176
    #man.listVM()
BarthaM@212
   177
    #man.listVM()
BarthaM@212
   178
    #man.genCertificateISO('SecurityDVM0')
BarthaM@212
   179
    #man.guestExecute('SecurityDVM0', '/bin/ls -la')
BarthaM@212
   180
    #logger = setupLogger('VMManager')
BarthaM@212
   181
    #c = Cygwin()
BarthaM@212
   182
    
BarthaM@212
   183
    #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
BarthaM@212
   184
    #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
BarthaM@212
   185
    #man.removeVM('SecurityDVM0')
BarthaM@212
   186
    #man.netUse('192.168.56.134', 'USB\\')
BarthaM@212
   187
    #ip = '192.168.56.139'
BarthaM@212
   188
    
BarthaM@212
   189
    #man.cygwin_path = 'c:\\cygwin64\\bin\\'
BarthaM@212
   190
    #man.handleDeviceChange()
BarthaM@212
   191
    #print man.listSDVM()
BarthaM@212
   192
    #man.configureHostNetworking()
BarthaM@212
   193
    #new_vm = man.generateSDVMName()
BarthaM@212
   194
    #man.createVM(new_vm)
BarthaM@212
   195
    
BarthaM@212
   196
    #print Cygwin.cmd()
BarthaM@212
   197
    #man.isAvailable('c:')
BarthaM@212
   198
    #ip = man.getHostOnlyIP('SecurityDVM0')
BarthaM@212
   199
    #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
BarthaM@212
   200
    
BarthaM@212
   201
    #man.genCertificateISO(new_vm)
BarthaM@212
   202
    #man.attachCertificateISO(new_vm)
BarthaM@212
   203
    
BarthaM@212
   204
    #man.attachCertificateISO(vm_name)
BarthaM@212
   205
    #man.guestExecute(vm_name, "ls")
BarthaM@212
   206
    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
BarthaM@212
   207
    #time.sleep(60)
BarthaM@212
   208
    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
BarthaM@212
   209
    #man.genCertificateISO('SecurityDVM')
BarthaM@212
   210
    #man.attachCertificateISO('SecurityDVM')
BarthaM@212
   211
    #man.isStorageAttached('SecurityDVM')
BarthaM@212
   212
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
BarthaM@212
   213
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
BarthaM@212
   214
    
BarthaM@212
   215
    #man.stopVM('SecurityDVM')
BarthaM@219
   216
    #man.detachStorage('SecurityDVM')
BarthaM@212
   217
    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
BarthaM@212
   218
    #man.storageAttach('SecurityDVM')
BarthaM@212
   219
    
BarthaM@212
   220
    
BarthaM@212
   221
    #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
BarthaM@212
   222
    #man.execute(cmd)