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.

995 lines
17 KiB

  1. //
  2. // Test the quick return timeouts
  3. //
  4. // Assume that we are using a loopback connector.
  5. //
  6. // Assume that it isn't running on a stressed machine.
  7. //
  8. #include "windows.h"
  9. #include "stdio.h"
  10. #define FAILURE printf("FAIL: %d\n",__LINE__);exit(1)
  11. int __cdecl main(int argc, char *argv[]) {
  12. CHAR *myPort = "COM1";
  13. DCB myDcb;
  14. DWORD junk;
  15. COMMTIMEOUTS myTimeOuts;
  16. DWORD numberActuallyRead;
  17. DWORD numberActuallyWritten;
  18. UCHAR readBuff[1000];
  19. HANDLE comHandle;
  20. DWORD startingTicks;
  21. OVERLAPPED readOl;
  22. OVERLAPPED writeOl;
  23. UCHAR writeBuff[5] = {0,1,2,3,4};
  24. if (argc > 1) {
  25. myPort = argv[1];
  26. }
  27. if ((comHandle = CreateFile(
  28. myPort,
  29. GENERIC_READ | GENERIC_WRITE,
  30. 0,
  31. NULL,
  32. CREATE_ALWAYS,
  33. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  34. NULL
  35. )) == ((HANDLE)-1)) {
  36. FAILURE;
  37. }
  38. if (!(readOl.hEvent = CreateEvent(
  39. NULL,
  40. TRUE,
  41. FALSE,
  42. NULL
  43. ))) {
  44. FAILURE;
  45. }
  46. if (!(writeOl.hEvent = CreateEvent(
  47. NULL,
  48. TRUE,
  49. FALSE,
  50. NULL
  51. ))) {
  52. FAILURE;
  53. }
  54. if (!GetCommState(
  55. comHandle,
  56. &myDcb
  57. )) {
  58. FAILURE;
  59. }
  60. myDcb.BaudRate = 19200;
  61. myDcb.ByteSize = 8;
  62. myDcb.StopBits = ONESTOPBIT;
  63. myDcb.Parity = NOPARITY;
  64. myDcb.fOutxCtsFlow = FALSE;
  65. myDcb.fOutxDsrFlow = FALSE;
  66. myDcb.fDsrSensitivity = FALSE;
  67. myDcb.fOutX = FALSE;
  68. myDcb.fInX = FALSE;
  69. myDcb.fRtsControl = RTS_CONTROL_ENABLE;
  70. myDcb.fDtrControl = DTR_CONTROL_ENABLE;
  71. if (!SetCommState(
  72. comHandle,
  73. &myDcb
  74. )) {
  75. FAILURE;
  76. }
  77. //
  78. // Test to make sure that all maxdword on read is illegal.
  79. //
  80. myTimeOuts.ReadIntervalTimeout = MAXDWORD;
  81. myTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD;
  82. myTimeOuts.ReadTotalTimeoutConstant = MAXDWORD;
  83. myTimeOuts.WriteTotalTimeoutMultiplier = MAXDWORD;
  84. myTimeOuts.WriteTotalTimeoutConstant = MAXDWORD;
  85. if (SetCommTimeouts(
  86. comHandle,
  87. &myTimeOuts
  88. )) {
  89. FAILURE;
  90. }
  91. //
  92. // Test that MAXDWORD,0,0 will return immediately with whatever
  93. // is there
  94. //
  95. myTimeOuts.ReadIntervalTimeout = MAXDWORD;
  96. myTimeOuts.ReadTotalTimeoutMultiplier = 0;
  97. myTimeOuts.ReadTotalTimeoutConstant = 0;
  98. myTimeOuts.WriteTotalTimeoutMultiplier = MAXDWORD;
  99. myTimeOuts.WriteTotalTimeoutConstant = MAXDWORD;
  100. if (!SetCommTimeouts(
  101. comHandle,
  102. &myTimeOuts
  103. )) {
  104. FAILURE;
  105. }
  106. startingTicks = GetTickCount();
  107. if (!ReadFile(
  108. comHandle,
  109. &readBuff[0],
  110. 1000,
  111. &numberActuallyRead,
  112. &readOl
  113. )) {
  114. if (GetLastError() != ERROR_IO_PENDING) {
  115. FAILURE;
  116. }
  117. if (!GetOverlappedResult(
  118. comHandle,
  119. &readOl,
  120. &numberActuallyRead,
  121. TRUE
  122. )) {
  123. FAILURE;
  124. }
  125. }
  126. //
  127. // We certainly should have gotten back in less than a
  128. // a half a second.
  129. //
  130. if ((GetTickCount() - startingTicks) > 500) {
  131. FAILURE;
  132. }
  133. if (numberActuallyRead) {
  134. FAILURE;
  135. }
  136. //
  137. // Write out five bytes and make sure that is what we get back
  138. //
  139. if (!WriteFile(
  140. comHandle,
  141. &writeBuff[0],
  142. 5,
  143. &numberActuallyWritten,
  144. &writeOl
  145. )) {
  146. if (GetLastError() != ERROR_IO_PENDING) {
  147. FAILURE;
  148. }
  149. if (!GetOverlappedResult(
  150. comHandle,
  151. &writeOl,
  152. &numberActuallyWritten,
  153. TRUE
  154. )) {
  155. FAILURE;
  156. }
  157. if (numberActuallyWritten != 5) {
  158. FAILURE;
  159. }
  160. }
  161. //
  162. // Give some time for the chars to get there.
  163. //
  164. Sleep (100);
  165. startingTicks = GetTickCount();
  166. if (!ReadFile(
  167. comHandle,
  168. &readBuff[0],
  169. 1000,
  170. &numberActuallyRead,
  171. &readOl
  172. )) {
  173. if (GetLastError() != ERROR_IO_PENDING) {
  174. FAILURE;
  175. }
  176. if (!GetOverlappedResult(
  177. comHandle,
  178. &readOl,
  179. &numberActuallyRead,
  180. TRUE
  181. )) {
  182. FAILURE;
  183. }
  184. }
  185. //
  186. // We certainly should have gotten back in less than a
  187. // a half a second.
  188. //
  189. if ((GetTickCount() - startingTicks) > 500) {
  190. FAILURE;
  191. }
  192. if (numberActuallyRead != 5) {
  193. FAILURE;
  194. }
  195. //
  196. // Test that the os2 wait for something works.
  197. //
  198. // First test that if there is something in the buffer
  199. // it returns right away.
  200. //
  201. // Then test that if there isn't something, then if we
  202. // put in the amount expected before the timeout expires
  203. // that it returns.
  204. //
  205. // The test that if there isn't something and nothing
  206. // happens before the timeout it returns after the timeout
  207. // with nothing.
  208. //
  209. myTimeOuts.ReadIntervalTimeout = MAXDWORD;
  210. myTimeOuts.ReadTotalTimeoutMultiplier = 0;
  211. myTimeOuts.ReadTotalTimeoutConstant = 5000;
  212. myTimeOuts.WriteTotalTimeoutMultiplier = MAXDWORD;
  213. myTimeOuts.WriteTotalTimeoutConstant = MAXDWORD;
  214. if (!SetCommTimeouts(
  215. comHandle,
  216. &myTimeOuts
  217. )) {
  218. FAILURE;
  219. }
  220. if (!WriteFile(
  221. comHandle,
  222. &writeBuff[0],
  223. 5,
  224. &numberActuallyWritten,
  225. &writeOl
  226. )) {
  227. if (GetLastError() != ERROR_IO_PENDING) {
  228. FAILURE;
  229. }
  230. if (!GetOverlappedResult(
  231. comHandle,
  232. &writeOl,
  233. &numberActuallyWritten,
  234. TRUE
  235. )) {
  236. FAILURE;
  237. }
  238. if (numberActuallyWritten != 5) {
  239. FAILURE;
  240. }
  241. }
  242. //
  243. // Give some time for the chars to get there.
  244. //
  245. Sleep (100);
  246. startingTicks = GetTickCount();
  247. if (!ReadFile(
  248. comHandle,
  249. &readBuff[0],
  250. 1000,
  251. &numberActuallyRead,
  252. &readOl
  253. )) {
  254. if (GetLastError() != ERROR_IO_PENDING) {
  255. FAILURE;
  256. }
  257. //
  258. // Give it at most a 1/2 second to finish for
  259. // the irp to complete immediately.
  260. //
  261. Sleep(500);
  262. if (!GetOverlappedResult(
  263. comHandle,
  264. &readOl,
  265. &numberActuallyRead,
  266. FALSE
  267. )) {
  268. FAILURE;
  269. }
  270. }
  271. if ((GetTickCount() - startingTicks) > 1000) {
  272. FAILURE;
  273. }
  274. if (numberActuallyRead != 5) {
  275. FAILURE;
  276. }
  277. //
  278. // Do the second os2 test
  279. //
  280. if (ReadFile(
  281. comHandle,
  282. &readBuff[0],
  283. 1000,
  284. &numberActuallyRead,
  285. &readOl
  286. )) {
  287. FAILURE;
  288. }
  289. if (GetLastError() != ERROR_IO_PENDING) {
  290. FAILURE;
  291. }
  292. //
  293. // Give it a second for the the read to complete
  294. //
  295. //
  296. Sleep(1000);
  297. //
  298. // Call the GetOverlapped and make sure that it returns
  299. // ERROR_IO_INCOMPLETE.
  300. //
  301. if (GetOverlappedResult(
  302. comHandle,
  303. &readOl,
  304. &numberActuallyRead,
  305. FALSE
  306. )) {
  307. FAILURE;
  308. }
  309. if (GetLastError() != ERROR_IO_INCOMPLETE) {
  310. FAILURE;
  311. }
  312. //
  313. // Do the write file and make sure that there is enough
  314. // time for the chars to make it.
  315. //
  316. if (!WriteFile(
  317. comHandle,
  318. &writeBuff[0],
  319. 5,
  320. &numberActuallyWritten,
  321. &writeOl
  322. )) {
  323. if (GetLastError() != ERROR_IO_PENDING) {
  324. FAILURE;
  325. }
  326. if (!GetOverlappedResult(
  327. comHandle,
  328. &writeOl,
  329. &numberActuallyWritten,
  330. TRUE
  331. )) {
  332. FAILURE;
  333. }
  334. if (numberActuallyWritten != 5) {
  335. FAILURE;
  336. }
  337. }
  338. //
  339. // Give some time for the chars to get there.
  340. //
  341. Sleep(100);
  342. //
  343. // Wait for no more than 6 seconds for the IO to complete
  344. //
  345. if (WaitForSingleObject(
  346. readOl.hEvent,
  347. 6000
  348. ) != WAIT_OBJECT_0) {
  349. FAILURE;
  350. }
  351. //
  352. // Make sure we got everything we wrote
  353. //
  354. if (!GetOverlappedResult(
  355. comHandle,
  356. &readOl,
  357. &numberActuallyRead,
  358. FALSE
  359. )) {
  360. FAILURE;
  361. }
  362. if (numberActuallyRead != 5) {
  363. FAILURE;
  364. }
  365. //
  366. // Do the third os2 wait for something test.
  367. //
  368. startingTicks = GetTickCount();
  369. if (ReadFile(
  370. comHandle,
  371. &readBuff[0],
  372. 1000,
  373. &numberActuallyRead,
  374. &readOl
  375. )) {
  376. FAILURE;
  377. }
  378. if (GetLastError() != ERROR_IO_PENDING) {
  379. FAILURE;
  380. }
  381. //
  382. // Give it a second for the the read to complete
  383. //
  384. //
  385. Sleep(1000);
  386. //
  387. // Call the GetOverlapped and make sure that it returns
  388. // ERROR_IO_INCOMPLETE.
  389. //
  390. if (GetOverlappedResult(
  391. comHandle,
  392. &readOl,
  393. &numberActuallyRead,
  394. FALSE
  395. )) {
  396. FAILURE;
  397. }
  398. if (GetLastError() != ERROR_IO_INCOMPLETE) {
  399. FAILURE;
  400. }
  401. //
  402. // Wait for no more than 10 seconds for the IO to complete
  403. //
  404. if (WaitForSingleObject(
  405. readOl.hEvent,
  406. 10000
  407. ) != WAIT_OBJECT_0) {
  408. FAILURE;
  409. }
  410. //
  411. // It shouldn't be more than 6 seconds for the Io to be done.
  412. //
  413. if ((GetTickCount() - startingTicks) > 6000) {
  414. FAILURE;
  415. }
  416. //
  417. // Make sure we got everything we wrote, which in this case is zero.
  418. //
  419. if (!GetOverlappedResult(
  420. comHandle,
  421. &readOl,
  422. &numberActuallyRead,
  423. FALSE
  424. )) {
  425. FAILURE;
  426. }
  427. if (numberActuallyRead) {
  428. FAILURE;
  429. }
  430. //
  431. // Test the graphics mode quick return.
  432. //
  433. // First test that if there is something in the buffer
  434. // it returns right away.
  435. //
  436. // Then test that if there isn't something, then if we
  437. // put in 2 characters it returns right away with one
  438. // and then the other read will return right away with
  439. // 1.
  440. //
  441. // Then test that if there isn't something and nothing
  442. // happens before the timeout it returns after the timeout
  443. // with nothing.
  444. //
  445. myTimeOuts.ReadIntervalTimeout = MAXDWORD;
  446. myTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD;
  447. myTimeOuts.ReadTotalTimeoutConstant = 5000;
  448. myTimeOuts.WriteTotalTimeoutMultiplier = MAXDWORD;
  449. myTimeOuts.WriteTotalTimeoutConstant = MAXDWORD;
  450. if (!SetCommTimeouts(
  451. comHandle,
  452. &myTimeOuts
  453. )) {
  454. FAILURE;
  455. }
  456. if (!WriteFile(
  457. comHandle,
  458. &writeBuff[0],
  459. 5,
  460. &numberActuallyWritten,
  461. &writeOl
  462. )) {
  463. if (GetLastError() != ERROR_IO_PENDING) {
  464. FAILURE;
  465. }
  466. if (!GetOverlappedResult(
  467. comHandle,
  468. &writeOl,
  469. &numberActuallyWritten,
  470. TRUE
  471. )) {
  472. FAILURE;
  473. }
  474. if (numberActuallyWritten != 5) {
  475. FAILURE;
  476. }
  477. }
  478. //
  479. // Give some time for the chars to get there.
  480. //
  481. Sleep (100);
  482. startingTicks = GetTickCount();
  483. if (!ReadFile(
  484. comHandle,
  485. &readBuff[0],
  486. 1000,
  487. &numberActuallyRead,
  488. &readOl
  489. )) {
  490. if (GetLastError() != ERROR_IO_PENDING) {
  491. FAILURE;
  492. }
  493. //
  494. // Give it at most a 1/2 second to finish for
  495. // the irp to complete immediately.
  496. //
  497. Sleep(500);
  498. if (!GetOverlappedResult(
  499. comHandle,
  500. &readOl,
  501. &numberActuallyRead,
  502. FALSE
  503. )) {
  504. FAILURE;
  505. }
  506. }
  507. if ((GetTickCount() - startingTicks) > 1000) {
  508. FAILURE;
  509. }
  510. if (numberActuallyRead != 5) {
  511. FAILURE;
  512. }
  513. //
  514. // Do the second graphics wait test.
  515. //
  516. if (ReadFile(
  517. comHandle,
  518. &readBuff[0],
  519. 1000,
  520. &numberActuallyRead,
  521. &readOl
  522. )) {
  523. FAILURE;
  524. }
  525. if (GetLastError() != ERROR_IO_PENDING) {
  526. FAILURE;
  527. }
  528. //
  529. // Give it a second for the the read to complete
  530. //
  531. //
  532. Sleep(1000);
  533. //
  534. // Call the GetOverlapped and make sure that it returns
  535. // ERROR_IO_INCOMPLETE.
  536. //
  537. if (GetOverlappedResult(
  538. comHandle,
  539. &readOl,
  540. &numberActuallyRead,
  541. FALSE
  542. )) {
  543. FAILURE;
  544. }
  545. if (GetLastError() != ERROR_IO_INCOMPLETE) {
  546. FAILURE;
  547. }
  548. //
  549. // Do the write file and make sure that there is enough
  550. // time for the chars to make it.
  551. //
  552. if (!WriteFile(
  553. comHandle,
  554. &writeBuff[0],
  555. 5,
  556. &numberActuallyWritten,
  557. &writeOl
  558. )) {
  559. if (GetLastError() != ERROR_IO_PENDING) {
  560. FAILURE;
  561. }
  562. if (!GetOverlappedResult(
  563. comHandle,
  564. &writeOl,
  565. &numberActuallyWritten,
  566. TRUE
  567. )) {
  568. FAILURE;
  569. }
  570. if (numberActuallyWritten != 5) {
  571. FAILURE;
  572. }
  573. }
  574. //
  575. // Give some time for the chars to get there.
  576. //
  577. Sleep(100);
  578. //
  579. // Wait for no more than 1 second for the IO to complete
  580. //
  581. if (WaitForSingleObject(
  582. readOl.hEvent,
  583. 1000
  584. ) != WAIT_OBJECT_0) {
  585. FAILURE;
  586. }
  587. //
  588. // Make sure we got everything we wrote
  589. //
  590. if (!GetOverlappedResult(
  591. comHandle,
  592. &readOl,
  593. &numberActuallyRead,
  594. FALSE
  595. )) {
  596. FAILURE;
  597. }
  598. if (numberActuallyRead != 1) {
  599. FAILURE;
  600. }
  601. startingTicks = GetTickCount();
  602. if (!ReadFile(
  603. comHandle,
  604. &readBuff[0],
  605. 1000,
  606. &numberActuallyRead,
  607. &readOl
  608. )) {
  609. if (GetLastError() != ERROR_IO_PENDING) {
  610. FAILURE;
  611. }
  612. //
  613. // Give it at most a 1/2 second to finish for
  614. // the irp to complete immediately.
  615. //
  616. Sleep(500);
  617. if (!GetOverlappedResult(
  618. comHandle,
  619. &readOl,
  620. &numberActuallyRead,
  621. FALSE
  622. )) {
  623. FAILURE;
  624. }
  625. }
  626. if ((GetTickCount() - startingTicks) > 1000) {
  627. FAILURE;
  628. }
  629. if (numberActuallyRead != 4) {
  630. FAILURE;
  631. }
  632. //
  633. // Do the third graphics wait test.
  634. //
  635. startingTicks = GetTickCount();
  636. if (ReadFile(
  637. comHandle,
  638. &readBuff[0],
  639. 1000,
  640. &numberActuallyRead,
  641. &readOl
  642. )) {
  643. FAILURE;
  644. }
  645. if (GetLastError() != ERROR_IO_PENDING) {
  646. FAILURE;
  647. }
  648. //
  649. // Give it a second for the the read to complete
  650. //
  651. //
  652. Sleep(1000);
  653. //
  654. // Call the GetOverlapped and make sure that it returns
  655. // ERROR_IO_INCOMPLETE.
  656. //
  657. if (GetOverlappedResult(
  658. comHandle,
  659. &readOl,
  660. &numberActuallyRead,
  661. FALSE
  662. )) {
  663. FAILURE;
  664. }
  665. if (GetLastError() != ERROR_IO_INCOMPLETE) {
  666. FAILURE;
  667. }
  668. //
  669. // Wait for no more than 10 seconds for the IO to complete
  670. //
  671. if (WaitForSingleObject(
  672. readOl.hEvent,
  673. 10000
  674. ) != WAIT_OBJECT_0) {
  675. FAILURE;
  676. }
  677. //
  678. // It shouldn't be more than 6 seconds for the Io to be done.
  679. //
  680. if ((GetTickCount() - startingTicks) > 6000) {
  681. FAILURE;
  682. }
  683. //
  684. // Make sure we got everything we wrote, which in this case is zero.
  685. //
  686. if (!GetOverlappedResult(
  687. comHandle,
  688. &readOl,
  689. &numberActuallyRead,
  690. FALSE
  691. )) {
  692. FAILURE;
  693. }
  694. if (numberActuallyRead) {
  695. FAILURE;
  696. }
  697. return 1;
  698. }