Leaked source code of windows server 2003
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.

424 lines
10 KiB

  1. Include('types.js');
  2. Include('utils.js');
  3. Include('robocopy.js');
  4. var g_robocopy;
  5. var g_params;
  6. /*
  7. */
  8. var g_robocopy;
  9. function test_js::ScriptMain()
  10. {
  11. PrivateData.fnExecScript = TestRemoteExec;
  12. SignalThreadSync('TestThreadReady');
  13. ResetSync('TestThreadExit,DoTest');
  14. do
  15. {
  16. nEvent = WaitForMultipleSyncs('TestThreadExit,DoTest,robocopytest', 0, 0);
  17. if (nEvent == 2)
  18. {
  19. ResetSync('DoTest');
  20. PDtest(g_params);
  21. }
  22. if (nEvent == 3)
  23. {
  24. ResetSync('robocopytest');
  25. robocopytest();
  26. }
  27. }
  28. while(nEvent != 1);
  29. delete PrivateData.objPDTest;
  30. if (g_robocopy != null)
  31. g_robocopy.UnRegister();
  32. LogMsg("TestThread exit");
  33. }
  34. function TestRemoteExec(cmd, params)
  35. {
  36. var vRet = 'ok';
  37. LogMsg("Test received command " + cmd);
  38. switch (cmd)
  39. {
  40. case 'terminate':
  41. SignalThreadSync('TestThreadExit');
  42. Sleep(1000);
  43. break;
  44. case 'lock':
  45. TakeThreadLock("foo");
  46. break;
  47. case 'test':
  48. test();
  49. break;
  50. case 'robocopy':
  51. SignalThreadSync('robocopytest');
  52. break;
  53. case 'pcopy':
  54. pcopytest();
  55. break;
  56. case 'exec':
  57. var pid = RunLocalCommand(params,
  58. '',
  59. 'Test Program',
  60. false,
  61. true,
  62. false);
  63. if (pid == 0)
  64. vRet = GetLastRunLocalError();
  65. else
  66. vRet = pid;
  67. break;
  68. case 'exec_noout':
  69. var pid = RunLocalCommand(params,
  70. '',
  71. 'Test Program',
  72. false,
  73. false,
  74. false,
  75. false,
  76. false);
  77. if (pid == 0)
  78. vRet = GetLastRunLocalError();
  79. else
  80. vRet = pid;
  81. break;
  82. case 'getoutput':
  83. vRet = GetProcessOutput(params);
  84. break;
  85. case 'send':
  86. var args = params.split(',');
  87. vRet = SendToProcess(args[0], args[1], '');
  88. break;
  89. case 'pdtest': // PrivateData Access test
  90. g_params = params;
  91. SignalThreadSync('DoTest');
  92. break;
  93. case 'build':
  94. BuildExeTest();
  95. break;
  96. default:
  97. vRet = 'invalid command: ' + cmd;
  98. }
  99. return vRet;
  100. }
  101. function test_js::OnProcessEvent(pid, evt, param)
  102. {
  103. NotifyScript('ProcEvent: ', pid+','+evt+','+param);
  104. }
  105. function DeepCopy(from)
  106. {
  107. var index;
  108. var obj = new Object();
  109. for(index in from)
  110. {
  111. switch(typeof(from[index]))
  112. {
  113. case 'number':
  114. case 'boolean':
  115. obj[index] = from[index];
  116. break;
  117. case 'string':
  118. obj[index] = (from[index] + "X").slice(0, -1); // make a local copy
  119. break;
  120. case 'object':
  121. if (from[index] != null)
  122. {
  123. obj[index] = DeepCopy(from[index]);
  124. }
  125. break;
  126. case 'function':
  127. LogMsg("Deepcopy - won't copy function " + index);
  128. break;
  129. case 'undefined':
  130. break;
  131. default:
  132. LogMsg("Deepcopy: Unexpected type: " + typeof(from[index]));
  133. break;
  134. }
  135. }
  136. return obj;
  137. }
  138. var j = 0;
  139. function test()
  140. {
  141. var i;
  142. for(i = j; i - j < 50; i++)
  143. {
  144. LogMsg("MESG " + i );
  145. SignalThreadSync("MESG " + i );
  146. }
  147. j = i
  148. }
  149. function RemoveExtension(strName)
  150. {
  151. var nDot = strName.lastIndexOf('.');
  152. var nSlash = strName.lastIndexOf('\\');
  153. var nColon = strName.lastIndexOf(':');
  154. if (nDot >= 0 && nDot > nSlash && nDot > nColon)
  155. {
  156. return strName.slice(0, nDot);
  157. }
  158. return this;
  159. }
  160. function pcopytest()
  161. //function MakeNumberedBackup(strFileName)
  162. {
  163. debugger;
  164. var index;
  165. var strFileName = "E:\\PCopy.log";
  166. var objFS;
  167. var strBase;
  168. var strExt;
  169. var strNewFileName;
  170. try
  171. {
  172. debugger;
  173. objFS = new ActiveXObject('Scripting.FileSystemObject');
  174. if (objFS.FileExists(strFileName))
  175. {
  176. strBase = RemoveExtension(strFileName);
  177. strExt = objFS.GetExtensionName(strFileName);
  178. if (strExt > '')
  179. strExt = '.' + strExt;
  180. index = 1;
  181. while ( objFS.FileExists(strBase + index + strExt))
  182. {
  183. ++index;
  184. }
  185. objFS.MoveFile(stdFileName, strBase + index + strExt);
  186. }
  187. }
  188. catch(ex)
  189. {
  190. LogMsg("an error occurred while executing 'MakeNumberedBackup'\n" + ex.description);
  191. throw ex;
  192. }
  193. }
  194. /*
  195. StatusProgress()
  196. This is called as a RoboCopy member function.
  197. We use it to print 1 message per file.
  198. */
  199. var nFiles = -5;
  200. function StatusProgress(nPercent, nSize, nCopiedBytes)
  201. {
  202. if (WaitForSync('TestThreadExit', 1) == 1)
  203. {
  204. LogMsg("ABORTING FILE COPY");
  205. return this.RC_FAIL;
  206. }
  207. if (nPercent == 0)
  208. {
  209. if (nFiles ==0)
  210. {
  211. LogMsg("ABORT CopyFileProgress: " + this.strSrcFile + " is " + nSize + " bytes");
  212. return this.PROGRESS_CANCEL;
  213. }
  214. nFiles--;
  215. LogMsg("CopyFileProgress: " + this.strSrcFile + " is " + nSize + " bytes");
  216. return this.PROGRESS_QUIET;
  217. }
  218. if (nPercent == 100)
  219. LogMsg("CopyFileProgress COMPLETE: " + this.strSrcFile + " is " + nSize + " bytes");
  220. return this.PROGRESS_STOP;
  221. return this.PROGRESS_CANCEL;
  222. return this.PROGRESS_QUIET;
  223. return this.PROGRESS_CONTINUE;
  224. }
  225. /*
  226. StatusError()
  227. This is called as a RoboCopy member function.
  228. Called when RoboCopy cannot copy a file for some reason.
  229. Log the event and continue.
  230. */
  231. function StatusError()
  232. {
  233. // Note, that the paths printed can be inaccurate.
  234. // We only know the starting directories and the filename
  235. // of the file in question.
  236. // Since we may be doing a recursive copy, some of the
  237. // path information is not available to us.
  238. var strErrDetail = 'Unknown';
  239. if (WaitForSync('TestThreadExit', 1) == 1)
  240. {
  241. LogMsg("ABORTING FILE COPY");
  242. return this.RC_FAIL;
  243. }
  244. if (this.nErrorCode == 0 || this.nErrorCode == this.RCERR_RETRYING)
  245. return this.RC_CONTINUE; // Eliminate some clutter in the log file.
  246. if (this.ErrorMessages[this.nErrorCode])
  247. strErrDetail = this.ErrorMessages[this.nErrorCode];
  248. var strMsg = "CopyBinariesFiles error " +
  249. this.nErrorCode +
  250. "(" + strErrDetail + ")" +
  251. " copying file " +
  252. this.strSrcFile +
  253. " to " +
  254. this.strDstDir;
  255. LogMsg(strMsg);
  256. // return this.RC_FAIL;
  257. return this.RC_CONTINUE;
  258. }
  259. function StatusMessage(nErrorCode, strErrorMessage, strRoboCopyMessage, strFileName)
  260. {
  261. var strMsg = "CopyBinariesFiles message (" +
  262. nErrorCode +
  263. ": " + strErrorMessage +
  264. ") " + strRoboCopyMessage +
  265. " " +
  266. strFileName;
  267. LogMsg(strMsg);
  268. return this.RC_CONTINUE;
  269. }
  270. function robocopytest()
  271. {
  272. try
  273. {
  274. if (!RoboCopyInit())
  275. {
  276. LogMsg("RoboCopyInit() failed");
  277. return false;
  278. }
  279. g_robocopy.StatusProgress = StatusProgress;
  280. g_robocopy.StatusError = StatusError;
  281. g_robocopy.StatusMessage = StatusMessage;
  282. g_robocopy.CopyDir("C:\\RoboTest1\\", "C:\\RoboTest2\\", true);
  283. LogMsg("ROBOCOPY COMPLETED\n");
  284. }
  285. catch(ex)
  286. {
  287. LogMsg("an error occurred while executing 'robocopytest'\n" + ex.description);
  288. }
  289. }
  290. function test_js::OnEventSourceEvent(RemoteObj, DispID, cmd, params)
  291. {
  292. var objRet = new Object;
  293. objRet.rc = 0;
  294. try
  295. {
  296. if (g_robocopy == null || !g_robocopy.OnEventSource(objRet, arguments))
  297. {
  298. }
  299. }
  300. catch(ex)
  301. {
  302. JAssert(false, "an error occurred in OnEventSourceEvent() \n" + ex);
  303. }
  304. return objRet.rc;
  305. }
  306. function PDtest(params)
  307. {
  308. var aTests = [ "spin", "sleep", "hostrpc", "remoterpc" ] ;
  309. var aTests = [ "exec" ];
  310. var nDuration = 10 * 1000; // 10 second test.
  311. var i;
  312. var nStartTime;
  313. var aStart = new Array();
  314. PrivateData.objPDTest = new Object();
  315. ResetSync("starttest,exittest");
  316. for( i = 0; i < aTests.length ; ++i)
  317. {
  318. SpawnScript('pdtest.js', [aTests[i], nDuration, "starttest" + aTests[i], "exittest"].toString() );
  319. aStart[aStart.length] = "starttest" + aTests[i];
  320. }
  321. LogMsg("Test threads created");
  322. for( i = 0; i < aTests.length ; ++i)
  323. WaitForSync(aTests[i] + "ThreadReady", 0);
  324. LogMsg("Test threads ready");
  325. /*LogMsg("Test access data while threads are idle (ATest)");
  326. nStartTime = (new Date()).getTime();
  327. while ( (new Date()).getTime() - nStartTime < nDuration)
  328. {
  329. AccessPDTestData(aTests, "ATest");
  330. }
  331. */
  332. Sleep(1);
  333. for( i = 0; i < aTests.length ; ++i)
  334. {
  335. LogMsg("Test access data while thread " + aTests[i] + " is busy (BTest)");
  336. SignalThreadSync(aStart[i]);
  337. nStartTime = (new Date()).getTime();
  338. while ( (new Date()).getTime() - nStartTime < nDuration)
  339. {
  340. AccessPDTestData(aTests, "BTest");
  341. }
  342. }
  343. SignalThreadSync('exittest');
  344. LogMsg("Waiting for threads to exit");
  345. for( i = 0; i < aTests.length ; ++i)
  346. WaitForSync(aTests[i] + "ThreadExit", 0);
  347. PrivateData.objUtil.fnDumpTimes();
  348. }
  349. function AccessPDTestData(aTests, strPrefix)
  350. {
  351. var i;
  352. for( i = 0; i < aTests.length ; ++i)
  353. {
  354. LogMsg("Attempting to access PrivateData.objPDTest." + aTests[i]);
  355. var watch = PrivateData.objUtil.fnBeginWatch(strPrefix + "PrivateData.objPDTest." + aTests[i]);
  356. var str = PrivateData.objUtil.fnUneval( PrivateData.objPDTest[aTests[i]]);
  357. MyEval(str);
  358. watch.Stop();
  359. }
  360. }
  361. function BuildExeTest()
  362. {
  363. }