refactor(3p/nix): Remove custom base64 implementation

Replace the custom, rather questionable base64 implementation with
absl::Base64{Une,E}scape. To make sure that the custom implementation
was doing the same thing I've also added a test covering
nix::Hash::to_string, which was one function that used it - the test
passed prior to the replacement, and continued to pass afterwards.

The previous base64Decode function threw an exception on failure - to
avoid going too far down the rabbit hole I've replicated that
functionality at all call sites, but this should be replaced with more
sensible error handling such as StatusOr eventually.

Also, before this change:

    ❯ nix eval -f . users.tazjin.emacs.outPath
    "/nix/store/g6ri2q8nra96ix20bcsc734r1yyaylb1-tazjins-emacs"

And after:

    ❯ ./result/bin/nix eval -f . users.tazjin.emacs.outPath
    "/nix/store/g6ri2q8nra96ix20bcsc734r1yyaylb1-tazjins-emacs"

Change-Id: Id292ffbb82fe808f3f1b34670afbe7b8c13ad615
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1385
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Griffin Smith 2020-07-23 17:18:00 -04:00 committed by glittershark
parent ece66d081b
commit 2fcf2d0d20
7 changed files with 56 additions and 78 deletions

View file

@ -11,6 +11,14 @@ target_link_libraries(attr-set
gtest_discover_tests(attr-set)
add_executable(hash_test hash_test.cc)
target_link_libraries(hash_test
nixutil
GTest::gtest_main
)
gtest_discover_tests(hash_test)
add_executable(value-to-json value-to-json.cc)
target_link_libraries(value-to-json
nixexpr

24
third_party/nix/src/tests/hash_test.cc vendored Normal file
View file

@ -0,0 +1,24 @@
#include "libutil/hash.hh"
#include <gtest/gtest.h>
class HashTest : public ::testing::Test {};
namespace nix {
TEST(HASH_TEST, SHA256) {
auto hash = hashString(HashType::htSHA256, "foo");
ASSERT_EQ(hash.base64Len(), 44);
ASSERT_EQ(hash.base32Len(), 52);
ASSERT_EQ(hash.base16Len(), 64);
ASSERT_EQ(hash.to_string(Base16),
"sha256:"
"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae");
ASSERT_EQ(hash.to_string(Base32),
"sha256:1bp7cri8hplaz6hbz0v4f0nl44rl84q1sg25kgwqzipzd1mv89ic");
ASSERT_EQ(hash.to_string(Base64),
"sha256:LCa0a2j/xo/5m0U8HTBBNBNCLXBkg7+g+YpeiGJm564=");
}
} // namespace nix