1 | /* $Id: tstRTProcQueryUsername.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTProcQueryUsername.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-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/initterm.h>
|
---|
32 | #include <iprt/err.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/process.h>
|
---|
35 | #include <iprt/test.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | static void tstRTProcQueryUsername(void)
|
---|
39 | {
|
---|
40 | RTTestISub("Basics");
|
---|
41 |
|
---|
42 | size_t cbUser;
|
---|
43 | char szUser[1024];
|
---|
44 | memset(szUser, '-', sizeof(szUser));
|
---|
45 |
|
---|
46 | /* negative stuff that may assert: */
|
---|
47 | bool fMayPanic = RTAssertSetMayPanic(false);
|
---|
48 | bool fQuiet = RTAssertSetQuiet(true);
|
---|
49 |
|
---|
50 | RTTESTI_CHECK_RC(RTProcQueryUsername(RTProcSelf(), NULL, 8, &cbUser), VERR_INVALID_PARAMETER);
|
---|
51 | RTTESTI_CHECK_RC(RTProcQueryUsername(RTProcSelf(), szUser, 0, &cbUser), VERR_INVALID_PARAMETER);
|
---|
52 | RTTESTI_CHECK_RC(RTProcQueryUsername(RTProcSelf(), NULL, 0, NULL), VERR_INVALID_PARAMETER);
|
---|
53 | RTTESTI_CHECK_RC(RTProcQueryUsernameA(RTProcSelf(), NULL), VERR_INVALID_POINTER);
|
---|
54 |
|
---|
55 | RTAssertSetMayPanic(fMayPanic);
|
---|
56 | RTAssertSetQuiet(fQuiet);
|
---|
57 |
|
---|
58 | RTTESTI_CHECK_RC(RTProcQueryUsername(RTProcSelf(), NULL, 0, &cbUser), VERR_BUFFER_OVERFLOW);
|
---|
59 | memset(szUser, '-', sizeof(szUser));
|
---|
60 | RTTESTI_CHECK_RC(RTProcQueryUsername(RTProcSelf(), szUser, cbUser - 1, &cbUser), VERR_BUFFER_OVERFLOW);
|
---|
61 | memset(szUser, '-', sizeof(szUser));
|
---|
62 | RTTESTI_CHECK_RC(RTProcQueryUsername(RTProcSelf(), szUser, sizeof(szUser), &cbUser), VINF_SUCCESS);
|
---|
63 | RTTestPrintf(NULL, RTTESTLVL_ALWAYS, "Username: %s\n", szUser); /* */
|
---|
64 |
|
---|
65 | char *pszUser = NULL;
|
---|
66 | RTTESTI_CHECK_RC(RTProcQueryUsernameA(RTProcSelf(), &pszUser), VINF_SUCCESS);
|
---|
67 | RTTestPrintf(NULL, RTTESTLVL_ALWAYS, "Username: %s\n", pszUser);
|
---|
68 | RTTESTI_CHECK(strcmp(pszUser, szUser) == 0);
|
---|
69 | RTStrFree(pszUser);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | int main(int argc, char **argv)
|
---|
74 | {
|
---|
75 | /*
|
---|
76 | * Initialize IPRT and create the test.
|
---|
77 | */
|
---|
78 | RTTEST hTest;
|
---|
79 | int rc = RTTestInitAndCreate("tstRTProcQueryUsername", &hTest);
|
---|
80 | if (rc)
|
---|
81 | return rc;
|
---|
82 | RTTestBanner(hTest);
|
---|
83 |
|
---|
84 | tstRTProcQueryUsername();
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Summary.
|
---|
88 | */
|
---|
89 | return RTTestSummaryAndDestroy(hTest);
|
---|
90 | }
|
---|