multi-base 1.0.2

multibase in rust
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
441
442
443
444
// SPDX-License-Identifier: MIT

//! Thread safety tests for the multibase crate.
//!
//! This module contains compile-time and runtime tests to verify that all
//! public types are properly Send and Sync, and that concurrent operations
//! are safe and correct.

use multi_base::{Base, EncodedString, Error};

/// Compile-time assertion that a type implements Send.
const fn assert_send<T: Send>() {}

/// Compile-time assertion that a type implements Sync.
const fn assert_sync<T: Sync>() {}

/// Compile-time assertion that a type implements both Send and Sync.
const fn assert_send_sync<T: Send + Sync>() {}

/// Test that Base enum is Send and Sync.
///
/// The Base enum should be Send and Sync because:
/// - It's a simple enum with no interior mutability
/// - All variants contain no data or only simple data
/// - It's Copy, which implies Send + Sync for its contents
#[test]
fn base_is_send_sync() {
    assert_send::<Base>();
    assert_sync::<Base>();
    assert_send_sync::<Base>();
}

/// Test that Error type is Send and Sync.
///
/// The Error type should be Send and Sync because:
/// - It uses thiserror which generates Send + Sync implementations
/// - All error variants contain only Send + Sync types
/// - No interior mutability is present
#[test]
fn error_is_send_sync() {
    assert_send::<Error>();
    assert_sync::<Error>();
    assert_send_sync::<Error>();
}

/// Test that `EncodedString` is Send and Sync.
///
/// `EncodedString` should be Send and Sync because:
/// - It contains a Base (which is Send + Sync)
/// - It contains a String (which is Send + Sync)
/// - No interior mutability is present
#[test]
fn encoded_string_is_send_sync() {
    assert_send::<EncodedString>();
    assert_sync::<EncodedString>();
    assert_send_sync::<EncodedString>();
}

/// Test that Result types are Send and Sync.
///
/// Result types should inherit Send + Sync from their contained types.
#[test]
fn result_types_are_send_sync() {
    assert_send::<Result<(Base, Vec<u8>), Error>>();
    assert_sync::<Result<(Base, Vec<u8>), Error>>();
    assert_send_sync::<Result<(Base, Vec<u8>), Error>>();

    assert_send::<Result<EncodedString, Error>>();
    assert_sync::<Result<EncodedString, Error>>();
    assert_send_sync::<Result<EncodedString, Error>>();
}

/// Test that Base can be safely sent between threads.
#[test]
fn base_send_between_threads() {
    use std::thread;

    let base = Base::Base64;
    let handle = thread::spawn(move || {
        // Use the base in another thread
        assert_eq!(base.code(), 'm');
    });
    handle.join().unwrap();
}

/// Test that Base can be safely shared between threads.
#[test]
fn base_sync_between_threads() {
    use std::sync::Arc;
    use std::thread;

    let base = Arc::new(Base::Base58Btc);
    let base_clone = Arc::clone(&base);

    let handle = thread::spawn(move || {
        assert_eq!(base_clone.code(), 'z');
    });

    assert_eq!(base.code(), 'z');
    handle.join().unwrap();
}

/// Test that Error can be safely sent between threads.
#[test]
fn error_send_between_threads() {
    use std::thread;

    let error = Error::UnknownBase { code: 'x' };
    let handle = thread::spawn(move || {
        assert!(matches!(error, Error::UnknownBase { code: 'x' }));
    });
    handle.join().unwrap();
}

/// Test that Error can be safely shared between threads.
#[test]
fn error_sync_between_threads() {
    use std::sync::Arc;
    use std::thread;

    let error = Arc::new(Error::EmptyInput);
    let error_clone = Arc::clone(&error);

    let handle = thread::spawn(move || {
        assert!(matches!(*error_clone, Error::EmptyInput));
    });

    assert!(matches!(*error, Error::EmptyInput));
    handle.join().unwrap();
}

