Debian Codegraph @

winpr/libwinpr/comm/test/TestCommDevice.c

raw
1 /**
2 * WinPR: Windows Portable Runtime
3 * Serial Communication API
4 *
5 * Copyright 2014 Hewlett-Packard Development Company, L.P.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
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] = WINPR_C_ARRAY_INIT;
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(
70 _T("QueryCommDevice failure: device name: %s, the second nullptr character is ")
71 _T("missing at the end of the buffer\n"),
72 lpDeviceName);
73 return FALSE;
74 }
75 }
76 else
77 {
78 if (tclen > 0)
79 {
80 _tprintf(_T("QueryCommDevice failure: device name: %s, expected result: <none>, ")
81 _T("result: %") _T(PRIuz) _T(" %s\n"),
82 lpDeviceName, tclen, lpTargetPath);
83
84 return FALSE;
85 }
86 }
87
88 return TRUE;
89 }
90
91 int TestCommDevice(int argc, char* argv[])
92 {
93 if (!test_CommDevice(_T("COM0"), FALSE))
94 return EXIT_FAILURE;
95
96 if (!test_CommDevice(_T("COM1"), TRUE))
97 return EXIT_FAILURE;
98
99 if (!test_CommDevice(_T("COM1"), TRUE))
100 return EXIT_FAILURE;
101
102 if (!test_CommDevice(_T("COM10"), FALSE))
103 return EXIT_FAILURE;
104
105 if (!test_CommDevice(_T("\\\\.\\COM5"), TRUE))
106 return EXIT_FAILURE;
107
108 if (!test_CommDevice(_T("\\\\.\\COM10"), TRUE))
109 return EXIT_FAILURE;
110
111 if (!test_CommDevice(_T("\\\\.COM10"), FALSE))
112 return EXIT_FAILURE;
113
114 return 0;
115 }