s3rm-rs 1.3.2

Fast Amazon S3 object deletion tool.
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Property-based tests for AWS configuration support.
//
// Feature: s3rm-rs, Property 34: AWS Credential Loading
// For any AWS credential configuration (environment variables, credentials file,
// IAM roles), the tool should successfully load credentials through standard
// AWS SDK mechanisms.
// **Validates: Requirements 8.4**
//
// Feature: s3rm-rs, Property 35: Custom Endpoint Support
// For any custom endpoint specified, the tool should use that endpoint for
// S3 API calls, enabling support for S3-compatible services.
// **Validates: Requirements 8.6**

#[cfg(test)]
mod tests {
    use crate::config::args::parse_from_args;
    use crate::config::{CLITimeoutConfig, ClientConfig, Config, RetryConfig};
    use crate::types::{AccessKeys, ClientConfigLocation, S3Credentials};

    use aws_smithy_types::checksum_config::RequestChecksumCalculation;
    use proptest::prelude::*;

    // -----------------------------------------------------------------------
    // Generators
    // -----------------------------------------------------------------------

    /// Generate a random AWS access key string (alphanumeric, 16-20 chars).
    fn arb_access_key() -> impl Strategy<Value = String> {
        "[A-Z0-9]{16,20}"
    }

    /// Generate a random AWS secret access key string (alphanumeric, 30-40 chars).
    fn arb_secret_key() -> impl Strategy<Value = String> {
        "[A-Za-z0-9/+]{30,40}"
    }

    /// Generate an optional session token.
    fn arb_session_token() -> impl Strategy<Value = Option<String>> {
        prop_oneof![Just(None), "[A-Za-z0-9/+=]{20,40}".prop_map(Some),]
    }

    /// Generate a random AWS profile name.
    fn arb_profile_name() -> impl Strategy<Value = String> {
        prop_oneof![
            Just("default".to_string()),
            Just("production".to_string()),
            Just("staging".to_string()),
            "[a-z][a-z0-9_-]{2,15}".prop_map(|s| s),
        ]
    }

    /// Generate a random AWS region.
    fn arb_region() -> impl Strategy<Value = String> {
        prop_oneof![
            Just("us-east-1".to_string()),
            Just("us-west-2".to_string()),
            Just("eu-west-1".to_string()),
            Just("ap-northeast-1".to_string()),
        ]
    }

    /// Generate a random custom endpoint URL for S3-compatible services.
    fn arb_endpoint_url() -> impl Strategy<Value = String> {
        prop_oneof![
            Just("https://localhost:9000".to_string()),
            Just("https://minio.local:9000".to_string()),
            Just("https://s3.wasabisys.com".to_string()),
            Just("https://storage.googleapis.com".to_string()),
            Just("https://play.min.io".to_string()),
        ]
    }

    // -----------------------------------------------------------------------
    // Feature: s3rm-rs, Property 34: AWS Credential Loading
    // -----------------------------------------------------------------------

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// Feature: s3rm-rs, Property 34: AWS Credential Loading (access keys via CLI)
        /// **Validates: Requirements 8.4**
        ///
        /// For any access key and secret key provided via CLI args, the parsed
        /// Config should contain S3Credentials::Credentials with matching values.
        #[test]
        fn prop_credential_loading_access_keys(
            access_key in arb_access_key(),
            secret_key in arb_secret_key(),
            session_token in arb_session_token(),
            region in arb_region(),
        ) {
            let mut args: Vec<String> = vec![
                "s3rm".to_string(),
                "s3://test-bucket/prefix/".to_string(),
                "--target-access-key".to_string(),
                access_key.clone(),
                "--target-secret-access-key".to_string(),
                secret_key.clone(),
                "--target-region".to_string(),
                region.clone(),
            ];
            if let Some(ref token) = session_token {
                args.push("--target-session-token".to_string());
                args.push(token.clone());
            }

            let args_ref: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
            let cli = parse_from_args(args_ref).unwrap();
            let config = Config::try_from(cli).unwrap();

            let client_config = config.target_client_config.as_ref().unwrap();

            match &client_config.credential {
                S3Credentials::Credentials { access_keys } => {
                    prop_assert_eq!(
                        &access_keys.access_key,
                        &access_key,
                        "Access key must match CLI input"
                    );
                    prop_assert_eq!(
                        &access_keys.secret_access_key,
                        &secret_key,
                        "Secret key must match CLI input"
                    );
                    prop_assert_eq!(
                        &access_keys.session_token,
                        &session_token,
                        "Session token must match CLI input"
                    );
                }
                other => {
                    prop_assert!(
                        false,
                        "Expected S3Credentials::Credentials, got {:?}",
                        other
                    );
                }
            }
        }

