OpenSecurity/bin/test_vmmanager.pyw
author BarthaM@N3SIM1218.D03.arc.local
Fri, 18 Jul 2014 13:45:09 +0100
changeset 213 2e0b94e12bfc
parent 212 59ebaa44c12c
child 217 4162648fb167
permissions -rw-r--r--
Addded http proxy server support.
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@212
    59
        template = vmmanager.VMManager.getTemplateUUID()
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@213
    91
        
BarthaM@213
    92
    def testMatchProxy(self):
BarthaM@213
    93
        #http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080
BarthaM@213
    94
        #212.17.86.109:8080
BarthaM@213
    95
        text = 'http=212.17.86.109:8080;https=212.17.86.109:8080;ftp=212.17.86.109:8080'
BarthaM@213
    96
        print re.search(r"(?<=http=)(?P<HttpProxy>.*?)(?=;)", text).groupdict()
BarthaM@213
    97
        print re.search(r"(?<=http=)(.*?)(?=;)", text)
BarthaM@213
    98
        
BarthaM@212
    99
    #@classmethod
BarthaM@212
   100
    #def tearOffClass(self):
BarthaM@212
   101
    #    gvm_mgr.stop()
BarthaM@212
   102
    #    gvm_mgr.cleanup()
BarthaM@212
   103
        
BarthaM@212
   104
BarthaM@212
   105
if __name__ == '__main__':
BarthaM@212
   106
    TestVMManager.setUpClass()
BarthaM@212
   107
    
BarthaM@212
   108
    suite = unittest.TestLoader().loadTestsFromTestCase(TestVMManager)
BarthaM@212
   109
    unittest.TextTestRunner().run(suite)
BarthaM@212
   110
    
BarthaM@212
   111
#    logger = setupLogger('Cygwin')
BarthaM@212
   112
#    c = Cygwin()
BarthaM@212
   113
#    unittest.main()
BarthaM@212
   114
    #man = VMManager.getInstance()
BarthaM@212
   115
    #man.listVM()
BarthaM@212
   116
    #print man.getConnectedRSDs()
BarthaM@212
   117
    #print man.getNetworkDrives()
BarthaM@212
   118
    #man.genNetworkDrive()
BarthaM@212
   119
    #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
BarthaM@212
   120
    #print list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
BarthaM@212
   121
    #print list(map(chr, range(68, 91))) 
BarthaM@212
   122
    #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
BarthaM@212
   123
    #devices = VMManager.getConnectedRSDS()
BarthaM@212
   124
    #print devices
BarthaM@212
   125
    
BarthaM@212
   126
    #drives = VMManager.getLogicalDrives()
BarthaM@212
   127
    #print drives
BarthaM@212
   128
    #print VMManager.getDriveType("E")
BarthaM@212
   129
    #print VMManager.getVolumeInfo("E")
BarthaM@212
   130
    #print VMManager.getNetworkPath("E")
BarthaM@212
   131
    
BarthaM@212
   132
    #vmm.backupFile()
BarthaM@212
   133
    #for device in devices.values():
BarthaM@212
   134
    #    #print device
BarthaM@212
   135
    #    if VMManager.isMassStorageDevice(device):
BarthaM@212
   136
    #        print device
BarthaM@212
   137
        
BarthaM@212
   138
    
BarthaM@212
   139
    
BarthaM@212
   140
    #time.sleep(-1)
BarthaM@212
   141
    #man.listVM()
BarthaM@212
   142
    #man.listVM()
BarthaM@212
   143
    #man.listVM()
BarthaM@212
   144
    #man.listVM()
BarthaM@212
   145
    #man.genCertificateISO('SecurityDVM0')
BarthaM@212
   146
    #man.guestExecute('SecurityDVM0', '/bin/ls -la')
BarthaM@212
   147
    #logger = setupLogger('VMManager')
BarthaM@212
   148
    #c = Cygwin()
BarthaM@212
   149
    
BarthaM@212
   150
    #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
BarthaM@212
   151
    #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
BarthaM@212
   152
    #man.removeVM('SecurityDVM0')
BarthaM@212
   153
    #man.netUse('192.168.56.134', 'USB\\')
BarthaM@212
   154
    #ip = '192.168.56.139'
BarthaM@212
   155
    
BarthaM@212
   156
    #man.cygwin_path = 'c:\\cygwin64\\bin\\'
BarthaM@212
   157
    #man.handleDeviceChange()
BarthaM@212
   158
    #print man.listSDVM()
BarthaM@212
   159
    #man.configureHostNetworking()
BarthaM@212
   160
    #new_vm = man.generateSDVMName()
BarthaM@212
   161
    #man.createVM(new_vm)
BarthaM@212
   162
    
BarthaM@212
   163
    #print Cygwin.cmd()
BarthaM@212
   164
    #man.isAvailable('c:')
BarthaM@212
   165
    #ip = man.getHostOnlyIP('SecurityDVM0')
BarthaM@212
   166
    #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
BarthaM@212
   167
    
BarthaM@212
   168
    #man.genCertificateISO(new_vm)
BarthaM@212
   169
    #man.attachCertificateISO(new_vm)
BarthaM@212
   170
    
BarthaM@212
   171
    #man.attachCertificateISO(vm_name)
BarthaM@212
   172
    #man.guestExecute(vm_name, "ls")
BarthaM@212
   173
    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
BarthaM@212
   174
    #time.sleep(60)
BarthaM@212
   175
    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
BarthaM@212
   176
    #man.genCertificateISO('SecurityDVM')
BarthaM@212
   177
    #man.attachCertificateISO('SecurityDVM')
BarthaM@212
   178
    #man.isStorageAttached('SecurityDVM')
BarthaM@212
   179
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
BarthaM@212
   180
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
BarthaM@212
   181
    
BarthaM@212
   182
    #man.stopVM('SecurityDVM')
BarthaM@212
   183
    #man.storageDetach('SecurityDVM')
BarthaM@212
   184
    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
BarthaM@212
   185
    #man.storageAttach('SecurityDVM')
BarthaM@212
   186
    
BarthaM@212
   187
    
BarthaM@212
   188
    #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
BarthaM@212
   189
    #man.execute(cmd)