quilt-rs 0.30.0

Rust library for accessing Quilt data packages.
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
//! SHA256 checksum implementation

use aws_smithy_checksums::ChecksumAlgorithm;
use multihash::Multihash;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;
use std::fmt;
use tokio::fs::File;
use tokio::io::AsyncRead;
use tokio::io::AsyncReadExt;
use tokio::io::BufReader;

use crate::checksum::hash::Hash;
use crate::error::ChecksumError;
use crate::Error;
use crate::Res;

/// Multihash code for legacy or single-chunked checksums
pub const MULTIHASH_SHA256: u64 = 0x12;

/// SHA256 (legacy) checksum wrapper
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Sha256Hash(Multihash<256>);

impl Sha256Hash {
    /// Calculates legacy or single-chunk checksum from any async reader
    pub async fn from_async_read<F: AsyncRead + Unpin>(file: F) -> Res<Self> {
        let mut hasher = ChecksumAlgorithm::Sha256.into_impl();
        let mut reader = BufReader::new(file);
        let mut buf = [0; 4096];
        loop {
            let n = reader.read(&mut buf).await?;
            if n == 0 {
                break;
            }
            hasher.update(&buf[0..n]);
        }
        Ok(Self(Multihash::wrap(MULTIHASH_SHA256, &hasher.finalize())?))
    }
}

impl Hash for Sha256Hash {
    /// Get the inner multihash
    fn multihash(&self) -> &Multihash<256> {
        &self.0
    }

    /// Calculates legacy or single-chunk checksum from file
    async fn from_file(file: File) -> Res<Self> {
        Self::from_async_read(file).await
    }
}

// From/TryFrom conversions for Sha256Hash
impl From<Sha256Hash> for Multihash<256> {
    fn from(sha256: Sha256Hash) -> Self {
        sha256.0
    }
}

impl TryFrom<Multihash<256>> for Sha256Hash {
    type Error = Error;

    fn try_from(hash: Multihash<256>) -> Result<Self, Self::Error> {
        if hash.code() == MULTIHASH_SHA256 {
            Ok(Self(hash))
        } else {
            Err(Error::Checksum(ChecksumError::InvalidMultihash(format!(
                "Expected SHA256 hash (code {:#06x}), got code {:#06x}",
                MULTIHASH_SHA256,
                hash.code()
            ))))
        }
    }
}

impl TryFrom<&str> for Sha256Hash {
    type Error = Error;

    fn try_from(hex_str: &str) -> Result<Self, Self::Error> {
        // Add multibase prefix to plain hex and decode with multibase
        let prefixed_value = format!("{}{}", multibase::Base::Base16Lower.code(), hex_str);
        let (_, hash_bytes) = multibase::decode(&prefixed_value)?;
        let multihash = Multihash::wrap(MULTIHASH_SHA256, &hash_bytes)?;
        Ok(Self(multihash))
    }
}

impl fmt::Display for Sha256Hash {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Use multibase encoding but strip the prefix to get plain hex
        let multibase_encoded = multibase::encode(multibase::Base::Base16Lower, self.digest());
        let hex_value = &multibase_encoded[1..]; // Remove the multibase prefix
        write!(f, "{}", hex_value)
    }
}

impl Serialize for Sha256Hash {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeMap;
        let mut map = serializer.serialize_map(Some(2))?;
        map.serialize_entry("type", "SHA256")?;
        map.serialize_entry("value", &self.to_string())?;
        map.end()
    }
}

#[derive(Deserialize)]
struct Sha256HashJson {
    #[serde(rename = "type")]
    hash_type: String,
    value: String,
}

impl<'de> Deserialize<'de> for Sha256Hash {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;
        use serde::de::Unexpected;

        let json = Sha256HashJson::deserialize(deserializer)?;

        if json.hash_type != "SHA256" {
            return Err(Error::invalid_value(
                Unexpected::Str(&json.hash_type),
                &"SHA256",
            ));
        }

