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