VirtualBox

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

Last change on this file since 97698 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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