OpenSecurity/bin/test_vmmanager.pyw
author Bartha Mihai <mihai.bartha@ait.ac.at>
Tue, 13 Jan 2015 18:26:41 +0100
changeset 252 824ae4324f57
parent 240 d7ef04254e9c
permissions -rw-r--r--
changed settings restore functionality for the browser.
Needs addition of rsync on VM and setup
BarthaM@212
     1
#!/bin/env python
BarthaM@212
     2
# -*- coding: utf-8 -*-
BarthaM@212
     3
BarthaM@212
     4
# ------------------------------------------------------------
oliver@240
     5
# test_vmmanger
oliver@240
     6
#
oliver@240
     7
# test the open security vmmanager
BarthaM@212
     8
#   
BarthaM@212
     9
# Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
BarthaM@212
    10
#
oliver@240
    11
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
BarthaM@212
    12
# 
BarthaM@212
    13
# 
oliver@240
    14
#     X-Net Services GmbH
oliver@240
    15
#     Elisabethstrasse 1
oliver@240
    16
#     4020 Linz
oliver@240
    17
#     AUSTRIA
oliver@240
    18
#     https://www.x-net.at
oliver@240
    19
# 
oliver@240
    20
#     AIT Austrian Institute of Technology
oliver@240
    21
#     Donau City Strasse 1
oliver@240
    22
#     1220 Wien
oliver@240
    23
#     AUSTRIA
oliver@240
    24
#     http://www.ait.ac.at
oliver@240
    25
# 
oliver@240
    26
# 
oliver@240
    27
# Licensed under the Apache License, Version 2.0 (the "License");
oliver@240
    28
# you may not use this file except in compliance with the License.
oliver@240
    29
# You may obtain a copy of the License at
oliver@240
    30
# 
oliver@240
    31
#    http://www.apache.org/licenses/LICENSE-2.0
oliver@240
    32
# 
oliver@240
    33
# Unless required by applicable law or agreed to in writing, software
oliver@240
    34
# distributed under the License is distributed on an "AS IS" BASIS,
oliver@240
    35
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
oliver@240
    36
# See the License for the specific language governing permissions and
oliver@240
    37
# limitations under the License.
BarthaM@212
    38
# ------------------------------------------------------------
BarthaM@212
    39
BarthaM@212
    40
BarthaM@212
    41
# ------------------------------------------------------------
BarthaM@212
    42
# imports
BarthaM@212
    43
BarthaM@212
    44
import unittest
BarthaM@212
    45
BarthaM@212
    46
import os
BarthaM@213
    47
import re
BarthaM@212
    48
import os.path
BarthaM@212
    49
import sys
BarthaM@212
    50
import cygwin
BarthaM@212
    51
import vmmanager
BarthaM@213
    52
import _winreg
BarthaM@234
    53
import time
BarthaM@212
    54
gvm_mgr = None
BarthaM@212
    55
BarthaM@212
    56
class TestVMManager(unittest.TestCase):
BarthaM@212
    57
    
BarthaM@212
    58
    def setUp(self):
BarthaM@212
    59
        pass
BarthaM@212
    60
    
BarthaM@212
    61
    @classmethod
BarthaM@212
    62
    def setUpClass(self):
BarthaM@212
    63
        global gvm_mgr
BarthaM@212
    64
        gvm_mgr = vmmanager.VMManager.getInstance()
BarthaM@212
    65
        pass
BarthaM@212
    66
    
BarthaM@234
    67
    #@classmethod
BarthaM@234
    68
    #def tearOffClass(self):
BarthaM@234
    69
    #    gvm_mgr.stop()
BarthaM@234
    70
    #    gvm_mgr.cleanup()
BarthaM@234
    71
    
BarthaM@212
    72
    @unittest.skip("skipping")
BarthaM@212
    73
    def testGetTemplateUUID(self):
BarthaM@221
    74
        template = vmmanager.VMManager.getVDiskUUID(gvm_mgr.templateImage)
BarthaM@212
    75
        self.assertIsNotNone(template,  "returned no UUID for template")
BarthaM@212
    76
    
BarthaM@213
    77
    @unittest.skip("skipping")    
BarthaM@212
    78
    def testUpdateTemplate(self):
BarthaM@212
    79
        gvm_mgr.updateTemplate()
BarthaM@212
    80
        pass
BarthaM@212
    81
    
BarthaM@213
    82
    def setKey(self, key, name, value):
BarthaM@213
    83
        _, reg_type = _winreg.QueryValueEx(key, name)
BarthaM@213
    84
        _winreg.SetValueEx(key, name, 0, reg_type, value)
BarthaM@213
    85
    
BarthaM@213
    86
    @unittest.skip("skipping")    
BarthaM@213
    87
    def testGetProxySettings(self):
BarthaM@213
    88
        #sudo echo "http_proxy=http://80.122.169.38:8080/" >> /etc/environment
