Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate oscoin tests #51

Open
adinapoli-mndc opened this issue Apr 19, 2019 · 0 comments
Open

Migrate oscoin tests #51

adinapoli-mndc opened this issue Apr 19, 2019 · 0 comments

Comments

@adinapoli-mndc
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant