Kaynağa Gözat

tests; removed byte array

master
Robin Thoni 9 yıl önce
ebeveyn
işleme
0bc0023241

+ 0
- 3
src/CMakeLists.txt Dosyayı Görüntüle

@@ -4,9 +4,6 @@ set(SOURCE_FILES
4 4
   DBO/ScCommand.cpp
5 5
   include/ScCommand.h
6 6
   include/ScCommand.hxx
7
-  DBO/ScByteArray.cpp
8
-  include/ScByteArray.h
9
-  include/ScByteArray.hxx
10 7
   DBO/ScBasicCommand.cpp
11 8
   include/ScBasicCommand.h
12 9
   include/ScBasicCommand.hxx

+ 1
- 1
src/DBO/ScBasicCommand.cpp Dosyayı Görüntüle

@@ -15,7 +15,7 @@ ScBasicCommand::~ScBasicCommand()
15 15
 {
16 16
 }
17 17
 
18
-ScByteArray ScBasicCommand::getData() const
18
+const std::string ScBasicCommand::getData() const
19 19
 {
20 20
     return _data;
21 21
 }

+ 0
- 6
src/DBO/ScByteArray.cpp Dosyayı Görüntüle

@@ -1,6 +0,0 @@
1
-//
2
-// Created by robin on 6/27/15.
3
-//
4
-
5
-#include "ScByteArray.h"
6
-

+ 10
- 9
src/DBO/ScHex.cpp Dosyayı Görüntüle

@@ -2,11 +2,12 @@
2 2
 // Created by robin on 6/29/15.
3 3
 //
4 4
 
5
-#include <sstream>
5
+#include <string>
6
+#include <wintypes.h>
6 7
 #include "ScHex.h"
7 8
 
8 9
 
9
-char ScHex::hexCharToInt(const char& c)
10
+const char ScHex::hexCharToInt(const char& c)
10 11
 {
11 12
     if (c >= 'a')
12 13
         return (char)(10 + (c - ('a' - 'A')) - 'A');
@@ -15,7 +16,7 @@ char ScHex::hexCharToInt(const char& c)
15 16
     return c - '0';
16 17
 }
17 18
 
18
-ScByteArray ScHex::stringToByteArray(const std::string &str)
19
+const std::string ScHex::stringToByteArray(const std::string &str)
19 20
 {
20 21
     std::string hexa;
21 22
     for (auto c : str)
@@ -30,22 +31,22 @@ ScByteArray ScHex::stringToByteArray(const std::string &str)
30 31
     {
31 32
         data[i / 2] = (hexCharToInt(hexa[i]) << 4) | hexCharToInt(hexa[i + 1]);
32 33
     }
33
-    return ScByteArray(data, hexa.size() / 2);
34
+    return std::string((char*)data, hexa.size() / 2);
34 35
 }
35 36
 
36
-std::string ScHex::byteArrayToString(const ScByteArray &bytes, const std::string &separator)
37
+const std::string ScHex::byteArrayToString(const std::string &bytes, const std::string &separator)
37 38
 {
38 39
     std::string res;
39
-    for (DWORD i = 0; i < bytes.getSize();++i)
40
+    for (DWORD i = 0; i < bytes.size();++i)
40 41
     {
41
-        res += intToHexChar(bytes.getData()[i]);
42
-        if (i != bytes.getSize() - 1)
42
+        res += intToHexChar(bytes.data()[i]);
43
+        if (i != bytes.size() - 1)
43 44
             res += separator;
44 45
     }
45 46
     return res;
46 47
 }
47 48
 
48
-std::string ScHex::intToHexChar(const char &c)
49
+const std::string ScHex::intToHexChar(const char &c)
49 50
 {
50 51
     std::string res;
51 52
     for (auto c1 : {(c & 0xf0) >> 4, c & 0x0f})

+ 1
- 1
src/DBO/ScResult.cpp Dosyayı Görüntüle

@@ -4,7 +4,7 @@
4 4
 
5 5
 #include "ScResult.h"
6 6
 
7
-ScResult::ScResult(const ScByteArray &data)
7
+ScResult::ScResult(const std::string &data)
8 8
     : _data(data)
9 9
 {
10 10
 }

+ 8
- 3
src/DataAccess/ScReader.cpp Dosyayı Görüntüle

@@ -4,6 +4,7 @@
4 4
 
5 5
 #include <winscard.h>
6 6
 #include <vector>
7
+#include <string.h>
7 8
 #include "ScReader.h"
8 9
 
9 10
 thread_local LPSCARDCONTEXT ScReader::_context = nullptr;
@@ -39,6 +40,10 @@ bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
39 40
     _card = new SCARDCONTEXT[1];
40 41
     _proto = new DWORD[1];
41 42
     auto res = SCardConnect(*_context, _name.c_str(), shareMode, protocols, _card, _proto);
43
+    if (*_proto == SCARD_PROTOCOL_T0)
44
+        _sendPci = *SCARD_PCI_T0;
45
+    if (*_proto == SCARD_PROTOCOL_T1)
46
+        _sendPci = *SCARD_PCI_T1;
42 47
     return res == SCARD_S_SUCCESS;
43 48
 }
44 49
 
@@ -58,10 +63,10 @@ std::shared_ptr<ScResult> ScReader::transmit(const ScCommand& command, DWORD siz
58 63
 {
59 64
     auto data = command.getData();
60 65
     auto resBuffer = new BYTE[size];
61
-    auto res = SCardTransmit(*_card, _sendPci, data.getData(), data.getSize(), nullptr, resBuffer, &size);
66
+    auto res = SCardTransmit(*_card, &_sendPci, (BYTE*)data.data(), data.size(), nullptr, resBuffer, &size);
62 67
     if (res == SCARD_S_SUCCESS)
63
-        return std::make_shared<ScResult>(ScByteArray(resBuffer, size));
64
-    return std::make_shared<ScResult>(ScByteArray());
68
+        return std::make_shared<ScResult>(std::string((char*)resBuffer, size));
69
+    return std::make_shared<ScResult>("");
65 70
 }
66 71
 
67 72
 std::vector<std::shared_ptr<ScReader>> ScReader::getReaders()

+ 2
- 2
src/include/ScBasicCommand.h Dosyayı Görüntüle

@@ -16,10 +16,10 @@ public:
16 16
 
17 17
     virtual ~ScBasicCommand();
18 18
 
19
-    virtual ScByteArray getData() const override;
19
+    virtual const std::string getData() const override;
20 20
 
21 21
 private:
22
-    ScByteArray _data;
22
+    std::string _data;
23 23
 };
24 24
 
25 25
 # include "ScCommand.hxx"

+ 0
- 33
src/include/ScByteArray.h Dosyayı Görüntüle

@@ -1,33 +0,0 @@
1
-//
2
-// Created by robin on 6/27/15.
3
-//
4
-
5
-#ifndef LIBPCSC_CPPTOOLS_SCBYTEARRAY_H
6
-# define LIBPCSC_CPPTOOLS_SCBYTEARRAY_H
7
-
8
-# include <winscard.h>
9
-
10
-class ScByteArray {
11
-
12
-public:
13
-    inline ScByteArray();
14
-
15
-    inline ScByteArray(BYTE* const data, const DWORD size);
16
-
17
-    inline ScByteArray(const ScByteArray& data);
18
-
19
-    inline ~ScByteArray();
20
-
21
-    inline DWORD getSize() const;
22
-
23
-    inline BYTE* getData() const;
24
-
25
-private:
26
-    DWORD _size;
27
-
28
-    BYTE* _data;
29
-};
30
-
31
-# include "ScByteArray.hxx"
32
-
33
-#endif //LIBPCSC_CPPTOOLS_SCBYTEARRAY_H

+ 0
- 39
src/include/ScByteArray.hxx Dosyayı Görüntüle

@@ -1,39 +0,0 @@
1
-//
2
-// Created by robin on 6/27/15.
3
-//
4
-
5
-#include <stdlib.h>
6
-#include <string.h>
7
-
8
-ScByteArray::ScByteArray()
9
-    : _size(0)
10
-    , _data(nullptr)
11
-{
12
-}
13
-
14
-ScByteArray::ScByteArray(BYTE * const data, const DWORD size)
15
-        : _data(data)
16
-        , _size(size)
17
-{
18
-}
19
-
20
-ScByteArray::ScByteArray(const ScByteArray &data)
21
-{
22
-    _size = data._size;
23
-    _data = (BYTE*)memcpy(malloc(_size), data._data, _size);
24
-}
25
-
26
-ScByteArray::~ScByteArray()
27
-{
28
-    delete[] _data;
29
-}
30
-
31
-BYTE* ScByteArray::getData() const
32
-{
33
-    return _data;
34
-}
35
-
36
-DWORD ScByteArray::getSize() const
37
-{
38
-    return _size;
39
-}

+ 2
- 2
src/include/ScCommand.h Dosyayı Görüntüle

@@ -5,14 +5,14 @@
5 5
 #ifndef LIBPCSC_CPPTOOLS_SCCOMMAND_H
6 6
 # define LIBPCSC_CPPTOOLS_SCCOMMAND_H
7 7
 
8
-# include "ScByteArray.h"
8
+# include <string>
9 9
 
10 10
 class ScCommand {
11 11
 
12 12
 public:
13 13
     virtual ~ScCommand();
14 14
 
15
-    virtual ScByteArray getData() const = 0;
15
+    virtual const std::string getData() const = 0;
16 16
 
17 17
 };
18 18
 

+ 4
- 5
src/include/ScHex.h Dosyayı Görüntüle

@@ -6,18 +6,17 @@
6 6
 # define PCSC_CPPTOOLS_SCHEX_H
7 7
 
8 8
 # include <string>
9
-#include "ScByteArray.h"
10 9
 
11 10
 class ScHex
12 11
 {
13 12
 public:
14
-    static char hexCharToInt(const char& c);
13
+    static const char hexCharToInt(const char& c);
15 14
 
16
-    static std::string intToHexChar(const char& c);
15
+    static const std::string intToHexChar(const char& c);
17 16
 
18
-    static std::string byteArrayToString(const ScByteArray& bytes, const std::string& separator = " ");
17
+    static const std::string byteArrayToString(const std::string& bytes, const std::string& separator = " ");
19 18
 
20
-    static ScByteArray stringToByteArray(const std::string& str);
19
+    static const std::string stringToByteArray(const std::string& str);
21 20
 
22 21
 };
23 22
 

+ 1
- 1
src/include/ScReader.h Dosyayı Görüntüle

@@ -43,7 +43,7 @@ private:
43 43
 
44 44
     LPDWORD _proto;
45 45
 
46
-    LPSCARD_IO_REQUEST _sendPci;
46
+    SCARD_IO_REQUEST _sendPci;
47 47
 
48 48
     LONG _lastResult;
49 49
 };

