summaryrefslogtreecommitdiffstats
path: root/src/VBox/Frontends/VBoxHeadless/testcase/tstHeadless.cpp
blob: 180a952e68b82b20024e84fc314a13c626027a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/** @file
 *
 * VBox frontends: VBoxHeadless frontend:
 * Testcases
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <VBox/com/array.h>
#include <VBox/com/com.h>
#include <VBox/com/string.h>
#include <VBox/com/Guid.h>
#include <VBox/com/ErrorInfo.h>
#include <VBox/com/errorprint.h>

#include <VBox/com/VirtualBox.h>

using namespace com;

#include <VBox/log.h>
#include <iprt/initterm.h>
#include <iprt/stream.h>


////////////////////////////////////////////////////////////////////////////////

/**
 *  Entry point.
 */
int main(int argc, char **argv)
{
    // initialize VBox Runtime
    RTR3InitExe(argc, &argv, 0);

    // the below cannot be Bstr because on Linux Bstr doesn't work
    // until XPCOM (nsMemory) is initialized
    const char *name = NULL;
    const char *operation = NULL;

    // parse the command line
    if (argc > 1)
        name = argv [1];
    if (argc > 2)
        operation = argv [2];

    if (!name || !operation)
    {
        RTPrintf("\nUsage:\n\n"
                 "%s <machine_name> [on|off|pause|resume]\n\n",
                 argv [0]);
        return 0;
    }

    RTPrintf("\n");
    RTPrintf("tstHeadless STARTED.\n");

    RTPrintf("VM name   : {%s}\n"
             "Operation : %s\n\n",
             name, operation);

    HRESULT hrc;

    hrc = com::Initialize();
    if (FAILED(hrc))
    {
        RTPrintf("ERROR: failed to initialize COM!\n");
        return hrc;
    }

    do
    {
        ComPtr <IVirtualBoxClient> virtualBoxClient;
        ComPtr <IVirtualBox> virtualBox;
        ComPtr <ISession> session;

        RTPrintf("Creating VirtualBox object...\n");
        hrc = virtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
        if (SUCCEEDED(hrc))
            hrc = virtualBoxClient->COMGETTER(VirtualBox)(virtualBox.asOutParam());
        if (FAILED(hrc))
            RTPrintf("ERROR: failed to create the VirtualBox object!\n");
        else
        {
            hrc = session.createInprocObject(CLSID_Session);
            if (FAILED(hrc))
                RTPrintf("ERROR: failed to create a session object!\n");
        }

        if (FAILED(hrc))
        {
            com::ErrorInfo info;
            if (!info.isFullAvailable() && !info.isBasicAvailable())
            {
                com::GluePrintRCMessage(hrc);
                RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
            }
            else
                com::GluePrintErrorInfo(info);
            break;
        }

        ComPtr <IMachine> m;

        // find ID by name
        CHECK_ERROR_BREAK(virtualBox, FindMachine(Bstr(name).raw(),
                                                  m.asOutParam()));

        if (!strcmp(operation, "on"))
        {
            ComPtr <IProgress> progress;
            RTPrintf("Opening a new (remote) session...\n");
            CHECK_ERROR_BREAK(m,
                              LaunchVMProcess(session, Bstr("vrdp").raw(),
                                              ComSafeArrayNullInParam(), progress.asOutParam()));

            RTPrintf("Waiting for the remote session to open...\n");
            CHECK_ERROR_BREAK(progress, WaitForCompletion(-1));

            BOOL completed;
            CHECK_ERROR_BREAK(progress, COMGETTER(Completed)(&completed));
            ASSERT(completed);

            LONG resultCode;
            CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&resultCode));
            if (FAILED(resultCode))
            {
                ProgressErrorInfo info(progress);
                com::GluePrintErrorInfo(info);
            }
            else
            {
                RTPrintf("Remote session has been successfully opened.\n");
            }
        }
        else
        {
            RTPrintf("Opening an existing session...\n");
            CHECK_ERROR_BREAK(m, LockMachine(session, LockType_Shared));

            ComPtr <IConsole> console;
            CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));

            if (!strcmp(operation, "off"))
            {
                ComPtr <IProgress> progress;
                RTPrintf("Powering the VM off...\n");
                CHECK_ERROR_BREAK(console, PowerDown(progress.asOutParam()));

                RTPrintf("Waiting for the VM to power down...\n");
                CHECK_ERROR_BREAK(progress, WaitForCompletion(-1));

                BOOL completed;
                CHECK_ERROR_BREAK(progress, COMGETTER(Completed)(&completed));
                ASSERT(completed);

                LONG resultCode;
                CHECK_ERROR_BREAK(progress, COMGETTER(ResultCode)(&resultCode));
                if (FAILED(resultCode))
                {
                    ProgressErrorInfo info(progress);
                    com::GluePrintErrorInfo(info);
                }
                else
                {
                    RTPrintf("VM is powered down.\n");
                }
            }
            else
            if (!strcmp(operation, "pause"))
            {
                RTPrintf("Pausing the VM...\n");
                CHECK_ERROR_BREAK(console, Pause());
            }
            else
            if (!strcmp(operation, "resume"))
            {
                RTPrintf("Resuming the VM...\n");
                CHECK_ERROR_BREAK(console, Resume());
            }
            else
            {
                RTPrintf("Invalid operation!\n");
            }
        }

        RTPrintf("Closing the session (may fail after power off)...\n");
        CHECK_ERROR(session, UnlockMachine());
    }
    while (0);
    RTPrintf("\n");

    com::Shutdown();

    RTPrintf("tstHeadless FINISHED.\n");

    return SUCCEEDED(hrc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
}