FreeRDP
TestCommDevice.c
1 
20 #include <stdio.h>
21 
22 #include <winpr/comm.h>
23 #include <winpr/tchar.h>
24 
25 static int test_CommDevice(LPCTSTR lpDeviceName, BOOL expectedResult)
26 {
27  TCHAR lpTargetPath[MAX_PATH] = { 0 };
28 
29  BOOL result = DefineCommDevice(lpDeviceName, _T("/dev/test"));
30  if ((!expectedResult && result) || (expectedResult && !result)) /* logical XOR */
31  {
32  _tprintf(_T("DefineCommDevice failure: device name: %s, expected result: %s, result: %s\n"),
33  lpDeviceName, (expectedResult ? "TRUE" : "FALSE"), (result ? "TRUE" : "FALSE"));
34 
35  return FALSE;
36  }
37 
38  result = IsCommDevice(lpDeviceName);
39  if ((!expectedResult && result) || (expectedResult && !result)) /* logical XOR */
40  {
41  _tprintf(_T("IsCommDevice failure: device name: %s, expected result: %s, result: %s\n"),
42  lpDeviceName, (expectedResult ? "TRUE" : "FALSE"), (result ? "TRUE" : "FALSE"));
43 
44  return FALSE;
45  }
46 
47  const size_t tclen = QueryCommDevice(lpDeviceName, lpTargetPath, MAX_PATH);
48  if (expectedResult)
49  {
50  const size_t tlen = _tcsnlen(lpTargetPath, ARRAYSIZE(lpTargetPath) - 1);
51  if (tclen <= tlen) /* at least 2 more TCHAR are expected */
52  {
53  _tprintf(_T("QueryCommDevice failure: didn't find the device name: %s\n"),
54  lpDeviceName);
55  return FALSE;
56  }
57 
58  if (_tcsncmp(_T("/dev/test"), lpTargetPath, ARRAYSIZE(lpTargetPath)) != 0)
59  {
60  _tprintf(
61  _T("QueryCommDevice failure: device name: %s, expected result: %s, result: %s\n"),
62  lpDeviceName, _T("/dev/test"), lpTargetPath);
63 
64  return FALSE;
65  }
66 
67  if ((tlen >= (ARRAYSIZE(lpTargetPath) - 1)) || (lpTargetPath[tlen + 1] != 0))
68  {
69  _tprintf(_T("QueryCommDevice failure: device name: %s, the second NULL character is ")
70  _T("missing at the end of the buffer\n"),
71  lpDeviceName);
72  return FALSE;
73  }
74  }
75  else
76  {
77  if (tclen > 0)
78  {
79  _tprintf(_T("QueryCommDevice failure: device name: %s, expected result: <none>, ")
80  _T("result: %") _T(PRIuz) _T(" %s\n"),
81  lpDeviceName, tclen, lpTargetPath);
82 
83  return FALSE;
84  }
85  }
86 
87  return TRUE;
88 }
89 
90 int TestCommDevice(int argc, char* argv[])
91 {
92  if (!test_CommDevice(_T("COM0"), FALSE))
93  return EXIT_FAILURE;
94 
95  if (!test_CommDevice(_T("COM1"), TRUE))
96  return EXIT_FAILURE;
97 
98  if (!test_CommDevice(_T("COM1"), TRUE))
99  return EXIT_FAILURE;
100 
101  if (!test_CommDevice(_T("COM10"), FALSE))
102  return EXIT_FAILURE;
103 
104  if (!test_CommDevice(_T("\\\\.\\COM5"), TRUE))
105  return EXIT_FAILURE;
106 
107  if (!test_CommDevice(_T("\\\\.\\COM10"), TRUE))
108  return EXIT_FAILURE;
109 
110  if (!test_CommDevice(_T("\\\\.COM10"), FALSE))
111  return EXIT_FAILURE;
112 
113  return 0;
114 }