FreeRDP
TestGetCommState.c
1 
20 #include <stdio.h>
21 #include <sys/stat.h>
22 
23 #include <winpr/comm.h>
24 #include <winpr/crt.h>
25 
26 #include "../comm.h"
27 
28 static BOOL test_generic(HANDLE hComm)
29 {
30  DCB dcb = { 0 };
31  DCB* pDcb = NULL;
32  BOOL result = 0;
33 
34  ZeroMemory(&dcb, sizeof(DCB));
35  result = GetCommState(hComm, &dcb);
36  if (result)
37  {
38  printf("GetCommState failure, should have returned false because dcb.DCBlength has been "
39  "let uninitialized\n");
40  return FALSE;
41  }
42 
43  ZeroMemory(&dcb, sizeof(DCB));
44  dcb.DCBlength = sizeof(DCB) / 2; /* improper value */
45  result = GetCommState(hComm, &dcb);
46  if (result)
47  {
48  printf("GetCommState failure, should have return false because dcb.DCBlength was not "
49  "correctly initialized\n");
50  return FALSE;
51  }
52 
53  ZeroMemory(&dcb, sizeof(DCB));
54  dcb.DCBlength = sizeof(DCB);
55  result = GetCommState(hComm, &dcb);
56  if (!result)
57  {
58  printf("GetCommState failure: Ox%x, with adjusted DCBlength\n", GetLastError());
59  return FALSE;
60  }
61 
62  pDcb = (DCB*)calloc(2, sizeof(DCB));
63  if (!pDcb)
64  return FALSE;
65  pDcb->DCBlength = sizeof(DCB) * 2;
66  result = GetCommState(hComm, pDcb);
67  result = result && (pDcb->DCBlength == sizeof(DCB) * 2);
68  free(pDcb);
69  if (!result)
70  {
71  printf("GetCommState failure: 0x%x, with bigger DCBlength\n", GetLastError());
72  return FALSE;
73  }
74 
75  return TRUE;
76 }
77 
78 int TestGetCommState(int argc, char* argv[])
79 {
80  struct stat statbuf = { 0 };
81  BOOL result = 0;
82  HANDLE hComm = NULL;
83 
84  if (stat("/dev/ttyS0", &statbuf) < 0)
85  {
86  (void)fprintf(stderr, "/dev/ttyS0 not available, making the test to succeed though\n");
87  return EXIT_SUCCESS;
88  }
89 
90  result = DefineCommDevice("COM1", "/dev/ttyS0");
91  if (!result)
92  {
93  printf("DefineCommDevice failure: 0x%x\n", GetLastError());
94  return EXIT_FAILURE;
95  }
96 
97  hComm = CreateFileA("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
98 
99  if (hComm == INVALID_HANDLE_VALUE)
100  {
101  printf("CreateFileA failure: 0x%x\n", GetLastError());
102  return EXIT_FAILURE;
103  }
104 
105  if (!test_generic(hComm))
106  {
107  printf("test_generic failure (SerialDriverUnknown)\n");
108  return EXIT_FAILURE;
109  }
110 
111  _comm_setServerSerialDriver(hComm, SerialDriverSerialSys);
112  if (!test_generic(hComm))
113  {
114  printf("test_generic failure (SerialDriverSerialSys)\n");
115  return EXIT_FAILURE;
116  }
117 
118  _comm_setServerSerialDriver(hComm, SerialDriverSerCxSys);
119  if (!test_generic(hComm))
120  {
121  printf("test_generic failure (SerialDriverSerCxSys)\n");
122  return EXIT_FAILURE;
123  }
124 
125  _comm_setServerSerialDriver(hComm, SerialDriverSerCx2Sys);
126  if (!test_generic(hComm))
127  {
128  printf("test_generic failure (SerialDriverSerCx2Sys)\n");
129  return EXIT_FAILURE;
130  }
131 
132  if (!CloseHandle(hComm))
133  {
134  (void)fprintf(stderr, "CloseHandle failure, GetLastError()=%08x\n", GetLastError());
135  return EXIT_FAILURE;
136  }
137 
138  return EXIT_SUCCESS;
139 }