summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/path/test/TestPathCchAddExtension.c
blob: 71f5ddf75b1ba4bfff9a2e3bcb2be329a609dccc (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
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/tchar.h>
#include <winpr/winpr.h>

static const TCHAR testExtDot[] = _T(".exe");
static const TCHAR testExtNoDot[] = _T("exe");
static const TCHAR testPathNoExtension[] = _T("C:\\Windows\\System32\\cmd");
static const TCHAR testPathExtension[] = _T("C:\\Windows\\System32\\cmd.exe");

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

	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);

	/* Path: no extension, Extension: dot */

	_tcscpy(Path, testPathNoExtension);

	status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);

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

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

	/* Path: no extension, Extension: no dot */

	_tcscpy(Path, testPathNoExtension);

	status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtNoDot);

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

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

	/* Path: extension, Extension: dot */

	_tcscpy(Path, testPathExtension);

	status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);

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

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

	/* Path: extension, Extension: no dot */

	_tcscpy(Path, testPathExtension);

	status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);

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

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

	/* Path: NULL */

	status = PathCchAddExtension(NULL, PATHCCH_MAX_CCH, testExtDot);
	if (status != E_INVALIDARG)
	{
		_tprintf(_T("PathCchAddExtension with null buffer returned status: 0x%08") _T(
		             PRIX32) _T(" (expected E_INVALIDARG)\n"),
		         status);
		return -1;
	}

	/* Extension: NULL */

	status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, NULL);
	if (status != E_INVALIDARG)
	{
		_tprintf(_T("PathCchAddExtension with null extension returned status: 0x%08") _T(
		             PRIX32) _T(" (expected E_INVALIDARG)\n"),
		         status);
		return -1;
	}

	/* Insufficient Buffer size */

	_tcscpy(Path, _T("C:\\456789"));
	status = PathCchAddExtension(Path, 9 + 4, _T(".jpg"));
	if (SUCCEEDED(status))
	{
		_tprintf(_T("PathCchAddExtension with insufficient buffer unexpectedly succeeded with ")
		         _T("status: 0x%08") _T(PRIX32) _T("\n"),
		         status);
		return -1;
	}

	/* Minimum required buffer size */

	_tcscpy(Path, _T("C:\\456789"));
	status = PathCchAddExtension(Path, 9 + 4 + 1, _T(".jpg"));
	if (FAILED(status))
	{
		_tprintf(_T("PathCchAddExtension with sufficient buffer unexpectedly failed with status: ")
		         _T("0x%08") _T(PRIX32) _T("\n"),
		         status);
		return -1;
	}

	return 0;
}