s7cmd 1.5.0

Reliable, flexible, and fast command-line tool for Amazon S3
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Process-level CLI tests for the `get-object-annotation` subcommand.
//! These run without AWS credentials or network access (mock-endpoint
//! tests talk only to a loopback HTTP server).

mod common;
use common::{MockResponse, MockS3Server, mock_target_args, s7cmd_cmd_clean_env};

use std::process::{Command, Stdio};

fn s7cmd() -> Command {
    Command::new(env!("CARGO_BIN_EXE_s7cmd"))
}

fn run(cmd: &mut Command) -> (bool, String, String, Option<i32>) {
    let output = cmd
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("failed to spawn s7cmd binary");
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    (
        output.status.success(),
        stdout,
        stderr,
        output.status.code(),
    )
}

#[test]
fn help_succeeds_and_lists_option_groups() {
    let (ok, stdout, _stderr, _code) = run(s7cmd().args(["get-object-annotation", "--help"]));
    assert!(ok, "get-object-annotation --help must succeed");
    assert!(stdout.contains("AWS Configuration"));
    assert!(stdout.contains("Retry Options"));
    assert!(stdout.contains("Timeout Options"));
}

#[test]
fn missing_positional_exits_2() {
    let (ok, _stdout, stderr, code) = run(s7cmd().arg("get-object-annotation"));
    assert!(!ok);
    assert_eq!(code, Some(2), "clap missing-arg should exit 2");
    assert!(stderr.to_lowercase().contains("required") || stderr.to_lowercase().contains("usage"));
}

#[test]
fn missing_annotation_name_exits_2() {
    // target + outfile present, but --annotation-name is required.
    let (ok, _stdout, stderr, code) =
        run(s7cmd().args(["get-object-annotation", "s3://bucket/key", "-"]));
    assert!(!ok);
    assert_eq!(
        code,
        Some(2),
        "--annotation-name is required; should exit 2"
    );
    assert!(stderr.to_lowercase().contains("required") || stderr.to_lowercase().contains("usage"));
}

#[test]
fn missing_outfile_exits_2() {
    // target + --annotation-name present, but the outfile positional is required.
    let (ok, _stdout, stderr, code) = run(s7cmd().args([
        "get-object-annotation",
        "s3://bucket/key",
        "--annotation-name",
        "note",
    ]));
    assert!(!ok);
    assert_eq!(
        code,
        Some(2),
        "outfile positional is required; should exit 2"
    );
    assert!(stderr.to_lowercase().contains("required") || stderr.to_lowercase().contains("usage"));
}

#[test]
fn bucket_only_path_no_key_exits_1() {
    let (ok, _stdout, stderr, code) = run(s7cmd().args([
        "get-object-annotation",
        "s3://bucket",
        "-",
        "--annotation-name",
        "note",
    ]));
    assert!(!ok);
    assert_eq!(code, Some(1), "bucket-only path should exit 1 (validation)");
    assert!(
        !stderr.is_empty(),
        "should have an error message on stderr; got empty"
    );
}

// NOTE: s3util-rs's auto_complete_shell_short_circuits_without_target test
// is omitted — s7cmd hides the per-subcommand --auto-complete-shell flag
// and exposes only the top-level form (covered by tests/cli_help.rs).

#[test]
fn target_access_key_without_secret_exits_non_zero() {
    let (ok, _stdout, stderr, code) = run(s7cmd().args([
        "get-object-annotation",
        "s3://bucket/key",
        "-",
        "--annotation-name",
        "note",
        "--target-access-key",
        "AKIA",
    ]));
    assert!(!ok);
    assert_eq!(
        code,
        Some(2),
        "clap missing-arg should exit 2; stderr: {stderr}"
    );
    assert!(
        stderr.to_lowercase().contains("required")
            || stderr.to_lowercase().contains("--target-secret-access-key")
    );
}

#[test]
fn help_mentions_annotation_name_and_target_version_id() {
    let (ok, stdout, _stderr, _code) = run(s7cmd().args(["get-object-annotation", "--help"]));
    assert!(ok);
    assert!(
        stdout.contains("annotation-name"),
        "help should list --annotation-name; got: {stdout}"
    );
    assert!(
        stdout.contains("target-version-id"),
        "help should list --target-version-id; got: {stdout}"
    );
}

// ---------- mock-endpoint tests ----------

