Skip to content

Migrate oscoin tests #51

@adinapoli-mndc

Description

@adinapoli-mndc

See oscoin/oscoin#505 . We now have a bunch of tests in oscoin that belongs to multibase and multihash-cryptonite, really:

tests :: TestTree
tests = testGroup "Multihash"
    [ testGroup "Examples from https://github.com/multiformats/multihash"
        [ testGroup "sha1(\"multihash\")"
            [ testCase "Base16" $
                example BaseN.Base16 C.SHA1 "multihash"
                    "111488c2f11fb2ce392acb5b2986e640211c4690073e"
            , testCase "Base58" $
                example BaseN.Base58btc C.SHA1 "multihash"
                    "5dsgvJGnvAfiR3K6HCBc4hcokSfmjj"
            , testCase "Base64" $
                example BaseN.Base64 C.SHA1 "multihash"
                    "ERSIwvEfss45KstbKYbmQCEcRpAHPg=="
            ]
        , testGroup "sha2-256(\"multihash\")"
            [ testCase "Base16" $
                example BaseN.Base16 C.SHA256 "multihash"
                    "12209cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47"
            , testCase "Base58" $
                example BaseN.Base58btc C.SHA256 "multihash"
                    "QmYtUc4iTCbbfVSDNKvtQqrfyezPPnFvE33wFmutw9PBBk"
            , testCase "Base64" $
                example BaseN.Base64 C.SHA256 "multihash"
                    "EiCcvAfD+ZFyWDajqipYHKICkZiqQgudmbwOEx2fPiy+Rw=="
            ]
        ]

    , testGroup "Roundtrip"
        [ testProperty "Base16: decode . encode = id" . property $ do
            bs <- forAll genBytes
            propRoundtrip BaseN.Base16 C.Blake2b_256 bs

        , testProperty "Base58: decode . encode = id" . property $ do
            bs <- forAll genBytes
            propRoundtrip BaseN.Base58btc C.Blake2b_256 bs

        , testProperty "Base64: decode . encode = id" . property $ do
            bs <- forAll genBytes
            propRoundtrip BaseN.Base64 C.Blake2b_256 bs
        ]
    ]

propRoundtrip
    :: ( Multihashable a
       , DecodeBase    b
       )
    => Base b
    -> a
    -> ByteString
    -> PropertyT IO ()
propRoundtrip base algo bs =
    let digest = C.hashWith algo bs
        enc    = encodeAtBase base . Multihash.fromDigest
        dec    = decodeAtBase base . BaseN.encodedBytes
     in (dec . enc) digest === Right digest

example
    :: Multihashable a
    => BaseN.Base b
    -> a
    -> ByteString
    -> ByteString
    -> Assertion
example base algo input expected =
    BaseN.encodedBytes (multihash base algo input) @?= expected

--------------------------------------------------------------------------------

genBytes :: MonadGen m => m ByteString
genBytes = Gen.utf8 (Range.constantFrom 8 8 512) Gen.unicodeAll

multihash
    :: Multihashable a
    => BaseN.Base b
    -> a
    -> ByteString
    -> BaseN.AtBase b
multihash base algo bs =
    encodeAtBase base $ Multihash.multihash algo bs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions