oneio 0.24.2

OneIO is a Rust library that provides unified simple IO interface for reading and writing to and from data files from different sources and compressions.
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
//! S3 integration tests against Cloudflare R2.
//!
//! These tests require R2 credentials in environment variables:
//! - AWS_ACCESS_KEY_ID
//! - AWS_SECRET_ACCESS_KEY
//! - AWS_ENDPOINT (e.g., https://xxx.r2.cloudflarestorage.com)
//! - AWS_REGION (use "auto" for R2)
//! - ONEIO_TEST_BUCKET (test bucket name, default: "oneio-test")
//!
//! Optional configuration:
//! - ONEIO_S3_CHUNK_SIZE (multipart part size in bytes, default: 8MB)
//! - ONEIO_S3_MULTIPART_THRESHOLD (file size threshold for multipart, default: 5MB)
//!
//! Run with: cargo test --features s3 -- --ignored --test-threads=1

use std::io::Read;
use std::sync::{Mutex, OnceLock};

// Serial execution lock to prevent test collisions
static S3_TEST_LOCK: OnceLock<Mutex<()>> = OnceLock::new();

fn test_bucket() -> String {
    std::env::var("ONEIO_TEST_BUCKET").unwrap_or_else(|_| "oneio-test".to_string())
}

fn begin_s3_test() -> (String, std::sync::MutexGuard<'static, ()>) {
    let guard = S3_TEST_LOCK
        .get_or_init(|| Mutex::new(()))
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    (test_bucket(), guard)
}

fn test_prefix(name: &str) -> String {
    let ts = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    format!("test-{ts}-{name}/")
}

fn generate_test_data(size: usize, seed: &str) -> Vec<u8> {
    let seed_bytes = seed.as_bytes();
    (0..size)
        .map(|i| seed_bytes[i % seed_bytes.len()])
        .collect()
}

/// RAII wrapper for a temporary file that deletes on drop.
struct TempFile {
    path: std::path::PathBuf,
}

impl TempFile {
    fn new(data: &[u8]) -> Self {
        let path = std::env::temp_dir().join(format!(
            "oneio_test_{}.bin",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::write(&path, data).unwrap();
        Self { path }
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.path);
    }
}

impl AsRef<std::path::Path> for TempFile {
    fn as_ref(&self) -> &std::path::Path {
        &self.path
    }
}

fn cleanup_test_objects(bucket: &str, prefix: &str) {
    if let Ok(keys) = oneio::s3_list(bucket, prefix, None, false) {
        for key in keys {
            let _ = oneio::s3_delete(bucket, &key);
        }
    }
}

fn assert_stream_matches(bucket: &str, key: &str, expected: &[u8]) {
    let mut reader = oneio::s3_reader(bucket, key).unwrap();
    let mut buffer = vec![0u8; 64 * 1024];
    let mut offset = 0;

    loop {
        let bytes_read = reader.read(&mut buffer).unwrap();
        if bytes_read == 0 {
            break;
        }
        assert_eq!(
            &buffer[..bytes_read],
            &expected[offset..offset + bytes_read]
        );
        offset += bytes_read;
    }

    assert_eq!(offset, expected.len());
}

// ========== Upload Tests ==========

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_single_put_small() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("single-put-small");
    let key = format!("{prefix}small-file.txt");
    let data = generate_test_data(1024, "small");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    // Verify by downloading
    let mut downloaded = Vec::new();
    oneio::s3_reader(&bucket, &key)
        .unwrap()
        .read_to_end(&mut downloaded)
        .unwrap();
    assert_eq!(downloaded, data);

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_single_put_small_leading_slash_key() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = format!("/{}", test_prefix("single-put-leading-slash"));
    let key = format!("{prefix}small-file.txt");
    let data = generate_test_data(1024, "leading-slash-small");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();
    assert_eq!(
        oneio::s3_list(&bucket, &prefix, None, false).unwrap(),
        vec![key.clone()]
    );
    assert_stream_matches(&bucket, &key, &data);
    oneio::s3_delete(&bucket, &key).unwrap();
    assert!(!oneio::s3_exists(&bucket, &key).unwrap());

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_single_put_just_under_5mb() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("single-put-just-under-5mb");
    let key = format!("{prefix}just-under-5mb.bin");
    let size = 5 * 1024 * 1024 - 1;
    let data = generate_test_data(size, "5mbfile");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    let stats = oneio::s3_stats(&bucket, &key).unwrap();
    assert_eq!(stats.content_length, size as u64);
    assert_stream_matches(&bucket, &key, &data);

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_multipart_just_over_5mb() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("multipart-just-over-5mb");
    let key = format!("{prefix}just-over-5mb.bin");
    let size = 5 * 1024 * 1024 + 1;
    let data = generate_test_data(size, "5mbplus1");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    let stats = oneio::s3_stats(&bucket, &key).unwrap();
    assert_eq!(stats.content_length, size as u64);
    assert_stream_matches(&bucket, &key, &data);

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_multipart_10mb() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("multipart-10mb");
    let key = format!("{prefix}multipart-10mb.bin");
    let size = 10 * 1024 * 1024;
    let data = generate_test_data(size, "10mbfile");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    let stats = oneio::s3_stats(&bucket, &key).unwrap();
    assert_eq!(stats.content_length, size as u64);
    assert_stream_matches(&bucket, &key, &data);

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_multipart_10mb_leading_slash_key() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = format!("/{}", test_prefix("multipart-10mb-leading-slash"));
    let key = format!("{prefix}multipart-10mb.bin");
    let data = generate_test_data(10 * 1024 * 1024, "leading-slash-10mb");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();
    assert_eq!(
        oneio::s3_list(&bucket, &prefix, None, false).unwrap(),
        vec![key.clone()]
    );

