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
|
use std::ffi::CStr;
use tests::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
use {Authenticate, CommandError, CommandStatus, Config, Device};
static ADMIN_NEW_PASSWORD: &str = "1234567890";
static USER_NEW_PASSWORD: &str = "abcdefghij";
#[test]
#[cfg_attr(not(feature = "test-no-device"), ignore)]
fn connect_no_device() {
assert!(::connect().is_err());
assert!(::Pro::connect().is_err());
assert!(::Storage::connect().is_err());
}
#[test]
#[cfg_attr(not(feature = "test-pro"), ignore)]
fn connect_pro() {
assert!(::connect().is_ok());
assert!(::Pro::connect().is_ok());
assert!(::Storage::connect().is_err());
}
#[test]
#[cfg_attr(not(feature = "test-storage"), ignore)]
fn connect_storage() {
assert!(::connect().is_ok());
assert!(::Pro::connect().is_err());
assert!(::Storage::connect().is_ok());
}
fn assert_empty_serial_number() {
unsafe {
let ptr = ::nitrokey_sys::NK_device_serial_number();
assert!(!ptr.is_null());
let cstr = CStr::from_ptr(ptr);
assert_eq!(cstr.to_string_lossy(), "");
}
}
#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn disconnect() {
Target::connect().unwrap();
assert_empty_serial_number();
Target::connect()
.unwrap()
.authenticate_admin(ADMIN_PASSWORD)
.unwrap();
assert_empty_serial_number();
Target::connect()
.unwrap()
.authenticate_user(USER_PASSWORD)
.unwrap();
assert_empty_serial_number();
}
#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn get_serial_number() {
let device = Target::connect().unwrap();
let result = device.get_serial_number();
assert!(result.is_ok());
let serial_number = result.unwrap();
assert!(serial_number.is_ascii());
assert!(serial_number.chars().all(|c| c.is_ascii_hexdigit()));
}
#[test]
#[cfg_attr(not(feature = "test-pro"), ignore)]
// TODO: adapt for storage
fn get_firmware_version() {
let device = Target::connect().unwrap();
assert_eq!(0, device.get_major_firmware_version());
let minor = device.get_minor_firmware_version();
assert!(minor == 7 || minor == 8);
}
fn admin_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) -> T {
let result = device.authenticate_admin(&(ADMIN_PASSWORD.to_owned() + suffix));
let device = match result {
Ok(admin) => admin.device(),
Err((device, _)) => device,
};
assert_eq!(count, device.get_admin_retry_count());
return device;
}
fn user_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) -> T {
let result = device.authenticate_user(&(USER_PASSWORD.to_owned() + suffix));
let device = match result {
Ok(admin) => admin.device(),
Err((device, _)) => device,
};
assert_eq!(count, device.get_user_retry_count());
return device;
}
#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn get_retry_count() {
let device = Target::connect().unwrap();
let device = admin_retry(device, "", 3);
let device = admin_retry(device, "123", 2);
let device = admin_retry(device, "456", 1);
let device = admin_retry(device, "", 3);
let device = user_retry(device, "", 3);
let device = user_retry(device, "123", 2);
let device = user_retry(device, "456", 1);
user_retry(device, "", 3);
}
#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn config() {
let device = Target::connect().unwrap();
let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap();
let config = Config::new(None, None, None, true);
assert_eq!(CommandStatus::Success, admin.write_config(config));
let get_config = admin.get_config().unwrap();
assert_eq!(config, get_config);
let config = Config::new(None, Some(9), None, true);
assert_eq!(
CommandStatus::Error(CommandError::InvalidSlot),
admin.write_config(config)
);
let config = Config::new(Some(1), None, Some(0), false);
assert_eq!(CommandStatus::Success, admin.write_config(config));
let get_config = admin.get_config().unwrap();
assert_eq!(config, get_config);
let config = Config::new(None, None, None, false);
assert_eq!(CommandStatus::Success, admin.write_config(config));
let get_config = admin.get_config().unwrap();
assert_eq!(config, get_config);
}
#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn change_user_pin() {
let device = Target::connect().unwrap();
let device = device.authenticate_user(USER_PASSWORD).unwrap().device();
let device = device.authenticate_user(USER_NEW_PASSWORD).unwrap_err().0;
let result = device.change_user_pin(USER_PASSWORD, USER_NEW_PASSWORD);
assert_eq!(CommandStatus::Success, result);
let device = device.authenticate_user(USER_PASSWORD).unwrap_err().0;
let device = device
.authenticate_user(USER_NEW_PASSWORD)
.unwrap()
.device();
let result = device.change_user_pin(USER_PASSWORD, USER_PASSWORD);
assert_eq!(CommandStatus::Error(CommandError::WrongPassword), result);
let result = device.change_user_pin(USER_NEW_PASSWORD, USER_PASSWORD);
assert_eq!(CommandStatus::Success, result);
let device = device.authenticate_user(USER_PASSWORD).unwrap().device();
device.authenticate_user(USER_NEW_PASSWORD).unwrap_err();
}
#[test]
#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
fn change_admin_pin() {
let device = Target::connect().unwrap();
let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap().device();
let device = device.authenticate_admin(ADMIN_NEW_PASSWORD).unwrap_err().0;
let result = device.change_admin_pin(ADMIN_PASSWORD, ADMIN_NEW_PASSWORD);
assert_eq!(CommandStatus::Success, result);
let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap_err().0;
let device = device
.authenticate_admin(ADMIN_NEW_PASSWORD)
.unwrap()
.device();
let result = device.change_admin_pin(ADMIN_PASSWORD, ADMIN_PASSWORD);
assert_eq!(CommandStatus::Error(CommandError::WrongPassword), result);
let result = device.change_admin_pin(ADMIN_NEW_PASSWORD, ADMIN_PASSWORD);
assert_eq!(CommandStatus::Success, result);
let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap().device();
device.authenticate_admin(ADMIN_NEW_PASSWORD).unwrap_err();
}
|