fix(tvix/store/nixbase32): fix encoder/decoder

Replace our data_encoding usage with the implementation taken from
https://github.com/nix-community/go-nix/tree/master/pkg/nixbase32

Also uncomment some of the unit tests, and add a regression test for a
NIXBASE32.encode with a 32 bytes sequence.

The previous implementation of NIXBASE32.encode is wrong in that case:

```
❯ nix hash to-base32 sha256-s6JN6XqP28g1uYMxaVAQMLiXcDG8tUs7OsE3QPhGqzA=
0c5b8vw40dy178xlpddw65q9gf1h2186jcc3p4swinwggbllv8mk
❯ echo -n s6JN6XqP28g1uYMxaVAQMLiXcDG8tUs7OsE3QPhGqzA= | base64 -d | hexdump -C
00000000  b3 a2 4d e9 7a 8f db c8  35 b9 83 31 69 50 10 30  |..M.z...5..1iP.0|
00000010  b8 97 70 31 bc b5 4b 3b  3a c1 37 40 f8 46 ab 30  |..p1..K;:.7@.F.0|
00000020
```

Change-Id: I0468b62bbbab390f8d7d3812e657e5d59dceed59
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7934
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: Adam Joseph <adam@westernsemico.com>
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Florian Klink 2023-01-26 14:42:16 +01:00 committed by flokli
parent 85e563c554
commit 8ec8d03175
5 changed files with 121 additions and 76 deletions

View file

@ -1,5 +1,4 @@
use crate::nixbase32::NIXBASE32;
use data_encoding::DecodeError;
use crate::nixbase32::{self, Nixbase32DecodeError};
use std::fmt;
use thiserror::Error;
@ -19,7 +18,7 @@ pub enum ParseStorePathError {
#[error("Dash is missing between hash and name")]
MissingDash(),
#[error("Hash encoding is invalid: {0}")]
InvalidHashEncoding(DecodeError),
InvalidHashEncoding(Nixbase32DecodeError),
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Tried to parse an absolute path which was missing the store dir prefix.")]
@ -53,7 +52,7 @@ impl StorePath {
return Err(ParseStorePathError::InvalidName("".to_string()));
}
let digest = match NIXBASE32.decode(s[..ENCODED_DIGEST_SIZE].as_bytes()) {
let digest = match nixbase32::decode(s[..ENCODED_DIGEST_SIZE].as_bytes()) {
Ok(decoded) => decoded,
Err(decoder_error) => {
return Err(ParseStorePathError::InvalidHashEncoding(decoder_error))
@ -110,25 +109,20 @@ impl StorePath {
impl fmt::Display for StorePath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}-{}",
crate::nixbase32::NIXBASE32.encode(&self.digest),
self.name
)
write!(f, "{}-{}", nixbase32::encode(&self.digest), self.name)
}
}
#[cfg(test)]
mod tests {
use crate::nixbase32::NIXBASE32;
use crate::nixbase32;
use crate::store_path::{DIGEST_SIZE, ENCODED_DIGEST_SIZE};
use super::{ParseStorePathError, StorePath};
#[test]
fn encoded_digest_size() {
assert_eq!(ENCODED_DIGEST_SIZE, NIXBASE32.encode_len(DIGEST_SIZE));
assert_eq!(ENCODED_DIGEST_SIZE, nixbase32::encode_len(DIGEST_SIZE));
}
#[test]