aboutsummaryrefslogtreecommitdiff
path: root/unittest/test2.cc
blob: 604b3ba8aeb4f1dcc452d37170c8af7755b8c91d (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
225
226
227
228
229
230
231
232
233
/*
 * Copyright (c) 2015-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";

#include "catch2/catch.hpp"

#include <iostream>
#include <string.h>
#include <NitrokeyManager.h>
#include "device_proto.h"
#include "log.h"
//#include "stick10_commands.h"
#include "stick20_commands.h"

using namespace std;
using namespace nitrokey::device;
using namespace nitrokey::proto;
using namespace nitrokey::proto::stick20;
using namespace nitrokey::log;
using namespace nitrokey::misc;

#include <memory>

template<typename CMDTYPE>
void execute_password_command(std::shared_ptr<Device> stick, const char *password, const char kind = 'P') {
  auto p = get_payload<CMDTYPE>();
  if (kind == 'P'){
    p.set_kind_user();
  } else {
    p.set_kind_admin();
  }
  strcpyT(p.password, password);
  CMDTYPE::CommandTransaction::run(stick, p);
  this_thread::sleep_for(1000ms);
}

/**
 *  fail on purpose (will result in failed test)
 *  disable from running unwillingly
 */
void SKIP_TEST() {
  CAPTURE("Failing current test to SKIP it");
  REQUIRE(false);
}


TEST_CASE("long operation test", "[test_long]") {
  SKIP_TEST();

  auto stick = make_shared<Stick20>();
  bool connected = stick->connect();
  REQUIRE(connected == true);
  Log::instance().set_loglevel(Loglevel::DEBUG);
  try{
    auto p = get_payload<FillSDCardWithRandomChars>();
    p.set_defaults();
    strcpyT(p.admin_pin, default_admin_pin);
    FillSDCardWithRandomChars::CommandTransaction::run(stick, p);
    this_thread::sleep_for(1000ms);

    CHECK(false);
  }
  catch (LongOperationInProgressException &progressException){
    CHECK(true);
  }


  for (int i = 0; i < 30; ++i) {
    try {
      stick10::GetStatus::CommandTransaction::run(stick);
    }
    catch (LongOperationInProgressException &progressException){
      CHECK((int)progressException.progress_bar_value>=0);
      CAPTURE((int)progressException.progress_bar_value);
      this_thread::sleep_for(2000ms);
    }

  }

}


TEST_CASE("test device internal status with various commands", "[fast]") {
  auto stick = make_shared<Stick20>();
  bool connected = stick->connect();
  REQUIRE(connected == true);

  Log::instance().set_loglevel(Loglevel::DEBUG);
  auto p = get_payload<stick20::SendStartup>();
  p.set_defaults();
  auto device_status = stick20::SendStartup::CommandTransaction::run(stick, p);
  REQUIRE(device_status.data().AdminPwRetryCount == 3);
  REQUIRE(device_status.data().UserPwRetryCount == 3);
  REQUIRE(device_status.data().ActiveSmartCardID_u32 != 0);

  auto production_status = stick20::ProductionTest::CommandTransaction::run(stick);
  REQUIRE(production_status.data().SD_Card_Size_u8 == 8);
  REQUIRE(production_status.data().SD_CardID_u32 != 0);

  auto sdcard_occupancy = stick20::GetSDCardOccupancy::CommandTransaction::run(stick);
  REQUIRE((int) sdcard_occupancy.data().ReadLevelMin >= 0);
  REQUIRE((int) sdcard_occupancy.data().ReadLevelMax <= 100);
  REQUIRE((int) sdcard_occupancy.data().WriteLevelMin >= 0);
  REQUIRE((int) sdcard_occupancy.data().WriteLevelMax <= 100);
}

TEST_CASE("setup hidden volume test", "[hidden]") {
  auto stick = make_shared<Stick20>();
  bool connected = stick->connect();
  REQUIRE(connected == true);
  Log::instance().set_loglevel(Loglevel::DEBUG);
  stick10::LockDevice::CommandTransaction::run(stick);
  this_thread::sleep_for(2000ms);

  auto user_pin = default_user_pin;
  execute_password_command<EnableEncryptedPartition>(stick, user_pin);

  auto p = get_payload<stick20::SetupHiddenVolume>();
  p.SlotNr_u8 = 0;
  p.StartBlockPercent_u8 = 70;
  p.EndBlockPercent_u8 = 90;
  auto hidden_volume_password = "123123123";
  strcpyT(p.HiddenVolumePassword_au8, hidden_volume_password);
  stick20::SetupHiddenVolume::CommandTransaction::run(stick, p);
  this_thread::sleep_for(2000ms);

  execute_password_command<EnableHiddenEncryptedPartition>(stick, hidden_volume_password);
}

TEST_CASE("setup multiple hidden volumes", "[hidden]") {
  auto stick = make_shared<Stick20>();
  bool connected = stick->connect();
  REQUIRE(connected == true);
  Log::instance().set_loglevel(Loglevel::DEBUG);

  auto user_pin = default_user_pin;
  stick10::LockDevice::CommandTransaction::run(stick);
  this_thread::sleep_for(2000ms);
  execute_password_command<EnableEncryptedPartition>(stick, user_pin);

  constexpr int volume_count = 4;
  for (int i = 0; i < volume_count; ++i) {
    auto p = get_payload<stick20::SetupHiddenVolume>();
    p.SlotNr_u8 = i;
    p.StartBlockPercent_u8 = 20 + 10*i;
    p.EndBlockPercent_u8 = p.StartBlockPercent_u8+i+1;
    auto hidden_volume_password = std::string("123123123")+std::to_string(i);
    strcpyT(p.HiddenVolumePassword_au8, hidden_volume_password.c_str());
    stick20::SetupHiddenVolume::CommandTransaction::run(stick, p);
    this_thread::sleep_for(2000ms);
  }


  for (int i = 0; i < volume_count; ++i) {
    execute_password_command<EnableEncryptedPartition>(stick, user_pin);
    auto hidden_volume_password = std::string("123123123")+std::to_string(i);
    execute_password_command<EnableHiddenEncryptedPartition>(stick, hidden_volume_password.c_str());
    this_thread::sleep_for(2000ms);
  }
}


//in case of a bug this could change update PIN to some unexpected value
// - please save log with packet dump if this test will not pass
TEST_CASE("update password change", "[dangerous]") {
  SKIP_TEST();

  auto stick = make_shared<Stick20>();
  bool connected = stick->connect();
  REQUIRE(connected == true);
  Log::instance().set_loglevel(Loglevel::DEBUG);

  auto pass1 = default_admin_pin;
  auto pass2 = "12345678901234567890";

  auto data = {
      make_pair(pass1, pass2),
      make_pair(pass2, pass1),
  };
  for (auto &&  password: data) {
    auto p = get_payload<stick20::ChangeUpdatePassword>();
    strcpyT(p.current_update_password, password.first);
    strcpyT(p.new_update_password, password.second);
    stick20::ChangeUpdatePassword::CommandTransaction::run(stick, p);
  }
}

TEST_CASE("general test", "[test]") {
  auto stick = make_shared<Stick20>();
  bool connected = stick->connect();
  REQUIRE(connected == true);

  Log::instance().set_loglevel(Loglevel::DEBUG);

  stick10::LockDevice::CommandTransaction::run(stick);
//  execute_password_command<EnableEncryptedPartition>(stick, "123456");
//  execute_password_command<DisableEncryptedPartition>(stick, "123456");
//  execute_password_command<DisableHiddenEncryptedPartition>(stick, "123123123");

  execute_password_command<SendSetReadonlyToUncryptedVolume>(stick, default_user_pin);
  execute_password_command<SendSetReadwriteToUncryptedVolume>(stick, default_user_pin);
  execute_password_command<SendClearNewSdCardFound>(stick, default_admin_pin, 'A');
  stick20::GetDeviceStatus::CommandTransaction::run(stick);
  this_thread::sleep_for(1000ms);
//  execute_password_command<LockFirmware>(stick, "123123123"); //CAUTION
//  execute_password_command<EnableFirmwareUpdate>(stick, "123123123"); //CAUTION FIRMWARE PIN

  execute_password_command<ExportFirmware>(stick, "12345678", 'A');
//  execute_password_command<FillSDCardWithRandomChars>(stick, "12345678", 'A');

  stick10::LockDevice::CommandTransaction::run(stick);
}