#[test]
fn mock_endpoint_no_such_version_exits_4_with_version_in_message() {
    let dir = tempfile::tempdir().unwrap();
    let outfile = dir.path().join("annotation.bin");
    let server = MockS3Server::start(vec![MockResponse::s3_error(404, "NoSuchVersion")]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args([
        "get-object-annotation",
        "--annotation-name",
        "note",
        "--target-version-id",
        "mock-version",
    ])
    .args(mock_target_args(&server.endpoint_url()))
    .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(
        code,
        Some(4),
        "NoSuchVersion should exit 4; stderr: {stderr}"
    );
    assert!(
        stderr.contains("s3://mock-bucket/mock-key (versionId=mock-version) not found"),
        "expected the versioned not-found message; got: {stderr}"
    );
    assert!(!outfile.exists(), "no outfile may be created on error");
}

#[test]
fn mock_endpoint_no_such_annotation_with_version_exits_4() {
    let dir = tempfile::tempdir().unwrap();
    let outfile = dir.path().join("annotation.bin");
    let server = MockS3Server::start(vec![MockResponse::s3_error(404, "NoSuchAnnotation")]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args([
        "get-object-annotation",
        "--annotation-name",
        "note",
        "--target-version-id",
        "mock-version",
    ])
    .args(mock_target_args(&server.endpoint_url()))
    .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(
        code,
        Some(4),
        "NoSuchAnnotation should exit 4; stderr: {stderr}"
    );
    assert!(
        stderr.contains(
            "annotation note not found for s3://mock-bucket/mock-key (versionId=mock-version)"
        ),
        "expected the versioned annotation-not-found message; got: {stderr}"
    );
    assert!(!outfile.exists(), "no outfile may be created on error");
}

#[test]
fn mock_endpoint_unverifiable_payload_written_to_bare_outfile() {
    // No SSE header and no checksum headers: nothing to verify, so the
    // payload is written with the "could not be verified" warning. The bare
    // (parent-less) outfile exercises the `Path::new(".")` fallback — the
    // child's working directory is a temp dir so the file lands there.
    let dir = tempfile::tempdir().unwrap();
    let payload = "mock annotation payload";
    let server = MockS3Server::start(vec![MockResponse::new(200, payload)]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.current_dir(dir.path())
        .args(["get-object-annotation", "--annotation-name", "note"])
        .args(mock_target_args(&server.endpoint_url()))
        .args(["s3://mock-bucket/mock-key", "annotation.bin"]);
    let (code, stdout, stderr) = common::run(&mut cmd);
    assert_eq!(code, Some(0), "expected success; stderr: {stderr}");
    assert!(
        stderr.contains("payload integrity could not be verified"),
        "expected the unverifiable warning; got: {stderr}"
    );
    assert_eq!(
        std::fs::read(dir.path().join("annotation.bin")).unwrap(),
        payload.as_bytes(),
        "outfile must hold the annotation payload"
    );
    assert!(
        stdout.contains("\"ContentLength\""),
        "expected the JSON metadata on stdout; got: {stdout}"
    );
}

#[test]
fn mock_endpoint_composite_checksum_skips_checksum_verification() {
    // A COMPOSITE checksum type (multipart-style) cannot be recomputed from
    // the payload bytes, so the runner must ignore the checksum header
    // (rather than fail the mismatch) and fall back to unverifiable.
    let dir = tempfile::tempdir().unwrap();
    let outfile = dir.path().join("annotation.bin");
    let payload = "mock annotation payload";
    let server = MockS3Server::start(vec![MockResponse::new(200, payload).with_headers(&[
        ("x-amz-checksum-type", "COMPOSITE"),
        ("x-amz-checksum-crc32", "AAAAAA==-2"),
    ])]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args(["get-object-annotation", "--annotation-name", "note"])
        .args(mock_target_args(&server.endpoint_url()))
        .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(
        code,
        Some(0),
        "composite checksum must not be verified; stderr: {stderr}"
    );
    assert!(
        stderr.contains("payload integrity could not be verified"),
        "expected the unverifiable warning; got: {stderr}"
    );
    assert_eq!(
        std::fs::read(&outfile).unwrap(),
        payload.as_bytes(),
        "outfile must hold the annotation payload"
    );
}

#[test]
fn mock_endpoint_payload_over_1mib_exits_1_without_outfile() {
    // A buggy or hostile endpoint returning more than the 1 MiB annotation
    // cap must abort the bounded read, exit 1, and leave no outfile behind.
    let dir = tempfile::tempdir().unwrap();
    let outfile = dir.path().join("annotation.bin");
    let oversized = vec![b'a'; 1024 * 1024 + 1];
    let server = MockS3Server::start(vec![MockResponse::new(200, oversized)]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args(["get-object-annotation", "--annotation-name", "note"])
        .args(mock_target_args(&server.endpoint_url()))
        .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(
        code,
        Some(1),
        "oversized payload should exit 1; stderr: {stderr}"
    );
    assert!(
        stderr.contains("exceeds the 1 MiB limit"),
        "expected the cap message; got: {stderr}"
    );
    assert!(!outfile.exists(), "no outfile may be created on error");
}

#[test]
fn mock_endpoint_matching_checksum_written_and_verified() {
    // A correct CRC64NVME response header: the in-transit and post-write
    // checks both pass and the run reports "written and verified".
    let dir = tempfile::tempdir().unwrap();
    let outfile = dir.path().join("annotation.bin");
    let payload = "mock annotation payload";
    let crc64 = s3util_rs::storage::annotation::compute_checksum_base64(
        payload.as_bytes(),
        aws_sdk_s3::types::ChecksumAlgorithm::Crc64Nvme,
    );
    let server = MockS3Server::start(vec![
        MockResponse::new(200, payload)
            .with_headers(&[("x-amz-checksum-crc64nvme", crc64.as_str())]),
    ]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args(["get-object-annotation", "-v", "--annotation-name", "note"])
        .args(mock_target_args(&server.endpoint_url()))
        .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(code, Some(0), "expected success; stderr: {stderr}");
    assert!(
        stderr.contains("written and verified"),
        "expected the verified info line (-v); got: {stderr}"
    );
    assert!(
        !stderr.contains("could not be verified"),
        "must not warn when the checksum matched; got: {stderr}"
    );
    assert_eq!(
        std::fs::read(&outfile).unwrap(),
        payload.as_bytes(),
        "outfile must hold the annotation payload"
    );
}

#[test]
fn mock_endpoint_checksum_mismatch_exits_1_without_outfile() {
    // The response checksum does not match the payload bytes. The SDK's
    // response-checksum validation (checksum mode ENABLED) catches this
    // while streaming the body — the run must exit 1 and never create the
    // outfile (a corrupted download must never be written).
    let dir = tempfile::tempdir().unwrap();
    let outfile = dir.path().join("annotation.bin");
    let payload = "mock annotation payload";
    let wrong_crc64 = s3util_rs::storage::annotation::compute_checksum_base64(
        b"different bytes entirely",
        aws_sdk_s3::types::ChecksumAlgorithm::Crc64Nvme,
    );
    let server = MockS3Server::start(vec![
        MockResponse::new(200, payload)
            .with_headers(&[("x-amz-checksum-crc64nvme", wrong_crc64.as_str())]),
    ]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args(["get-object-annotation", "--annotation-name", "note"])
        .args(mock_target_args(&server.endpoint_url()))
        .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(
        code,
        Some(1),
        "checksum mismatch should exit 1; stderr: {stderr}"
    );
    assert!(
        stderr.contains("checksum mismatch"),
        "expected the streaming checksum-mismatch error; got: {stderr}"
    );
    assert!(!outfile.exists(), "no outfile may be created on mismatch");
}

#[test]
fn mock_endpoint_aes256_etag_mismatch_exits_1_without_outfile() {
    // An AES256 object whose ETag is not the MD5 of the received bytes. The
    // SDK does not validate ETags, so this exercises the runner's own
    // in-transit integrity check: it must abort with exit 1 and leave no
    // outfile behind.
    let dir = tempfile::tempdir().unwrap();
    let outfile = dir.path().join("annotation.bin");
    let payload = "mock annotation payload";
    let wrong_etag = format!("\"{:x}\"", md5::compute(b"different bytes entirely"));
    let server = MockS3Server::start(vec![MockResponse::new(200, payload).with_headers(&[
        ("x-amz-server-side-encryption", "AES256"),
        ("ETag", wrong_etag.as_str()),
    ])]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args(["get-object-annotation", "--annotation-name", "note"])
        .args(mock_target_args(&server.endpoint_url()))
        .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(
        code,
        Some(1),
        "ETag mismatch should exit 1; stderr: {stderr}"
    );
    assert!(
        stderr.contains("ETag (MD5) verification failed"),
        "expected the ETag verification error; got: {stderr}"
    );
    assert!(!outfile.exists(), "no outfile may be created on mismatch");
}

#[test]
fn mock_endpoint_write_to_unwritable_outfile_exits_1_without_outfile() {
    // The S3 fetch succeeds and the payload passes the in-transit checks
    // (unverifiable, but writable), yet the <OUTFILE>'s parent directory does
    // not exist so the temp-file write fails. run_get_object_annotation must
    // propagate that as exit 1 and create nothing — covering the
    // write_verified_output error branch reached only after a successful fetch.
    let dir = tempfile::tempdir().unwrap();
    let missing_parent = dir.path().join("no-such-subdir");
    let outfile = missing_parent.join("annotation.bin");
    let payload = "mock annotation payload";
    let server = MockS3Server::start(vec![MockResponse::new(200, payload)]);
    let mut cmd = s7cmd_cmd_clean_env();
    cmd.args(["get-object-annotation", "--annotation-name", "note"])
        .args(mock_target_args(&server.endpoint_url()))
        .args(["s3://mock-bucket/mock-key", outfile.to_str().unwrap()]);
    let (code, _stdout, stderr) = common::run(&mut cmd);
    assert_eq!(
        code,
        Some(1),
        "an unwritable outfile must exit 1; stderr: {stderr}"
    );
    assert!(
        stderr.contains("creating temp file next to"),
        "expected the temp-file creation error; got: {stderr}"
    );
    assert!(
        !outfile.exists(),
        "no outfile may be created on write failure"
    );
    assert!(
        !missing_parent.exists(),
        "the missing parent dir must not be created"
    );
}