/// Test that `EncodedString` can be safely sent between threads.
#[test]
fn encoded_string_send_between_threads() {
    use std::thread;

    let encoded = EncodedString::new("zCn8eVZg").unwrap();
    let handle = thread::spawn(move || {
        assert_eq!(encoded.base(), Base::Base58Btc);
        assert_eq!(encoded.as_str(), "zCn8eVZg");
    });
    handle.join().unwrap();
}

/// Test that `EncodedString` can be safely shared between threads.
#[test]
fn encoded_string_sync_between_threads() {
    use std::sync::Arc;
    use std::thread;

    let encoded = Arc::new(EncodedString::new("md29ybGQ").unwrap());
    let encoded_clone = Arc::clone(&encoded);

    let handle = thread::spawn(move || {
        assert_eq!(encoded_clone.base(), Base::Base64);
        assert_eq!(encoded_clone.as_str(), "md29ybGQ");
    });

    assert_eq!(encoded.base(), Base::Base64);
    handle.join().unwrap();
}

/// Test concurrent encoding operations from multiple threads.
#[test]
fn concurrent_encoding_correctness() {
    use std::sync::Arc;
    use std::thread;

    let test_data = Arc::new(vec![0xAB; 1000]);
    let mut handles = vec![];

    // Spawn 10 threads that all encode the same data
    for _ in 0..10 {
        let data = Arc::clone(&test_data);
        let handle = thread::spawn(move || {
            let encoded = multi_base::encode(Base::Base64, &*data);
            assert!(encoded.starts_with('m'));
            encoded
        });
        handles.push(handle);
    }

    // All threads should produce identical results
    let results: Vec<String> = handles.into_iter().map(|h| h.join().unwrap()).collect();
    for result in &results[1..] {
        assert_eq!(&results[0], result);
    }
}

/// Test concurrent decoding operations from multiple threads.
#[test]
fn concurrent_decoding_correctness() {
    use std::sync::Arc;
    use std::thread;

    let encoded = Arc::new("zCn8eVZg".to_string());
    let mut handles = vec![];

    // Spawn 10 threads that all decode the same string
    for _ in 0..10 {
        let enc = Arc::clone(&encoded);
        let handle = thread::spawn(move || {
            let (base, decoded) = multi_base::decode(&*enc, true).unwrap();
            assert_eq!(base, Base::Base58Btc);
            decoded
        });
        handles.push(handle);
    }

    // All threads should produce identical results
    let results: Vec<Vec<u8>> = handles.into_iter().map(|h| h.join().unwrap()).collect();
    for result in &results[1..] {
        assert_eq!(&results[0], result);
    }
}

/// Test concurrent `Base::from_code` operations.
#[test]
fn concurrent_base_from_code() {
    use std::thread;

    let codes = vec!['m', 'z', 'f', 'b', 'u', 'M', 'Z', 'F', 'B', 'U'];
    let mut handles = vec![];

    for code in codes {
        let handle = thread::spawn(move || {
            let base = Base::from_code(code).unwrap();
            (code, base)
        });
        handles.push(handle);
    }

    for handle in handles {
        let (code, base) = handle.join().unwrap();
        // Verify round-trip
        assert_eq!(base.code(), code);
    }
}

