tstHeadless.cpp revision bd8e360cd1db83dcb2694ea9122ce3bc5bae678a
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/** @file
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * VBox frontends: VBoxHeadless frontend:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Testcases
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Copyright (C) 2006-2007 innotek GmbH
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This file is part of VirtualBox Open Source Edition (OSE), as
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * available from http://www.virtualbox.org. This file is free software;
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov * you can redistribute it and/or modify it under the terms of the GNU
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov * General Public License (GPL) as published by the Free Software
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov * Foundation, in version 2 as it comes in the "COPYING" file of the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <VBox/com/com.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <VBox/com/string.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <VBox/com/Guid.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <VBox/com/ErrorInfo.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <VBox/com/EventQueue.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <VBox/com/VirtualBox.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweusing namespace com;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <iprt/runtime.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <iprt/stream.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe////////////////////////////////////////////////////////////////////////////////
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/**
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Entry point.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint main (int argc, char **argv)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // initialize VBox Runtime
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTR3Init();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // the below cannot be Bstr because on Linux Bstr doesn't work
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // until XPCOM (nsMemory) is initialized
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe const char *name = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe const char *operation = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // parse the command line
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (argc > 1)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name = argv [1];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (argc > 2)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe operation = argv [2];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!name || !operation)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("\nUsage:\n\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "%s <machine_name> [on|off|pause|resume]\n\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe argv [0]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return -1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("tstHeadless STARTED.\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("VM name : {%s}\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "Operation : %s\n\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name, operation);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe HRESULT rc;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_RC_RET (com::Initialize());
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ComPtr <IVirtualBox> virtualBox;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ComPtr <ISession> session;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Creating VirtualBox object...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_NI_BREAK (virtualBox.createLocalObject (CLSID_VirtualBox));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Creating Session object...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_NI_BREAK (session.createInprocObject (CLSID_Session));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // create the event queue
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // (here it is necessary only to process remaining XPCOM/IPC events
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // after the session is closed)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe EventQueue eventQ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe // find ID by name
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe Guid id;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ComPtr <IMachine> m;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (virtualBox, FindMachine (Bstr (name), m.asOutParam()));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (m, COMGETTER(Id) (id.asOutParam()));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!strcmp (operation, "on"))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ComPtr <IProgress> progress;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Opening a new (remote) session...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (virtualBox,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe OpenRemoteSession (session, id, Bstr ("vrdp"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe NULL, progress.asOutParam()));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Waiting for the remote session to open...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe BOOL completed;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ASSERT (completed);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe HRESULT resultCode;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (FAILED (resultCode))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ComPtr <IVirtualBoxErrorInfo> errorInfo;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (progress,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe COMGETTER(ErrorInfo) (errorInfo.asOutParam()));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ErrorInfo info (errorInfo);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe PRINT_ERROR_INFO (info);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Remote session has been successfully opened.\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Opening an existing session...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (virtualBox, OpenExistingSession (session, id));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ComPtr <IConsole> console;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (session, COMGETTER (Console) (console.asOutParam()));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!strcmp (operation, "off"))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Powering the VM off...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (console, PowerDown());
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!strcmp (operation, "pause"))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Pausing the VM...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (console, Pause());
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!strcmp (operation, "resume"))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Resuming the VM...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR_BREAK (console, Resume());
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Invalid operation!\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("Closing the session (may fail after power off)...\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe CHECK_ERROR (session, Close());
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe while (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe com::Shutdown();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe RTPrintf ("tstHeadless FINISHED.\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return rc;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe