about summary refs log tree commit diff
path: root/tvix/store/src/tests/nar_renderer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tvix/store/src/tests/nar_renderer.rs')
-rw-r--r--tvix/store/src/tests/nar_renderer.rs164
1 files changed, 92 insertions, 72 deletions
diff --git a/tvix/store/src/tests/nar_renderer.rs b/tvix/store/src/tests/nar_renderer.rs
index 3d7cfd4a96a7..48c07e53c93e 100644
--- a/tvix/store/src/tests/nar_renderer.rs
+++ b/tvix/store/src/tests/nar_renderer.rs
@@ -1,28 +1,29 @@
 use crate::directoryservice::DirectoryService;
-use crate::nar::NARRenderer;
+use crate::nar::calculate_size_and_sha256;
+use crate::nar::writer_nar;
 use crate::proto::DirectoryNode;
 use crate::proto::FileNode;
 use crate::proto::SymlinkNode;
 use crate::tests::fixtures::*;
 use crate::tests::utils::*;
+use sha2::{Digest, Sha256};
 use std::io;
 
 #[test]
 fn single_symlink() {
-    let renderer = NARRenderer::new(gen_blob_service(), gen_directory_service());
-    // don't put anything in the stores, as we don't actually do any requests.
-
     let mut buf: Vec<u8> = vec![];
 
-    renderer
-        .write_nar(
-            &mut buf,
-            &crate::proto::node::Node::Symlink(SymlinkNode {
-                name: "doesntmatter".to_string(),
-                target: "/nix/store/somewhereelse".to_string(),
-            }),
-        )
-        .expect("must succeed");
+    writer_nar(
+        &mut buf,
+        &crate::proto::node::Node::Symlink(SymlinkNode {
+            name: "doesntmatter".to_string(),
+            target: "/nix/store/somewhereelse".to_string(),
+        }),
+        // don't put anything in the stores, as we don't actually do any requests.
+        &gen_blob_service(),
+        gen_directory_service(),
+    )
+    .expect("must succeed");
 
     assert_eq!(buf, NAR_CONTENTS_SYMLINK.to_vec());
 }
@@ -30,20 +31,21 @@ fn single_symlink() {
 /// Make sure the NARRenderer fails if a referred blob doesn't exist.
 #[test]
 fn single_file_missing_blob() {
-    let renderer = NARRenderer::new(gen_blob_service(), gen_directory_service());
     let mut buf: Vec<u8> = vec![];
 
-    let e = renderer
-        .write_nar(
-            &mut buf,
-            &crate::proto::node::Node::File(FileNode {
-                name: "doesntmatter".to_string(),
-                digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
-                size: HELLOWORLD_BLOB_CONTENTS.len() as u32,
-                executable: false,
-            }),
-        )
-        .expect_err("must fail");
+    let e = writer_nar(
+        &mut buf,
+        &crate::proto::node::Node::File(FileNode {
+            name: "doesntmatter".to_string(),
+            digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
+            size: HELLOWORLD_BLOB_CONTENTS.len() as u32,
+            executable: false,
+        }),
+        // the blobservice is empty intentionally, to provoke the error.
+        &gen_blob_service(),
+        gen_directory_service(),
+    )
+    .expect_err("must fail");
 
     match e {
         crate::nar::RenderError::NARWriterError(e) => {
@@ -68,22 +70,22 @@ fn single_file_wrong_blob_size() {
     .unwrap();
     assert_eq!(HELLOWORLD_BLOB_DIGEST.clone(), writer.close().unwrap());
 
-    let renderer = NARRenderer::new(blob_service, gen_directory_service());
-
     // Test with a root FileNode of a too big size
     {
         let mut buf: Vec<u8> = vec![];
-        let e = renderer
-            .write_nar(
-                &mut buf,
-                &crate::proto::node::Node::File(FileNode {
-                    name: "doesntmatter".to_string(),
-                    digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
-                    size: 42, // <- note the wrong size here!
-                    executable: false,
-                }),
-            )
-            .expect_err("must fail");
+
+        let e = writer_nar(
+            &mut buf,
+            &crate::proto::node::Node::File(FileNode {
+                name: "doesntmatter".to_string(),
+                digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
+                size: 42, // <- note the wrong size here!
+                executable: false,
+            }),
+            &blob_service,
+            gen_directory_service(),
+        )
+        .expect_err("must fail");
 
         match e {
             crate::nar::RenderError::NARWriterError(e) => {
@@ -96,17 +98,19 @@ fn single_file_wrong_blob_size() {
     // Test with a root FileNode of a too small size
     {
         let mut buf: Vec<u8> = vec![];
-        let e = renderer
-            .write_nar(
-                &mut buf,
-                &crate::proto::node::Node::File(FileNode {
-                    name: "doesntmatter".to_string(),
-                    digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
-                    size: 2, // <- note the wrong size here!
-                    executable: false,
-                }),
-            )
-            .expect_err("must fail");
+
+        let e = writer_nar(
+            &mut buf,
+            &crate::proto::node::Node::File(FileNode {
+                name: "doesntmatter".to_string(),
+                digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
+                size: 2, // <- note the wrong size here!
+                executable: false,
+            }),
+            &blob_service,
+            gen_directory_service(),
+        )
+        .expect_err("must fail");
 
         match e {
             crate::nar::RenderError::NARWriterError(e) => {
@@ -130,20 +134,20 @@ fn single_file() {
     .unwrap();
     assert_eq!(HELLOWORLD_BLOB_DIGEST.clone(), writer.close().unwrap());
 
-    let renderer = NARRenderer::new(blob_service, gen_directory_service());
     let mut buf: Vec<u8> = vec![];
 
-    renderer
-        .write_nar(
-            &mut buf,
-            &crate::proto::node::Node::File(FileNode {
-                name: "doesntmatter".to_string(),
-                digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
-                size: HELLOWORLD_BLOB_CONTENTS.len() as u32,
-                executable: false,
-            }),
-        )
-        .expect("must succeed");
+    writer_nar(
+        &mut buf,
+        &crate::proto::node::Node::File(FileNode {
+            name: "doesntmatter".to_string(),
+            digest: HELLOWORLD_BLOB_DIGEST.to_vec(),
+            size: HELLOWORLD_BLOB_CONTENTS.len() as u32,
+            executable: false,
+        }),
+        &blob_service,
+        gen_directory_service(),
+    )
+    .expect("must succeed");
 
     assert_eq!(buf, NAR_CONTENTS_HELLOWORLD.to_vec());
 }
@@ -168,19 +172,35 @@ fn test_complicated() {
         .put(DIRECTORY_COMPLICATED.clone())
         .unwrap();
 
-    let renderer = NARRenderer::new(blob_service, directory_service);
     let mut buf: Vec<u8> = vec![];
 
-    renderer
-        .write_nar(
-            &mut buf,
-            &crate::proto::node::Node::Directory(DirectoryNode {
-                name: "doesntmatter".to_string(),
-                digest: DIRECTORY_COMPLICATED.digest().to_vec(),
-                size: DIRECTORY_COMPLICATED.size(),
-            }),
-        )
-        .expect("must succeed");
+    writer_nar(
+        &mut buf,
+        &crate::proto::node::Node::Directory(DirectoryNode {
+            name: "doesntmatter".to_string(),
+            digest: DIRECTORY_COMPLICATED.digest().to_vec(),
+            size: DIRECTORY_COMPLICATED.size(),
+        }),
+        &blob_service,
+        directory_service.clone(),
+    )
+    .expect("must succeed");
 
     assert_eq!(buf, NAR_CONTENTS_COMPLICATED.to_vec());
+
+    // ensure calculate_nar does return the correct sha256 digest and sum.
+    let (nar_size, nar_digest) = calculate_size_and_sha256(
+        &crate::proto::node::Node::Directory(DirectoryNode {
+            name: "doesntmatter".to_string(),
+            digest: DIRECTORY_COMPLICATED.digest().to_vec(),
+            size: DIRECTORY_COMPLICATED.size(),
+        }),
+        &blob_service,
+        directory_service,
+    )
+    .expect("must succeed");
+
+    assert_eq!(NAR_CONTENTS_COMPLICATED.len() as u64, nar_size);
+    let d = Sha256::digest(NAR_CONTENTS_COMPLICATED.clone());
+    assert_eq!(d.as_slice(), nar_digest);
 }