/// Test concurrent `encode_into` with thread-local buffers.
#[test]
fn concurrent_encode_into_thread_local_buffers() {
    use std::sync::Arc;
    use std::thread;

    let test_data = Arc::new(b"test data for concurrent encoding".to_vec());
    let mut handles = vec![];

    for i in 0..10 {
        let data = Arc::clone(&test_data);
        let handle = thread::spawn(move || {
            // Each thread has its own buffer
            let mut buffer = String::new();
            for _ in 0..100 {
                multi_base::encode_into(Base::Base64, &*data, &mut buffer);
                assert!(buffer.starts_with('m'));
            }
            i
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

/// Test concurrent `decode_into` with thread-local buffers.
#[test]
fn concurrent_decode_into_thread_local_buffers() {
    use std::sync::Arc;
    use std::thread;

    let encoded = Arc::new("md29ybGQ".to_string());
    let mut handles = vec![];

    for i in 0..10 {
        let enc = Arc::clone(&encoded);
        let handle = thread::spawn(move || {
            // Each thread has its own buffer
            let mut buffer = Vec::new();
            for _ in 0..100 {
                let base = multi_base::decode_into(&*enc, true, &mut buffer).unwrap();
                assert_eq!(base, Base::Base64);
                assert_eq!(buffer, b"world");
            }
            i
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

/// Test that multiple threads can work with different bases simultaneously.
#[test]
fn concurrent_multi_base_operations() {
    use std::thread;

    let bases = vec![
        Base::Base2,
        Base::Base8,
        Base::Base10,
        Base::Base16Lower,
        Base::Base32Lower,
        Base::Base58Btc,
        Base::Base64,
        Base::Base64Url,
    ];

    let mut handles = vec![];

    for base in bases {
        let handle = thread::spawn(move || {
            let data = b"concurrent test data";
            let encoded = multi_base::encode(base, data);
            let (decoded_base, decoded) = multi_base::decode(&encoded, true).unwrap();
            assert_eq!(decoded_base, base);
            assert_eq!(&decoded[..], data);
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

/// Test that `EncodedString` operations are thread-safe.
#[test]
fn concurrent_encoded_string_operations() {
    use std::sync::Arc;
    use std::thread;

    let encoded_strings = vec![
        EncodedString::new("zCn8eVZg").unwrap(),
        EncodedString::new("md29ybGQ").unwrap(),
        EncodedString::new("f48656c6c6f").unwrap(),
    ];

    let shared = Arc::new(encoded_strings);
    let mut handles = vec![];

    for i in 0..10 {
        let strings = Arc::clone(&shared);
        let handle = thread::spawn(move || {
            for encoded in strings.iter() {
                let _base = encoded.base();
                let _str = encoded.as_str();
                let _decoded = encoded.decode().unwrap();
            }
            i
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

/// Test no data races with concurrent read-only access.
#[test]
fn no_data_races_read_only() {
    use std::sync::Arc;
    use std::thread;

    let base = Arc::new(Base::Base64);
    let mut handles = vec![];

    // Multiple threads reading the same Base concurrently
    for _ in 0..20 {
        let b = Arc::clone(&base);
        let handle = thread::spawn(move || {
            for _ in 0..1000 {
                let _code = b.code();
                let _encoded = b.encode(b"test");
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

/// Test that error handling is thread-safe.
#[test]
fn concurrent_error_handling() {
    use std::thread;

    let invalid_inputs = vec!["", "x123", "!invalid", "?bad", "@wrong"];

    let mut handles = vec![];

    for input in invalid_inputs {
        let handle = thread::spawn(move || {
            let result = multi_base::decode(input, true);
            assert!(result.is_err());
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

/// Test stress scenario with many concurrent operations.
#[test]
fn stress_test_concurrent_operations() {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::thread;

    let counter = Arc::new(AtomicUsize::new(0));
    let mut handles = vec![];

    // Spawn 20 threads that each perform 100 operations
    for _ in 0..20 {
        let c = Arc::clone(&counter);
        let handle = thread::spawn(move || {
            for i in 0..100 {
                let data = format!("data{i}");
                let encoded = multi_base::encode(Base::Base64, data.as_bytes());
                let (base, decoded) = multi_base::decode(&encoded, true).unwrap();
                assert_eq!(base, Base::Base64);
                assert_eq!(decoded, data.as_bytes());
                c.fetch_add(1, Ordering::SeqCst);
            }
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }

    // Verify all operations completed
    assert_eq!(counter.load(Ordering::SeqCst), 2000);
}