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

static const char testPathExtension[] = "C:\\Windows\\System32\\cmd.exe";

int TestPathCchFindExtension(int argc, char* argv[])
{
	PCSTR pszExt = NULL;
	PCSTR pszTmp = NULL;
	HRESULT hr = 0;

	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);

	/* Test invalid args */

	hr = PathCchFindExtensionA(NULL, sizeof(testPathExtension), &pszExt);
	if (SUCCEEDED(hr))
	{
		printf(
		    "PathCchFindExtensionA unexpectedly succeeded with pszPath = NULL. result: 0x%08" PRIX32
		    "\n",
		    hr);
		return -1;
	}

	hr = PathCchFindExtensionA(testPathExtension, 0, &pszExt);
	if (SUCCEEDED(hr))
	{
		printf("PathCchFindExtensionA unexpectedly succeeded with cchPath = 0. result: 0x%08" PRIX32
		       "\n",
		       hr);
		return -1;
	}

	hr = PathCchFindExtensionA(testPathExtension, sizeof(testPathExtension), NULL);
	if (SUCCEEDED(hr))
	{
		printf(
		    "PathCchFindExtensionA unexpectedly succeeded with ppszExt = NULL. result: 0x%08" PRIX32
		    "\n",
		    hr);
		return -1;
	}

	/* Test missing null-termination of pszPath */

	hr = PathCchFindExtensionA("c:\\45.789", 9, &pszExt); /* nb: correct would be 10 */
	if (SUCCEEDED(hr))
	{
		printf("PathCchFindExtensionA unexpectedly succeeded with unterminated pszPath. result: "
		       "0x%08" PRIX32 "\n",
		       hr);
		return -1;
	}

	/* Test passing of an empty terminated string (must succeed) */

	pszExt = NULL;
	pszTmp = "";
	hr = PathCchFindExtensionA(pszTmp, 1, &pszExt);
	if (hr != S_OK)
	{
		printf("PathCchFindExtensionA failed with an empty terminated string. result: 0x%08" PRIX32
		       "\n",
		       hr);
		return -1;
	}
	/* pszExt must point to the strings terminating 0 now */
	if (pszExt != pszTmp)
	{
		printf("PathCchFindExtensionA failed with an empty terminated string:  pszExt pointer "
		       "mismatch\n");
		return -1;
	}

	/* Test a path without file extension (must succeed) */

	pszExt = NULL;
	pszTmp = "c:\\4.678\\";
	hr = PathCchFindExtensionA(pszTmp, 10, &pszExt);
	if (hr != S_OK)
	{
		printf("PathCchFindExtensionA failed with a directory path. result: 0x%08" PRIX32 "\n", hr);
		return -1;
	}
	/* The extension must not have been found and pszExt must point to the
	 * strings terminating NULL now */
	if (pszExt != &pszTmp[9])
	{
		printf("PathCchFindExtensionA failed with a directory path: pszExt pointer mismatch\n");
		return -1;
	}

	/* Non-special tests */

	pszExt = NULL;
	if (PathCchFindExtensionA(testPathExtension, sizeof(testPathExtension), &pszExt) != S_OK)
	{
		printf("PathCchFindExtensionA failure: expected S_OK\n");
		return -1;
	}

	if (!pszExt || strcmp(pszExt, ".exe"))
	{
		printf("PathCchFindExtensionA failure: unexpected extension\n");
		return -1;
	}

	printf("Extension: %s\n", pszExt);

	return 0;
}