aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorJulien Tanguy <julien.tanguy@jhome.fr>2015-04-09 01:12:36 +0200
committerJulien Tanguy <julien.tanguy@jhome.fr>2015-04-09 01:12:36 +0200
commitf678145637ba6f42c36d07c19f8c764e5d537f72 (patch)
tree3e8578900501968de7404131a89e8e063dd0f12e /test
downloadhmacaroons-f678145637ba6f42c36d07c19f8c764e5d537f72.tar.gz
hmacaroons-f678145637ba6f42c36d07c19f8c764e5d537f72.tar.zst
hmacaroons-f678145637ba6f42c36d07c19f8c764e5d537f72.zip
Initial commit
Diffstat (limited to 'test')
-rw-r--r--test/Crypto/Macaroon/Tests.hs74
-rw-r--r--test/tests.hs66
2 files changed, 140 insertions, 0 deletions
diff --git a/test/Crypto/Macaroon/Tests.hs b/test/Crypto/Macaroon/Tests.hs
new file mode 100644
index 0000000..cdfb620
--- /dev/null
+++ b/test/Crypto/Macaroon/Tests.hs
@@ -0,0 +1,74 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-|
3Copyright : (c) 2015 Julien Tanguy
4License : BSD3
5
6Maintainer : julien.tanguy@jhome.fr
7
8
9This test suite is based on the pymacaroons test suite:
10<https://github.com/ecordell/pymacaroons>
11-}
12module Crypto.Macaroon.Tests where
13
14import Data.Byteable
15import qualified Data.ByteString.Char8 as B8
16import Data.Hex
17import Test.Tasty
18import Test.Tasty.HUnit
19
20import Crypto.Macaroon
21
22tests :: TestTree
23tests = testGroup "Crypto.Macaroon" [ basicSignature
24 , basicSerialize
25 , basicMint
26 , basicMintTrimmed
27 ]
28
29
30m :: Macaroon
31m = create secret key loc
32 where
33 secret = B8.pack "this is our super secret key; only we should know it"
34 key = B8.pack "we used our secret key"
35 loc = B8.pack "http://mybank/"
36
37m2 :: Macaroon
38m2 = addFirstPartyCaveat "test = caveat" m
39
40m3 :: Macaroon
41m3 = addFirstPartyCaveat "test = acaveat" m
42
43m4 :: Macaroon
44m4 = addThirdPartyCaveat caveat_key caveat_id caveat_loc n
45 where
46 n = addFirstPartyCaveat "account = 3735928559" $ create sec key loc
47 key = B8.pack "we used our other secret key"
48 loc = B8.pack "http://mybank/"
49 sec = B8.pack "this is a different super-secret key; never use the same secret twice"
50 caveat_key = B8.pack "4; guaranteed random by a fair toss of the dice"
51 caveat_id = B8.pack "this was how we remind auth of key/pred"
52 caveat_loc = B8.pack "http://auth.mybank/"
53
54
55basicSignature = testCase "Basic signature" $
56 "E3D9E02908526C4C0039AE15114115D97FDD68BF2BA379B342AAF0F617D0552F" @=? (hex . signature) m
57
58basicSerialize = testCase "Serialization" $
59 "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudG\
60 \lmaWVyIHdlIHVzZWQgb3VyIHNlY3JldCBrZXkKMDAyZnNpZ25h\
61 \dHVyZSDj2eApCFJsTAA5rhURQRXZf91ovyujebNCqvD2F9BVLwo" @=? serialize m
62
63basicMint = testCase "First Party Caveat" $
64 "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVzZ\
65 \WQgb3VyIHNlY3JldCBrZXkKMDAxNmNpZCB0ZXN0ID0gY2F2ZWF0CjAwMmZzaWduYXR1cmUgGXusegR\
66 \K8zMyhluSZuJtSTvdZopmDkTYjOGpmMI9vWcK" @=? serialize m2
67
68basicMintTrimmed = testCase "Trimmed base64" $
69 "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVz\
70 \ZWQgb3VyIHNlY3JldCBrZXkKMDAxN2NpZCB0ZXN0ID0gYWNhdmVhdAowMDJmc2ln\
71 \bmF0dXJlIJRJ_V3WNJQnqlVq5eez7spnltwU_AXs8NIRY739sHooCg" @=? serialize m3
72
73basicThirdParty = testCase "Third Party Caveat" $
74 "6B99EDB2EC6D7A4382071D7D41A0BF7DFA27D87D2F9FEA86E330D7850FFDA2B2" @=? (hex . signature) m4
diff --git a/test/tests.hs b/test/tests.hs
new file mode 100644
index 0000000..ba5dafd
--- /dev/null
+++ b/test/tests.hs
@@ -0,0 +1,66 @@
1{-#LANGUAGE OverloadedStrings#-}
2
3import Crypto.Hash
4import Data.ByteString (ByteString)
5import qualified Data.ByteString as B
6import Data.Hex
7import Data.Byteable
8
9import Test.Tasty
10import Test.Tasty.HUnit
11
12import qualified Crypto.Macaroon.Tests
13
14main = defaultMain tests
15
16tests :: TestTree
17tests = testGroup "Tests" [ sanityCheck
18 , Crypto.Macaroon.Tests.tests
19 ]
20
21sanityCheck :: TestTree
22sanityCheck = testGroup "Python HMAC Sanity check" [ checkKey
23 , checkMac1
24 , checkMac2
25 , checkMac3
26 , checkMac4
27 ]
28
29
30secret :: ByteString
31secret = "this is our super secret key; only we should know it"
32
33public :: ByteString
34public = "we used our secret key"
35
36key :: ByteString
37key = B.take 32 secret
38
39mac1 :: ByteString
40mac1 = toBytes $ (hmac key public :: HMAC SHA256)
41
42mac2 :: ByteString
43mac2 = toBytes $ (hmac mac1 "account = 3735928559" :: HMAC SHA256)
44
45mac3 :: ByteString
46mac3 = toBytes $ (hmac mac2 "time < 2015-01-01T00:00" :: HMAC SHA256)
47
48mac4 :: ByteString
49mac4 = toBytes $ (hmac mac3 "email = alice@example.org" :: HMAC SHA256)
50
51
52checkKey = testCase "Truncated key" $
53 key @?= "this is our super secret key; on"
54
55checkMac1 = testCase "HMAC key" $
56 "C60B4B3540BB1B2F2EF28D1C895691CC4A5E07A38A9D3B1C3379FB485293372F" @=? hex mac1
57
58checkMac2 = testCase "HMAC key account" $
59 "5C933DC9A7D036DFCD1740B4F26D737397A1FF635EAC900F3226973503CAAAA5" @=? hex mac2
60
61checkMac3 = testCase "HMAC key account time" $
62 "7A559B20C8B607009EBCE138C200585E9D0DECA6D23B3EAD6C5E0BA6861D3858" @=? hex mac3
63
64checkMac4 = testCase "HMAC key account time email" $
65 "E42BBB02A9A5A303483CB6295C497AE51AD1D5CB10003CBE548D907E7E62F5E4" @=? hex mac4
66