VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstUnattendedScript.cpp@ 93410

Last change on this file since 93410 was 93405, checked in by vboxsync, 3 years ago

Main/Unattended: Use the detected guest OS type instead of the configured VM guest OS type for picking the installer. bugref:10186 bugref:9515

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 KB
Line 
1/* $Id: tstUnattendedScript.cpp 93405 2022-01-24 09:56:23Z vboxsync $ */
2/** @file
3 * tstUnattendedScript - testcases for UnattendedScript.
4 */
5
6/*
7 * Copyright (C) 2022 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 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "UnattendedScript.h"
23
24#include <VBox/com/VirtualBox.h>
25#include <VBox/com/errorprint.h>
26
27#include <iprt/file.h>
28#include <iprt/path.h>
29#include <iprt/test.h>
30#include <iprt/stream.h>
31
32#include "VirtualBoxBase.h"
33#include "UnattendedImpl.h"
34#include "UnattendedScript.h"
35#include "VirtualBoxImpl.h"
36#include "MachineImpl.h"
37
38using namespace std;
39
40
41/*********************************************************************************************************************************
42* Unattended Stub Implementation *
43*********************************************************************************************************************************/
44Unattended::Unattended()
45 : mhThreadReconfigureVM(NIL_RTNATIVETHREAD), mfRtcUseUtc(false), mfGuestOs64Bit(false)
46 , mpInstaller(NULL), mpTimeZoneInfo(NULL), mfIsDefaultAuxiliaryBasePath(true), mfDoneDetectIsoOS(false)
47{
48 mStrUser = "vboxuser";
49 mStrPassword = "changeme";
50 mStrFullUserName = "VBox & VBox;";
51 mStrProductKey = "911";
52 mStrIsoPath = "/iso/path/file.iso";
53 mStrAdditionsIsoPath = "/iso/path/addition.iso";
54 mfInstallGuestAdditions = true;
55 mfInstallTestExecService = true;
56 mStrValidationKitIsoPath = "/iso/path/valkit.iso";
57 mStrTimeZone = "cet";
58 mpTimeZoneInfo = NULL;
59 mStrLocale = "dk_DK";
60 mStrLanguage = "dk";
61 mStrCountry = "DK";
62 //mPackageSelectionAdjustments = "minimal";
63 mStrHostname = "my-extra-long-name.hostname.com";
64 mStrAuxiliaryBasePath = "/aux/path/pfx-";
65 mfIsDefaultAuxiliaryBasePath = false;
66 midxImage = 42;
67 mStrScriptTemplatePath = "/path/to/script-template.file";
68 mStrPostInstallScriptTemplatePath = "/path/to/post-install-template.file";
69 mStrPostInstallCommand = "/bin/post-install-command arg1 arg2 --amp=& --lt=< --gt=> --dq-word=\"word\" --sq-word='word'";
70 mStrExtraInstallKernelParameters = "extra=kernel parameters quiet amp=& lt=< gt=>";
71 mStrProxy = "http://proxy.intranet.com:443";
72
73 mfDoneDetectIsoOS = true;
74 mStrDetectedOSTypeId = "MyOSTypeId";
75 mStrDetectedOSVersion = "3.4.2";
76 mStrDetectedOSFlavor = "server";
77 //mDetectedOSLanguages = "en_UK"
78 mStrDetectedOSHints = "nudge nudge wink wink";
79}
80
81Unattended::~Unattended()
82{
83}
84
85HRESULT Unattended::FinalConstruct()
86{
87 return BaseFinalConstruct();
88}
89
90void Unattended::FinalRelease()
91{
92 uninit();
93 BaseFinalRelease();
94}
95
96void Unattended::uninit()
97{
98}
99
100HRESULT Unattended::initUnattended(VirtualBox *aParent)
101{
102 unconst(mParent) = aParent;
103 return S_OK;
104}
105
106HRESULT Unattended::detectIsoOS()
107{
108 return E_NOTIMPL;
109}
110
111
112HRESULT Unattended::prepare()
113{
114 return E_NOTIMPL;
115}
116
117HRESULT Unattended::constructMedia()
118{
119 return E_NOTIMPL;
120}
121
122HRESULT Unattended::reconfigureVM()
123{
124 return E_NOTIMPL;
125}
126
127HRESULT Unattended::done()
128{
129 return E_NOTIMPL;
130}
131
132HRESULT Unattended::getIsoPath(com::Utf8Str &isoPath)
133{
134 RT_NOREF(isoPath);
135 return E_NOTIMPL;
136}
137
138HRESULT Unattended::setIsoPath(const com::Utf8Str &isoPath)
139{
140 RT_NOREF(isoPath);
141 return E_NOTIMPL;
142}
143
144HRESULT Unattended::getUser(com::Utf8Str &user)
145{
146 RT_NOREF(user);
147 return E_NOTIMPL;
148}
149
150
151HRESULT Unattended::setUser(const com::Utf8Str &user)
152{
153 RT_NOREF(user);
154 return E_NOTIMPL;
155}
156
157HRESULT Unattended::getPassword(com::Utf8Str &password)
158{
159 RT_NOREF(password);
160 return E_NOTIMPL;
161}
162
163HRESULT Unattended::setPassword(const com::Utf8Str &password)
164{
165 RT_NOREF(password);
166 return E_NOTIMPL;
167}
168
169HRESULT Unattended::getFullUserName(com::Utf8Str &fullUserName)
170{
171 RT_NOREF(fullUserName);
172 return E_NOTIMPL;
173}
174
175HRESULT Unattended::setFullUserName(const com::Utf8Str &fullUserName)
176{
177 RT_NOREF(fullUserName);
178 return E_NOTIMPL;
179}
180
181HRESULT Unattended::getProductKey(com::Utf8Str &productKey)
182{
183 RT_NOREF(productKey);
184 return E_NOTIMPL;
185}
186
187HRESULT Unattended::setProductKey(const com::Utf8Str &productKey)
188{
189 RT_NOREF(productKey);
190 return E_NOTIMPL;
191}
192
193HRESULT Unattended::getAdditionsIsoPath(com::Utf8Str &additionsIsoPath)
194{
195 RT_NOREF(additionsIsoPath);
196 return E_NOTIMPL;
197}
198
199HRESULT Unattended::setAdditionsIsoPath(const com::Utf8Str &additionsIsoPath)
200{
201 RT_NOREF(additionsIsoPath);
202 return E_NOTIMPL;
203}
204
205HRESULT Unattended::getInstallGuestAdditions(BOOL *installGuestAdditions)
206{
207 RT_NOREF(installGuestAdditions);
208 return E_NOTIMPL;
209}
210
211HRESULT Unattended::setInstallGuestAdditions(BOOL installGuestAdditions)
212{
213 RT_NOREF(installGuestAdditions);
214 return E_NOTIMPL;
215}
216
217HRESULT Unattended::getValidationKitIsoPath(com::Utf8Str &aValidationKitIsoPath)
218{
219 RT_NOREF(aValidationKitIsoPath);
220 return E_NOTIMPL;
221}
222
223HRESULT Unattended::setValidationKitIsoPath(const com::Utf8Str &aValidationKitIsoPath)
224{
225 RT_NOREF(aValidationKitIsoPath);
226 return E_NOTIMPL;
227}
228
229HRESULT Unattended::getInstallTestExecService(BOOL *aInstallTestExecService)
230{
231 RT_NOREF(aInstallTestExecService);
232 return E_NOTIMPL;
233}
234
235HRESULT Unattended::setInstallTestExecService(BOOL aInstallTestExecService)
236{
237 RT_NOREF(aInstallTestExecService);
238 return E_NOTIMPL;
239}
240
241HRESULT Unattended::getTimeZone(com::Utf8Str &aTimeZone)
242{
243 RT_NOREF(aTimeZone);
244 return E_NOTIMPL;
245}
246
247HRESULT Unattended::setTimeZone(const com::Utf8Str &aTimezone)
248{
249 RT_NOREF(aTimezone);
250 return E_NOTIMPL;
251}
252
253HRESULT Unattended::getLocale(com::Utf8Str &aLocale)
254{
255 RT_NOREF(aLocale);
256 return E_NOTIMPL;
257}
258
259HRESULT Unattended::setLocale(const com::Utf8Str &aLocale)
260{
261 RT_NOREF(aLocale);
262 return E_NOTIMPL;
263}
264
265HRESULT Unattended::getLanguage(com::Utf8Str &aLanguage)
266{
267 RT_NOREF(aLanguage);
268 return E_NOTIMPL;
269}
270
271HRESULT Unattended::setLanguage(const com::Utf8Str &aLanguage)
272{
273 RT_NOREF(aLanguage);
274 return E_NOTIMPL;
275}
276
277HRESULT Unattended::getCountry(com::Utf8Str &aCountry)
278{
279 RT_NOREF(aCountry);
280 return E_NOTIMPL;
281}
282
283HRESULT Unattended::setCountry(const com::Utf8Str &aCountry)
284{
285 RT_NOREF(aCountry);
286 return E_NOTIMPL;
287}
288
289HRESULT Unattended::getProxy(com::Utf8Str &aProxy)
290{
291 RT_NOREF(aProxy);
292 return E_NOTIMPL;
293}
294
295HRESULT Unattended::setProxy(const com::Utf8Str &aProxy)
296{
297 RT_NOREF(aProxy);
298 return E_NOTIMPL;
299}
300
301HRESULT Unattended::getPackageSelectionAdjustments(com::Utf8Str &aPackageSelectionAdjustments)
302{
303 RT_NOREF(aPackageSelectionAdjustments);
304 return E_NOTIMPL;
305}
306
307HRESULT Unattended::setPackageSelectionAdjustments(const com::Utf8Str &aPackageSelectionAdjustments)
308{
309 RT_NOREF(aPackageSelectionAdjustments);
310 return E_NOTIMPL;
311}
312
313HRESULT Unattended::getHostname(com::Utf8Str &aHostname)
314{
315 RT_NOREF(aHostname);
316 return E_NOTIMPL;
317}
318
319HRESULT Unattended::setHostname(const com::Utf8Str &aHostname)
320{
321 RT_NOREF(aHostname);
322 return E_NOTIMPL;
323}
324
325HRESULT Unattended::getAuxiliaryBasePath(com::Utf8Str &aAuxiliaryBasePath)
326{
327 RT_NOREF(aAuxiliaryBasePath);
328 return E_NOTIMPL;
329}
330
331HRESULT Unattended::setAuxiliaryBasePath(const com::Utf8Str &aAuxiliaryBasePath)
332{
333 RT_NOREF(aAuxiliaryBasePath);
334 return E_NOTIMPL;
335}
336
337HRESULT Unattended::getImageIndex(ULONG *index)
338{
339 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
340 *index = midxImage;
341 return S_OK;
342}
343
344HRESULT Unattended::setImageIndex(ULONG index)
345{
346 RT_NOREF(index);
347 return E_NOTIMPL;
348}
349
350HRESULT Unattended::getMachine(ComPtr<IMachine> &aMachine)
351{
352 RT_NOREF(aMachine);
353 return E_NOTIMPL;
354}
355
356HRESULT Unattended::setMachine(const ComPtr<IMachine> &aMachine)
357{
358 RT_NOREF(aMachine);
359 return E_NOTIMPL;
360}
361
362HRESULT Unattended::getScriptTemplatePath(com::Utf8Str &aScriptTemplatePath)
363{
364 RT_NOREF(aScriptTemplatePath);
365 return E_NOTIMPL;
366}
367
368HRESULT Unattended::setScriptTemplatePath(const com::Utf8Str &aScriptTemplatePath)
369{
370 RT_NOREF(aScriptTemplatePath);
371 return E_NOTIMPL;
372
373}
374
375HRESULT Unattended::getPostInstallScriptTemplatePath(com::Utf8Str &aPostInstallScriptTemplatePath)
376{
377 RT_NOREF(aPostInstallScriptTemplatePath);
378 return E_NOTIMPL;
379}
380
381HRESULT Unattended::setPostInstallScriptTemplatePath(const com::Utf8Str &aPostInstallScriptTemplatePath)
382{
383 RT_NOREF(aPostInstallScriptTemplatePath);
384 return E_NOTIMPL;
385}
386
387HRESULT Unattended::getPostInstallCommand(com::Utf8Str &aPostInstallCommand)
388{
389 RT_NOREF(aPostInstallCommand);
390 return E_NOTIMPL;
391}
392
393HRESULT Unattended::setPostInstallCommand(const com::Utf8Str &aPostInstallCommand)
394{
395 RT_NOREF(aPostInstallCommand);
396 return E_NOTIMPL;
397}
398
399HRESULT Unattended::getExtraInstallKernelParameters(com::Utf8Str &aExtraInstallKernelParameters)
400{
401 RT_NOREF(aExtraInstallKernelParameters);
402 return E_NOTIMPL;
403}
404
405HRESULT Unattended::setExtraInstallKernelParameters(const com::Utf8Str &aExtraInstallKernelParameters)
406{
407 RT_NOREF(aExtraInstallKernelParameters);
408 return E_NOTIMPL;
409}
410
411HRESULT Unattended::getDetectedOSTypeId(com::Utf8Str &aDetectedOSTypeId)
412{
413 RT_NOREF(aDetectedOSTypeId);
414 return E_NOTIMPL;
415}
416
417HRESULT Unattended::getDetectedOSVersion(com::Utf8Str &aDetectedOSVersion)
418{
419 RT_NOREF(aDetectedOSVersion);
420 return E_NOTIMPL;
421}
422
423HRESULT Unattended::getDetectedOSFlavor(com::Utf8Str &aDetectedOSFlavor)
424{
425 RT_NOREF(aDetectedOSFlavor);
426 return E_NOTIMPL;
427}
428
429HRESULT Unattended::getDetectedOSLanguages(com::Utf8Str &aDetectedOSLanguages)
430{
431 RT_NOREF(aDetectedOSLanguages);
432 return E_NOTIMPL;
433}
434
435HRESULT Unattended::getDetectedOSHints(com::Utf8Str &aDetectedOSHints)
436{
437 RT_NOREF(aDetectedOSHints);
438 return E_NOTIMPL;
439}
440
441/*
442 * Getters that the installer and script classes can use.
443 */
444Utf8Str const &Unattended::i_getIsoPath() const
445{
446 return mStrIsoPath;
447}
448
449Utf8Str const &Unattended::i_getUser() const
450{
451 return mStrUser;
452}
453
454Utf8Str const &Unattended::i_getPassword() const
455{
456 return mStrPassword;
457}
458
459Utf8Str const &Unattended::i_getFullUserName() const
460{
461 return mStrFullUserName.isNotEmpty() ? mStrFullUserName : mStrUser;
462}
463
464Utf8Str const &Unattended::i_getProductKey() const
465{
466 return mStrProductKey;
467}
468
469Utf8Str const &Unattended::i_getProxy() const
470{
471 return mStrProxy;
472}
473
474Utf8Str const &Unattended::i_getAdditionsIsoPath() const
475{
476 return mStrAdditionsIsoPath;
477}
478
479bool Unattended::i_getInstallGuestAdditions() const
480{
481 return mfInstallGuestAdditions;
482}
483
484Utf8Str const &Unattended::i_getValidationKitIsoPath() const
485{
486 return mStrValidationKitIsoPath;
487}
488
489bool Unattended::i_getInstallTestExecService() const
490{
491 return mfInstallTestExecService;
492}
493
494Utf8Str const &Unattended::i_getTimeZone() const
495{
496 return mStrTimeZone;
497}
498
499PCRTTIMEZONEINFO Unattended::i_getTimeZoneInfo() const
500{
501 return mpTimeZoneInfo;
502}
503
504Utf8Str const &Unattended::i_getLocale() const
505{
506 return mStrLocale;
507}
508
509Utf8Str const &Unattended::i_getLanguage() const
510{
511 return mStrLanguage;
512}
513
514Utf8Str const &Unattended::i_getCountry() const
515{
516 return mStrCountry;
517}
518
519bool Unattended::i_isMinimalInstallation() const
520{
521 size_t i = mPackageSelectionAdjustments.size();
522 while (i-- > 0)
523 if (mPackageSelectionAdjustments[i].equals("minimal"))
524 return true;
525 return false;
526}
527
528Utf8Str const &Unattended::i_getHostname() const
529{
530 return mStrHostname;
531}
532
533Utf8Str const &Unattended::i_getAuxiliaryBasePath() const
534{
535 return mStrAuxiliaryBasePath;
536}
537
538ULONG Unattended::i_getImageIndex() const
539{
540 return midxImage;
541}
542
543Utf8Str const &Unattended::i_getScriptTemplatePath() const
544{
545 return mStrScriptTemplatePath;
546}
547
548Utf8Str const &Unattended::i_getPostInstallScriptTemplatePath() const
549{
550 return mStrPostInstallScriptTemplatePath;
551}
552
553Utf8Str const &Unattended::i_getPostInstallCommand() const
554{
555 return mStrPostInstallCommand;
556}
557
558Utf8Str const &Unattended::i_getAuxiliaryInstallDir() const
559{
560 static Utf8Str s_strAuxInstallDir("/aux/install/dir");
561 return s_strAuxInstallDir;
562}
563
564Utf8Str const &Unattended::i_getExtraInstallKernelParameters() const
565{
566 return mStrExtraInstallKernelParameters;
567}
568
569bool Unattended::i_isRtcUsingUtc() const
570{
571 return mfRtcUseUtc;
572}
573
574bool Unattended::i_isGuestOs64Bit() const
575{
576 return mfGuestOs64Bit;
577}
578
579bool Unattended::i_isFirmwareEFI() const
580{
581 return menmFirmwareType != FirmwareType_BIOS;
582}
583
584Utf8Str const &Unattended::i_getDetectedOSVersion()
585{
586 return mStrDetectedOSVersion;
587}
588
589
590/*********************************************************************************************************************************
591* The Testcase *
592*********************************************************************************************************************************/
593
594static bool loadFileAsString(const char *pszFilename, Utf8Str &rstrContent)
595{
596 rstrContent.setNull();
597
598 char szPath[RTPATH_MAX];
599 RTTESTI_CHECK_RC_RET(RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS, false);
600 RTTESTI_CHECK_RC_RET(RTPathAppend(szPath, sizeof(szPath), pszFilename), VINF_SUCCESS, false);
601
602 RTFILE hFile;
603 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE), VINF_SUCCESS, false);
604
605 uint64_t cbFile = 0;
606 RTTESTI_CHECK_RC_RET(RTFileQuerySize(hFile, &cbFile), VINF_SUCCESS, false);
607
608 rstrContent.reserve((size_t)cbFile + 1);
609 RTTESTI_CHECK_RC_RET(RTFileRead(hFile, rstrContent.mutableRaw(), (size_t)cbFile, NULL), VINF_SUCCESS, false);
610 rstrContent.mutableRaw()[cbFile] = '\0';
611 rstrContent.jolt();
612
613 RTTESTI_CHECK_RC_RET(RTFileClose(hFile), VINF_SUCCESS, false);
614
615 return true;
616}
617
618static void doTest1()
619{
620 RTTestISub("tstUnattendedScript-1.template");
621
622 /* Create the parent class instance: */
623 ComObjPtr<Unattended> ptrParent;
624 HRESULT hrc = ptrParent.createObject();
625 RTTESTI_CHECK_MSG_RETV(SUCCEEDED(hrc), ("hrc=%Rhrc\n", hrc));
626
627 /* Instantiate the script editor. */
628 UnattendedScriptTemplate Tmpl(ptrParent, "template.ext", "file.ext");
629#define CHECK_HRESULT(a_Expr) do { \
630 HRESULT hrcThis = a_Expr; \
631 if (SUCCEEDED(hrcThis)) break; \
632 RTTestIFailed("line %d: %s -> %Rhrc", __LINE__, #a_Expr, hrcThis); \
633 GlueHandleComError(ptrParent, NULL, hrcThis, NULL, __LINE__); \
634 } while (0)
635
636 /* Load the exercise script. */
637 char szPath[RTPATH_MAX];
638 RTTESTI_CHECK_RC_RETV(RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
639 RTTESTI_CHECK_RC_RETV(RTPathAppend(szPath, sizeof(szPath), "tstUnattendedScript-1.template"), VINF_SUCCESS);
640 CHECK_HRESULT(Tmpl.read(szPath));
641
642 /* Save the template to string. */
643 Utf8Str strActual;
644 CHECK_HRESULT(Tmpl.saveToString(strActual));
645
646 /* Load the expected result. */
647 Utf8Str strExpected;
648 RTTESTI_CHECK_RETV(loadFileAsString("tstUnattendedScript-1.expected", strExpected));
649
650 /* Compare the two. */
651 if (strExpected != strActual)
652 {
653 RTTestIFailed("Output does not match tstUnattendedScript-1.expect!");
654 RTTestIFailureDetails("------ BEGIN OUTPUT ------\n");
655 RTStrmWrite(g_pStdErr, strActual.c_str(), strActual.length());
656 RTTestIFailureDetails("------- END OUTPUT -------\n");
657
658 RTCList<RTCString, RTCString *> const lstActual = strActual.split("\n");
659 RTCList<RTCString, RTCString *> const lstExpected = strExpected.split("\n");
660 size_t const cLines = RT_MIN(lstActual.size(), lstExpected.size());
661 for (size_t i = 0; i < cLines; i++)
662 if (lstActual[i] != lstExpected[i])
663 {
664 RTTestIFailureDetails("First difference on line %u:\n%s\nexpected:\n%s\n",
665 i + 1, lstActual[i].c_str(), lstExpected[i].c_str());
666 break;
667 }
668 }
669}
670
671int main()
672{
673 RTTEST hTest;
674 RTEXITCODE rcExit = RTTestInitAndCreate("tstUnattendedScript", &hTest);
675 if (rcExit != RTEXITCODE_SUCCESS)
676 return rcExit;
677
678#ifdef RT_OS_WINDOWS
679 /*ATL::CComModule *g_pAtlComModule = */ new(ATL::CComModule);
680#endif
681
682 doTest1();
683
684 return RTTestSummaryAndDestroy(hTest);
685}
686
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