aboutsummaryrefslogtreecommitdiff
path: root/unittest/test_multiple_devices.cc
blob: bc1b60b2673d80a1426fa0dbc3710f5b639785c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (c) 2017-2018 Nitrokey UG
 *
 * This file is part of libnitrokey.
 *
 * libnitrokey is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * libnitrokey is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libnitrokey. If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-3.0
 */

static const char *const default_admin_pin = "12345678";
static const char *const default_user_pin = "123456";
const char * temporary_password = "123456789012345678901234";
const char * RFC_SECRET = "12345678901234567890";

#include "catch2/catch.hpp"

#include <iostream>
#include <NitrokeyManager.h>
#include <stick20_commands.h>

using namespace nitrokey;


TEST_CASE("List devices", "[BASIC]") {
    auto v = Device::enumerate();
    REQUIRE(v.size() > 0);
    for (auto i : v){
        auto d = Device::create(i.m_deviceModel);
        if (!d) {
            std::cout << "Could not create device with model " << i.m_deviceModel << "\n";
            continue;
        }
        std::cout << i.m_deviceModel << " " << i.m_path << " "
          << i.m_serialNumber << " |";
        d->set_path(i.m_path);
        d->connect();
        auto res = GetStatus::CommandTransaction::run(d);
        std::cout << " " << res.data().card_serial_u32 << " "
                  << res.data().get_card_serial_hex()
                  << std::endl;
        d->disconnect();
    }
}

TEST_CASE("List Storage devices", "[BASIC]") {
    shared_ptr<Stick20> d = make_shared<Stick20>();
    auto v = Device::enumerate();
    REQUIRE(v.size() > 0);
    for (auto i : v){
        if (i.m_deviceModel != DeviceModel::STORAGE)
            continue;
        auto a = i.m_path;
        std::cout << a;
        d->set_path(a);
        d->connect();
        auto res = GetStatus::CommandTransaction::run(d);
        auto res2 = GetDeviceStatus::CommandTransaction::run(d);
        std::cout << " " << res.data().card_serial_u32 << " "
                  << res.data().get_card_serial_hex()
                  << " " << std::to_string(res2.data().versionInfo.minor)
                  << std::endl;
        d->disconnect();
    }
}

TEST_CASE("Regenerate AES keys", "[BASIC]") {
    shared_ptr<Stick20> d = make_shared<Stick20>();
    auto v = Device::enumerate();
    REQUIRE(v.size() > 0);

    std::vector<shared_ptr<Stick20>> devices;
    for (auto i : v){
        if (i.m_deviceModel != DeviceModel::STORAGE)
            continue;
        auto a = i.m_path;
        std::cout << a << endl;
        d = make_shared<Stick20>();
        d->set_path(a);
        d->connect();
        devices.push_back(d);
    }

    for (auto d : devices){
        auto res2 = GetDeviceStatus::CommandTransaction::run(d);
        std::cout << std::to_string(res2.data().versionInfo.minor) << std::endl;
//        nitrokey::proto::stick20::CreateNewKeys::CommandPayload p;
//        p.set_defaults();
//        memcpy(p.password, "12345678", 8);
//        auto res3 = nitrokey::proto::stick20::CreateNewKeys::CommandTransaction::run(d, p);
    }

    for (auto d : devices){
        //TODO watch out for multiple hid_exit calls
        d->disconnect();
    }
}


TEST_CASE("Use API", "[BASIC]") {
    auto nm = NitrokeyManager::instance();
    nm->set_loglevel(2);
    auto v = nm->list_devices();
    REQUIRE(v.size() > 0);

    for (auto i : v) {
      std::cout << "Connect with: " << i.m_deviceModel << " " << i.m_path << " "
        << i.m_serialNumber << " | " << std::boolalpha << nm->connect_with_path(i.m_path) << " |";
      try {
        auto status = nm->get_status();
        std::cout << " " << status.card_serial_u32 << " "
                  << status.get_card_serial_hex()
                  << std::endl;
      } catch (const LongOperationInProgressException &e) {
        std::cout << "long operation in progress on " << i.m_path
          << " " << std::to_string(e.progress_bar_value) << std::endl;
      }
    }
}


TEST_CASE("Use Storage API", "[BASIC]") {
    auto nm = NitrokeyManager::instance();
    nm->set_loglevel(2);
    auto v = nm->list_devices();
    REQUIRE(v.size() > 0);

    for (int i=0; i<10; i++){
        for (auto i : v) {
            if (i.m_deviceModel != DeviceModel::STORAGE)
                continue;
            auto a = i.m_path;
            std::cout <<"Connect with: " << a <<
            " " << std::boolalpha << nm->connect_with_path(a) << " ";
            try{
                auto status_storage = nm->get_status_storage();
                std::cout << status_storage.ActiveSmartCardID_u32
                          << " " << status_storage.ActiveSD_CardID_u32
                          << std::endl;

//                nm->fill_SD_card_with_random_data("12345678");
            }
            catch (const LongOperationInProgressException &e){
                std::cout << "long operation in progress on " << a
                        << " " << std::to_string(e.progress_bar_value) << std::endl;
//                this_thread::sleep_for(1000ms);
            }
        }
        std::cout <<"Iteration: " << i << std::endl;
    }

}


TEST_CASE("Use API ID", "[BASIC]") {
    auto nm = NitrokeyManager::instance();
    nm->set_loglevel(2);

    auto v = nm->list_devices_by_cpuID();
    REQUIRE(v.size() > 0);

    //no refresh - should not reconnect to new devices
    // Scenario:
    // 1. Run test
    // 2. Remove one of the devices and reinsert it after a while
    // 3. Device should not be reconnected and test should not crash
    // 4. Remove all devices - test should continue

    for (int j = 0; j < 100; j++) {
        for (auto i : v) {
            if (!nm->connect_with_ID(i)) continue;
            int retry_count = 99;
            try {
                retry_count = nm->get_admin_retry_count();
                std::cout << j << " " << i << " " << to_string(retry_count) << std::endl;
            }
            catch (...) {
                retry_count = 99;
                //pass
            }
        }
    }
    std::cout << "finished" << std::endl;
}

TEST_CASE("Use API ID refresh", "[BASIC]") {
    auto nm = NitrokeyManager::instance();
    nm->set_loglevel(2);

    //refresh in each iteration - should reconnect to new devices
    // Scenario:
    // 1. Run test
    // 2. Remove one of the devices and reinsert it after a while
    // 3. Device should be reconnected

    for(int j=0; j<100; j++) {
        auto v = nm->list_devices_by_cpuID();
        REQUIRE(v.size() > 0);
        for (auto i : v) {
            nm->connect_with_ID(i);
            int retry_count = 99;
            try {
                retry_count = nm->get_admin_retry_count();
                std::cout << j <<" " << i << " " << to_string(retry_count) << std::endl;
            }
            catch (...){
                retry_count = 99;
                //pass
            }
        }
    }
    std::cout << "finished" << std::endl;
}