VirtualBox

Ticket #11134: UnitTests.cs

File UnitTests.cs, 9.9 KB (added by Rami Abughazaleh, 12 years ago)

added UnitTests.cs which contains a unit test that checks if a file exists on Windows 8

Line 
1using System;
2using System.Text;
3using System.Collections.Generic;
4using System.Linq;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6using System.Diagnostics;
7using System.Threading;
8
9namespace Oracle.VirtualBox.Tests
10{
11 /// <summary>
12 /// Summary description for UnitTests
13 /// </summary>
14 [TestClass]
15 public class UnitTests
16 {
17
18 [TestMethod]
19 public void CreateProcess_OnWindows8_Succeeds()
20 {
21 //add web service reference to VirtualBox
22 //add reference to System.Web.Services
23
24 //running on Windows 7 64-bit
25 //Oracle VM VirtualBox 4.2.6r82870
26
27 //"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" setproperty websrvauthlibrary null
28
29 //run VirtualBox non-elevated
30
31 //run the following command in a non-elevated command prompt:
32 //"c:\Program Files\Oracle\VirtualBox\VBoxWebSrv.exe" --verbose
33
34 vboxBinding webServiceClient = new vboxBinding(new Uri("http://localhost:18083/"));
35
36 //Windows 8 32-bit
37 string virtualMachineNameOrId = "Win8-PC";
38
39 //Base snapshot was created when specified user is logged in and after guest additions are installed
40 //VirtualBox Guest Additions 4.2.6r82870
41 string snapshotNameOrId = "Base";
42
43 string username = "rami.abughazaleh@live.com";
44 string password = "password";
45
46 string filePath = @"c:\windows\system32\notepad.exe";
47 string webSessionId = null;
48 string sessionId = null;
49
50 try
51 {
52 //log into service and get session id
53 webSessionId = webServiceClient.IWebsessionManager_logon("", "");
54 sessionId = webServiceClient.IWebsessionManager_getSessionObject(webSessionId);
55
56 //get version
57 string version = webServiceClient.IVirtualBox_getVersion(webSessionId);
58
59 Trace.WriteLine(version);
60
61 //get virtual machine
62 string virtualMachineId = webServiceClient.IVirtualBox_findMachine(webSessionId, virtualMachineNameOrId);
63
64 //power off virtual machine
65 webServiceClient.IMachine_lockMachine(virtualMachineId, sessionId, LockType.Shared);
66
67 string consoleId = webServiceClient.ISession_getConsole(sessionId);
68
69 MachineState machineState = webServiceClient.IConsole_getState(consoleId);
70
71 if (!((machineState == MachineState.Saved)
72 || (machineState == MachineState.PoweredOff)))
73 {
74 string progressId = webServiceClient.IConsole_powerDown(consoleId);
75
76 webServiceClient.IProgress_waitForCompletion(progressId, -1);
77
78 string errorInfo = webServiceClient.IProgress_getErrorInfo(progressId);
79 if (!string.IsNullOrEmpty(errorInfo))
80 {
81 string errorText = webServiceClient.IVirtualBoxErrorInfo_getText(errorInfo);
82
83 throw new Exception(string.Format("{0} {1}", errorText, errorInfo));
84 }
85 }
86
87 SessionState sessionState = webServiceClient.ISession_getState(sessionId);
88
89 if (sessionState == SessionState.Locked)
90 {
91 webServiceClient.ISession_unlockMachine(sessionId);
92 }
93
94 //restore snapshot
95 {
96 webServiceClient.IMachine_lockMachine(virtualMachineId, sessionId, LockType.Shared);
97
98 consoleId = webServiceClient.ISession_getConsole(sessionId);
99
100 string snapshotId = webServiceClient.IMachine_findSnapshot(virtualMachineId, snapshotNameOrId);
101
102 string progressId = webServiceClient.IConsole_restoreSnapshot(consoleId, snapshotId);
103
104 webServiceClient.IProgress_waitForCompletion(progressId, -1);
105
106 string errorInfo = webServiceClient.IProgress_getErrorInfo(progressId);
107 if (!string.IsNullOrEmpty(errorInfo))
108 {
109 string errorText = webServiceClient.IVirtualBoxErrorInfo_getText(errorInfo);
110
111 throw new Exception(string.Format("{0} {1}", errorText, errorInfo));
112 }
113
114 sessionState = webServiceClient.ISession_getState(sessionId);
115
116 if (sessionState == SessionState.Locked)
117 {
118 webServiceClient.ISession_unlockMachine(sessionId);
119 }
120 }
121
122 //power on virtual machine
123 {
124 string progressId = webServiceClient.IMachine_launchVMProcess(virtualMachineId, sessionId, "gui", "");
125
126 webServiceClient.IProgress_waitForCompletion(progressId, -1);
127
128 string errorInfo = webServiceClient.IProgress_getErrorInfo(progressId);
129 if (!string.IsNullOrEmpty(errorInfo))
130 {
131 string errorText = webServiceClient.IVirtualBoxErrorInfo_getText(errorInfo);
132
133 throw new Exception(string.Format("{0} {1}", errorText, errorInfo));
134 }
135 }
136
137 //wait for guest additions
138 consoleId = webServiceClient.ISession_getConsole(sessionId);
139
140 string guestId = webServiceClient.IConsole_getGuest(consoleId);
141
142 string osTypeId = webServiceClient.IGuest_getOSTypeId(guestId);
143
144 Trace.WriteLine(osTypeId);
145
146 AdditionsRunLevelType additionsRunLevelType = webServiceClient.IGuest_getAdditionsRunLevel(guestId);
147
148 if (additionsRunLevelType == AdditionsRunLevelType.None)
149 {
150 throw new Exception("Guest additions not installed");
151 }
152
153 Trace.WriteLine(additionsRunLevelType);
154
155 bool active = webServiceClient.IGuest_getAdditionsStatus(guestId, AdditionsRunLevelType.System);
156
157 while (!active)
158 {
159 Thread.Sleep(3000);
160
161 active = webServiceClient.IGuest_getAdditionsStatus(guestId, AdditionsRunLevelType.System);
162 }
163
164 Trace.WriteLine("Guest drivers are loaded.");
165
166 if (additionsRunLevelType == AdditionsRunLevelType.System)
167 {
168 return;
169 }
170
171 active = webServiceClient.IGuest_getAdditionsStatus(guestId, AdditionsRunLevelType.Userland);
172
173 while (!active)
174 {
175 Thread.Sleep(3000);
176
177 active = webServiceClient.IGuest_getAdditionsStatus(guestId, AdditionsRunLevelType.Userland);
178 }
179
180 Trace.WriteLine("Common components (such as application services) are loaded.");
181
182 if (additionsRunLevelType == AdditionsRunLevelType.Userland)
183 {
184 return;
185 }
186
187 active = webServiceClient.IGuest_getAdditionsStatus(guestId, AdditionsRunLevelType.Desktop);
188
189 while (!active)
190 {
191 Thread.Sleep(3000);
192
193 active = webServiceClient.IGuest_getAdditionsStatus(guestId, AdditionsRunLevelType.Desktop);
194 }
195
196 Trace.WriteLine("Per-user desktop components are loaded.");
197
198 //log into guest and check if file exists
199 string guestSessionId = webServiceClient.IGuest_createSession(guestId, username, password, null, "sessionName");
200
201 webServiceClient.IGuest_setCredentials(guestId, username, password, null, true);
202
203 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getId(guestSessionId)));
204 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getDomain(guestSessionId)));
205 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getDirectories(guestSessionId)));
206 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getFiles(guestSessionId)));
207 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getProcesses(guestSessionId)));
208 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getEnvironment(guestSessionId)));
209 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getName(guestSessionId)));
210 Trace.WriteLine(string.Join(Environment.NewLine, webServiceClient.IGuestSession_getUser(guestSessionId)));
211
212 //System.Web.Services.Protocols.SoapException: VirtualBox error: Querying file information for "c:\windows\system32\notepad.exe" failed: VERR_NOT_FOUND (0x80BB0005)
213 bool fileExists = webServiceClient.IGuestSession_fileExists(guestSessionId, filePath);
214
215 Trace.WriteLine(fileExists);
216 }
217 finally
218 {
219 //unlock machine
220 if (sessionId != null)
221 {
222 SessionState sessionState = webServiceClient.ISession_getState(sessionId);
223
224 if (sessionState == SessionState.Locked)
225 {
226 webServiceClient.ISession_unlockMachine(sessionId);
227 }
228 }
229
230 //log off
231 if (webSessionId != null)
232 {
233 Trace.WriteLine("logging off");
234
235 webServiceClient.IWebsessionManager_logoff(webSessionId);
236 }
237 }
238 }
239 }
240}

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