diff options
author | Julien Tanguy <julien.tanguy@jhome.fr> | 2015-04-09 01:12:36 +0200 |
---|---|---|
committer | Julien Tanguy <julien.tanguy@jhome.fr> | 2015-04-09 01:12:36 +0200 |
commit | f678145637ba6f42c36d07c19f8c764e5d537f72 (patch) | |
tree | 3e8578900501968de7404131a89e8e063dd0f12e /test | |
download | hmacaroons-f678145637ba6f42c36d07c19f8c764e5d537f72.tar.gz hmacaroons-f678145637ba6f42c36d07c19f8c764e5d537f72.tar.zst hmacaroons-f678145637ba6f42c36d07c19f8c764e5d537f72.zip |
Initial commit
Diffstat (limited to 'test')
-rw-r--r-- | test/Crypto/Macaroon/Tests.hs | 74 | ||||
-rw-r--r-- | test/tests.hs | 66 |
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 | {-| | ||
3 | Copyright : (c) 2015 Julien Tanguy | ||
4 | License : BSD3 | ||
5 | |||
6 | Maintainer : julien.tanguy@jhome.fr | ||
7 | |||
8 | |||
9 | This test suite is based on the pymacaroons test suite: | ||
10 | <https://github.com/ecordell/pymacaroons> | ||
11 | -} | ||
12 | module Crypto.Macaroon.Tests where | ||
13 | |||
14 | import Data.Byteable | ||
15 | import qualified Data.ByteString.Char8 as B8 | ||
16 | import Data.Hex | ||
17 | import Test.Tasty | ||
18 | import Test.Tasty.HUnit | ||
19 | |||
20 | import Crypto.Macaroon | ||
21 | |||
22 | tests :: TestTree | ||
23 | tests = testGroup "Crypto.Macaroon" [ basicSignature | ||
24 | , basicSerialize | ||
25 | , basicMint | ||
26 | , basicMintTrimmed | ||
27 | ] | ||
28 | |||
29 | |||
30 | m :: Macaroon | ||
31 | m = 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 | |||
37 | m2 :: Macaroon | ||
38 | m2 = addFirstPartyCaveat "test = caveat" m | ||
39 | |||
40 | m3 :: Macaroon | ||
41 | m3 = addFirstPartyCaveat "test = acaveat" m | ||
42 | |||
43 | m4 :: Macaroon | ||
44 | m4 = 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 | |||
55 | basicSignature = testCase "Basic signature" $ | ||
56 | "E3D9E02908526C4C0039AE15114115D97FDD68BF2BA379B342AAF0F617D0552F" @=? (hex . signature) m | ||
57 | |||
58 | basicSerialize = testCase "Serialization" $ | ||
59 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudG\ | ||
60 | \lmaWVyIHdlIHVzZWQgb3VyIHNlY3JldCBrZXkKMDAyZnNpZ25h\ | ||
61 | \dHVyZSDj2eApCFJsTAA5rhURQRXZf91ovyujebNCqvD2F9BVLwo" @=? serialize m | ||
62 | |||
63 | basicMint = testCase "First Party Caveat" $ | ||
64 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVzZ\ | ||
65 | \WQgb3VyIHNlY3JldCBrZXkKMDAxNmNpZCB0ZXN0ID0gY2F2ZWF0CjAwMmZzaWduYXR1cmUgGXusegR\ | ||
66 | \K8zMyhluSZuJtSTvdZopmDkTYjOGpmMI9vWcK" @=? serialize m2 | ||
67 | |||
68 | basicMintTrimmed = testCase "Trimmed base64" $ | ||
69 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVz\ | ||
70 | \ZWQgb3VyIHNlY3JldCBrZXkKMDAxN2NpZCB0ZXN0ID0gYWNhdmVhdAowMDJmc2ln\ | ||
71 | \bmF0dXJlIJRJ_V3WNJQnqlVq5eez7spnltwU_AXs8NIRY739sHooCg" @=? serialize m3 | ||
72 | |||
73 | basicThirdParty = 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 | |||
3 | import Crypto.Hash | ||
4 | import Data.ByteString (ByteString) | ||
5 | import qualified Data.ByteString as B | ||
6 | import Data.Hex | ||
7 | import Data.Byteable | ||
8 | |||
9 | import Test.Tasty | ||
10 | import Test.Tasty.HUnit | ||
11 | |||
12 | import qualified Crypto.Macaroon.Tests | ||
13 | |||
14 | main = defaultMain tests | ||
15 | |||
16 | tests :: TestTree | ||
17 | tests = testGroup "Tests" [ sanityCheck | ||
18 | , Crypto.Macaroon.Tests.tests | ||
19 | ] | ||
20 | |||
21 | sanityCheck :: TestTree | ||
22 | sanityCheck = testGroup "Python HMAC Sanity check" [ checkKey | ||
23 | , checkMac1 | ||
24 | , checkMac2 | ||
25 | , checkMac3 | ||
26 | , checkMac4 | ||
27 | ] | ||
28 | |||
29 | |||
30 | secret :: ByteString | ||
31 | secret = "this is our super secret key; only we should know it" | ||
32 | |||
33 | public :: ByteString | ||
34 | public = "we used our secret key" | ||
35 | |||
36 | key :: ByteString | ||
37 | key = B.take 32 secret | ||
38 | |||
39 | mac1 :: ByteString | ||
40 | mac1 = toBytes $ (hmac key public :: HMAC SHA256) | ||
41 | |||
42 | mac2 :: ByteString | ||
43 | mac2 = toBytes $ (hmac mac1 "account = 3735928559" :: HMAC SHA256) | ||
44 | |||
45 | mac3 :: ByteString | ||
46 | mac3 = toBytes $ (hmac mac2 "time < 2015-01-01T00:00" :: HMAC SHA256) | ||
47 | |||
48 | mac4 :: ByteString | ||
49 | mac4 = toBytes $ (hmac mac3 "email = alice@example.org" :: HMAC SHA256) | ||
50 | |||
51 | |||
52 | checkKey = testCase "Truncated key" $ | ||
53 | key @?= "this is our super secret key; on" | ||
54 | |||
55 | checkMac1 = testCase "HMAC key" $ | ||
56 | "C60B4B3540BB1B2F2EF28D1C895691CC4A5E07A38A9D3B1C3379FB485293372F" @=? hex mac1 | ||
57 | |||
58 | checkMac2 = testCase "HMAC key account" $ | ||
59 | "5C933DC9A7D036DFCD1740B4F26D737397A1FF635EAC900F3226973503CAAAA5" @=? hex mac2 | ||
60 | |||
61 | checkMac3 = testCase "HMAC key account time" $ | ||
62 | "7A559B20C8B607009EBCE138C200585E9D0DECA6D23B3EAD6C5E0BA6861D3858" @=? hex mac3 | ||
63 | |||
64 | checkMac4 = testCase "HMAC key account time email" $ | ||
65 | "E42BBB02A9A5A303483CB6295C497AE51AD1D5CB10003CBE548D907E7E62F5E4" @=? hex mac4 | ||
66 | |||