diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2014-03-26 19:21:20 +0000 |
---|---|---|
committer | <> | 2014-05-08 15:03:54 +0000 |
commit | fb123f93f9f5ce42c8e5785d2f8e0edaf951740e (patch) | |
tree | c2103d76aec5f1f10892cd1d3a38e24f665ae5db /src/VBox/Main/glue/tests | |
parent | 58ed4748338f9466599adfc8a9171280ed99e23f (diff) | |
download | VirtualBox-master.tar.gz |
Imported from /home/lorry/working-area/delta_VirtualBox/VirtualBox-4.3.10.tar.bz2.HEADVirtualBox-4.3.10master
Diffstat (limited to 'src/VBox/Main/glue/tests')
-rw-r--r-- | src/VBox/Main/glue/tests/Makefile | 2 | ||||
-rw-r--r-- | src/VBox/Main/glue/tests/TestVBox.java | 88 | ||||
-rw-r--r-- | src/VBox/Main/glue/tests/TestVBoxNATEngine.java | 187 |
3 files changed, 257 insertions, 20 deletions
diff --git a/src/VBox/Main/glue/tests/Makefile b/src/VBox/Main/glue/tests/Makefile index e1f0cb20..0b6f9096 100644 --- a/src/VBox/Main/glue/tests/Makefile +++ b/src/VBox/Main/glue/tests/Makefile @@ -2,7 +2,7 @@ # Makefile for a sample/testcase using the 'glue' Java API bindings # -# Copyright (C) 2010-2011 Oracle Corporation +# Copyright (C) 2010-2012 Oracle Corporation # # This file is part of VirtualBox Open Source Edition (OSE), as # available from http://www.virtualbox.org. This file is free software; diff --git a/src/VBox/Main/glue/tests/TestVBox.java b/src/VBox/Main/glue/tests/TestVBox.java index 0a703831..ce823720 100644 --- a/src/VBox/Main/glue/tests/TestVBox.java +++ b/src/VBox/Main/glue/tests/TestVBox.java @@ -4,7 +4,7 @@ * be used to connect to the webservice and (XP)COM APIs. */ /* - * Copyright (C) 2010-2011 Oracle Corporation + * Copyright (C) 2010-2013 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; @@ -14,7 +14,7 @@ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. */ -import org.virtualbox_4_2.*; +import org.virtualbox_4_3.*; import java.util.List; import java.util.Arrays; import java.math.BigInteger; @@ -25,7 +25,7 @@ public class TestVBox { System.out.println("got event: " + ev); VBoxEventType type = ev.getType(); - System.out.println("type = "+type); + System.out.println("type = " + type); switch (type) { case OnMachineStateChanged: @@ -34,7 +34,7 @@ public class TestVBox if (mcse == null) System.out.println("Cannot query an interface"); else - System.out.println("mid="+mcse.getMachineId()); + System.out.println("mid=" + mcse.getMachineId()); break; } } @@ -63,7 +63,7 @@ public class TestVBox es.registerListener(listener, Arrays.asList(VBoxEventType.Any), false); try { - for (int i=0; i<50; i++) + for (int i = 0; i < 50; i++) { System.out.print("."); IEvent ev = es.getEvent(listener, 500); @@ -97,6 +97,8 @@ public class TestVBox hwvirtEnabled = m.getHWVirtExProperty(HWVirtExPropertyType.Enabled); hwvirtNestedPaging = m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging); paeEnabled = m.getCPUProperty(CPUPropertyType.PAE); + String osType = m.getOSTypeId(); + IGuestOSType foo = vbox.getGuestOSType(osType); } catch (VBoxException e) { @@ -114,11 +116,29 @@ public class TestVBox } } + static boolean progressBar(VirtualBoxManager mgr, IProgress p, long waitMillis) + { + long end = System.currentTimeMillis() + waitMillis; + while (!p.getCompleted()) + { + mgr.waitForEvents(0); + p.waitForCompletion(200); + if (System.currentTimeMillis() >= end) + return false; + } + return true; + } + static void testStart(VirtualBoxManager mgr, IVirtualBox vbox) { - String m = vbox.getMachines().get(0).getName(); - System.out.println("\nAttempting to start VM '" + m + "'"); - mgr.startVm(m, null, 7000); + IMachine m = vbox.getMachines().get(0); + String name = m.getName(); + System.out.println("\nAttempting to start VM '" + name + "'"); + + ISession session = mgr.getSessionObject(); + IProgress p = m.launchVMProcess(session, "gui", ""); + progressBar(mgr, p, 10000); + session.unlockMachine(); } static void testMultiServer() @@ -130,11 +150,18 @@ public class TestVBox mgr1.connect("http://i7:18083", "", ""); mgr2.connect("http://main:18083", "", ""); - String mach1 = mgr1.getVBox().getMachines().get(0).getName(); - String mach2 = mgr2.getVBox().getMachines().get(0).getName(); - - mgr1.startVm(mach1, null, 7000); - mgr2.startVm(mach2, null, 7000); + IMachine m1 = mgr1.getVBox().getMachines().get(0); + IMachine m2 = mgr2.getVBox().getMachines().get(0); + String name1 = m1.getName(); + String name2 = m2.getName(); + ISession session1 = mgr1.getSessionObject(); + ISession session2 = mgr2.getSessionObject(); + IProgress p1 = m1.launchVMProcess(session1, "gui", ""); + IProgress p2 = m2.launchVMProcess(session2, "gui", ""); + progressBar(mgr1, p1, 10000); + progressBar(mgr2, p2, 10000); + session1.unlockMachine(); + session2.unlockMachine(); } finally { mgr1.cleanup(); mgr2.cleanup(); @@ -157,6 +184,23 @@ public class TestVBox } } + static void printErrorInfo(VBoxException e) + { + System.out.println("VBox error: " + e.getMessage()); + System.out.println("Error cause message: " + e.getCause()); + System.out.println("Overall result code: " + Integer.toHexString(e.getResultCode())); + int i = 1; + for (IVirtualBoxErrorInfo ei = e.getVirtualBoxErrorInfo(); ei != null; ei = ei.getNext(), i++) + { + System.out.println("Detail information #" + i); + System.out.println("Error mesage: " + ei.getText()); + System.out.println("Result code: " + Integer.toHexString(ei.getResultCode())); + // optional, usually provides little additional information: + System.out.println("Component: " + ei.getComponent()); + System.out.println("Interface ID: " + ei.getInterfaceID()); + } + } + public static void main(String[] args) { @@ -167,15 +211,15 @@ public class TestVBox String user = null; String passwd = null; - for (int i = 0; i<args.length; i++) + for (int i = 0; i < args.length; i++) { - if ("-w".equals(args[i])) + if (args[i].equals("-w")) ws = true; - else if ("-url".equals(args[i])) + else if (args[i].equals("-url")) url = args[++i]; - else if ("-user".equals(args[i])) + else if (args[i].equals("-user")) user = args[++i]; - else if ("-passwd".equals(args[i])) + else if (args[i].equals("-passwd")) passwd = args[++i]; } @@ -206,7 +250,13 @@ public class TestVBox } catch (VBoxException e) { - System.out.println("VBox error: "+e.getMessage()+" original="+e.getWrapped()); + printErrorInfo(e); + System.out.println("Java stack trace:"); + e.printStackTrace(); + } + catch (RuntimeException e) + { + System.out.println("Runtime error: " + e.getMessage()); e.printStackTrace(); } catch (java.io.IOException e) diff --git a/src/VBox/Main/glue/tests/TestVBoxNATEngine.java b/src/VBox/Main/glue/tests/TestVBoxNATEngine.java new file mode 100644 index 00000000..8f284205 --- /dev/null +++ b/src/VBox/Main/glue/tests/TestVBoxNATEngine.java @@ -0,0 +1,187 @@ +/* $Id: TestVBoxNATEngine.java $ */ + +/* Small sample/testcase which demonstrates that the same source code can + * be used to connect to the webservice and (XP)COM APIs. */ + +/* + * Copyright (C) 2013 Oracle Corporation + * + * This file is part of VirtualBox Open Source Edition (OSE), as + * available from http://www.virtualbox.org. This file is free software; + * you can redistribute it and/or modify it under the terms of the GNU + * General Public License (GPL) as published by the Free Software + * Foundation, in version 2 as it comes in the "COPYING" file of the + * VirtualBox OSE distribution. VirtualBox OSE is distributed in the + * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. + */ +import org.virtualbox_4_3.*; +import java.util.List; +import java.util.Arrays; +import java.util.Iterator; +import java.math.BigInteger; + +public class TestVBoxNATEngine +{ + void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm) + { + String name; + boolean inaccessible = false; + /* different chipsets might have different number of attachments */ + ChipsetType chipsetType = vm.getChipsetType(); + INetworkAdapter adapters[] = + new INetworkAdapter[ + vbox.getSystemProperties().getMaxNetworkAdapters(chipsetType).intValue()]; + + try + { + name = vm.getName(); + /* + * Dump adapters and if it's got NAT attachment + * dump it settings + */ + + for (int nidx = 0; nidx < adapters.length; ++nidx) + { + /* select available and NATs only. */ + adapters[nidx] = vm.getNetworkAdapter(new Long(nidx)); + INetworkAdapter n = adapters[nidx]; + + if (n == null) + continue; + NetworkAttachmentType attachmentType = n.getAttachmentType(); + if (attachmentType.equals(NetworkAttachmentType.NAT)) + { + INATEngine nat = n.getNATEngine(); + List<String> portForward = nat.getRedirects(); + String pf = null; + Iterator<String> itPortForward = portForward.iterator(); + for (;itPortForward.hasNext();) + { + pf = itPortForward.next(); + System.out.println(name + ":NIC" + n.getSlot() /* name:NIC<slot number>*/ + + " pf: " + pf); /* port-forward rule */ + } + if (pf != null) + { + String pfAttributes[] = pf.split(","); + /* name,proto,hostip,host,hostport,guestip,guestport */ + nat.removeRedirect(pfAttributes[0]); + nat.addRedirect("", + NATProtocol.fromValue(new Integer(pfAttributes[1]).longValue()), + pfAttributes[2], + new Integer( + new Integer(pfAttributes[3]).intValue() + 1), + pfAttributes[4], + new Integer(pfAttributes[5])); + } + + } + } + + } + catch (VBoxException e) + { + name = "<inaccessible>"; + inaccessible = true; + } + } + + static void testStart(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm) + { + System.out.println("\nAttempting to start VM '" + vm.getName() + "'"); + mgr.startVm(vm.getName(), null, 7000); + } + + public TestVBoxNATEngine(String[] args) + { + VirtualBoxManager mgr = VirtualBoxManager.createInstance(null); + + boolean ws = false; + String url = null; + String user = null; + String passwd = null; + String vmname = null; + IMachine vm = null; + + for (int i = 0; i<args.length; i++) + { + if ("-w".equals(args[i])) + ws = true; + else if ("-url".equals(args[i])) + url = args[++i]; + else if ("-user".equals(args[i])) + user = args[++i]; + else if ("-passwd".equals(args[i])) + passwd = args[++i]; + else if ("-vm".equals(args[i])) + vmname = args[++i]; + } + + if (ws) + { + try { + mgr.connect(url, user, passwd); + } catch (VBoxException e) { + e.printStackTrace(); + System.out.println("Cannot connect, start webserver first!"); + } + } + + try + { + IVirtualBox vbox = mgr.getVBox(); + if (vbox != null) + { + if (vmname != null) + { + for (IMachine m:vbox.getMachines()) + { + if (m.getName().equals(vmname)) + { + vm = m; + break; + } + } + + } + else + vm = vbox.getMachines().get(0); + System.out.println("VirtualBox version: " + vbox.getVersion() + "\n"); + if (vm != null) + { + testEnumeration(mgr, vbox, vm); + testStart(mgr, vbox, vm); + //testEvents(mgr, vbox.getEventSource()); + } + System.out.println("done, press Enter..."); + int ch = System.in.read(); + } + } + catch (VBoxException e) + { + System.out.println("VBox error: "+e.getMessage()+" original="+e.getWrapped()); + e.printStackTrace(); + } + catch (java.io.IOException e) + { + e.printStackTrace(); + } + + if (ws) + { + try { + mgr.disconnect(); + } catch (VBoxException e) { + e.printStackTrace(); + } + } + /* cleanup do the disconnect */ + mgr.cleanup(); + + } + public static void main(String[] args) + { + new TestVBoxNATEngine(args); + } + +} |