tstPath.cpp revision 44368d5e83c7ba97ad7763a1ad193cc4f739b76d
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync/* $Id$ */
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync/** @file
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * IPRT Testcase - Test various path functions.
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync */
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync/*
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync *
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * available from http://www.virtualbox.org. This file is free software;
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * General Public License (GPL) as published by the Free Software
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync *
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * The contents of this file may alternatively be used under the terms
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * of the Common Development and Distribution License Version 1.0
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * CDDL are applicable instead of those of the GPL.
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync *
b8ae33177906f4130b85b7c861cae0e78ed4859bvboxsync * You may elect to license modified versions of this file under the
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * terms and conditions of either the GPL or the CDDL or both.
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync *
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync * additional information or have any questions.
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync */
4651430e55b9df9726347e3e3968618e540fe729vboxsync
4651430e55b9df9726347e3e3968618e540fe729vboxsync/*******************************************************************************
4651430e55b9df9726347e3e3968618e540fe729vboxsync* Header Files *
4651430e55b9df9726347e3e3968618e540fe729vboxsync*******************************************************************************/
76f5d3db08b007661ae4af93200838d24683af52vboxsync#include <iprt/path.h>
8a99522dee886d4ed00c8cd18788e9e722febd71vboxsync#include <iprt/process.h>
8a99522dee886d4ed00c8cd18788e9e722febd71vboxsync#include <iprt/initterm.h>
badc6d0a88e64de11285f73127a7e1651b69a00bvboxsync#include <iprt/stream.h>
4651430e55b9df9726347e3e3968618e540fe729vboxsync#include <iprt/string.h>
d5bf937d132098565e18a0d1fc408fb777c5e5b6vboxsync#include <iprt/err.h>
d5bf937d132098565e18a0d1fc408fb777c5e5b6vboxsync#include <iprt/param.h>
d5bf937d132098565e18a0d1fc408fb777c5e5b6vboxsync
21293ca6a2967f796ed30a6c8492911619476a48vboxsync
d5bf937d132098565e18a0d1fc408fb777c5e5b6vboxsync#define CHECK_RC(method) \
35d8d2372afb180daa0ba01af33a1922511d6b07vboxsync do { \
d5bf937d132098565e18a0d1fc408fb777c5e5b6vboxsync rc = method; \
d5bf937d132098565e18a0d1fc408fb777c5e5b6vboxsync if (RT_FAILURE(rc)) \
4651430e55b9df9726347e3e3968618e540fe729vboxsync { \
4651430e55b9df9726347e3e3968618e540fe729vboxsync cErrors++; \
025f9c7729f643fb8780153dc0e6f1c76baddd69vboxsync RTPrintf("\ntstPath: FAILED calling " #method " at line %d: rc=%Rrc\n", __LINE__, rc); \
4651430e55b9df9726347e3e3968618e540fe729vboxsync } \
4651430e55b9df9726347e3e3968618e540fe729vboxsync } while (0)
4651430e55b9df9726347e3e3968618e540fe729vboxsync
4651430e55b9df9726347e3e3968618e540fe729vboxsyncint main()
4651430e55b9df9726347e3e3968618e540fe729vboxsync{
4651430e55b9df9726347e3e3968618e540fe729vboxsync /*
4651430e55b9df9726347e3e3968618e540fe729vboxsync * Init RT.
4651430e55b9df9726347e3e3968618e540fe729vboxsync */
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync int rc;
eeb2d581c99b451fb1a26a5870af65981dc9cd5dvboxsync int cErrors = 0;
0ce6ae9d6efed5d54222a13bbdabce9e688e4447vboxsync CHECK_RC(RTR3Init());
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync if (RT_FAILURE(rc))
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync return 1;
e07acfb7f2dbb8bb40804024c79fd3139bdb3f24vboxsync
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync /*
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync * RTPathProgram, RTPathUserHome and RTProcGetExecutableName.
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync */
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync char szPath[RTPATH_MAX];
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync CHECK_RC(RTPathProgram(szPath, sizeof(szPath)));
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync if (RT_SUCCESS(rc))
c7a378ed2fbad681c0b674351d698ef20a368935vboxsync RTPrintf("Program={%s}\n", szPath);
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync CHECK_RC(RTPathUserHome(szPath, sizeof(szPath)));
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync if (RT_SUCCESS(rc))
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync RTPrintf("UserHome={%s}\n", szPath);
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync if (RTProcGetExecutableName(szPath, sizeof(szPath)) == szPath)
f2490dbf97c8247439446844458461b00e50beb0vboxsync RTPrintf("ExecutableName={%s}\n", szPath);
f2490dbf97c8247439446844458461b00e50beb0vboxsync else
f2490dbf97c8247439446844458461b00e50beb0vboxsync {
f2490dbf97c8247439446844458461b00e50beb0vboxsync RTPrintf("tstPath: FAILED - RTProcGetExecutableName\n");
f2490dbf97c8247439446844458461b00e50beb0vboxsync cErrors++;
f2490dbf97c8247439446844458461b00e50beb0vboxsync }
f2490dbf97c8247439446844458461b00e50beb0vboxsync
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync
c7a378ed2fbad681c0b674351d698ef20a368935vboxsync /*
c7a378ed2fbad681c0b674351d698ef20a368935vboxsync * RTPathAbsEx
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync */
e52c9984c4f6b8ea141bc50a3bdaf31941d8cd7dvboxsync RTPrintf("tstPath: TESTING RTPathAbsEx()\n");
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync static const char *aInput[] =
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync {
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync // NULL, NULL, -- assertion in RTStrToUtf16
ee5869bb73f07de9c139dc6a608836a7b5767933vboxsync NULL, "/absolute/..",
f6dde23cd488aee26203b38ec67220270f5c4273vboxsync NULL, "/absolute\\\\../..",
f6dde23cd488aee26203b38ec67220270f5c4273vboxsync NULL, "/absolute//../path",
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync NULL, "/absolute/../../path",
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync NULL, "relative/../dir\\.\\.\\.\\file.txt",
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync NULL, "\\",
dc311b529996345ea65d51c87b60b6849642d894vboxsync "relative_base/dir\\", "\\from_root",
b8ae33177906f4130b85b7c861cae0e78ed4859bvboxsync "relative_base/dir/", "relative_also",
ee5869bb73f07de9c139dc6a608836a7b5767933vboxsync#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
e52c9984c4f6b8ea141bc50a3bdaf31941d8cd7dvboxsync NULL, "C:\\",
dc311b529996345ea65d51c87b60b6849642d894vboxsync "C:\\", "..",
dc311b529996345ea65d51c87b60b6849642d894vboxsync "C:\\temp", "..",
231051c77670a4142b7545d51bd2251ae603189dvboxsync "C:\\VirtualBox/Machines", "..\\VirtualBox.xml",
dc311b529996345ea65d51c87b60b6849642d894vboxsync "C:\\MustDie", "\\from_root/dir/..",
c4d6c81bd0de216d0f42db2e477b1d788fbaefb4vboxsync "C:\\temp", "D:\\data",
c4d6c81bd0de216d0f42db2e477b1d788fbaefb4vboxsync NULL, "\\\\server\\../share", // -- on Win32, GetFullPathName doesn't remove .. here
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync /* the three below use cases should fail with VERR_INVALID_NAME */
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync //NULL, "\\\\server",
c2029891dc7fbce0f08a378d0335568cf9cbb4c5vboxsync //NULL, "\\\\",
c2029891dc7fbce0f08a378d0335568cf9cbb4c5vboxsync //NULL, "\\\\\\something",
c2029891dc7fbce0f08a378d0335568cf9cbb4c5vboxsync "\\\\server\\share_as_base", "/from_root",
c2029891dc7fbce0f08a378d0335568cf9cbb4c5vboxsync "\\\\just_server", "/from_root",
c2029891dc7fbce0f08a378d0335568cf9cbb4c5vboxsync "\\\\server\\share_as_base", "relative\\data",
c2029891dc7fbce0f08a378d0335568cf9cbb4c5vboxsync "base", "\\\\?\\UNC\\relative/edwef/..",
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync "base", "\\\\?\\UNC\\relative/edwef/..",
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync /* this is not (and I guess should not be) supported, should fail */
482a222498aae619afda0255a7edf4ebda97268cvboxsync ///@todo "\\\\?\\UNC\\base", "/from_root",
d19316699d7f91959d88c850fd7e0d64840f39a7vboxsync#else
482a222498aae619afda0255a7edf4ebda97268cvboxsync "\\temp", "..",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "\\VirtualBox/Machines", "..\\VirtualBox.xml",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "\\MustDie", "\\from_root/dir/..",
d020036eb167a2f97f5010276fda5daa3f4773b3vboxsync "\\temp", "\\data",
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync#endif
8184f68cdef3c26869cdebc6a18829c854d0d17dvboxsync };
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync for (unsigned i = 0; i < RT_ELEMENTS(aInput); i += 2)
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync {
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync RTPrintf("tstPath: base={%s}, path={%s}, ", aInput[i], aInput[i + 1]);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync CHECK_RC(RTPathAbsEx(aInput[i], aInput[i + 1], szPath, sizeof(szPath)));
25c5ce7baf4b227ea80250c46d427d0e9fdab4c1vboxsync if (RT_SUCCESS(rc))
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync RTPrintf("abs={%s}\n", szPath);
d91dc86210d602f51a4701762488a5e696c80a45vboxsync }
d91dc86210d602f51a4701762488a5e696c80a45vboxsync
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /*
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync * RTPathStripFilename
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync */
59d96bafa78f7257869e30b88f2f98142ef5928evboxsync RTPrintf("tstPath: RTPathStripFilename...\n");
c4d6c81bd0de216d0f42db2e477b1d788fbaefb4vboxsync static const char *apszStripFilenameTests[] =
35d8d2372afb180daa0ba01af33a1922511d6b07vboxsync {
e07acfb7f2dbb8bb40804024c79fd3139bdb3f24vboxsync "/usr/include///", "/usr/include//",
b8ae33177906f4130b85b7c861cae0e78ed4859bvboxsync "/usr/include/", "/usr/include",
6b9316bfe743cc7d2ee00d925f4ab455bc224e86vboxsync "/usr/include", "/usr",
b8ae33177906f4130b85b7c861cae0e78ed4859bvboxsync "/usr", "/",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "usr", ".",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync#if defined (RT_OS_OS2) || defined (RT_OS_WINDOWS)
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "c:/windows", "c:/",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "c:/", "c:/",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "D:", "D:",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "C:\\OS2\\DLLS", "C:\\OS2",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync#endif
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync };
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync for (unsigned i = 0; i < RT_ELEMENTS(apszStripFilenameTests); i += 2)
badc6d0a88e64de11285f73127a7e1651b69a00bvboxsync {
6b9316bfe743cc7d2ee00d925f4ab455bc224e86vboxsync const char *pszInput = apszStripFilenameTests[i];
b8ae33177906f4130b85b7c861cae0e78ed4859bvboxsync const char *pszExpect = apszStripFilenameTests[i + 1];
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync char szPath[RTPATH_MAX];
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync strcpy(szPath, pszInput);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync RTPathStripFilename(szPath);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync if (strcmp(szPath, pszExpect))
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync {
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync RTPrintf("tstPath: RTPathStripFilename failed!\n"
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync " input: '%s'\n"
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync " output: '%s'\n"
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync "expected: '%s'\n",
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync pszInput, szPath, pszExpect);
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync cErrors++;
badc6d0a88e64de11285f73127a7e1651b69a00bvboxsync }
badc6d0a88e64de11285f73127a7e1651b69a00bvboxsync }
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync /*
d5bf937d132098565e18a0d1fc408fb777c5e5b6vboxsync * Summary.
d76ca6cbea4e45aa218c74e33c784bde5e997ffcvboxsync */
d76ca6cbea4e45aa218c74e33c784bde5e997ffcvboxsync if (!cErrors)
15760030457c4d8548d42a9d543646b27eae7040vboxsync RTPrintf("tstPath: SUCCESS\n");
d76ca6cbea4e45aa218c74e33c784bde5e997ffcvboxsync else
d76ca6cbea4e45aa218c74e33c784bde5e997ffcvboxsync RTPrintf("tstPath: FAILURE %d errors\n", cErrors);
35d8d2372afb180daa0ba01af33a1922511d6b07vboxsync return !!cErrors;
151d00780265beb420bf27f96edfb4909d5fc76avboxsync}
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync
382dd16193dd5770e53a99d3e3f3bc4e96f1ddd8vboxsync