Blame winpr/libwinpr/comm/test/TestCommDevice.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * WinPR: Windows Portable Runtime
Packit 1fb8d4
 * Serial Communication API
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2014 Hewlett-Packard Development Company, L.P.
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/comm.h>
Packit 1fb8d4
#include <winpr/tchar.h>
Packit 1fb8d4
Packit 1fb8d4
static int test_CommDevice(LPCTSTR lpDeviceName, BOOL expectedResult)
Packit 1fb8d4
{
Packit 1fb8d4
	BOOL result;
Packit 1fb8d4
	TCHAR lpTargetPath[MAX_PATH];
Packit 1fb8d4
	size_t tcslen;
Packit 1fb8d4
Packit 1fb8d4
	result = DefineCommDevice(lpDeviceName, _T("/dev/test"));
Packit 1fb8d4
	if ((!expectedResult && result) || (expectedResult && !result)) /* logical XOR */
Packit 1fb8d4
	{
Packit 1fb8d4
		_tprintf(_T("DefineCommDevice failure: device name: %s, expected result: %s, result: %s\n"),
Packit Service 5a9772
		         lpDeviceName, (expectedResult ? "TRUE" : "FALSE"), (result ? "TRUE" : "FALSE"));
Packit 1fb8d4
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	result = IsCommDevice(lpDeviceName);
Packit 1fb8d4
	if ((!expectedResult && result) || (expectedResult && !result)) /* logical XOR */
Packit 1fb8d4
	{
Packit 1fb8d4
		_tprintf(_T("IsCommDevice failure: device name: %s, expected result: %s, result: %s\n"),
Packit Service 5a9772
		         lpDeviceName, (expectedResult ? "TRUE" : "FALSE"), (result ? "TRUE" : "FALSE"));
Packit 1fb8d4
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	tcslen = (size_t)QueryCommDevice(lpDeviceName, lpTargetPath, MAX_PATH);
Packit 1fb8d4
	if (expectedResult)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (tcslen <= _tcslen(lpTargetPath)) /* at least 2 more TCHAR are expected */
Packit 1fb8d4
		{
Packit Service 5a9772
			_tprintf(_T("QueryCommDevice failure: didn't find the device name: %s\n"),
Packit Service 5a9772
			         lpDeviceName);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (_tcscmp(_T("/dev/test"), lpTargetPath) != 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			_tprintf(
Packit Service 5a9772
			    _T("QueryCommDevice failure: device name: %s, expected result: %s, result: %s\n"),
Packit Service 5a9772
			    lpDeviceName, _T("/dev/test"), lpTargetPath);
Packit 1fb8d4
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (lpTargetPath[_tcslen(lpTargetPath) + 1] != 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			_tprintf(_T("QueryCommDevice failure: device name: %s, the second NULL character is ")
Packit Service 5a9772
			         _T("missing at the end of the buffer\n"),
Packit Service 5a9772
			         lpDeviceName);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		if (tcslen > 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			_tprintf(_T("QueryCommDevice failure: device name: %s, expected result: <none>, ")
Packit Service 5a9772
			         _T("result: %") _T(PRIuz) _T(" %s\n"),
Packit Service 5a9772
			         lpDeviceName, tcslen, lpTargetPath);
Packit 1fb8d4
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int TestCommDevice(int argc, char* argv[])
Packit 1fb8d4
{
Packit 1fb8d4
	if (!test_CommDevice(_T("COM0"), FALSE))
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_CommDevice(_T("COM1"), TRUE))
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_CommDevice(_T("COM1"), TRUE))
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_CommDevice(_T("COM10"), FALSE))
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_CommDevice(_T("\\\\.\\COM5"), TRUE))
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_CommDevice(_T("\\\\.\\COM10"), TRUE))
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
Packit 1fb8d4
	if (!test_CommDevice(_T("\\\\.COM10"), FALSE))
Packit 1fb8d4
		return EXIT_FAILURE;
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}