mirror of
https://github.com/NixOS/nix.git
synced 2025-11-10 20:46:01 +01:00
Clean up Base* code
Make it separate from Hash, since other things can be base-encoded too. This isn't really needed for Nix, but it makes the code easier to read e.g. for someone reimplementing this stuff in a different language. (Of course, Base16/Base64 should be gotten off-the-shelf, but now the hash code, which is more bespoke, is less cluttered with the parts that would be from some library.) Many reimplementations of "Nix32" and our hash type already exist, so this cleanup is coming years too late, but I say better late than never / it is always good to nudge the code in the direction of being a "living spec". Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
This commit is contained in:
parent
664f06c94c
commit
991831227e
18 changed files with 357 additions and 244 deletions
68
src/libutil-tests/base-n.cc
Normal file
68
src/libutil-tests/base-n.cc
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include <numeric>
|
||||
|
||||
#include "nix/util/base-n.hh"
|
||||
#include "nix/util/error.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
static const std::span<const std::byte> stringToByteSpan(const std::string_view s)
|
||||
{
|
||||
return {(const std::byte *) s.data(), s.size()};
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* base64::encode
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
||||
TEST(base64Encode, emptyString)
|
||||
{
|
||||
ASSERT_EQ(base64::encode(stringToByteSpan("")), "");
|
||||
}
|
||||
|
||||
TEST(base64Encode, encodesAString)
|
||||
{
|
||||
ASSERT_EQ(base64::encode(stringToByteSpan("quod erat demonstrandum")), "cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0=");
|
||||
}
|
||||
|
||||
TEST(base64Encode, encodeAndDecode)
|
||||
{
|
||||
auto s = "quod erat demonstrandum";
|
||||
auto encoded = base64::encode(stringToByteSpan(s));
|
||||
auto decoded = base64::decode(encoded);
|
||||
|
||||
ASSERT_EQ(decoded, s);
|
||||
}
|
||||
|
||||
TEST(base64Encode, encodeAndDecodeNonPrintable)
|
||||
{
|
||||
char s[256];
|
||||
std::iota(std::rbegin(s), std::rend(s), 0);
|
||||
|
||||
auto encoded = base64::encode(std::as_bytes(std::span<const char>{std::string_view{s}}));
|
||||
auto decoded = base64::decode(encoded);
|
||||
|
||||
EXPECT_EQ(decoded.length(), 255u);
|
||||
ASSERT_EQ(decoded, s);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* base64::decode
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
||||
TEST(base64Decode, emptyString)
|
||||
{
|
||||
ASSERT_EQ(base64::decode(""), "");
|
||||
}
|
||||
|
||||
TEST(base64Decode, decodeAString)
|
||||
{
|
||||
ASSERT_EQ(base64::decode("cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0="), "quod erat demonstrandum");
|
||||
}
|
||||
|
||||
TEST(base64Decode, decodeThrowsOnInvalidChar)
|
||||
{
|
||||
ASSERT_THROW(base64::decode("cXVvZCBlcm_0IGRlbW9uc3RyYW5kdW0="), Error);
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
@ -44,6 +44,7 @@ subdir('nix-meson-build-support/common')
|
|||
|
||||
sources = files(
|
||||
'args.cc',
|
||||
'base-n.cc',
|
||||
'canon-path.cc',
|
||||
'checked-arithmetic.cc',
|
||||
'chunked-vector.cc',
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include "nix/util/file-system.hh"
|
||||
#include "nix/util/terminal.hh"
|
||||
#include "nix/util/strings.hh"
|
||||
#include "nix/util/base-n.hh"
|
||||
|
||||
#include <limits.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
|
@ -48,60 +49,6 @@ TEST(hasSuffix, trivialCase)
|
|||
ASSERT_TRUE(hasSuffix("foobar", "bar"));
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* base64Encode
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
||||
TEST(base64Encode, emptyString)
|
||||
{
|
||||
ASSERT_EQ(base64Encode(""), "");
|
||||
}
|
||||
|
||||
TEST(base64Encode, encodesAString)
|
||||
{
|
||||
ASSERT_EQ(base64Encode("quod erat demonstrandum"), "cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0=");
|
||||
}
|
||||
|
||||
TEST(base64Encode, encodeAndDecode)
|
||||
{
|
||||
auto s = "quod erat demonstrandum";
|
||||
auto encoded = base64Encode(s);
|
||||
auto decoded = base64Decode(encoded);
|
||||
|
||||
ASSERT_EQ(decoded, s);
|
||||
}
|
||||
|
||||
TEST(base64Encode, encodeAndDecodeNonPrintable)
|
||||
{
|
||||
char s[256];
|
||||
std::iota(std::rbegin(s), std::rend(s), 0);
|
||||
|
||||
auto encoded = base64Encode(s);
|
||||
auto decoded = base64Decode(encoded);
|
||||
|
||||
EXPECT_EQ(decoded.length(), 255u);
|
||||
ASSERT_EQ(decoded, s);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* base64Decode
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
||||
TEST(base64Decode, emptyString)
|
||||
{
|
||||
ASSERT_EQ(base64Decode(""), "");
|
||||
}
|
||||
|
||||
TEST(base64Decode, decodeAString)
|
||||
{
|
||||
ASSERT_EQ(base64Decode("cXVvZCBlcmF0IGRlbW9uc3RyYW5kdW0="), "quod erat demonstrandum");
|
||||
}
|
||||
|
||||
TEST(base64Decode, decodeThrowsOnInvalidChar)
|
||||
{
|
||||
ASSERT_THROW(base64Decode("cXVvZCBlcm_0IGRlbW9uc3RyYW5kdW0="), Error);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* getLine
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue