Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.5 KiB

  1. #include <windows.h>
  2. #include <imagehlp.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. void __cdecl main(void);
  6. void foo (void);
  7. void foo1(void);
  8. void foo2(void);
  9. void foo3(void);
  10. void WalkTheStack(void);
  11. void TestFindExecutableImage( void );
  12. void __cdecl
  13. main(void)
  14. {
  15. puts("Entering main");
  16. foo();
  17. TestFindExecutableImage();
  18. puts("Ending main");
  19. }
  20. void
  21. TestFindExecutableImage(
  22. void
  23. )
  24. {
  25. HANDLE Handle;
  26. CHAR szCorrectName[MAX_PATH];
  27. CHAR szActualName [MAX_PATH];
  28. CHAR szTestPath[MAX_PATH];
  29. CHAR szDrive[_MAX_DRIVE];
  30. CHAR szDir[_MAX_DIR];
  31. CHAR *FilePart;
  32. DWORD ErrorCount = 0;
  33. _splitpath(_pgmptr, szDrive, szDir, NULL, NULL);
  34. strcpy(szTestPath, szDrive);
  35. strcat(szTestPath, szDir);
  36. GetFullPathName(_pgmptr, MAX_PATH, szCorrectName, &FilePart);
  37. __try {
  38. Handle = FindExecutableImage(FilePart, szTestPath, szActualName);
  39. } __except (EXCEPTION_EXECUTE_HANDLER) {
  40. Handle = NULL;
  41. printf("ERROR: FindExecutableImage GPF (test %d)\n", 1);
  42. ErrorCount++;
  43. }
  44. if (Handle == NULL) {
  45. printf("ERROR: FindExecutableImage (test %d) failed\n", 1);
  46. ErrorCount++;
  47. } else {
  48. CloseHandle(Handle);
  49. if (strcmp(szCorrectName, szActualName)) {
  50. printf("ERROR: FindExecutableImage() (test %d) found wrong image.\nExpected: %s\nFound: %s\n", 1, szCorrectName, szActualName);
  51. ErrorCount++;
  52. }
  53. }
  54. // Test long paths to ExpandPath()
  55. strcat(szTestPath, ";%path%;%path%;%path%;%path%;%path%;%path%;%path%;%path%");
  56. __try {
  57. Handle = FindExecutableImage(FilePart, szTestPath, szActualName);
  58. } __except (EXCEPTION_EXECUTE_HANDLER) {
  59. Handle = NULL;
  60. printf("ERROR: FindExecutableImage GPF (test %d)\n", 2);
  61. ErrorCount++;
  62. }
  63. if (Handle == NULL) {
  64. printf("ERROR: FindExecutableImage (test %d) failed\n", 2);
  65. ErrorCount++;
  66. } else {
  67. CloseHandle(Handle);
  68. if (strcmp(szCorrectName, szActualName)) {
  69. printf("ERROR: FindExecutableImage() (test %d) found wrong image.\nExpected: %s\nFound: %s\n", 2, szCorrectName, szActualName);
  70. ErrorCount++;
  71. }
  72. }
  73. // Test invalid paths (should return failure)
  74. szTestPath[0] = '\0';
  75. __try {
  76. Handle = FindExecutableImage(FilePart, szTestPath, szActualName);
  77. } __except (EXCEPTION_EXECUTE_HANDLER) {
  78. Handle = NULL;
  79. printf("ERROR: FindExecutableImage GPF (test %d)\n", 3);
  80. ErrorCount++;
  81. }
  82. if (Handle != NULL) {
  83. CloseHandle(Handle);
  84. printf("ERROR: FindExecutableImage (test %d) failed - Expected: <nothing>\nFound: %s\n", 3, szActualName);
  85. ErrorCount++;
  86. } else {
  87. if (strlen(szActualName)) {
  88. printf("ERROR: FindExecutableImage() (test %d) failed to clear ImageName on failure\n", 3);
  89. ErrorCount++;
  90. }
  91. }
  92. // Test NULL name (should return failure)
  93. __try {
  94. Handle = FindExecutableImage(NULL, szTestPath, szActualName);
  95. } __except (EXCEPTION_EXECUTE_HANDLER) {
  96. Handle = NULL;
  97. printf("ERROR: FindExecutableImage GPF (test %d)\n", 4);
  98. ErrorCount++;
  99. }
  100. if (Handle != NULL) {
  101. CloseHandle(Handle);
  102. printf("ERROR: FindExecutableImage (test %d) failed - Expected: <nothing>\nFound: %s\n", 4, szActualName);
  103. ErrorCount++;
  104. } else {
  105. if (strlen(szActualName)) {
  106. printf("ERROR: FindExecutableImage() (test %d) failed to clear ImageName on failure\n", 4);
  107. ErrorCount++;
  108. }
  109. }
  110. // Valid name and path, invalid end result.
  111. strcpy(szTestPath, szDrive);
  112. strcat(szTestPath, szDir);
  113. __try {
  114. Handle = FindExecutableImage(FilePart, szTestPath, NULL);
  115. } __except (EXCEPTION_EXECUTE_HANDLER) {
  116. Handle = NULL;
  117. printf("ERROR: FindExecutableImage GPF (test %d)\n", 5);
  118. ErrorCount++;
  119. }
  120. if (Handle != NULL) {
  121. CloseHandle(Handle);
  122. printf("ERROR: FindExecutableImage (test %d) failed - Supposed to fail if filepath is invalid\n", 5);
  123. ErrorCount++;
  124. }
  125. printf("FindExecutableImage - %s\n", ErrorCount ? "Failed" : "Passed");
  126. return;
  127. }
  128. void foo(void) {
  129. puts("Entering foo");
  130. foo1();
  131. puts("Ending foo");
  132. }
  133. void foo1(void) {
  134. puts("Entering foo1");
  135. foo2();
  136. puts("Ending foo1");
  137. }
  138. void foo2(void) {
  139. puts("Entering foo2");
  140. foo3();
  141. puts("Ending foo2");
  142. }
  143. void foo3(void) {
  144. puts("Entering foo3");
  145. WalkTheStack();
  146. puts("Ending foo2");
  147. }
  148. void
  149. WalkTheStack(){
  150. }