BarthaM@213
    89
        aReg = _winreg.ConnectRegistry(None,_winreg.HKEY_CURRENT_USER)
BarthaM@213
    90
        aKey = _winreg.OpenKey(aReg, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
BarthaM@213
    91
        subCount, valueCount, lastModified = _winreg.QueryInfoKey(aKey)
BarthaM@213
    92
        proxy = dict()
BarthaM@213
    93
        for i in range(valueCount):                                           
BarthaM@213
    94
            try:
BarthaM@213
    95
                n,v,t = _winreg.EnumValue(aKey,i)
BarthaM@213
    96
                proxy[n] = v
BarthaM@213
    97
            except EnvironmentError:                                               
BarthaM@213
    98
                break
BarthaM@213
    99
        _winreg.CloseKey(aKey)
BarthaM@213
   100
        print proxy
BarthaM@213
   101
        if 'ProxyEnable' in proxy.keys() and proxy['ProxyEnable'] == 1:
BarthaM@213
   102
            print proxy['ProxyServer']
BarthaM@213
   103
            return proxy['ProxyServer']
BarthaM@213
   104
        else: 
BarthaM@213
   105
            return ""
BarthaM@217
   106
    
BarthaM@217
   107
    @unittest.skip("skipping")      
BarthaM@213
   108
    def testMatchProxy(self):
BarthaM@213
   109
        #http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080
BarthaM@213
   110
        #212.17.86.109:8080
BarthaM@213
   111
        text = 'http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080'
BarthaM@213
   112
        print re.search(r"(?<=http=)(?P<HttpProxy>.*?)(?=;)", text).groupdict()
BarthaM@213
   113
        print re.search(r"(?<=http=)(.*?)(?=;)", text)
BarthaM@213
   114
        
BarthaM@217
   115
    
BarthaM@217
   116
    
BarthaM@234
   117
    @unittest.skip("skipping")
BarthaM@221
   118
    def testImportTemplate(self):
BarthaM@221
   119
        gvm_mgr.cleanup()
BarthaM@221
   120
        if 'SecurityDVM' in gvm_mgr.listVMS():
BarthaM@221
   121
            gvm_mgr.removeVM('SecurityDVM')
BarthaM@221
   122
        
BarthaM@221
   123
        uuid = gvm_mgr.getVDiskUUID(gvm_mgr.templateImage)
BarthaM@221
   124
        if uuid:
BarthaM@221
   125
            gvm_mgr.removeImage(uuid)
BarthaM@221
   126
        
BarthaM@221
   127
        gvm_mgr.removeVMFolder('SecurityDVM')
BarthaM@221
   128
        gvm_mgr.importTemplate('C:\Windows\System32\config\systemprofile\VirtualBox VMs\OsecVM.ova')
BarthaM@221
   129
        gvm_mgr.updateTemplate()
BarthaM@234
   130
   
BarthaM@234
   131
    @unittest.skip("skipping")   
BarthaM@217
   132
    def testHostOnlyDHCP(self):
BarthaM@217
   133
        #list hostonlyifs
BarthaM@217
   134
        #Cygwin.vboxExecute("list hostonlyifs")
BarthaM@212
   135
        
BarthaM@217
   136
        hostonlyifs = gvm_mgr.getHostOnlyIFs()
BarthaM@217
   137
        print hostonlyifs
BarthaM@217
   138
        
BarthaM@217
   139
        dhcpservers = gvm_mgr.getDHCPServers()
BarthaM@217
   140
        print dhcpservers
mihai@252
   141
    
mihai@252
   142
    @unittest.skip("skipping")       
BarthaM@234
   143
    def testBrowsingRequest(self):
BarthaM@234
   144
        gvm_mgr.start()
BarthaM@234
   145
        gvm_mgr.handleBrowsingRequest()
BarthaM@234
   146
        time.sleep(3000)
BarthaM@234
   147
        pass
mihai@252
   148
    
mihai@252
   149
    def testRsync(self):
mihai@252
   150
        gvm_mgr.start()
mihai@252
   151
                    
mihai@252
   152
        gvm_mgr.syncRemoteFile("SecurityDVM0", gvm_mgr.getHostOnlyIP("SecurityDVM0"), gvm_mgr.getAppDataDir() + '/OpenSecurity/chromium', '/home/osecuser/.config/')
mihai@252
   153
        pass
mihai@252
   154
        
BarthaM@234
   155
        
BarthaM@212
   156
if __name__ == '__main__':
BarthaM@212
   157
    TestVMManager.setUpClass()
BarthaM@212
   158
    
BarthaM@212
   159
    suite = unittest.TestLoader().loadTestsFromTestCase(TestVMManager)
BarthaM@212
   160
    unittest.TextTestRunner().run(suite)
BarthaM@212
   161
    
BarthaM@234
   162
    #VBoxManage list hostonlyifs
BarthaM@234
   163
    #VBoxManage list dhcpservers
BarthaM@234
   164
    #VBoxManage dhcpserver remove --netname "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter"
BarthaM@234
   165
    #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
   166
    #VBoxManage dhcpserver modify --ifname "VirtualBox Host-Only Ethernet Adapter" --enable
BarthaM@234
   167
    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --dhcp
BarthaM@234
   168
    #VBoxManage hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.1 --netmask 255.255.255.0    
BarthaM@212
   169
#    logger = setupLogger('Cygwin')
BarthaM@212
   170
#    c = Cygwin()
BarthaM@212
   171
#    unittest.main()
BarthaM@212
   172
    #man = VMManager.getInstance()
BarthaM@212
   173
    #man.listVM()
BarthaM@212
   174
    #print man.getConnectedRSDs()
BarthaM@212
   175
    #print man.getNetworkDrives()
BarthaM@212
   176
    #man.genNetworkDrive()
BarthaM@212
   177
    #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
BarthaM@212
   178
    #print list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
BarthaM@212
   179
    #print list(map(chr, range(68, 91))) 
BarthaM@212
   180
    #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
BarthaM@212
   181
    #devices = VMManager.getConnectedRSDS()
BarthaM@212
   182
    #print devices
BarthaM@212
   183
    
BarthaM@212
   184
    #drives = VMManager.getLogicalDrives()
BarthaM@212
   185
    #print drives
BarthaM@212
   186
    #print VMManager.getDriveType("E")
BarthaM@212
   187
    #print VMManager.getVolumeInfo("E")
BarthaM@212
   188
    #print VMManager.getNetworkPath("E")
BarthaM@212
   189
    
BarthaM@212
   190
    #vmm.backupFile()
BarthaM@212
   191
    #for device in devices.values():
BarthaM@212
   192
    #    #print device
BarthaM@212
   193
    #    if VMManager.isMassStorageDevice(device):
BarthaM@212
   194
    #        print device
BarthaM@212
   195
        
BarthaM@212
   196
    
BarthaM@212
   197
    
BarthaM@212
   198
    #time.sleep(-1)
BarthaM@212
   199
    #man.listVM()
BarthaM@212
   200
    #man.listVM()
BarthaM@212
   201
    #man.listVM()
BarthaM@212
   202
    #man.listVM()
BarthaM@212
   203
    #man.genCertificateISO('SecurityDVM0')
BarthaM@212
   204
    #man.guestExecute('SecurityDVM0', '/bin/ls -la')
BarthaM@212
   205
    #logger = setupLogger('VMManager')
BarthaM@212
   206
    #c = Cygwin()
BarthaM@212
   207
    
BarthaM@212
   208
    #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
BarthaM@212
   209
    #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
BarthaM@212
   210
    #man.removeVM('SecurityDVM0')
BarthaM@212
   211
    #man.netUse('192.168.56.134', 'USB\\')
BarthaM@212
   212
    #ip = '192.168.56.139'
BarthaM@212
   213
    
BarthaM@212
   214
    #man.cygwin_path = 'c:\\cygwin64\\bin\\'
BarthaM@212
   215
    #man.handleDeviceChange()
BarthaM@212
   216
    #print man.listSDVM()
BarthaM@212
   217
    #man.configureHostNetworking()
BarthaM@212
   218
    #new_vm = man.generateSDVMName()
BarthaM@212
   219
    #man.createVM(new_vm)
BarthaM@212
   220
    
BarthaM@212
   221
    #print Cygwin.cmd()
BarthaM@212
   222
    #man.isAvailable('c:')
BarthaM@212
   223
    #ip = man.getHostOnlyIP('SecurityDVM0')
BarthaM@212
   224
    #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
BarthaM@212
   225
    
BarthaM@212
   226
    #man.genCertificateISO(new_vm)
BarthaM@212
   227
    #man.attachCertificateISO(new_vm)
BarthaM@212
   228
    
BarthaM@212
   229
    #man.attachCertificateISO(vm_name)
BarthaM@212
   230
    #man.guestExecute(vm_name, "ls")
BarthaM@212
   231
    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
BarthaM@212
   232
    #time.sleep(60)
BarthaM@212
   233
    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
BarthaM@212
   234
    #man.genCertificateISO('SecurityDVM')
BarthaM@212
   235
    #man.attachCertificateISO('SecurityDVM')
BarthaM@212
   236
    #man.isStorageAttached('SecurityDVM')
BarthaM@212
   237
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
BarthaM@212
   238
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
BarthaM@212
   239
    
BarthaM@212
   240
    #man.stopVM('SecurityDVM')
BarthaM@219
   241
    #man.detachStorage('SecurityDVM')
BarthaM@212
   242
    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
BarthaM@212
   243
    #man.storageAttach('SecurityDVM')
BarthaM@212
   244
    
BarthaM@212
   245
    
BarthaM@212
   246
    #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
oliver@240
   247
    #man.execute(cmd)