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.

347 lines
9.5 KiB

  1. // tssdjetharness.cpp : Defines the entry point for the console application.
  2. //
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <initguid.h>
  6. #include "itssd.h"
  7. #include "itssd_i.c"
  8. #include "tssdshrd.h"
  9. DEFINE_GUID(CLSID_TSSDJET,
  10. 0x005a9c68, 0xe216, 0x4b27, 0x8f, 0x59, 0xb3, 0x36, 0x82, 0x9b, 0x38,
  11. 0x68);
  12. #define TSSD_MaxDisconnectedSessions 10
  13. #define SESSDIR_MACHINE_NAME L"trevorfodev"
  14. ITSSessionDirectory *pTSSD;
  15. // STATUS_UNSUCCESSFUL or STATUS_SUCCESS
  16. DWORD RepopSessDir() {
  17. printf("RepopSessDir called.\n");
  18. return pTSSD->Repopulate(0, NULL);
  19. }
  20. HRESULT ConnectUser(WCHAR *UserName, WCHAR *Domain, DWORD SessionID,
  21. DWORD TSProtocol, WCHAR *Application, DWORD HRes, DWORD VRes,
  22. DWORD ColorDepth, DWORD LowTime, DWORD HighTime)
  23. {
  24. TSSD_CreateSessionInfo ts;
  25. wcscpy(ts.UserName, UserName);
  26. wcscpy(ts.Domain, Domain);
  27. ts.SessionID = SessionID;
  28. ts.TSProtocol = TSProtocol;
  29. wcscpy(ts.ApplicationType, Application);
  30. ts.ResolutionWidth = HRes;
  31. ts.ResolutionHeight = VRes;
  32. ts.ColorDepth = ColorDepth;
  33. ts.CreateTime.dwLowDateTime = LowTime;
  34. ts.CreateTime.dwHighDateTime = HighTime;
  35. return pTSSD->NotifyCreateLocalSession(&ts);
  36. }
  37. void checkerr(wchar_t *Component, HRESULT hr)
  38. {
  39. if (SUCCEEDED(hr))
  40. printf("%S successful.\n", Component);
  41. else
  42. printf("FAIL: %S failed, err=%u\n", Component, hr);
  43. }
  44. void RunMainProcess()
  45. {
  46. DWORD numSessionsReturned;
  47. TSSD_DisconnectedSessionInfo SessionBuf[TSSD_MaxDisconnectedSessions];
  48. PROCESS_INFORMATION pi;
  49. STARTUPINFO si;
  50. HANDLE MainSignalEvent;
  51. HANDLE ASignalEvent;
  52. HRESULT hr;
  53. DWORD dr;
  54. BOOL br;
  55. // This is for us to signal Process 'A'.
  56. MainSignalEvent = CreateEvent(NULL, FALSE, FALSE, L"MainSignalEvent");
  57. if (MainSignalEvent == NULL) {
  58. printf("Problem %u creating MainSignalEvent.\n", GetLastError());
  59. exit(1);
  60. }
  61. // This is for process A to signal us.
  62. ASignalEvent = CreateEvent(NULL, FALSE, FALSE, L"ASignalEvent");
  63. if (ASignalEvent == NULL) {
  64. printf("Problem %u creating ASignalEvent.\n", GetLastError());
  65. exit(1);
  66. }
  67. printf("Now initializing...");
  68. hr = pTSSD->Initialize(L"WRONGIP", SESSDIR_MACHINE_NAME, L"Cluster1",
  69. L"OpaqueSettings", 0, RepopSessDir);
  70. checkerr(L"Initialization", hr);
  71. printf("Sleeping for 5 sec. to make sure connect happens.\n");
  72. printf("Should see a RepopSessDir right here.\n");
  73. Sleep(5000);
  74. hr = ConnectUser(L"trevorfo", L"NTDEV", 0, 1, L"Desktop", 640, 480, 24, 22,
  75. 33);
  76. checkerr(L"ConnectUser", hr);
  77. // Verify there are no disconnected sessions for the user.
  78. hr = pTSSD->GetUserDisconnectedSessions(L"trevorfo", L"NTDEV",
  79. &numSessionsReturned, SessionBuf);
  80. if (SUCCEEDED(hr)) {
  81. if (numSessionsReturned == 0)
  82. printf("GetUserDisconnectedSessions succeeded and returned 0 "
  83. "users\n");
  84. else
  85. printf("FAIL: GetUserDisconnectedSessions returned %d users"
  86. " (should be 0)\n", numSessionsReturned);
  87. }
  88. else {
  89. printf("FAIL: GetUserDisconnectedSessions failed, hr=%u\n", hr);
  90. }
  91. // Hand off to process 2.
  92. memset(&pi, 0, sizeof(pi));
  93. memset(&si, 0, sizeof(si));
  94. si.cb = sizeof(si);
  95. wchar_t CommandLine[] = L"tssdcli A";
  96. //br = CreateProcess(NULL, CommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
  97. br = TRUE;
  98. if (br == FALSE) {
  99. printf("CreateProcess failed.\n");
  100. exit(1);
  101. }
  102. printf("About to hand off to process A.\n");
  103. br = SetEvent(MainSignalEvent);
  104. if (br == FALSE) {
  105. printf("SetEvent failed.\n");
  106. exit(1);
  107. }
  108. // He's gonna signal me when he's in single session mode, then I am going
  109. // to switch to single session mode and see if I can get the "disconnected"
  110. // session.
  111. dr = WaitForSingleObject(ASignalEvent, INFINITE);
  112. if (dr != WAIT_OBJECT_0) {
  113. printf("Wait failed.\n");
  114. exit(1);
  115. }
  116. printf("Main: Updating\n");
  117. hr = pTSSD->Update(L"WRONGIP", SESSDIR_MACHINE_NAME, L"Cluster1",
  118. L"OpaqueSettings", SINGLE_SESSION_FLAG);
  119. checkerr(L"Update", hr);
  120. printf("Sleeping for 5 sec\n");
  121. Sleep(5000);
  122. // Verify it returns a disconnected session for the user.
  123. hr = pTSSD->GetUserDisconnectedSessions(L"trevorfo", L"NTDEV",
  124. &numSessionsReturned, SessionBuf);
  125. if (SUCCEEDED(hr)) {
  126. if (numSessionsReturned == 1)
  127. printf("GetUserDisconnectedSessions succeeded and returned 1 "
  128. "users\n");
  129. else
  130. printf("FAIL: GetUserDisconnectedSessions returned %d users"
  131. " (should be 1)\n", numSessionsReturned);
  132. }
  133. else {
  134. printf("FAIL: GetUserDisconnectedSessions failed, hr=%u\n", hr);
  135. }
  136. Sleep(60000);
  137. }
  138. void RunProcessA()
  139. {
  140. HANDLE MainSignalEvent;
  141. HANDLE ASignalEvent;
  142. DWORD numSessionsReturned;
  143. HRESULT hr;
  144. BOOL br;
  145. DWORD dr;
  146. TSSD_DisconnectedSessionInfo SessionBuf[TSSD_MaxDisconnectedSessions];
  147. // This is the main process to signal us.
  148. MainSignalEvent = CreateEvent(NULL, FALSE, FALSE, L"MainSignalEvent");
  149. if (MainSignalEvent == NULL) {
  150. printf("Problem %u creating MainSignalEvent.\n", GetLastError());
  151. exit(1);
  152. }
  153. // This is for us to process the main proces.
  154. ASignalEvent = CreateEvent(NULL, FALSE, FALSE, L"ASignalEvent");
  155. if (ASignalEvent == NULL) {
  156. printf("Problem %u creating ASignalEvent.\n", GetLastError());
  157. exit(1);
  158. }
  159. printf("This is process A.\n");
  160. printf("About to connect in single session mode and call GetDisconnectedSessions, which should fail.\n");
  161. hr = pTSSD->Initialize(L"PROCESSA", SESSDIR_MACHINE_NAME, L"Cluster1",
  162. L"OpaqueSettings", SINGLE_SESSION_FLAG, RepopSessDir);
  163. // Wait to be signaled.
  164. dr = WaitForSingleObject(MainSignalEvent, INFINITE);
  165. if (dr != WAIT_OBJECT_0) {
  166. printf("Wait failed.\n");
  167. exit(1);
  168. }
  169. checkerr(L"Initialize", hr);
  170. printf("Break in here.\n");
  171. Sleep(10000);
  172. // Verify there are no disconnected sessions for the user.
  173. hr = pTSSD->GetUserDisconnectedSessions(L"trevorfo", L"NTDEV",
  174. &numSessionsReturned, SessionBuf);
  175. if (SUCCEEDED(hr)) {
  176. if (numSessionsReturned == 0)
  177. printf("GetUserDisconnectedSessions succeeded and returned 0 "
  178. "users\n");
  179. else
  180. printf("FAIL: GetUserDisconnectedSessions returned %d users"
  181. " (should be 0)\n", numSessionsReturned);
  182. }
  183. else {
  184. printf("FAIL: GetUserDisconnectedSessions failed, hr=%u\n", hr);
  185. }
  186. printf("Switching to multisession to verify still no disconnected sessions.\n");
  187. // Switch to multisession and check disconnected sessions again.
  188. hr = pTSSD->Update(L"PROCESSA", SESSDIR_MACHINE_NAME, L"Cluster1",
  189. L"OpaqueSettings", 0);
  190. checkerr(L"Update", hr);
  191. // Wait for the update to go through.
  192. printf("Should get a repopulate here...sleeping for 15 sec.\n");
  193. Sleep(15000);
  194. // Verify there are no disconnected sessions for the user.
  195. hr = pTSSD->GetUserDisconnectedSessions(L"trevorfo", L"NTDEV",
  196. &numSessionsReturned, SessionBuf);
  197. if (SUCCEEDED(hr)) {
  198. if (numSessionsReturned == 0)
  199. printf("GetUserDisconnectedSessions succeeded and returned 0 "
  200. "users\n");
  201. else
  202. printf("FAIL: GetUserDisconnectedSessions returned %d users"
  203. " (should be 0)\n", numSessionsReturned);
  204. }
  205. else {
  206. printf("FAIL: GetUserDisconnectedSessions failed, hr=%u\n", hr);
  207. }
  208. // Switch back to single session and check the disconnected sessions.
  209. hr = pTSSD->Update(L"PROCESSA", SESSDIR_MACHINE_NAME, L"Cluster1",
  210. L"OpaqueSettings", SINGLE_SESSION_FLAG);
  211. checkerr(L"Update", hr);
  212. printf("Waiting for Repop.\n");
  213. Sleep(5000);
  214. // Verify there are no disconnected sessions for the user.
  215. hr = pTSSD->GetUserDisconnectedSessions(L"trevorfo", L"NTDEV",
  216. &numSessionsReturned, SessionBuf);
  217. if (SUCCEEDED(hr)) {
  218. if (numSessionsReturned == 0)
  219. printf("GetUserDisconnectedSessions succeeded and returned 0 "
  220. "users\n");
  221. else
  222. printf("FAIL: GetUserDisconnectedSessions returned %d users"
  223. " (should be 0)\n", numSessionsReturned);
  224. }
  225. else {
  226. printf("FAIL: GetUserDisconnectedSessions failed, hr=%u\n", hr);
  227. }
  228. // Log in a user (we don't repopulate in this test currently)
  229. hr = ConnectUser(L"trevorfo", L"NTDEV", 3, 1, L"Desktop", 640, 480, 24, 22,
  230. 33);
  231. checkerr(L"ConnectUser", hr);
  232. // OK, switch back to the other process.
  233. br = SetEvent(ASignalEvent);
  234. if (br == FALSE) {
  235. printf("SetEvent failed.\n");
  236. exit(1);
  237. }
  238. // Sleep 100000
  239. Sleep(100000);
  240. }
  241. int __cdecl main(int argc, char* argv[])
  242. {
  243. HRESULT hr;
  244. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  245. hr = CoCreateInstance(CLSID_TSSDJET, NULL, CLSCTX_INPROC_SERVER,
  246. IID_ITSSessionDirectory, (void **)&pTSSD);
  247. checkerr(L"CoCreateInstance", hr);
  248. if (argc == 2) {
  249. if (*argv[1] == 'A') {
  250. // We are process 'A'.
  251. RunProcessA();
  252. }
  253. }
  254. else {
  255. // We are ttsshe main process.
  256. RunMainProcess();
  257. }
  258. CoUninitialize();
  259. return 0;
  260. }