diff options
author | edef <edef@edef.eu> | 2023-10-27T01·12+0000 |
---|---|---|
committer | edef <edef@edef.eu> | 2023-10-27T07·37+0000 |
commit | 6a0a75c8e11aaf8dc8c3114eee354be34b7be16d (patch) | |
tree | d7d928428c71b2e3f47d8f00aa7575a583a5c791 /tvix | |
parent | 55c37a2871404e55aa2cb9e864dcfe389ac6015f (diff) |
refactor(tvix): condense long bytestrings r/6883
Change-Id: I3bea0827ec2c8db835334ce378a7bf3a39e9b1a3 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9849 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix')
-rw-r--r-- | tvix/Cargo.lock | 8 | ||||
-rw-r--r-- | tvix/Cargo.nix | 18 | ||||
-rw-r--r-- | tvix/castore/Cargo.toml | 1 | ||||
-rw-r--r-- | tvix/castore/src/fixtures.rs | 17 | ||||
-rw-r--r-- | tvix/castore/src/proto/tests/directory.rs | 22 | ||||
-rw-r--r-- | tvix/nix-compat/Cargo.toml | 1 | ||||
-rw-r--r-- | tvix/nix-compat/src/derivation/parser.rs | 8 | ||||
-rw-r--r-- | tvix/nix-compat/src/nixbase32.rs | 31 | ||||
-rw-r--r-- | tvix/nix-compat/src/nixhash/mod.rs | 43 | ||||
-rw-r--r-- | tvix/nix-compat/src/store_path/mod.rs | 6 | ||||
-rw-r--r-- | tvix/store/src/tests/fixtures.rs | 5 |
11 files changed, 67 insertions, 93 deletions
diff --git a/tvix/Cargo.lock b/tvix/Cargo.lock index 6cd38eabcb88..9c9a57c964b2 100644 --- a/tvix/Cargo.lock +++ b/tvix/Cargo.lock @@ -1013,6 +1013,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" [[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] name = "http" version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1435,6 +1441,7 @@ dependencies = [ "futures", "futures-util", "glob", + "hex-literal", "lazy_static", "nom", "serde", @@ -2874,6 +2881,7 @@ dependencies = [ "bytes", "data-encoding", "futures", + "hex-literal", "lazy_static", "pin-project-lite", "prost", diff --git a/tvix/Cargo.nix b/tvix/Cargo.nix index 759d808f0924..f0da4d5fdfa0 100644 --- a/tvix/Cargo.nix +++ b/tvix/Cargo.nix @@ -2935,6 +2935,16 @@ rec { }; resolvedDefaultFeatures = [ "default" ]; }; + "hex-literal" = rec { + crateName = "hex-literal"; + version = "0.4.1"; + edition = "2021"; + sha256 = "0iny5inkixsdr41pm2vkqh3fl66752z5j5c0cdxw16yl9ryjdqkg"; + authors = [ + "RustCrypto Developers" + ]; + + }; "http" = rec { crateName = "http"; version = "0.2.9"; @@ -4242,6 +4252,10 @@ rec { features = [ "executor" ]; } { + name = "hex-literal"; + packageId = "hex-literal"; + } + { name = "lazy_static"; packageId = "lazy_static"; } @@ -8678,6 +8692,10 @@ rec { ]; devDependencies = [ { + name = "hex-literal"; + packageId = "hex-literal"; + } + { name = "tempfile"; packageId = "tempfile"; } diff --git a/tvix/castore/Cargo.toml b/tvix/castore/Cargo.toml index 69da905a8777..573c7daa084f 100644 --- a/tvix/castore/Cargo.toml +++ b/tvix/castore/Cargo.toml @@ -36,6 +36,7 @@ tonic-build = "0.10.2" test-case = "2.2.2" tempfile = "3.3.0" tokio-retry = "0.3.0" +hex-literal = "0.4.1" [features] default = [] diff --git a/tvix/castore/src/fixtures.rs b/tvix/castore/src/fixtures.rs index ed3d1ca6e855..a206d9b7ddc6 100644 --- a/tvix/castore/src/fixtures.rs +++ b/tvix/castore/src/fixtures.rs @@ -9,20 +9,13 @@ pub const EMPTY_BLOB_CONTENTS: &[u8] = b""; lazy_static! { pub static ref DUMMY_DIGEST: B3Digest = { - let u: &[u8; 32] = &[ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, - ]; - u.into() + let u = [0u8; 32]; + (&u).into() }; pub static ref DUMMY_DIGEST_2: B3Digest = { - let u: &[u8; 32] = &[ - 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - ]; - u.into() + let mut u = [0u8; 32]; + u[0] = 0x10; + (&u).into() }; pub static ref DUMMY_DATA_1: bytes::Bytes = vec![0x01, 0x02, 0x03].into(); pub static ref DUMMY_DATA_2: bytes::Bytes = vec![0x04, 0x05].into(); diff --git a/tvix/castore/src/proto/tests/directory.rs b/tvix/castore/src/proto/tests/directory.rs index 69d9b5b4efe6..d4de19b77114 100644 --- a/tvix/castore/src/proto/tests/directory.rs +++ b/tvix/castore/src/proto/tests/directory.rs @@ -1,15 +1,11 @@ use crate::proto::{ Directory, DirectoryNode, FileNode, SymlinkNode, ValidateDirectoryError, ValidateNodeError, }; -use lazy_static::lazy_static; -lazy_static! { - static ref DUMMY_DIGEST: [u8; 32] = [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, - ]; -} +use hex_literal::hex; + +const DUMMY_DIGEST: [u8; 32] = [0; 32]; + #[test] fn size() { { @@ -145,13 +141,9 @@ fn digest() { assert_eq!( d.digest(), - vec![ - 0xaf, 0x13, 0x49, 0xb9, 0xf5, 0xf9, 0xa1, 0xa6, 0xa0, 0x40, 0x4d, 0xea, 0x36, 0xdc, - 0xc9, 0x49, 0x9b, 0xcb, 0x25, 0xc9, 0xad, 0xc1, 0x12, 0xb7, 0xcc, 0x9a, 0x93, 0xca, - 0xe4, 0x1f, 0x32, 0x62 - ] - .try_into() - .unwrap() + (&hex!("af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262")) + .try_into() + .unwrap() ) } diff --git a/tvix/nix-compat/Cargo.toml b/tvix/nix-compat/Cargo.toml index 19030612c283..cc6dfe5ebcdf 100644 --- a/tvix/nix-compat/Cargo.toml +++ b/tvix/nix-compat/Cargo.toml @@ -25,6 +25,7 @@ lazy_static = "1.4.0" serde_json = "1.0" test-case = "2.2.2" criterion = { version = "0.4", features = ["html_reports"] } +hex-literal = "0.4.1" [dev-dependencies.test-generator] # This fork of test-generator adds support for cargo workspaces, see diff --git a/tvix/nix-compat/src/derivation/parser.rs b/tvix/nix-compat/src/derivation/parser.rs index b04187c433dc..5d7b109f80de 100644 --- a/tvix/nix-compat/src/derivation/parser.rs +++ b/tvix/nix-compat/src/derivation/parser.rs @@ -300,13 +300,11 @@ mod tests { parse_error::ErrorKind, parser::from_algo_and_mode_and_digest, CAHash, NixHash, Output, }; use bstr::{BString, ByteSlice}; + use hex_literal::hex; use lazy_static::lazy_static; use test_case::test_case; - const DIGEST_SHA256: [u8; 32] = [ - 0xa5, 0xce, 0x9c, 0x15, 0x5e, 0xd0, 0x93, 0x97, 0x61, 0x46, 0x46, 0xc9, 0x71, 0x7f, 0xc7, - 0xcd, 0x94, 0xb1, 0x02, 0x3d, 0x7b, 0x76, 0xb6, 0x18, 0xd4, 0x09, 0xe4, 0xfe, 0xfd, 0x6e, - 0x9d, 0x39, - ]; + const DIGEST_SHA256: [u8; 32] = + hex!("a5ce9c155ed09397614646c9717fc7cd94b1023d7b76b618d409e4fefd6e9d39"); lazy_static! { pub static ref NIXHASH_SHA256: NixHash = NixHash::Sha256(DIGEST_SHA256); diff --git a/tvix/nix-compat/src/nixbase32.rs b/tvix/nix-compat/src/nixbase32.rs index 558d7ae8f48e..39b6b8cfe97d 100644 --- a/tvix/nix-compat/src/nixbase32.rs +++ b/tvix/nix-compat/src/nixbase32.rs @@ -124,37 +124,28 @@ pub fn encode_len(len: usize) -> usize { #[cfg(test)] mod tests { + use hex_literal::hex; use test_case::test_case; - #[test_case("", vec![] ; "empty bytes")] - #[test_case("0z", vec![0x1f]; "one byte")] - #[test_case("00bgd045z0d4icpbc2yyz4gx48ak44la", vec![ - 0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a, - 0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00]; "store path")] - #[test_case("0c5b8vw40dy178xlpddw65q9gf1h2186jcc3p4swinwggbllv8mk", vec![ - 0xb3, 0xa2, 0x4d, 0xe9, 0x7a, 0x8f, 0xdb, 0xc8, 0x35, 0xb9, 0x83, 0x31, 0x69, 0x50, 0x10, 0x30, - 0xb8, 0x97, 0x70, 0x31, 0xbc, 0xb5, 0x4b, 0x3b, 0x3a, 0xc1, 0x37, 0x40, 0xf8, 0x46, 0xab, 0x30, - ]; "sha256")] - fn encode(enc: &str, dec: Vec<u8>) { + #[test_case("", &[]; "empty bytes")] + #[test_case("0z", &hex!("1f"); "one byte")] + #[test_case("00bgd045z0d4icpbc2yyz4gx48ak44la", &hex!("8a12321522fd91efbd60ebb2481af88580f61600"); "store path")] + #[test_case("0c5b8vw40dy178xlpddw65q9gf1h2186jcc3p4swinwggbllv8mk", &hex!("b3a24de97a8fdbc835b9833169501030b8977031bcb54b3b3ac13740f846ab30"); "sha256")] + fn encode(enc: &str, dec: &[u8]) { assert_eq!(enc, super::encode(&dec)); } - #[test_case("", Some(vec![]) ; "empty bytes")] - #[test_case("0z", Some(vec![0x1f]); "one byte")] - #[test_case("00bgd045z0d4icpbc2yyz4gx48ak44la", Some(vec![ - 0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a, - 0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00]); "store path")] - #[test_case("0c5b8vw40dy178xlpddw65q9gf1h2186jcc3p4swinwggbllv8mk", Some(vec![ - 0xb3, 0xa2, 0x4d, 0xe9, 0x7a, 0x8f, 0xdb, 0xc8, 0x35, 0xb9, 0x83, 0x31, 0x69, 0x50, 0x10, 0x30, - 0xb8, 0x97, 0x70, 0x31, 0xbc, 0xb5, 0x4b, 0x3b, 0x3a, 0xc1, 0x37, 0x40, 0xf8, 0x46, 0xab, 0x30, - ]); "sha256")] + #[test_case("", Some(&[]) ; "empty bytes")] + #[test_case("0z", Some(&hex!("1f")); "one byte")] + #[test_case("00bgd045z0d4icpbc2yyz4gx48ak44la", Some(&hex!("8a12321522fd91efbd60ebb2481af88580f61600")); "store path")] + #[test_case("0c5b8vw40dy178xlpddw65q9gf1h2186jcc3p4swinwggbllv8mk", Some(&hex!("b3a24de97a8fdbc835b9833169501030b8977031bcb54b3b3ac13740f846ab30")); "sha256")] // this is invalid encoding, because it encodes 10 1-bits, so the carry // would be 2 1-bits #[test_case("zz", None; "invalid encoding-1")] // this is an even more specific example - it'd decode as 00000000 11 #[test_case("c0", None; "invalid encoding-2")] - fn decode(enc: &str, dec: Option<Vec<u8>>) { + fn decode(enc: &str, dec: Option<&[u8]>) { match dec { Some(dec) => { // The decode needs to match what's passed in dec diff --git a/tvix/nix-compat/src/nixhash/mod.rs b/tvix/nix-compat/src/nixhash/mod.rs index 1983d16feee9..d2c1d0bd2017 100644 --- a/tvix/nix-compat/src/nixhash/mod.rs +++ b/tvix/nix-compat/src/nixhash/mod.rs @@ -259,29 +259,14 @@ mod tests { nixhash::{self, HashAlgo, NixHash}, }; use data_encoding::{BASE64, BASE64_NOPAD, HEXLOWER}; + use hex_literal::hex; use test_case::test_case; - const DIGEST_SHA1: [u8; 20] = [ - 0x60, 0x16, 0x77, 0x79, 0x97, 0xc3, 0x0a, 0xb0, 0x24, 0x13, 0xcf, 0x50, 0x95, 0x62, 0x2c, - 0xd7, 0x92, 0x42, 0x83, 0xac, - ]; - - const DIGEST_SHA256: [u8; 32] = [ - 0xa5, 0xce, 0x9c, 0x15, 0x5e, 0xd0, 0x93, 0x97, 0x61, 0x46, 0x46, 0xc9, 0x71, 0x7f, 0xc7, - 0xcd, 0x94, 0xb1, 0x02, 0x3d, 0x7b, 0x76, 0xb6, 0x18, 0xd4, 0x09, 0xe4, 0xfe, 0xfd, 0x6e, - 0x9d, 0x39, - ]; - - const DIGEST_SHA512: [u8; 64] = [ - 0xab, 0x40, 0xd0, 0xbe, 0x35, 0x41, 0xf0, 0x77, 0x4b, 0xba, 0x78, 0x15, 0xd1, 0x3d, 0x10, - 0xb0, 0x32, 0x52, 0xe9, 0x6e, 0x95, 0xf7, 0xdb, 0xb4, 0xee, 0x99, 0xa3, 0xb4, 0x31, 0xc2, - 0x16, 0x62, 0xfd, 0x69, 0x71, 0xa0, 0x20, 0x16, 0x0e, 0x39, 0x84, 0x8a, 0xa5, 0xf3, 0x05, - 0xb9, 0xbe, 0x0f, 0x78, 0x72, 0x7b, 0x2b, 0x07, 0x89, 0xe3, 0x9f, 0x12, 0x4d, 0x21, 0xe9, - 0x2b, 0x8f, 0x39, 0xef, - ]; - const DIGEST_MD5: [u8; 16] = [ - 0xc4, 0x87, 0x4a, 0x88, 0x97, 0x44, 0x0b, 0x39, 0x3d, 0x86, 0x2d, 0x8f, 0xd4, 0x59, 0x07, - 0x3f, - ]; + + const DIGEST_SHA1: [u8; 20] = hex!("6016777997c30ab02413cf5095622cd7924283ac"); + const DIGEST_SHA256: [u8; 32] = + hex!("a5ce9c155ed09397614646c9717fc7cd94b1023d7b76b618d409e4fefd6e9d39"); + const DIGEST_SHA512: [u8; 64] = hex!("ab40d0be3541f0774bba7815d13d10b03252e96e95f7dbb4ee99a3b431c21662fd6971a020160e39848aa5f305b9be0f78727b2b0789e39f124d21e92b8f39ef"); + const DIGEST_MD5: [u8; 16] = hex!("c4874a8897440b393d862d8fd459073f"); fn to_base16(digest: &[u8]) -> String { HEXLOWER.encode(digest) @@ -412,12 +397,7 @@ mod tests { assert_eq!(HashAlgo::Sha256, nix_hash.algo()); assert_eq!( - vec![ - 0xa5, 0xce, 0x9c, 0x15, 0x5e, 0xd0, 0x93, 0x97, 0x61, 0x46, 0x46, 0xc9, 0x71, 0x7f, - 0xc7, 0xcd, 0x94, 0xb1, 0x02, 0x3d, 0x7b, 0x76, 0xb6, 0x18, 0xd4, 0x09, 0xe4, 0xfe, - 0xfd, 0x6e, 0x9d, 0x39 - ] - .as_slice(), + &hex!("a5ce9c155ed09397614646c9717fc7cd94b1023d7b76b618d409e4fefd6e9d39"), nix_hash.digest_as_bytes() ) } @@ -463,11 +443,8 @@ mod tests { fn sha256_broken_padding() { let broken_base64 = "fgIr3TyFGDAXP5+qoAaiMKDg/a1MlT6Fv/S/DaA24S8"; // if padded with a trailing '=' - let expected_digest = vec![ - 0x7e, 0x02, 0x2b, 0xdd, 0x3c, 0x85, 0x18, 0x30, 0x17, 0x3f, 0x9f, 0xaa, 0xa0, 0x06, - 0xa2, 0x30, 0xa0, 0xe0, 0xfd, 0xad, 0x4c, 0x95, 0x3e, 0x85, 0xbf, 0xf4, 0xbf, 0x0d, - 0xa0, 0x36, 0xe1, 0x2f, - ]; + let expected_digest = + hex!("7e022bdd3c851830173f9faaa006a230a0e0fdad4c953e85bff4bf0da036e12f"); // passing hash algo out of band should succeed let nix_hash = nixhash::from_str(&format!("sha256-{}", &broken_base64), Some("sha256")) diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs index 9253b2a8d741..e75d900ce006 100644 --- a/tvix/nix-compat/src/store_path/mod.rs +++ b/tvix/nix-compat/src/store_path/mod.rs @@ -210,6 +210,7 @@ mod tests { use crate::nixbase32; use crate::store_path::{DIGEST_SIZE, ENCODED_DIGEST_SIZE}; + use hex_literal::hex; use test_case::test_case; use super::{Error, StorePath}; @@ -226,10 +227,7 @@ mod tests { let nixpath = StorePath::from_bytes(example_nix_path_str.as_bytes()) .expect("Error parsing example string"); - let expected_digest: [u8; DIGEST_SIZE] = [ - 0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a, - 0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00, - ]; + let expected_digest: [u8; DIGEST_SIZE] = hex!("8a12321522fd91efbd60ebb2481af88580f61600"); assert_eq!("net-tools-1.60_p20170221182432", nixpath.name); assert_eq!(nixpath.digest, expected_digest); diff --git a/tvix/store/src/tests/fixtures.rs b/tvix/store/src/tests/fixtures.rs index 3f37d4a2a573..7c72d71c6d1e 100644 --- a/tvix/store/src/tests/fixtures.rs +++ b/tvix/store/src/tests/fixtures.rs @@ -8,10 +8,7 @@ pub const DUMMY_NAME: &str = "00000000000000000000000000000000-dummy"; lazy_static! { // output hash - pub static ref DUMMY_OUTPUT_HASH: bytes::Bytes = vec![ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00 - ].into(); + pub static ref DUMMY_OUTPUT_HASH: bytes::Bytes = vec![0; 20].into(); /// The NAR representation of a symlink pointing to `/nix/store/somewhereelse` pub static ref NAR_CONTENTS_SYMLINK: Vec<u8> = vec![ |