        /// Feature: s3rm-rs, Property 34: AWS Credential Loading (profile via CLI)
        /// **Validates: Requirements 8.4**
        ///
        /// For any profile name provided via CLI args, the parsed Config should
        /// contain S3Credentials::Profile with the matching profile name.
        #[test]
        fn prop_credential_loading_profile(
            profile in arb_profile_name(),
        ) {
            let args: Vec<&str> = vec![
                "s3rm",
                "s3://test-bucket/prefix/",
                "--target-profile",
                &profile,
            ];

            let cli = parse_from_args(args).unwrap();
            let config = Config::try_from(cli).unwrap();

            let client_config = config.target_client_config.as_ref().unwrap();

            match &client_config.credential {
                S3Credentials::Profile(name) => {
                    prop_assert_eq!(
                        name,
                        &profile,
                        "Profile name must match CLI input"
                    );
                }
                other => {
                    prop_assert!(
                        false,
                        "Expected S3Credentials::Profile, got {:?}",
                        other
                    );
                }
            }
        }

        /// Feature: s3rm-rs, Property 34: AWS Credential Loading (environment default)
        /// **Validates: Requirements 8.4**
        ///
        /// When no explicit credentials are provided via CLI, the Config should
        /// use S3Credentials::FromEnvironment, relying on the AWS SDK default
        /// credential chain (env vars, credentials file, IAM roles).
        #[test]
        fn prop_credential_loading_from_environment(
            region in arb_region(),
        ) {
            let args: Vec<&str> = vec![
                "s3rm",
                "s3://test-bucket/prefix/",
                "--target-region",
                &region,
            ];

            let cli = parse_from_args(args).unwrap();
            let config = Config::try_from(cli).unwrap();

            let client_config = config.target_client_config.as_ref().unwrap();

            match &client_config.credential {
                S3Credentials::FromEnvironment => {
                    // Expected — AWS SDK default chain
                }
                other => {
                    prop_assert!(
                        false,
                        "Expected S3Credentials::FromEnvironment, got {:?}",
                        other
                    );
                }
            }
        }

