You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_pcsc_cpp_tools.cpp 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <iostream>
  2. #include <string.h>
  3. #include <gtest/gtest.h>
  4. #include <ScHex.h>
  5. #include <ScReader.h>
  6. #include <ScBasicCommand.h>
  7. #define TEST_Hex(FUNCTION) do { \
  8. FUNCTION("", "", 0); \
  9. FUNCTION("42", "\x42", 1); \
  10. FUNCTION("42 00 FF AA 24", "\x42\x00\xff\xaa\x24", 5); \
  11. FUNCTION("00 01 02 03 04 05 06 FF", "\x00\x01\x02\x03\x04\x05\x06\xff", 8); \
  12. } while (0)
  13. #define TEST_byteArrayToString(STR, DATA, SIZE) \
  14. EXPECT_EQ(STR, ScHex::byteArrayToString(std::string((char*)memcpy(new BYTE[SIZE], DATA, SIZE), SIZE)))
  15. TEST(Hex, ByteArrayToString)
  16. {
  17. TEST_Hex(TEST_byteArrayToString);
  18. }
  19. #define TEST_stringToByteArray(STR, DATA, SIZE) do { \
  20. auto bytes = ScHex::stringToByteArray(STR); \
  21. EXPECT_EQ(SIZE, bytes.size()); \
  22. EXPECT_EQ(0, memcmp(bytes.data(), DATA, SIZE)); \
  23. } while (0)
  24. TEST(Hex, StringToByteArray)
  25. {
  26. TEST_Hex(TEST_stringToByteArray);
  27. }
  28. TEST(Reader, Test1)
  29. {
  30. std::cout << "Ensure that at lest 1 reader is connected";
  31. getchar();
  32. EXPECT_TRUE(ScReader::establishContext());
  33. auto readers = ScReader::getReaders();
  34. EXPECT_GE(readers.size(), 1);
  35. for (auto reader : readers)
  36. {
  37. std::cout << reader->getName() << std::endl;
  38. }
  39. auto reader = readers[0];
  40. EXPECT_TRUE(reader->connect());
  41. auto res = reader->transmit(ScBasicCommand("94 A4 00 00 02 20 00 00"));
  42. EXPECT_NE(nullptr, res->getData().data());
  43. auto resp = ScHex::byteArrayToString(res->getData());
  44. std::cout << "Response (" << res->getData().size() << "): " << resp << std::endl;
  45. EXPECT_TRUE(reader->disconnect());
  46. EXPECT_TRUE(ScReader::freeContext());
  47. }
  48. int main(int argc, char* argv[])
  49. {
  50. ::testing::InitGoogleTest(&argc, argv);
  51. return RUN_ALL_TESTS();
  52. /*std::cout << ScReader::establishContext() << std::endl;
  53. auto readers = ScReader::getReaders();
  54. for (auto reader : readers)
  55. {
  56. std::cout << reader->getName() << " ";
  57. std::cout << reader->connect() << " ";
  58. std::cout << reader->disconnect();
  59. std::cout << std::endl;
  60. }
  61. std::cout << ScReader::freeContext() << std::endl;
  62. return 0;*/
  63. }