TestPathCchAppend.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include <stdio.h>
  2. #include <winpr/crt.h>
  3. #include <winpr/path.h>
  4. #include <winpr/tchar.h>
  5. #include <winpr/winpr.h>
  6. static const TCHAR testBasePathBackslash[] = _T("C:\\Program Files\\");
  7. static const TCHAR testBasePathNoBackslash[] = _T("C:\\Program Files");
  8. static const TCHAR testMorePathBackslash[] = _T("\\Microsoft Visual Studio 11.0");
  9. static const TCHAR testMorePathNoBackslash[] = _T("Microsoft Visual Studio 11.0");
  10. static const TCHAR testPathOut[] = _T("C:\\Program Files\\Microsoft Visual Studio 11.0");
  11. int TestPathCchAppend(int argc, char* argv[])
  12. {
  13. HRESULT status;
  14. TCHAR Path[PATHCCH_MAX_CCH];
  15. size_t i;
  16. /* Base Path: Backslash, More Path: No Backslash */
  17. _tcscpy(Path, testBasePathBackslash);
  18. status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathNoBackslash);
  19. if (status != S_OK)
  20. {
  21. _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
  22. return -1;
  23. }
  24. if (_tcscmp(Path, testPathOut) != 0)
  25. {
  26. _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
  27. return -1;
  28. }
  29. /* Base Path: Backslash, More Path: Backslash */
  30. _tcscpy(Path, testBasePathBackslash);
  31. status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathBackslash);
  32. if (status != S_OK)
  33. {
  34. _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
  35. return -1;
  36. }
  37. if (_tcscmp(Path, testPathOut) != 0)
  38. {
  39. _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
  40. return -1;
  41. }
  42. /* Base Path: No Backslash, More Path: Backslash */
  43. _tcscpy(Path, testBasePathNoBackslash);
  44. status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathBackslash);
  45. if (status != S_OK)
  46. {
  47. _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
  48. return -1;
  49. }
  50. if (_tcscmp(Path, testPathOut) != 0)
  51. {
  52. _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
  53. return -1;
  54. }
  55. /* Base Path: No Backslash, More Path: No Backslash */
  56. _tcscpy(Path, testBasePathNoBackslash);
  57. status = PathCchAppend(Path, PATHCCH_MAX_CCH, testMorePathNoBackslash);
  58. if (status != S_OK)
  59. {
  60. _tprintf(_T("PathCchAppend status: 0x%08") _T(PRIX32) _T("\n"), status);
  61. return -1;
  62. }
  63. if (_tcscmp(Path, testPathOut) != 0)
  64. {
  65. _tprintf(_T("Path Mismatch: Actual: %s, Expected: %s\n"), Path, testPathOut);
  66. return -1;
  67. }
  68. /* According to msdn a NULL Path is an invalid argument */
  69. status = PathCchAppend(NULL, PATHCCH_MAX_CCH, testMorePathNoBackslash);
  70. if (status != E_INVALIDARG)
  71. {
  72. _tprintf(_T("PathCchAppend with NULL path unexpectedly returned status: 0x%08") _T(
  73. PRIX32) _T("\n"),
  74. status);
  75. return -1;
  76. }
  77. /* According to msdn a NULL pszMore is an invalid argument (although optional !?) */
  78. _tcscpy(Path, testBasePathNoBackslash);
  79. status = PathCchAppend(Path, PATHCCH_MAX_CCH, NULL);
  80. if (status != E_INVALIDARG)
  81. {
  82. _tprintf(_T("PathCchAppend with NULL pszMore unexpectedly returned status: 0x%08") _T(
  83. PRIX32) _T("\n"),
  84. status);
  85. return -1;
  86. }
  87. /* According to msdn cchPath must be > 0 and <= PATHCCH_MAX_CCH */
  88. _tcscpy(Path, testBasePathNoBackslash);
  89. status = PathCchAppend(Path, 0, testMorePathNoBackslash);
  90. if (status != E_INVALIDARG)
  91. {
  92. _tprintf(_T("PathCchAppend with cchPath value 0 unexpectedly returned status: 0x%08") _T(
  93. PRIX32) _T("\n"),
  94. status);
  95. return -1;
  96. }
  97. _tcscpy(Path, testBasePathNoBackslash);
  98. status = PathCchAppend(Path, PATHCCH_MAX_CCH + 1, testMorePathNoBackslash);
  99. if (status != E_INVALIDARG)
  100. {
  101. _tprintf(_T("PathCchAppend with cchPath value > PATHCCH_MAX_CCH unexpectedly returned ")
  102. _T("status: 0x%08") _T(PRIX32) _T("\n"),
  103. status);
  104. return -1;
  105. }
  106. /* Resulting file must not exceed PATHCCH_MAX_CCH */
  107. for (i = 0; i < PATHCCH_MAX_CCH - 1; i++)
  108. Path[i] = _T('X');
  109. Path[PATHCCH_MAX_CCH - 1] = 0;
  110. status = PathCchAppend(Path, PATHCCH_MAX_CCH, _T("\\This cannot be appended to Path"));
  111. if (SUCCEEDED(status))
  112. {
  113. _tprintf(_T("PathCchAppend unexepectedly succeeded with status: 0x%08") _T(PRIX32) _T("\n"),
  114. status);
  115. return -1;
  116. }
  117. return 0;
  118. }