        /// Feature: s3rm-rs, Property 34: AWS Credential Loading (client creation with access keys)
        /// **Validates: Requirements 8.4**
        ///
        /// For any access key credentials, ClientConfig::create_client() should
        /// successfully create an AWS S3 client with the configured region.
        #[test]
        fn prop_credential_client_creation(
            access_key in arb_access_key(),
            secret_key in arb_secret_key(),
            session_token in arb_session_token(),
            region in arb_region(),
        ) {
            let client_config = ClientConfig {
                client_config_location: ClientConfigLocation {
                    aws_config_file: None,
                    aws_shared_credentials_file: None,
                },
                credential: S3Credentials::Credentials {
                    access_keys: AccessKeys {
                        access_key: access_key.clone(),
                        secret_access_key: secret_key.clone(),
                        session_token: session_token.clone(),
                    },
                },
                region: Some(region.clone()),
                endpoint_url: Some("https://localhost:9000".to_string()),
                force_path_style: true,
                retry_config: RetryConfig {
                    aws_max_attempts: 3,
                    initial_backoff_milliseconds: 100,
                },
                cli_timeout_config: CLITimeoutConfig {
                    operation_timeout_milliseconds: None,
                    operation_attempt_timeout_milliseconds: None,
                    connect_timeout_milliseconds: None,
                    read_timeout_milliseconds: None,
                },
                disable_stalled_stream_protection: false,
                request_checksum_calculation: RequestChecksumCalculation::WhenRequired,
                accelerate: false,
                request_payer: None,
            };

            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();

            rt.block_on(async {
                let client = client_config.create_client().await;

                prop_assert_eq!(
                    client.config().region().unwrap().to_string(),
                    region,
                    "Client region must match configured region"
                );

                Ok(())
            })?;
        }
    }

    // -----------------------------------------------------------------------
    // Feature: s3rm-rs, Property 35: Custom Endpoint Support
    // -----------------------------------------------------------------------

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        /// Feature: s3rm-rs, Property 35: Custom Endpoint Support (CLI propagation)
        /// **Validates: Requirements 8.6**
        ///
        /// For any custom endpoint URL provided via CLI args, the parsed Config
        /// should contain the endpoint_url in the ClientConfig.
        #[test]
        fn prop_custom_endpoint_cli_propagation(
            endpoint in arb_endpoint_url(),
        ) {
            let args: Vec<&str> = vec![
                "s3rm",
                "s3://test-bucket/prefix/",
                "--target-endpoint-url",
                &endpoint,
            ];

            let cli = parse_from_args(args).unwrap();
            let config = Config::try_from(cli).unwrap();

            let client_config = config.target_client_config.as_ref().unwrap();

            prop_assert_eq!(
                client_config.endpoint_url.as_ref().unwrap(),
                &endpoint,
                "Endpoint URL must be propagated from CLI to ClientConfig"
            );
        }

        /// Feature: s3rm-rs, Property 35: Custom Endpoint Support (force path style)
        /// **Validates: Requirements 8.6**
        ///
        /// For S3-compatible services, --target-force-path-style must be correctly
        /// propagated to the ClientConfig.
        #[test]
        fn prop_custom_endpoint_force_path_style(
            endpoint in arb_endpoint_url(),
            force_path_style in proptest::bool::ANY,
        ) {
            let mut args: Vec<&str> = vec![
                "s3rm",
                "s3://test-bucket/prefix/",
                "--target-endpoint-url",
                &endpoint,
            ];
            if force_path_style {
                args.push("--target-force-path-style");
            }

            let cli = parse_from_args(args).unwrap();
            let config = Config::try_from(cli).unwrap();

            let client_config = config.target_client_config.as_ref().unwrap();

            prop_assert_eq!(
                client_config.force_path_style,
                force_path_style,
                "force_path_style must match CLI flag"
            );
        }

        /// Feature: s3rm-rs, Property 35: Custom Endpoint Support (client creation)
        /// **Validates: Requirements 8.6**
        ///
        /// For any custom endpoint URL, ClientConfig::create_client() should
        /// successfully create an AWS S3 client. The endpoint is applied via
        /// the SDK's endpoint_url configuration.
        #[test]
        fn prop_custom_endpoint_client_creation(
            endpoint in arb_endpoint_url(),
            force_path_style in proptest::bool::ANY,
            region in arb_region(),
        ) {
            let client_config = ClientConfig {
                client_config_location: ClientConfigLocation {
                    aws_config_file: None,
                    aws_shared_credentials_file: None,
                },
                credential: S3Credentials::Credentials {
                    access_keys: AccessKeys {
                        access_key: "AKIAIOSFODNN7EXAMPLE".to_string(),
                        secret_access_key: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY".to_string(),
                        session_token: None,
                    },
                },
                region: Some(region.clone()),
                endpoint_url: Some(endpoint.clone()),
                force_path_style,
                retry_config: RetryConfig {
                    aws_max_attempts: 3,
                    initial_backoff_milliseconds: 100,
                },
                cli_timeout_config: CLITimeoutConfig {
                    operation_timeout_milliseconds: None,
                    operation_attempt_timeout_milliseconds: None,
                    connect_timeout_milliseconds: None,
                    read_timeout_milliseconds: None,
                },
                disable_stalled_stream_protection: false,
                request_checksum_calculation: RequestChecksumCalculation::WhenRequired,
                accelerate: false,
                request_payer: None,
            };

            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();

            rt.block_on(async {
                let client = client_config.create_client().await;

                // The client should be successfully created with the custom endpoint
                prop_assert_eq!(
                    client.config().region().unwrap().to_string(),
                    region,
                    "Client region must be set correctly with custom endpoint"
                );

                // Verify endpoint_url is applied (client created without error)
                // Note: force_path_style is set via Builder::force_path_style()
                // in create_client() but the getter is not exposed on aws_sdk_s3::Config.
                // The fact that create_client() succeeds with any force_path_style
                // value confirms the config is accepted by the SDK.

                Ok(())
            })?;
        }

        /// Feature: s3rm-rs, Property 35: Custom Endpoint Support (no endpoint default)
        /// **Validates: Requirements 8.6**
        ///
        /// When no custom endpoint is specified, endpoint_url in ClientConfig
        /// should be None, using the default AWS S3 endpoint.
        #[test]
        fn prop_custom_endpoint_default_none(
            region in arb_region(),
        ) {
            let args: Vec<&str> = vec![
                "s3rm",
                "s3://test-bucket/prefix/",
                "--target-region",
                &region,
            ];

            let cli = parse_from_args(args).unwrap();
            let config = Config::try_from(cli).unwrap();

            let client_config = config.target_client_config.as_ref().unwrap();

            prop_assert!(
                client_config.endpoint_url.is_none(),
                "endpoint_url must be None when not specified"
            );
        }
    }
}