summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/path/test/TestPathCchStripPrefix.c
blob: aaec4bc1c902d46a765ee164ef8cb62d6ddd9bb7 (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
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>

/**
 * Naming Files, Paths, and Namespaces:
 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247/
 */

static const TCHAR testPathPrefixFileNamespace[] = _T("\\\\?\\C:\\Program Files\\");
static const TCHAR testPathNoPrefixFileNamespace[] = _T("C:\\Program Files\\");
static const TCHAR testPathPrefixFileNamespaceMinimum[] = _T("\\\\?\\C:");
static const TCHAR testPathNoPrefixFileNamespaceMinimum[] = _T("C:");

static const TCHAR testPathPrefixDeviceNamespace[] = _T("\\\\?\\GLOBALROOT");

int TestPathCchStripPrefix(int argc, char* argv[])
{
	HRESULT status = 0;
	TCHAR Path[PATHCCH_MAX_CCH];

	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);

	/**
	 * PathCchStripPrefix returns S_OK if the prefix was removed, S_FALSE if
	 * the path did not have a prefix to remove, or an HRESULT failure code.
	 */

	/* Path with prefix (File Namespace) */

	_tcscpy(Path, testPathPrefixFileNamespace);

	status = PathCchStripPrefix(Path, sizeof(testPathPrefixFileNamespace) / sizeof(TCHAR));

	if (status != S_OK)
	{
		_tprintf(_T("PathCchStripPrefix status: 0x%08") _T(PRIX32) _T("\n"), status);
		return -1;
	}

	if (_tcscmp(Path, testPathNoPrefixFileNamespace) != 0)
	{
		_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path,
		         testPathNoPrefixFileNamespace);
		return -1;
	}

	/* Path with prefix (Device Namespace) */

	_tcscpy(Path, testPathPrefixDeviceNamespace);

	status = PathCchStripPrefix(Path, sizeof(testPathPrefixDeviceNamespace) / sizeof(TCHAR));

	if (status != S_FALSE)
	{
		_tprintf(_T("PathCchStripPrefix status: 0x%08") _T(PRIX32) _T("\n"), status);
		return -1;
	}

	if (_tcscmp(Path, testPathPrefixDeviceNamespace) != 0)
	{
		_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path,
		         testPathPrefixDeviceNamespace);
		return -1;
	}

	/* NULL Path */
	status = PathCchStripPrefix(NULL, PATHCCH_MAX_CCH);
	if (status != E_INVALIDARG)
	{
		_tprintf(
		    _T("PathCchStripPrefix with null path unexpectedly succeeded with status 0x%08") _T(
		        PRIX32) _T("\n"),
		    status);
		return -1;
	}

	/* Invalid cchPath values: 0, 1, 2, 3 and > PATHCCH_MAX_CCH */
	for (int i = 0; i < 5; i++)
	{
		_tcscpy(Path, testPathPrefixFileNamespace);
		if (i == 4)
			i = PATHCCH_MAX_CCH + 1;
		status = PathCchStripPrefix(Path, i);
		if (status != E_INVALIDARG)
		{
			_tprintf(_T("PathCchStripPrefix with invalid cchPath value %d unexpectedly succeeded ")
			         _T("with status 0x%08") _T(PRIX32) _T("\n"),
			         i, status);
			return -1;
		}
	}

	/* Minimum Path that would get successfully stripped on windows */
	_tcscpy(Path, testPathPrefixFileNamespaceMinimum);
	size_t i = sizeof(testPathPrefixFileNamespaceMinimum) / sizeof(TCHAR);
	i = i - 1; /* include testing of a non-null terminated string */
	status = PathCchStripPrefix(Path, i);
	if (status != S_OK)
	{
		_tprintf(_T("PathCchStripPrefix with minimum valid strippable path length unexpectedly ")
		         _T("returned status 0x%08") _T(PRIX32) _T("\n"),
		         status);
		return -1;
	}
	if (_tcscmp(Path, testPathNoPrefixFileNamespaceMinimum))
	{
		_tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path,
		         testPathNoPrefixFileNamespaceMinimum);
		return -1;
	}

	/* Invalid drive letter symbol */
	_tcscpy(Path, _T("\\\\?\\5:"));
	status = PathCchStripPrefix(Path, 6);
	if (status == S_OK)
	{
		_tprintf(
		    _T("PathCchStripPrefix with invalid drive letter symbol unexpectedly succeeded\n"));
		return -1;
	}

	return 0;
}