FreeRDP
TestPathCchAddExtension.c
1 
2 #include <stdio.h>
3 #include <winpr/crt.h>
4 #include <winpr/path.h>
5 #include <winpr/tchar.h>
6 #include <winpr/winpr.h>
7 
8 static const TCHAR testExtDot[] = _T(".exe");
9 static const TCHAR testExtNoDot[] = _T("exe");
10 static const TCHAR testPathNoExtension[] = _T("C:\\Windows\\System32\\cmd");
11 static const TCHAR testPathExtension[] = _T("C:\\Windows\\System32\\cmd.exe");
12 
13 int TestPathCchAddExtension(int argc, char* argv[])
14 {
15  HRESULT status = 0;
16  TCHAR Path[PATHCCH_MAX_CCH] = { 0 };
17 
18  WINPR_UNUSED(argc);
19  WINPR_UNUSED(argv);
20 
21  /* Path: no extension, Extension: dot */
22 
23  _tcsncpy(Path, testPathNoExtension, ARRAYSIZE(Path));
24 
25  status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);
26 
27  if (status != S_OK)
28  {
29  _tprintf(_T("PathCchAddExtension status: 0x%08") _T(PRIX32) _T("\n"), status);
30  return -1;
31  }
32 
33  if (_tcsncmp(Path, testPathExtension, ARRAYSIZE(Path)) != 0)
34  {
35  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
36  return -1;
37  }
38 
39  /* Path: no extension, Extension: no dot */
40 
41  _tcsncpy(Path, testPathNoExtension, ARRAYSIZE(Path));
42 
43  status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtNoDot);
44 
45  if (status != S_OK)
46  {
47  _tprintf(_T("PathCchAddExtension status: 0x%08") _T(PRIX32) _T("\n"), status);
48  return -1;
49  }
50 
51  if (_tcsncmp(Path, testPathExtension, ARRAYSIZE(Path)) != 0)
52  {
53  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
54  return -1;
55  }
56 
57  /* Path: extension, Extension: dot */
58 
59  _tcsncpy(Path, testPathExtension, ARRAYSIZE(Path));
60 
61  status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);
62 
63  if (status != S_FALSE)
64  {
65  _tprintf(_T("PathCchAddExtension status: 0x%08") _T(PRIX32) _T("\n"), status);
66  return -1;
67  }
68 
69  if (_tcsncmp(Path, testPathExtension, ARRAYSIZE(Path)) != 0)
70  {
71  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
72  return -1;
73  }
74 
75  /* Path: extension, Extension: no dot */
76 
77  _tcsncpy(Path, testPathExtension, ARRAYSIZE(Path));
78 
79  status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, testExtDot);
80 
81  if (status != S_FALSE)
82  {
83  _tprintf(_T("PathCchAddExtension status: 0x%08") _T(PRIX32) _T("\n"), status);
84  return -1;
85  }
86 
87  if (_tcsncmp(Path, testPathExtension, ARRAYSIZE(Path)) != 0)
88  {
89  _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathExtension);
90  return -1;
91  }
92 
93  /* Path: NULL */
94 
95  status = PathCchAddExtension(NULL, PATHCCH_MAX_CCH, testExtDot);
96  if (status != E_INVALIDARG)
97  {
98  _tprintf(_T("PathCchAddExtension with null buffer returned status: 0x%08") _T(
99  PRIX32) _T(" (expected E_INVALIDARG)\n"),
100  status);
101  return -1;
102  }
103 
104  /* Extension: NULL */
105 
106  status = PathCchAddExtension(Path, PATHCCH_MAX_CCH, NULL);
107  if (status != E_INVALIDARG)
108  {
109  _tprintf(_T("PathCchAddExtension with null extension returned status: 0x%08") _T(
110  PRIX32) _T(" (expected E_INVALIDARG)\n"),
111  status);
112  return -1;
113  }
114 
115  /* Insufficient Buffer size */
116 
117  _tcsncpy(Path, _T("C:\\456789"), ARRAYSIZE(Path));
118  status = PathCchAddExtension(Path, 9 + 4, _T(".jpg"));
119  if (SUCCEEDED(status))
120  {
121  _tprintf(_T("PathCchAddExtension with insufficient buffer unexpectedly succeeded with ")
122  _T("status: 0x%08") _T(PRIX32) _T("\n"),
123  status);
124  return -1;
125  }
126 
127  /* Minimum required buffer size */
128 
129  _tcsncpy(Path, _T("C:\\456789"), ARRAYSIZE(Path));
130  status = PathCchAddExtension(Path, 9 + 4 + 1, _T(".jpg"));
131  if (FAILED(status))
132  {
133  _tprintf(_T("PathCchAddExtension with sufficient buffer unexpectedly failed with status: ")
134  _T("0x%08") _T(PRIX32) _T("\n"),
135  status);
136  return -1;
137  }
138 
139  return 0;
140 }