Gtest框架进行Windows API测试:CreateFile和枚举文件
来源:互联网 发布:域名劫持查询 编辑:程序博客网 时间:2024/06/06 04:29
前期准备:
测试之前需要下载gtest
我用的是gtest1.7.0 链接: https://pan.baidu.com/s/1jHEyazk 密码: cbvg
如果从官网下载gtest,需要多一步自己编译gtest/msvc下的VC工程文件gtest.sln。
建立测试工程,将gtest/include添加到头文件路径中;将gtestd.lib或者gtest.lib添加到包含库中。
开始测试:
我先对CreateFile进行了简单的测试:
因为我需要对C盘和挂载盘进行测试对比,所以一开始定义了两个盘符变量。
#include "stdafx.h"#include <gtest/gtest.h>#include <string>#include <afx.h>char test_0[] = "A:/";char test_c_0[] = "C:/";class CreateTest : public::testing::TestWithParam<::testing::tuple<DWORD, DWORD, DWORD>> {};TEST_P(CreateTest, HandleTrueReturn1) { //CREATE_NEW 文件不存在时 //获取CreateFile各种参数的组合情况 DWORD n1 = ::testing::get<0>(GetParam()); DWORD n2 = ::testing::get<1>(GetParam()); DWORD n3 = ::testing::get<2>(GetParam()); //路径拼接+字符串类型变换 char test_path_c[100]; strcpy_s(test_path_c, test_c_0); strcat_s(test_path_c, "1.txt"); CString cstr_c = test_path_c; LPCTSTR path_c = (LPCTSTR)cstr_c; HANDLE fFile01_c = (HANDLE)CreateFile(path_c, n1, n2, NULL, CREATE_NEW, n3, NULL); DWORD lss = GetLastError(); CloseHandle(fFile01_c); DeleteFile(path_c); char test_path[100]; strcpy_s(test_path, test_0); strcat_s(test_path, "1.txt"); CString cstr = test_path; LPCTSTR path = (LPCTSTR)cstr; HANDLE fFile01 = (HANDLE)CreateFile(path, n1, n2, NULL, CREATE_NEW, n3, NULL); DWORD lss2 = GetLastError(); CloseHandle(fFile01); DeleteFile(path); EXPECT_EQ(lss, lss2);}TEST_P(CreateTest, HandleTrueReturn2) { //CREATE_NEW 文件存在时 DWORD n1 = ::testing::get<0>(GetParam()); DWORD n2 = ::testing::get<1>(GetParam()); DWORD n3 = ::testing::get<2>(GetParam()); char test_path_c[100]; strcpy_s(test_path_c, test_c_0); strcat_s(test_path_c, "2.txt"); CString cstr_c = test_path_c; LPCTSTR path_c = (LPCTSTR)cstr_c; HANDLE fFile01_c = (HANDLE)CreateFile(path_c, n1, n2, NULL, CREATE_NEW, n3, NULL); CloseHandle(fFile01_c); HANDLE fFile1_c = (HANDLE)CreateFile(path_c, n1, n2, NULL, CREATE_NEW, n3, NULL); DWORD lss = GetLastError(); CloseHandle(fFile1_c); DeleteFile(path_c); char test_path[100]; strcpy_s(test_path, test_0); strcat_s(test_path, "2.txt"); CString cstr = test_path; LPCTSTR path = (LPCTSTR)cstr; HANDLE fFile01 = (HANDLE)CreateFile(path, n1, n2, NULL, CREATE_NEW, n3, NULL); CloseHandle(fFile01); HANDLE fFile1 = (HANDLE)CreateFile(path, n1, n2, NULL, CREATE_NEW, n3, NULL); DWORD lss2 = GetLastError(); CloseHandle(fFile1); DeleteFile(path); EXPECT_EQ(lss, lss2);}DWORD A_0[4] = { GENERIC_WRITE ,GENERIC_READ,0,GENERIC_READ | GENERIC_WRITE };DWORD B_0[4] = { 0,FILE_SHARE_DELETE,FILE_SHARE_READ,FILE_SHARE_WRITE };DWORD D_0[4] = {FILE_ATTRIBUTE_ENCRYPTED,FILE_ATTRIBUTE_HIDDEN,FILE_ATTRIBUTE_NORMAL,FILE_ATTRIBUTE_READONLY};//gtest框里的combine函数可以将给定值进行排列组合INSTANTIATE_TEST_CASE_P(Return, CreateTest, testing::Combine(testing::Values(A_0[0], A_0[1], A_0[2], A_0[3]), testing::Values(B_0[0], B_0[1], B_0[2], B_0[3]), testing::Values(D_0[0], D_0[1], D_0[2])));int main(int argc, _TCHAR* argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();}
接下来是枚举文件的测试,同样是C盘与挂载盘:
#include "stdafx.h" #include <gtest/gtest.h>#include <iostream> #include <string.h> #include <Strsafe.h> #include <afx.h>using namespace std;char test_7[] = "A:/";char test_c_7[] = "C:/";class EnumFileTest : public::testing::TestWithParam<int> {};DWORD TraverseDirectory(wchar_t Dir[MAX_PATH], char flag) //传入路径 { WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; //定义要遍历的文件夹的目录 wchar_t DirSpec[MAX_PATH]; // DWORD dwError; StringCchCopy(DirSpec, MAX_PATH, Dir); //定义要遍历的文件夹的完整路径\* StringCchCat(DirSpec, MAX_PATH, TEXT("\\*")); //找到文件夹中的第一个文件 hFind = FindFirstFile(DirSpec, &FindFileData); DWORD lss = GetLastError(); //如果hFind句柄创建失败,输出错误信息 if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); return lss; } else { //当文件或者文件夹存在时 while (FindNextFile(hFind, &FindFileData) != 0) { //判断是文件夹&&表示为"."||表示为".." if ((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != 0 && wcscmp(FindFileData.cFileName, L".") == 0 || wcscmp(FindFileData.cFileName, L"..") == 0) { continue; } //判断如果是文件夹 if ((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != 0) { wchar_t DirAdd[MAX_PATH]; StringCchCopy(DirAdd, MAX_PATH, Dir); StringCchCat(DirAdd, MAX_PATH, TEXT("\\")); //拼接得到此文件夹的完整路径 StringCchCat(DirAdd, MAX_PATH, FindFileData.cFileName); //递归调用 TraverseDirectory(DirAdd, flag); } //如果不是文件夹的情况 if ((FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == 0) { //输出完整路径 wcout << Dir << "\\" << FindFileData.cFileName << endl; if (flag == 'A') { wchar_t Dir0[MAX_PATH]; char test_path_c[MAX_PATH]; test_path_c[0] = 'C'; for (int i = 1; i < MAX_PATH; i++) { test_path_c[i] = Dir[i]; } strcat_s(test_path_c, "\\"); char file_na[256]; int iLength = WideCharToMultiByte(CP_ACP, 0, FindFileData.cFileName, -1, NULL, 0, NULL, NULL); //将tchar值赋给_char WideCharToMultiByte(CP_ACP, 0, FindFileData.cFileName, -1, file_na, iLength, NULL, NULL); strcat_s(test_path_c, file_na); CString cstr_c = test_path_c; LPCTSTR path_c = (LPCTSTR)cstr_c; //简单判断下是否另一个盘是否有这个文件 //该判断只写了个单向的,真正判断两个盘枚举文件的正确性需要再写一个判断 HANDLE fFile = (HANDLE)CreateFile(path_c, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); DWORD lss0 = GetLastError(); CloseHandle(fFile); if (lss0 == 2) { wcout << "--------------------------------------------------------" << "---------------------------The another disk is no this file " << "!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl; } } } } FindClose(hFind); return 0; }}TEST_P(EnumFileTest, HandleTrueReturn1) { char test_path_c[100]; strcpy_s(test_path_c, test_c_7); strcat_s(test_path_c, "EnumFileTest"); size_t len_c = strlen(test_path_c) + 1; size_t converted_c = 0; wchar_t *path_c = (wchar_t*)malloc(len_c * sizeof(wchar_t)); mbstowcs_s(&converted_c, path_c, len_c, test_path_c, _TRUNCATE); DWORD lss1 = TraverseDirectory(path_c, 'C'); char test_path[100]; strcpy_s(test_path, test_7); strcat_s(test_path, "EnumFileTest"); size_t len = strlen(test_path) + 1; size_t converted = 0; wchar_t *path = (wchar_t*)malloc(len * sizeof(wchar_t)); mbstowcs_s(&converted, path, len, test_path, _TRUNCATE); DWORD lss2 = TraverseDirectory(path, 'A'); EXPECT_EQ(lss1, lss2);}INSTANTIATE_TEST_CASE_P(Return, EnumFileTest, testing::Values(0));
阅读全文
1 0
- Gtest框架进行Windows API测试:CreateFile和枚举文件
- CreateFile - Windows API
- CreateFile - Windows API
- Windows API —CreateFile
- 使用CreateFile API 枚举串口
- UT测试框架cxxtest和gtest对比
- 利用WINDOWS API函数操作文件(CreateFile、ReadFile 、MultiByteToWideChar)
- Windows文件操作的API(CreateFile系列)
- Windows文件操作的API(CreateFile系列)
- Windows文件操作的API(CreateFile系列)
- Windows API ——CreateFile——创建文件
- WINDOWS API ——CREATEFILE——创建文件
- 利用WINDOWS API函数操作文件(CreateFile、ReadFile 、MultiByteToWideChar)
- 测试框架之GTest
- gtest 测试框架
- 测试框架之GTest
- 基于gtest测试框架设计
- 谷歌测试框架gtest
- 堆排序算法分析与实现
- python的numpy包中shape,tile,argsort函数与sorted函数使用方法
- UVALive 6694
- 【LeetCode】Longest Palindrome(最长回文串)
- vs2015 安装及番茄插件使用
- Gtest框架进行Windows API测试:CreateFile和枚举文件
- Java经典算法题(一)
- KFC游记——生活中的网络安全篇一
- MacOS下彻底删除多余的Python版本
- java 异常,java,异常
- 今天学习内容2017826
- 根据读写锁设计一个缓存类
- Mysql 索引实现原理
- attr()里面的回调函数