VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTProcIsRunningByName.cpp@ 67808

Last change on this file since 67808 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/* $Id: tstRTProcIsRunningByName.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTProcIsRunningByName
4 */
5
6/*
7 * Copyright (C) 2009-2016 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/process.h>
32#include <iprt/initterm.h>
33#include <iprt/stream.h>
34#include <iprt/err.h>
35#include <iprt/param.h>
36#include <iprt/path.h>
37#include <iprt/string.h>
38
39
40
41int main(int argc, char **argv)
42{
43 int cErrors = 0;
44
45 RTR3InitExe(argc, &argv, 0);
46 RTPrintf("tstRTPRocIsRunningByName: TESTING...\n");
47
48 /*
49 * Test 1: Check for a definitely not running process.
50 */
51 char szExecPath[RTPATH_MAX] = { "vbox-5b05e1ff-6ae2-4d10-885a-7d25018c4c5b" };
52 if (!RTProcIsRunningByName(szExecPath))
53 RTPrintf("tstRTProcIsRunningByName: Process '%s' is not running (expected).\n", szExecPath);
54 else
55 {
56 RTPrintf("tstRTProcIsRunningByName: FAILURE - '%s' is running! (test 1)\n", szExecPath);
57 cErrors++;
58 }
59
60 /*
61 * Test 2: Check for a definitely not running process.
62 */
63 strcpy(szExecPath, "/bin/vbox-5b05e1ff-6ae2-4d10-885a-7d25018c4c5b");
64 if (!RTProcIsRunningByName(szExecPath))
65 RTPrintf("tstRTProcIsRunningByName: Process '%s' is not running (expected).\n", szExecPath);
66 else
67 {
68 RTPrintf("tstRTProcIsRunningByName: FAILURE - '%s' is running! (test 1)\n", szExecPath);
69 cErrors++;
70 }
71
72 /*
73 * Test 3: Check for our own process, filename only.
74 */
75 if (RTProcGetExecutablePath(szExecPath, RTPATH_MAX))
76 {
77 /* Strip any path components */
78 char *pszFilename = RTPathFilename(szExecPath);
79 if (pszFilename)
80 {
81 if (RTProcIsRunningByName(pszFilename))
82 RTPrintf("tstRTProcIsRunningByName: Process '%s' (self) is running\n", pszFilename);
83 else
84 {
85 RTPrintf("tstRTProcIsRunningByName: FAILURE - Process '%s' (self) is not running!\n", pszFilename);
86 cErrors++;
87 }
88 }
89 else
90 {
91 RTPrintf("tstRTProcIsRunningByName: FAILURE - RTPathFilename failed!\n");
92 cErrors++;
93 }
94
95 /*
96 * Test 4: Check for our own process, full path.
97 */
98 if (RTProcIsRunningByName(szExecPath))
99 RTPrintf("tstRTProcIsRunningByName: Process '%s' (self) is running\n", szExecPath);
100 else
101 {
102 RTPrintf("tstRTProcIsRunningByName: FAILURE - Process '%s' (self) is not running!\n", szExecPath);
103 cErrors++;
104 }
105 }
106 else
107 {
108 RTPrintf("tstRTProcIsRunningByName: FAILURE - RTProcGetExecutablePath failed!\n");
109 cErrors++;
110 }
111
112
113 /*
114 * Summary.
115 */
116 if (!cErrors)
117 RTPrintf("tstRTProcIsRunningByName: SUCCESS\n");
118 else
119 RTPrintf("tstRTProcIsRunningByName: FAILURE - %d errors\n", cErrors);
120 return cErrors ? 1 : 0;
121}
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