about summary refs log tree commit diff
path: root/tvix/nix-compat
diff options
context:
space:
mode:
authoredef <edef@edef.eu>2023-10-27T01·12+0000
committeredef <edef@edef.eu>2023-10-27T07·37+0000
commit6a0a75c8e11aaf8dc8c3114eee354be34b7be16d (patch)
treed7d928428c71b2e3f47d8f00aa7575a583a5c791 /tvix/nix-compat
parent55c37a2871404e55aa2cb9e864dcfe389ac6015f (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/nix-compat')
-rw-r--r--tvix/nix-compat/Cargo.toml1
-rw-r--r--tvix/nix-compat/src/derivation/parser.rs8
-rw-r--r--tvix/nix-compat/src/nixbase32.rs31
-rw-r--r--tvix/nix-compat/src/nixhash/mod.rs43
-rw-r--r--tvix/nix-compat/src/store_path/mod.rs6
5 files changed, 27 insertions, 62 deletions
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);