+ 0
- 1
src/include/ScReader.hxx Dosyayı Görüntüle

@@ -6,7 +6,6 @@ ScReader::ScReader(std::string name)
6 6
     : _name(name)
7 7
     , _card(nullptr)
8 8
     , _proto(nullptr)
9
-    , _sendPci(nullptr)
10 9
     , _lastResult(SCARD_S_SUCCESS)
11 10
 {
12 11
 }

+ 5
- 3
src/include/ScResult.h Dosyayı Görüntüle

@@ -6,17 +6,19 @@
6 6
 # define LIBPCSC_CPPTOOLS_SCRESULT_H
7 7
 
8 8
 
9
-#include "ScByteArray.h"
9
+#include <string>
10 10
 
11 11
 class ScResult
12 12
 {
13 13
 public:
14
-    ScResult(const ScByteArray& data);
14
+    ScResult(const std::string& data);
15 15
 
16 16
     ~ScResult();
17 17
 
18
+    inline const std::string getData() const;
19
+
18 20
 protected:
19
-    ScByteArray _data;
21
+    std::string _data;
20 22
 };
21 23
 
22 24
 # include "ScResult.hxx"

+ 4
- 1
src/include/ScResult.hxx Dosyayı Görüntüle

@@ -2,4 +2,7 @@
2 2
 // Created by robin on 6/27/15.
3 3
 //
4 4
 
5
-
5
+const std::string ScResult::getData() const
6
+{
7
+    return _data;
8
+}

+ 27
- 5
tests/test_pcsc_cpp_tools.cpp Dosyayı Görüntüle

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

Loading…
İptal
Kaydet