    let stats = oneio::s3_stats(&bucket, &key).unwrap();
    assert_eq!(stats.content_length, data.len() as u64);
    assert_stream_matches(&bucket, &key, &data);
    oneio::s3_delete(&bucket, &key).unwrap();
    assert!(!oneio::s3_exists(&bucket, &key).unwrap());

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_leading_slash_key_lifecycle() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = format!("/{}", test_prefix("leading-slash-lifecycle"));
    let source_key = format!("{prefix}source.bin");
    let copied_key = format!("{prefix}copied.bin");
    let data = generate_test_data(1024, "leading-slash-lifecycle");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &source_key, temp_path.as_ref().to_str().unwrap()).unwrap();
    assert!(oneio::s3_exists(&bucket, &source_key).unwrap());

    oneio::s3_copy(&bucket, &source_key, &copied_key).unwrap();
    assert!(oneio::s3_exists(&bucket, &copied_key).unwrap());
    assert_stream_matches(&bucket, &copied_key, &data);

    oneio::s3_delete(&bucket, &copied_key).unwrap();
    assert!(!oneio::s3_exists(&bucket, &copied_key).unwrap());

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_multipart_80mb() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("multipart-80mb");
    let key = format!("{prefix}multipart-80mb.bin");
    let size = 80 * 1024 * 1024;
    let data = generate_test_data(size, "80mbfile");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    let stats = oneio::s3_stats(&bucket, &key).unwrap();
    assert_eq!(stats.content_length, size as u64);
    assert_stream_matches(&bucket, &key, &data);

    cleanup_test_objects(&bucket, &prefix);
}

// ========== Download Tests ==========

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_download() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("download");
    let key = format!("{prefix}download-test.bin");
    let data = generate_test_data(1024 * 1024, "download");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    let download_path = TempFile::new(b"");
    oneio::s3_download(&bucket, &key, download_path.as_ref().to_str().unwrap()).unwrap();

    let downloaded = std::fs::read(&download_path).unwrap();
    assert_eq!(downloaded, data);

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_reader_streaming() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("reader-streaming");
    let key = format!("{prefix}stream-test.bin");
    let data = generate_test_data(2 * 1024 * 1024, "stream");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    let mut reader = oneio::s3_reader(&bucket, &key).unwrap();
    let mut downloaded = Vec::new();
    reader.read_to_end(&mut downloaded).unwrap();
    assert_eq!(downloaded, data);

    cleanup_test_objects(&bucket, &prefix);
}

// ========== List Tests ==========

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_list_objects() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("list-objects");

    // Upload 3 files
    for i in 0..3 {
        let key = format!("{prefix}file-{i}.txt");
        let data = generate_test_data(1024, &format!("list{i}"));
        let temp_path = TempFile::new(&data);
        oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();
    }

    let keys = oneio::s3_list(&bucket, &prefix, None, false).unwrap();
    assert_eq!(keys.len(), 3);
    for key in &keys {
        assert!(key.starts_with(&prefix));
    }

    cleanup_test_objects(&bucket, &prefix);
}

// ========== Metadata Tests ==========

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_head_object() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("head-object");
    let key = format!("{prefix}head-test.bin");
    let data = generate_test_data(5 * 1024 * 1024, "headtest");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    let stats = oneio::s3_stats(&bucket, &key).unwrap();
    assert_eq!(stats.content_length, data.len() as u64);
    assert!(stats.etag.is_some());

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_exists() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("exists");
    let key = format!("{prefix}exists-test.bin");
    let data = generate_test_data(1024, "exists");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();

    assert!(oneio::s3_exists(&bucket, &key).unwrap());
    assert!(!oneio::s3_exists(&bucket, &format!("{prefix}nonexistent")).unwrap());

    cleanup_test_objects(&bucket, &prefix);
}

// ========== Copy and Delete Tests ==========

// NOTE: This test verifies s3_copy works on S3-compatible services.
// Uses rusty-s3 for proper AWS Signature V4 signing.
#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_copy() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("copy");
    let src_key = format!("{prefix}copy-src.bin");
    let dst_key = format!("{prefix}copy-dst.bin");
    let data = generate_test_data(1024 * 1024, "copytest");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &src_key, temp_path.as_ref().to_str().unwrap()).unwrap();
    oneio::s3_copy(&bucket, &src_key, &dst_key).unwrap();

    assert!(oneio::s3_exists(&bucket, &src_key).unwrap());
    assert!(oneio::s3_exists(&bucket, &dst_key).unwrap());

    cleanup_test_objects(&bucket, &prefix);
}

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_delete() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("delete");
    let key = format!("{prefix}delete-test.bin");
    let data = generate_test_data(1024, "delete");
    let temp_path = TempFile::new(&data);

    oneio::s3_upload(&bucket, &key, temp_path.as_ref().to_str().unwrap()).unwrap();
    assert!(oneio::s3_exists(&bucket, &key).unwrap());

    oneio::s3_delete(&bucket, &key).unwrap();
    assert!(!oneio::s3_exists(&bucket, &key).unwrap());

    cleanup_test_objects(&bucket, &prefix);
}

// ========== Error Tests ==========

#[test]
#[ignore = "requires R2 credentials"]
fn test_r2_error_404() {
    let (bucket, _guard) = begin_s3_test();
    let prefix = test_prefix("error-404");
    let key = format!("{prefix}definitely-not-here.bin");

    let result = oneio::s3_stats(&bucket, &key);
    assert!(result.is_err());
}