VirtualBox

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

Last change on this file since 93902 was 93535, checked in by vboxsync, 3 years ago

Main/Unattended: bugref:9781. Build fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
Line 
1/* $Id: tstUnattendedScript.cpp 93535 2022-02-01 16:56:55Z 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
441HRESULT Unattended::getDetectedImageNames(std::vector<com::Utf8Str> &aDetectedImageNames)
442{
443 RT_NOREF(aDetectedImageNames);
444 return E_NOTIMPL;
445}
446
447HRESULT Unattended::getDetectedImageIndices(std::vector<ULONG> &aDetectedImageIndices)
448{
449 RT_NOREF(aDetectedImageIndices);
450 return E_NOTIMPL;
451}
452
453/*
454 * Getters that the installer and script classes can use.
455 */
456Utf8Str const &Unattended::i_getIsoPath() const
457{
458 return mStrIsoPath;
459}
460
461Utf8Str const &Unattended::i_getUser() const
462{
463 return mStrUser;
464}
465
466Utf8Str const &Unattended::i_getPassword() const
467{
468 return mStrPassword;
469}
470
471Utf8Str const &Unattended::i_getFullUserName() const
472{
473 return mStrFullUserName.isNotEmpty() ? mStrFullUserName : mStrUser;
474}
475
476Utf8Str const &Unattended::i_getProductKey() const
477{
478 return mStrProductKey;
479}
480
481Utf8Str const &Unattended::i_getProxy() const
482{
483 return mStrProxy;
484}
485
486Utf8Str const &Unattended::i_getAdditionsIsoPath() const
487{
488 return mStrAdditionsIsoPath;
489}
490
491bool Unattended::i_getInstallGuestAdditions() const
492{
493 return mfInstallGuestAdditions;
494}
495
496Utf8Str const &Unattended::i_getValidationKitIsoPath() const
497{
498 return mStrValidationKitIsoPath;
499}
500
501bool Unattended::i_getInstallTestExecService() const
502{
503 return mfInstallTestExecService;
504}
505
506Utf8Str const &Unattended::i_getTimeZone() const
507{
508 return mStrTimeZone;
509}
510
511PCRTTIMEZONEINFO Unattended::i_getTimeZoneInfo() const
512{
513 return mpTimeZoneInfo;
514}
515
516Utf8Str const &Unattended::i_getLocale() const
517{
518 return mStrLocale;
519}
520
521Utf8Str const &Unattended::i_getLanguage() const
522{
523 return mStrLanguage;
524}
525
526Utf8Str const &Unattended::i_getCountry() const
527{
528 return mStrCountry;
529}
530
531bool Unattended::i_isMinimalInstallation() const
532{
533 size_t i = mPackageSelectionAdjustments.size();
534 while (i-- > 0)
535 if (mPackageSelectionAdjustments[i].equals("minimal"))
536 return true;
537 return false;
538}
539
540Utf8Str const &Unattended::i_getHostname() const
541{
542 return mStrHostname;
543}
544
545Utf8Str const &Unattended::i_getAuxiliaryBasePath() const
546{
547 return mStrAuxiliaryBasePath;
548}
549
550ULONG Unattended::i_getImageIndex() const
551{
552 return midxImage;
553}
554
555Utf8Str const &Unattended::i_getScriptTemplatePath() const
556{
557 return mStrScriptTemplatePath;
558}
559
560Utf8Str const &Unattended::i_getPostInstallScriptTemplatePath() const
561{
562 return mStrPostInstallScriptTemplatePath;
563}
564
565Utf8Str const &Unattended::i_getPostInstallCommand() const
566{
567 return mStrPostInstallCommand;
568}
569
570Utf8Str const &Unattended::i_getAuxiliaryInstallDir() const
571{
572 static Utf8Str s_strAuxInstallDir("/aux/install/dir");
573 return s_strAuxInstallDir;
574}
575
576Utf8Str const &Unattended::i_getExtraInstallKernelParameters() const
577{
578 return mStrExtraInstallKernelParameters;
579}
580
581bool Unattended::i_isRtcUsingUtc() const
582{
583 return mfRtcUseUtc;
584}
585
586bool Unattended::i_isGuestOs64Bit() const
587{
588 return mfGuestOs64Bit;
589}
590
591bool Unattended::i_isFirmwareEFI() const
592{
593 return menmFirmwareType != FirmwareType_BIOS;
594}
595
596Utf8Str const &Unattended::i_getDetectedOSVersion()
597{
598 return mStrDetectedOSVersion;
599}
600
601
602/*********************************************************************************************************************************
603* The Testcase *
604*********************************************************************************************************************************/
605
606static bool loadFileAsString(const char *pszFilename, Utf8Str &rstrContent)
607{
608 rstrContent.setNull();
609
610 char szPath[RTPATH_MAX];
611 RTTESTI_CHECK_RC_RET(RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS, false);
612 RTTESTI_CHECK_RC_RET(RTPathAppend(szPath, sizeof(szPath), pszFilename), VINF_SUCCESS, false);
613
614 RTFILE hFile;
615 RTTESTI_CHECK_RC_RET(RTFileOpen(&hFile, szPath, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE), VINF_SUCCESS, false);
616
617 uint64_t cbFile = 0;
618 RTTESTI_CHECK_RC_RET(RTFileQuerySize(hFile, &cbFile), VINF_SUCCESS, false);
619
620 rstrContent.reserve((size_t)cbFile + 1);
621 RTTESTI_CHECK_RC_RET(RTFileRead(hFile, rstrContent.mutableRaw(), (size_t)cbFile, NULL), VINF_SUCCESS, false);
622 rstrContent.mutableRaw()[cbFile] = '\0';
623 rstrContent.jolt();
624
625 RTTESTI_CHECK_RC_RET(RTFileClose(hFile), VINF_SUCCESS, false);
626
627 return true;
628}
629
630static void doTest1()
631{
632 RTTestISub("tstUnattendedScript-1.template");
633
634 /* Create the parent class instance: */
635 ComObjPtr<Unattended> ptrParent;
636 HRESULT hrc = ptrParent.createObject();
637 RTTESTI_CHECK_MSG_RETV(SUCCEEDED(hrc), ("hrc=%Rhrc\n", hrc));
638
639 /* Instantiate the script editor. */
640 UnattendedScriptTemplate Tmpl(ptrParent, "template.ext", "file.ext");
641#define CHECK_HRESULT(a_Expr) do { \
642 HRESULT hrcThis = a_Expr; \
643 if (SUCCEEDED(hrcThis)) break; \
644 RTTestIFailed("line %d: %s -> %Rhrc", __LINE__, #a_Expr, hrcThis); \
645 GlueHandleComError(ptrParent, NULL, hrcThis, NULL, __LINE__); \
646 } while (0)
647
648 /* Load the exercise script. */
649 char szPath[RTPATH_MAX];
650 RTTESTI_CHECK_RC_RETV(RTPathExecDir(szPath, sizeof(szPath)), VINF_SUCCESS);
651 RTTESTI_CHECK_RC_RETV(RTPathAppend(szPath, sizeof(szPath), "tstUnattendedScript-1.template"), VINF_SUCCESS);
652 CHECK_HRESULT(Tmpl.read(szPath));
653
654 /* Save the template to string. */
655 Utf8Str strActual;
656 CHECK_HRESULT(Tmpl.saveToString(strActual));
657
658 /* Load the expected result. */
659 Utf8Str strExpected;
660 RTTESTI_CHECK_RETV(loadFileAsString("tstUnattendedScript-1.expected", strExpected));
661
662 /* Compare the two. */
663 if (strExpected != strActual)
664 {
665 RTTestIFailed("Output does not match tstUnattendedScript-1.expect!");
666 RTTestIFailureDetails("------ BEGIN OUTPUT ------\n");
667 RTStrmWrite(g_pStdErr, strActual.c_str(), strActual.length());
668 RTTestIFailureDetails("------- END OUTPUT -------\n");
669
670 RTCList<RTCString, RTCString *> const lstActual = strActual.split("\n");
671 RTCList<RTCString, RTCString *> const lstExpected = strExpected.split("\n");
672 size_t const cLines = RT_MIN(lstActual.size(), lstExpected.size());
673 for (size_t i = 0; i < cLines; i++)
674 if (lstActual[i] != lstExpected[i])
675 {
676 RTTestIFailureDetails("First difference on line %u:\n%s\nexpected:\n%s\n",
677 i + 1, lstActual[i].c_str(), lstExpected[i].c_str());
678 break;
679 }
680 }
681}
682
683int main()
684{
685 RTTEST hTest;
686 RTEXITCODE rcExit = RTTestInitAndCreate("tstUnattendedScript", &hTest);
687 if (rcExit != RTEXITCODE_SUCCESS)
688 return rcExit;
689
690#ifdef RT_OS_WINDOWS
691 /*ATL::CComModule *g_pAtlComModule = */ new(ATL::CComModule);
692#endif
693
694 doTest1();
695
696 return RTTestSummaryAndDestroy(hTest);
697}
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