summaryrefslogtreecommitdiff
path: root/crates/arti/tests/cli_tests/keys.rs
blob: 803f430b0b984f19bdfc5056b300a309ed0a3a92 (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
//! Arti keys integration test suite.
//!
//! The `assert_cmd` crate is used here instead of the preferred `trycmd` (see
//! [`README`](../README.md)) because the output of `keys list` is not deterministic across
//! different machines. The design choices of some components are workarounds for this limitation.
//!
//! ## Note on the test data
//!
//! Test data for this suite is stored in the `keys/keys.in/local` directory. The structure is as follows:
//!
//! ```
//! local/
//! ├── ctor-keystore
//! │   ├── hostname
//! │   ├── hs_ed25519_public_key
//! │   ├── hs_ed25519_secret_key
//! │   └── hs_unrecognized_entry
//! └── state-dir
//!     └── keystore
//!         ├── client
//!         │   └── mnyizjj7m3hpcr7i5afph3zt7maa65johyu2ruis6z7cmnjmaj3h6tad
//!         │       └── ks_hsc_desc_enc.x25519_private
//!         ├── hss
//!         │   └── allium-cepa
//!         │       ├── ks_hs_id.ed25519_expanded_private
//!         │       └── unrecognized-entry
//!         └── unrecognized-path-dir
//!             └── ks_hs_id.ed25519_expanded_private
//! ```
//!
//! Where:
//!
//! - `local/ctor-keystore` is a fully populated CTor keystore.
//! - `local/state-dir` is an example Arti state directory, partially populated.
//!   This directory typically corresponds to `~/.local/share/arti`. For the
//!   purposes of testing, it contains only `local/state-dir/keystore`.
//! - `local/state-dir/keystore/hss/allium-cepa` is a partially populated keystore for the hidden service
//!   `allium-cepa`. It includes a long-term identity key and an unregistered entry.
//! - `local/state-dir/keystore/client/mnyizjj7m3hpcr7i5afph3zt7maa65johyu2ruis6z7cmnjmaj3h6tad/ks_hsc_desc_enc.x25519_private`
//!   is a service discovery key for the hidden service `mnyi[..]tad.onion`.
//! - `local/state-dir/keystore/unrecognized-path-dir/ks_hs_id.ed25519_expanded_private` is an
//!   unrecognized path.
//!
//! Form more information about unrecognized entries and paths see
//! [keys list documentation](../../../../doc/keys.md).
// TODO: it would be desirable to have a deterministic script that generates the test files, like
// the one that genearates the keys for `hsc ctor-migrate` (see `maint/keygen-client-auth-test`).
// See issue #2334

use crate::keys::util::{
    KeysListCmdBuilder, KeysListKeystoreCmdBuilder, LIST_OUTPUT_ARTI, LIST_OUTPUT_CTOR,
};
use crate::util::assert_log_message;
mod util;

#[test]
fn list_all_keystore_entries() {
    let output = KeysListCmdBuilder::default()
        .with_arti(true)
        .with_ctor(true)
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout.clone()).unwrap();
    for entry in LIST_OUTPUT_ARTI {
        assert!(stdout.contains(entry))
    }
    for entry in LIST_OUTPUT_CTOR {
        assert!(stdout.contains(entry))
    }
    assert_log_message(output);
}

#[test]
fn list_arti_keystore_entries() {
    let output = KeysListCmdBuilder::default()
        .with_arti(true)
        .with_ctor(true)
        .keystore(Some("arti".to_string()))
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout.clone()).unwrap();
    for entry in LIST_OUTPUT_ARTI {
        assert!(stdout.contains(entry))
    }
    for entry in LIST_OUTPUT_CTOR {
        assert!(!stdout.contains(entry))
    }
    assert_log_message(output);
}

#[test]
fn list_ctor_keystore_entries() {
    let output = KeysListCmdBuilder::default()
        .with_arti(true)
        .with_ctor(true)
        .keystore(Some("ctor".to_string()))
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout.clone()).unwrap();
    for entry in LIST_OUTPUT_ARTI {
        assert!(!stdout.contains(entry))
    }
    for entry in LIST_OUTPUT_CTOR {
        assert!(stdout.contains(entry))
    }
    assert_log_message(output);
}

#[test]
fn list_unregistered_keystore_fails() {
    let unregistered = "unregistered";
    let output = KeysListCmdBuilder::default()
        .keystore(Some(unregistered.to_string()))
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(!output.status.success());
    let error = String::from_utf8(output.stderr).unwrap();
    assert!(error.contains(&format!("arti: error: Keystore {} not found", unregistered)));
    assert!(output.stdout.is_empty())
}

#[test]
fn list_arti_with_empty_state_dir_and_full_ctor() {
    let output = KeysListCmdBuilder::default()
        .keystore(Some("arti".to_string()))
        .with_ctor(true)
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout.clone()).unwrap();
    assert!(stdout.contains("Currently there are no entries in the keystore arti."));
    for entry in LIST_OUTPUT_ARTI {
        assert!(!stdout.contains(entry))
    }
    for entry in LIST_OUTPUT_CTOR {
        assert!(!stdout.contains(entry))
    }
    assert_log_message(output);
}

#[test]
fn list_with_empty_state_dir_and_full_ctor() {
    let output = KeysListCmdBuilder::default()
        .with_ctor(true)
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout.clone()).unwrap();
    for entry in LIST_OUTPUT_ARTI {
        assert!(!stdout.contains(entry))
    }
    for entry in LIST_OUTPUT_CTOR {
        assert!(stdout.contains(entry))
    }
    assert_log_message(output);
}

#[test]
fn list_with_empty_state_dir_and_no_registered_ctor() {
    let output = KeysListCmdBuilder::default()
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("Currently there are no entries in any of the keystores."));
    for entry in LIST_OUTPUT_ARTI {
        assert!(!stdout.contains(entry))
    }
    for entry in LIST_OUTPUT_CTOR {
        assert!(!stdout.contains(entry))
    }
    assert!(output.stderr.is_empty())
}

#[test]
fn list_keystore_with_arti_and_ctor() {
    let output = KeysListKeystoreCmdBuilder::default()
        .with_ctor(true)
        .build()
        .unwrap()
        .output()
        .unwrap();

    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout.clone()).unwrap();
    assert!(stdout.contains("arti"));
    assert!(stdout.contains("ctor"));
    assert_log_message(output);
}

#[test]
fn list_keystore_with_arti() {
    let output = KeysListKeystoreCmdBuilder::default()
        .build()
        .unwrap()
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout.clone()).unwrap();
    assert!(stdout.contains("arti"));
    assert!(!stdout.contains("ctor"));
    assert!(output.stderr.is_empty())
}