        Sha256Hash::try_from(json.value.as_str())
            .map_err(|_| Error::invalid_value(Unexpected::Str(&json.value), &"valid hex string"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use test_log::test;

    use std::path::Path;

    use aws_sdk_s3::primitives::ByteStream;

    use crate::io::storage::mocks::MockStorage;
    use crate::io::storage::Storage;

    #[test]
    fn test_sha256_hash_algorithm() {
        let sha256_hash = multihash::Multihash::wrap(MULTIHASH_SHA256, b"test").unwrap();
        let sha256 = Sha256Hash::try_from(sha256_hash).unwrap();
        assert_eq!(sha256.algorithm(), MULTIHASH_SHA256);
    }

    #[test]
    fn test_sha256_hash_conversions() {
        // Create a SHA256 hash and test conversions
        let original_hash = multihash::Multihash::wrap(MULTIHASH_SHA256, b"test_data").unwrap();
        let sha256 = Sha256Hash::try_from(original_hash).unwrap();
        let converted_back: Multihash<256> = sha256.into();
        assert_eq!(original_hash, converted_back);
    }

    #[test]
    fn test_sha256_hash_conversion_error() {
        // Try to convert a SHA256Chunked hash to Sha256Hash (should fail)
        let sha256_chunked_hash = multihash::Multihash::wrap(0xb510, b"test").unwrap(); // SHA256_CHUNKED code
        let result = Sha256Hash::try_from(sha256_chunked_hash);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Expected SHA256 hash"));
    }

    #[test]
    fn test_sha256_hash_serde() {
        let original_hash = multihash::Multihash::wrap(MULTIHASH_SHA256, b"test_data").unwrap();
        let sha256 = Sha256Hash::try_from(original_hash).unwrap();

        // Test serialization
        let serialized = serde_json::to_string(&sha256).unwrap();

        // Test deserialization
        let deserialized: Sha256Hash = serde_json::from_str(&serialized).unwrap();
        assert_eq!(sha256, deserialized);

        // Test specific format (plain hex)
        let test_json = r#"{"type":"SHA256","value":"deadbeef"}"#;
        let parsed: Sha256Hash = serde_json::from_str(test_json).unwrap();
        let multibase_with_prefix =
            multibase::encode(multibase::Base::Base16Lower, parsed.digest());
        assert_eq!(multibase_with_prefix, "fdeadbeef");

        // Test serialized format (should be hex without prefix)
        let expected_multibase = multibase::encode(multibase::Base::Base16Lower, sha256.digest());
        let expected_hex = &expected_multibase[1..]; // Remove 'f' prefix
        assert!(serialized.contains("\"type\":\"SHA256\""));
        assert!(serialized.contains(&format!("\"value\":\"{}\"", expected_hex)));
    }

    #[test(tokio::test)]
    async fn test_sha256_hash_from_file() -> crate::Res {
        let storage = MockStorage::default();
        let test_data = b"test file content";
        let test_path = Path::new("test_file.txt");

        // Write test data to mock storage
        storage
            .write_byte_stream(test_path, ByteStream::from_static(test_data))
            .await?;

        // Test from_file method
        let file = storage.open_file(test_path).await?;
        let hash_from_method = Sha256Hash::from_async_read(file).await?;

        // Compare with manual creation
        let mut manual_hasher = ChecksumAlgorithm::Sha256.into_impl();
        manual_hasher.update(test_data);
        let expected_digest = manual_hasher.finalize();
        let expected_hash =
            Sha256Hash::try_from(Multihash::wrap(MULTIHASH_SHA256, &expected_digest)?)?;

        assert_eq!(hash_from_method, expected_hash);
        assert_eq!(hash_from_method.algorithm(), MULTIHASH_SHA256);
        assert_eq!(hash_from_method.digest(), &expected_digest);

        Ok(())
    }

    #[test]
    fn test_sha256_hash_serde_errors() {
        // Test invalid type
        let invalid_type = r#"{"type":"INVALID","value":"deadbeef"}"#;
        let result: Result<Sha256Hash, _> = serde_json::from_str(invalid_type);
        assert!(result.is_err());

        // Test invalid hex
        let invalid_hex = r#"{"type":"SHA256","value":"invalid_hex"}"#;
        let result: Result<Sha256Hash, _> = serde_json::from_str(invalid_hex);
        assert!(result.is_err());

        // Test missing fields
        let missing_type = r#"{"value":"deadbeef"}"#;
        let result: Result<Sha256Hash, _> = serde_json::from_str(missing_type);
        assert!(result.is_err());

        let missing_value = r#"{"type":"SHA256"}"#;
        let result: Result<Sha256Hash, _> = serde_json::from_str(missing_value);
        assert!(result.is_err());
    }

    #[test(tokio::test)]
    async fn test_calculate_sha256_checksum() -> crate::Res {
        let storage = MockStorage::default();
        let bytes = crate::fixtures::objects::less_than_8mb();
        let test_path = Path::new("checksum_test.txt");

        // Write test data to mock storage
        storage
            .write_byte_stream(test_path, ByteStream::from_static(bytes))
            .await?;

        // Test from_file method
        let file = storage.open_file(test_path).await?;
        let hash = Sha256Hash::from_async_read(file).await?;

        assert_eq!(hash.multihash().code(), MULTIHASH_SHA256);

        let mut double_hasher = ChecksumAlgorithm::Sha256.into_impl();
        double_hasher.update(hash.digest());
        let double_hash = double_hasher.finalize();
        assert_eq!(
            hex::encode(double_hash),
            crate::fixtures::objects::LESS_THAN_8MB_HASH_HEX
        );

        Ok(())
    }

    #[test(tokio::test)]
    async fn test_sha256_from_bytes() -> crate::Res {
        let storage = MockStorage::default();
        let bytes = crate::fixtures::objects::less_than_8mb();
        let test_path = Path::new("less_than_8mb.txt");

        // Write test data to mock storage
        storage
            .write_byte_stream(test_path, ByteStream::from_static(bytes))
            .await?;

        // Test from_file method
        let file = storage.open_file(test_path).await?;
        let hash = Sha256Hash::from_async_read(file).await?;

        assert_eq!(hash.algorithm(), MULTIHASH_SHA256);
        Ok(())
    }

    #[test(tokio::test)]
    async fn test_sha256_hash_conversions_from_file() -> crate::Res {
        let storage = MockStorage::default();
        let bytes = crate::fixtures::objects::less_than_8mb();
        let test_path = Path::new("conversion_test.txt");

        // Write test data to mock storage
        storage
            .write_byte_stream(test_path, ByteStream::from_static(bytes))
            .await?;

        // Test Sha256Hash conversions
        let file = storage.open_file(test_path).await?;
        let sha256 = Sha256Hash::from_async_read(file).await?;
        let multihash: Multihash<256> = sha256.clone().into();
        let back_to_sha256 = Sha256Hash::try_from(multihash)?;
        assert_eq!(sha256, back_to_sha256);

        Ok(())
    }

    #[test]
    fn test_sha256_hash_display() {
        let original_hash = multihash::Multihash::wrap(MULTIHASH_SHA256, b"test_data").unwrap();
        let sha256 = Sha256Hash::try_from(original_hash).unwrap();

        // Test Display implementation
        let display_string = format!("{}", sha256);

        // Should be hex without multibase prefix
        let expected_hex = hex::encode(b"test_data");
        assert_eq!(display_string, expected_hex);

        // Test that to_string() works (provided by Display)
        assert_eq!(sha256.to_string(), expected_hex);
    }

    #[test]
    fn test_sha256_hash_try_from_str() {
        // Test valid hex string
        let hex_str = "deadbeef";
        let hash = Sha256Hash::try_from(hex_str).unwrap();
        assert_eq!(hash.algorithm(), MULTIHASH_SHA256);

        // Test that we can convert back to string (round-trip)
        let back_to_string = hash.to_string();
        assert_eq!(back_to_string, hex_str);

        // Test invalid hex string (contains non-hex characters)
        let invalid_hex = "invalid_hex_string!";
        let result = Sha256Hash::try_from(invalid_hex);
        assert!(result.is_err());

        // Test odd-length hex string (multibase might handle this differently)
        let odd_hex = "abc";
        let _result = Sha256Hash::try_from(odd_hex);
        // This might succeed or fail depending on multibase implementation
        // Let's just check that it doesn't panic
    }

    #[test(tokio::test)]
    async fn test_sha256_hash_from_empty_slice() -> crate::Res {
        let storage = MockStorage::default();

        // Test with empty file (0 bytes) - simulates hash.digest() returning empty data
        let empty_path = Path::new("empty_test.bin");
        storage
            .write_byte_stream(empty_path, ByteStream::default())
            .await?;

        let file = storage.open_file(empty_path).await?;
        let hash = Sha256Hash::from_async_read(file).await?;

        // Verify the hash was created successfully
        assert_eq!(hash.algorithm(), MULTIHASH_SHA256);
        assert_eq!(hash.digest().len(), 32); // SHA256 digest is always 32 bytes

        let expected_empty_hash = crate::fixtures::objects::ZERO_HASH_HEX;
        assert_eq!(hex::encode(hash.digest()), expected_empty_hash);

        // Test with typical hash digest size (32 bytes for SHA256) to ensure no regression
        let digest_path = Path::new("digest_test.bin");
        storage
            .write_byte_stream(digest_path, ByteStream::from_static(&[0u8; 32]))
            .await?;

        let file2 = storage.open_file(digest_path).await?;
        let hash2 = Sha256Hash::from_async_read(file2).await?;

        // Should successfully create hash from 32-byte input
        assert_eq!(hash2.algorithm(), MULTIHASH_SHA256);
        assert_eq!(hash2.digest().len(), 32);

        // Different input should produce different hash
        assert_ne!(hash, hash2);

        Ok(())
    }
}