Browse Source

tests; removed byte array

master
Robin Thoni 9 years ago
parent
commit
0bc0023241

+ 0
- 3
src/CMakeLists.txt View File

4
   DBO/ScCommand.cpp
4
   DBO/ScCommand.cpp
5
   include/ScCommand.h
5
   include/ScCommand.h
6
   include/ScCommand.hxx
6
   include/ScCommand.hxx
7
-  DBO/ScByteArray.cpp
8
-  include/ScByteArray.h
9
-  include/ScByteArray.hxx
10
   DBO/ScBasicCommand.cpp
7
   DBO/ScBasicCommand.cpp
11
   include/ScBasicCommand.h
8
   include/ScBasicCommand.h
12
   include/ScBasicCommand.hxx
9
   include/ScBasicCommand.hxx

+ 1
- 1
src/DBO/ScBasicCommand.cpp View File

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

+ 0
- 6
src/DBO/ScByteArray.cpp View File

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

+ 10
- 9
src/DBO/ScHex.cpp View File

2
 // Created by robin on 6/29/15.
2
 // Created by robin on 6/29/15.
3
 //
3
 //
4
 
4
 
5
-#include <sstream>
5
+#include <string>
6
+#include <wintypes.h>
6
 #include "ScHex.h"
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
     if (c >= 'a')
12
     if (c >= 'a')
12
         return (char)(10 + (c - ('a' - 'A')) - 'A');
13
         return (char)(10 + (c - ('a' - 'A')) - 'A');
15
     return c - '0';
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
     std::string hexa;
21
     std::string hexa;
21
     for (auto c : str)
22
     for (auto c : str)
30
     {
31
     {
31
         data[i / 2] = (hexCharToInt(hexa[i]) << 4) | hexCharToInt(hexa[i + 1]);
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
     std::string res;
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
             res += separator;
44
             res += separator;
44
     }
45
     }
45
     return res;
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
     std::string res;
51
     std::string res;
51
     for (auto c1 : {(c & 0xf0) >> 4, c & 0x0f})
52
     for (auto c1 : {(c & 0xf0) >> 4, c & 0x0f})

+ 1
- 1
src/DBO/ScResult.cpp View File

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

+ 8
- 3
src/DataAccess/ScReader.cpp View File

4
 
4
 
5
 #include <winscard.h>
5
 #include <winscard.h>
6
 #include <vector>
6
 #include <vector>
7
+#include <string.h>
7
 #include "ScReader.h"
8
 #include "ScReader.h"
8
 
9
 
9
 thread_local LPSCARDCONTEXT ScReader::_context = nullptr;
10
 thread_local LPSCARDCONTEXT ScReader::_context = nullptr;
39
     _card = new SCARDCONTEXT[1];
40
     _card = new SCARDCONTEXT[1];
40
     _proto = new DWORD[1];
41
     _proto = new DWORD[1];
41
     auto res = SCardConnect(*_context, _name.c_str(), shareMode, protocols, _card, _proto);
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
     return res == SCARD_S_SUCCESS;
47
     return res == SCARD_S_SUCCESS;
43
 }
48
 }
44
 
49
 
58
 {
63
 {
59
     auto data = command.getData();
64
     auto data = command.getData();
60
     auto resBuffer = new BYTE[size];
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
     if (res == SCARD_S_SUCCESS)
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
 std::vector<std::shared_ptr<ScReader>> ScReader::getReaders()
72
 std::vector<std::shared_ptr<ScReader>> ScReader::getReaders()

+ 2
- 2
src/include/ScBasicCommand.h View File

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

+ 0
- 33
src/include/ScByteArray.h View File

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 View File

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 View File

5
 #ifndef LIBPCSC_CPPTOOLS_SCCOMMAND_H
5
 #ifndef LIBPCSC_CPPTOOLS_SCCOMMAND_H
6
 # define LIBPCSC_CPPTOOLS_SCCOMMAND_H
6
 # define LIBPCSC_CPPTOOLS_SCCOMMAND_H
7
 
7
 
8
-# include "ScByteArray.h"
8
+# include <string>
9
 
9
 
10
 class ScCommand {
10
 class ScCommand {
11
 
11
 
12
 public:
12
 public:
13
     virtual ~ScCommand();
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 View File

6
 # define PCSC_CPPTOOLS_SCHEX_H
6
 # define PCSC_CPPTOOLS_SCHEX_H
7
 
7
 
8
 # include <string>
8
 # include <string>
9
-#include "ScByteArray.h"
10
 
9
 
11
 class ScHex
10
 class ScHex
12
 {
11
 {
13
 public:
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 View File

43
 
43
 
44
     LPDWORD _proto;
44
     LPDWORD _proto;
45
 
45
 
46
-    LPSCARD_IO_REQUEST _sendPci;
46
+    SCARD_IO_REQUEST _sendPci;
47
 
47
 
48
     LONG _lastResult;
48
     LONG _lastResult;
49
 };
49
 };

+ 0
- 1
src/include/ScReader.hxx View File

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

+ 5
- 3
src/include/ScResult.h View File

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

+ 4
- 1
src/include/ScResult.hxx View File

2
 // Created by robin on 6/27/15.
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 View File

3
 #include <gtest/gtest.h>
3
 #include <gtest/gtest.h>
4
 #include <ScHex.h>
4
 #include <ScHex.h>
5
 #include <ScReader.h>
5
 #include <ScReader.h>
6
+#include <ScBasicCommand.h>
6
 
7
 
7
-#define TEST_Hex(FUNCTION) do {                                                                 \
8
+#define TEST_Hex(FUNCTION) do {                                                   \
8
     FUNCTION("", "", 0);                                                          \
9
     FUNCTION("", "", 0);                                                          \
9
     FUNCTION("42", "\x42", 1);                                                    \
10
     FUNCTION("42", "\x42", 1);                                                    \
10
     FUNCTION("42 00 FF AA 24", "\x42\x00\xff\xaa\x24", 5);                        \
11
     FUNCTION("42 00 FF AA 24", "\x42\x00\xff\xaa\x24", 5);                        \
12
     } while (0)
13
     } while (0)
13
 
14
 
14
 #define TEST_byteArrayToString(STR, DATA, SIZE) \
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
 TEST(Hex, ByteArrayToString)
18
 TEST(Hex, ByteArrayToString)
18
 {
19
 {
21
 
22
 
22
 #define TEST_stringToByteArray(STR, DATA, SIZE) do {    \
23
 #define TEST_stringToByteArray(STR, DATA, SIZE) do {    \
23
     auto bytes = ScHex::stringToByteArray(STR);         \
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
     } while (0)
27
     } while (0)
27
 
28
 
28
 TEST(Hex, StringToByteArray)
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
 int main(int argc, char* argv[])
55
 int main(int argc, char* argv[])

Loading…
Cancel
Save