VirtualBox

source: vbox/trunk/src/VBox/Main/glue/tests/TestVBoxNATEngine.java@ 55068

Last change on this file since 55068 was 45186, checked in by vboxsync, 12 years ago

API/binding: test for NAT Engine in Java.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/* $Id: TestVBoxNATEngine.java 45186 2013-03-26 07:59:00Z vboxsync $ */
2
3/* Small sample/testcase which demonstrates that the same source code can
4 * be used to connect to the webservice and (XP)COM APIs. */
5
6/*
7 * Copyright (C) 2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17import org.virtualbox_4_3.*;
18import java.util.List;
19import java.util.Arrays;
20import java.util.Iterator;
21import java.math.BigInteger;
22
23public class TestVBoxNATEngine
24{
25 void testEnumeration(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm)
26 {
27 String name;
28 boolean inaccessible = false;
29 /* different chipsets might have different number of attachments */
30 ChipsetType chipsetType = vm.getChipsetType();
31 INetworkAdapter adapters[] =
32 new INetworkAdapter[
33 vbox.getSystemProperties().getMaxNetworkAdapters(chipsetType).intValue()];
34
35 try
36 {
37 name = vm.getName();
38 /*
39 * Dump adapters and if it's got NAT attachment
40 * dump it settings
41 */
42
43 for (int nidx = 0; nidx < adapters.length; ++nidx)
44 {
45 /* select available and NATs only. */
46 adapters[nidx] = vm.getNetworkAdapter(new Long(nidx));
47 INetworkAdapter n = adapters[nidx];
48
49 if (n == null)
50 continue;
51 NetworkAttachmentType attachmentType = n.getAttachmentType();
52 if (attachmentType.equals(NetworkAttachmentType.NAT))
53 {
54 INATEngine nat = n.getNATEngine();
55 List<String> portForward = nat.getRedirects();
56 String pf = null;
57 Iterator<String> itPortForward = portForward.iterator();
58 for (;itPortForward.hasNext();)
59 {
60 pf = itPortForward.next();
61 System.out.println(name + ":NIC" + n.getSlot() /* name:NIC<slot number>*/
62 + " pf: " + pf); /* port-forward rule */
63 }
64 if (pf != null)
65 {
66 String pfAttributes[] = pf.split(",");
67 /* name,proto,hostip,host,hostport,guestip,guestport */
68 nat.removeRedirect(pfAttributes[0]);
69 nat.addRedirect("",
70 NATProtocol.fromValue(new Integer(pfAttributes[1]).longValue()),
71 pfAttributes[2],
72 new Integer(
73 new Integer(pfAttributes[3]).intValue() + 1),
74 pfAttributes[4],
75 new Integer(pfAttributes[5]));
76 }
77
78 }
79 }
80
81 }
82 catch (VBoxException e)
83 {
84 name = "<inaccessible>";
85 inaccessible = true;
86 }
87 }
88
89 static void testStart(VirtualBoxManager mgr, IVirtualBox vbox, IMachine vm)
90 {
91 System.out.println("\nAttempting to start VM '" + vm.getName() + "'");
92 mgr.startVm(vm.getName(), null, 7000);
93 }
94
95 public TestVBoxNATEngine(String[] args)
96 {
97 VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
98
99 boolean ws = false;
100 String url = null;
101 String user = null;
102 String passwd = null;
103 String vmname = null;
104 IMachine vm = null;
105
106 for (int i = 0; i<args.length; i++)
107 {
108 if ("-w".equals(args[i]))
109 ws = true;
110 else if ("-url".equals(args[i]))
111 url = args[++i];
112 else if ("-user".equals(args[i]))
113 user = args[++i];
114 else if ("-passwd".equals(args[i]))
115 passwd = args[++i];
116 else if ("-vm".equals(args[i]))
117 vmname = args[++i];
118 }
119
120 if (ws)
121 {
122 try {
123 mgr.connect(url, user, passwd);
124 } catch (VBoxException e) {
125 e.printStackTrace();
126 System.out.println("Cannot connect, start webserver first!");
127 }
128 }
129
130 try
131 {
132 IVirtualBox vbox = mgr.getVBox();
133 if (vbox != null)
134 {
135 if (vmname != null)
136 {
137 for (IMachine m:vbox.getMachines())
138 {
139 if (m.getName().equals(vmname))
140 {
141 vm = m;
142 break;
143 }
144 }
145
146 }
147 else
148 vm = vbox.getMachines().get(0);
149 System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
150 if (vm != null)
151 {
152 testEnumeration(mgr, vbox, vm);
153 testStart(mgr, vbox, vm);
154 //testEvents(mgr, vbox.getEventSource());
155 }
156 System.out.println("done, press Enter...");
157 int ch = System.in.read();
158 }
159 }
160 catch (VBoxException e)
161 {
162 System.out.println("VBox error: "+e.getMessage()+" original="+e.getWrapped());
163 e.printStackTrace();
164 }
165 catch (java.io.IOException e)
166 {
167 e.printStackTrace();
168 }
169
170 if (ws)
171 {
172 try {
173 mgr.disconnect();
174 } catch (VBoxException e) {
175 e.printStackTrace();
176 }
177 }
178 /* cleanup do the disconnect */
179 mgr.cleanup();
180
181 }
182 public static void main(String[] args)
183 {
184 new TestVBoxNATEngine(args);
185 }
186
187}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette