diff options
author | Julien Tanguy <julien.tanguy@jhome.fr> | 2015-04-14 18:18:26 +0200 |
---|---|---|
committer | Julien Tanguy <julien.tanguy@jhome.fr> | 2015-04-14 18:18:26 +0200 |
commit | b1f77d7e6dcf03dd20c36e1a10429e1b2b9900b1 (patch) | |
tree | 0ce2d7d6164de6c254ec4022072430045109a30f | |
parent | 26d38f73993db56811d198a3e0b5c710722dd472 (diff) | |
download | hmacaroons-b1f77d7e6dcf03dd20c36e1a10429e1b2b9900b1.tar.gz hmacaroons-b1f77d7e6dcf03dd20c36e1a10429e1b2b9900b1.tar.zst hmacaroons-b1f77d7e6dcf03dd20c36e1a10429e1b2b9900b1.zip |
Refactor tests
-rw-r--r-- | src/Crypto/Macaroon/Serializer/Base64.hs | 16 | ||||
-rw-r--r-- | test/Crypto/Macaroon/Serializer/Base64/Tests.hs | 83 | ||||
-rw-r--r-- | test/Crypto/Macaroon/Tests.hs | 49 | ||||
-rw-r--r-- | test/tests.hs | 2 |
4 files changed, 103 insertions, 47 deletions
diff --git a/src/Crypto/Macaroon/Serializer/Base64.hs b/src/Crypto/Macaroon/Serializer/Base64.hs index 6fc8fcb..f6527c2 100644 --- a/src/Crypto/Macaroon/Serializer/Base64.hs +++ b/src/Crypto/Macaroon/Serializer/Base64.hs | |||
@@ -16,22 +16,22 @@ module Crypto.Macaroon.Serializer.Base64 ( | |||
16 | , deserialize | 16 | , deserialize |
17 | ) where | 17 | ) where |
18 | 18 | ||
19 | import Control.Applicative | 19 | import Control.Applicative |
20 | import Control.Monad | 20 | import Control.Monad |
21 | import qualified Data.ByteString as BS | 21 | import Crypto.Macaroon.Internal |
22 | import qualified Data.ByteString.Base64.URL as B64 | 22 | import Data.Attoparsec.ByteString |
23 | import qualified Data.ByteString.Char8 as B8 | ||
24 | import Data.Attoparsec.ByteString | ||
25 | import qualified Data.Attoparsec.ByteString.Char8 as A8 | 23 | import qualified Data.Attoparsec.ByteString.Char8 as A8 |
26 | import Data.Bits | 24 | import Data.Bits |
25 | import qualified Data.ByteString as BS | ||
26 | import qualified Data.ByteString.Base64.URL as B64 | ||
27 | import qualified Data.ByteString.Char8 as B8 | ||
27 | import Data.Char | 28 | import Data.Char |
28 | import Data.Hex | 29 | import Data.Hex |
29 | import Data.Int | 30 | import Data.Int |
30 | import Data.List | 31 | import Data.List |
31 | import Data.Maybe | 32 | import Data.Maybe |
32 | import Data.Word | ||
33 | import Data.Serialize | 33 | import Data.Serialize |
34 | import Crypto.Macaroon.Internal | 34 | import Data.Word |
35 | 35 | ||
36 | 36 | ||
37 | -- | Serialize a macaroon in an URL-safe Base64 encoding | 37 | -- | Serialize a macaroon in an URL-safe Base64 encoding |
diff --git a/test/Crypto/Macaroon/Serializer/Base64/Tests.hs b/test/Crypto/Macaroon/Serializer/Base64/Tests.hs new file mode 100644 index 0000000..9c49e96 --- /dev/null +++ b/test/Crypto/Macaroon/Serializer/Base64/Tests.hs | |||
@@ -0,0 +1,83 @@ | |||
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.Serializer.Base64.Tests where | ||
13 | |||
14 | |||
15 | import qualified Data.ByteString.Char8 as B8 | ||
16 | import Test.Tasty | ||
17 | import Test.Tasty.HUnit | ||
18 | |||
19 | import Crypto.Macaroon | ||
20 | import Crypto.Macaroon.Serializer.Base64 | ||
21 | |||
22 | tests :: TestTree | ||
23 | tests = testGroup "Crypto.Macaroon.Serializer.Base64" [ basic | ||
24 | , minted | ||
25 | , minted2 | ||
26 | -- , minted3 | ||
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 | basic :: TestTree | ||
38 | basic = testGroup "Basic macaroon" [ basicSerialize | ||
39 | , basicDeserialize | ||
40 | ] | ||
41 | |||
42 | basicSerialize = testCase "Serialization" $ | ||
43 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudG\ | ||
44 | \lmaWVyIHdlIHVzZWQgb3VyIHNlY3JldCBrZXkKMDAyZnNpZ25h\ | ||
45 | \dHVyZSDj2eApCFJsTAA5rhURQRXZf91ovyujebNCqvD2F9BVLwo" @=? serialize m | ||
46 | |||
47 | basicDeserialize = testCase "Deserialization" $ | ||
48 | Right m @=? (deserialize . serialize) m | ||
49 | |||
50 | m2 :: Macaroon | ||
51 | m2 = addFirstPartyCaveat "test = caveat" m | ||
52 | |||
53 | minted :: TestTree | ||
54 | minted = testGroup "Macaroon with first party caveat" [ mintSerialize | ||
55 | , mintDeserialize | ||
56 | ] | ||
57 | |||
58 | |||
59 | mintSerialize = testCase "Serialization" $ | ||
60 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVzZ\ | ||
61 | \WQgb3VyIHNlY3JldCBrZXkKMDAxNmNpZCB0ZXN0ID0gY2F2ZWF0CjAwMmZzaWduYXR1cmUgGXusegR\ | ||
62 | \K8zMyhluSZuJtSTvdZopmDkTYjOGpmMI9vWcK" @=? serialize m2 | ||
63 | |||
64 | mintDeserialize = testCase "Deserialization" $ | ||
65 | Right m2 @=? (deserialize . serialize) m2 | ||
66 | |||
67 | |||
68 | m3 :: Macaroon | ||
69 | m3 = addFirstPartyCaveat "test = acaveat" m | ||
70 | |||
71 | minted2 :: TestTree | ||
72 | minted2 = testGroup "Macaroon with first party caveats" [ mint2Trimmed | ||
73 | , mint2Des | ||
74 | ] | ||
75 | |||
76 | mint2Trimmed = testCase "Serialization" $ | ||
77 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVz\ | ||
78 | \ZWQgb3VyIHNlY3JldCBrZXkKMDAxN2NpZCB0ZXN0ID0gYWNhdmVhdAowMDJmc2ln\ | ||
79 | \bmF0dXJlIJRJ_V3WNJQnqlVq5eez7spnltwU_AXs8NIRY739sHooCg" @=? serialize m3 | ||
80 | |||
81 | mint2Des = testCase "Deserialization" $ | ||
82 | Right m3 @=? (deserialize . serialize) m3 | ||
83 | |||
diff --git a/test/Crypto/Macaroon/Tests.hs b/test/Crypto/Macaroon/Tests.hs index 244ec50..25d77c8 100644 --- a/test/Crypto/Macaroon/Tests.hs +++ b/test/Crypto/Macaroon/Tests.hs | |||
@@ -23,8 +23,6 @@ import Crypto.Macaroon.Serializer.Base64 | |||
23 | tests :: TestTree | 23 | tests :: TestTree |
24 | tests = testGroup "Crypto.Macaroon" [ basic | 24 | tests = testGroup "Crypto.Macaroon" [ basic |
25 | , minted | 25 | , minted |
26 | , minted2 | ||
27 | -- , minted3 | ||
28 | ] | 26 | ] |
29 | 27 | ||
30 | 28 | ||
@@ -36,29 +34,24 @@ m = create secret key loc | |||
36 | loc = B8.pack "http://mybank/" | 34 | loc = B8.pack "http://mybank/" |
37 | 35 | ||
38 | basic :: TestTree | 36 | basic :: TestTree |
39 | basic = testGroup "Basic macaroon" [ basicSignature | 37 | basic = testGroup "Basic macaroon" [ basicInspect |
40 | , basicSerialize | 38 | , basicSignature |
41 | , basicDeserialize | ||
42 | ] | 39 | ] |
43 | 40 | ||
41 | basicInspect = testCase "Inspect" $ | ||
42 | "location http://mybank/\nidentifier we used\ | ||
43 | \ our secret key\n\nsignature E3D9E02908526C4C\ | ||
44 | \0039AE15114115D97FDD68BF2BA379B342AAF0F617D0552F" @=? inspect m | ||
45 | |||
44 | basicSignature = testCase "Signature" $ | 46 | basicSignature = testCase "Signature" $ |
45 | "E3D9E02908526C4C0039AE15114115D97FDD68BF2BA379B342AAF0F617D0552F" @=? (hex . signature) m | 47 | "E3D9E02908526C4C0039AE15114115D97FDD68BF2BA379B342AAF0F617D0552F" @=? (hex . signature) m |
46 | 48 | ||
47 | basicSerialize = testCase "Serialization" $ | ||
48 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudG\ | ||
49 | \lmaWVyIHdlIHVzZWQgb3VyIHNlY3JldCBrZXkKMDAyZnNpZ25h\ | ||
50 | \dHVyZSDj2eApCFJsTAA5rhURQRXZf91ovyujebNCqvD2F9BVLwo" @=? serialize m | ||
51 | |||
52 | basicDeserialize = testCase "Deserialization" $ | ||
53 | Right m @=? (deserialize . serialize) m | ||
54 | |||
55 | m2 :: Macaroon | 49 | m2 :: Macaroon |
56 | m2 = addFirstPartyCaveat "test = caveat" m | 50 | m2 = addFirstPartyCaveat "test = caveat" m |
57 | 51 | ||
58 | minted :: TestTree | 52 | minted :: TestTree |
59 | minted = testGroup "Macaroon with first party caveat" [ mintInspect | 53 | minted = testGroup "Macaroon with first party caveat" [ mintInspect |
60 | , mintSerialize | 54 | , mintSignature |
61 | , mintDeserialize | ||
62 | ] | 55 | ] |
63 | 56 | ||
64 | mintInspect = testCase "Inspect" $ | 57 | mintInspect = testCase "Inspect" $ |
@@ -68,30 +61,8 @@ mintInspect = testCase "Inspect" $ | |||
68 | \3BDD668A660E44D88CE1A998C23DBD67" @=? inspect m2 | 61 | \3BDD668A660E44D88CE1A998C23DBD67" @=? inspect m2 |
69 | 62 | ||
70 | 63 | ||
71 | mintSerialize = testCase "Serialization" $ | 64 | mintSignature = testCase "Signature" $ |
72 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVzZ\ | 65 | "197BAC7A044AF33332865B9266E26D493BDD668A660E44D88CE1A998C23DBD67" @=? (hex . signature) m2 |
73 | \WQgb3VyIHNlY3JldCBrZXkKMDAxNmNpZCB0ZXN0ID0gY2F2ZWF0CjAwMmZzaWduYXR1cmUgGXusegR\ | ||
74 | \K8zMyhluSZuJtSTvdZopmDkTYjOGpmMI9vWcK" @=? serialize m2 | ||
75 | |||
76 | mintDeserialize = testCase "Deserialization" $ | ||
77 | Right m2 @=? (deserialize . serialize) m2 | ||
78 | |||
79 | |||
80 | m3 :: Macaroon | ||
81 | m3 = addFirstPartyCaveat "test = acaveat" m | ||
82 | |||
83 | minted2 :: TestTree | ||
84 | minted2 = testGroup "Macaroon with first party caveats" [ mint2Trimmed | ||
85 | , mint2Des | ||
86 | ] | ||
87 | |||
88 | mint2Trimmed = testCase "Serialization" $ | ||
89 | "MDAxY2xvY2F0aW9uIGh0dHA6Ly9teWJhbmsvCjAwMjZpZGVudGlmaWVyIHdlIHVz\ | ||
90 | \ZWQgb3VyIHNlY3JldCBrZXkKMDAxN2NpZCB0ZXN0ID0gYWNhdmVhdAowMDJmc2ln\ | ||
91 | \bmF0dXJlIJRJ_V3WNJQnqlVq5eez7spnltwU_AXs8NIRY739sHooCg" @=? serialize m3 | ||
92 | |||
93 | mint2Des = testCase "Deserialization" $ | ||
94 | Right m3 @=? (deserialize . serialize) m3 | ||
95 | 66 | ||
96 | -- m4 :: Macaroon | 67 | -- m4 :: Macaroon |
97 | -- m4 = addThirdPartyCaveat caveat_key caveat_id caveat_loc n | 68 | -- m4 = addThirdPartyCaveat caveat_key caveat_id caveat_loc n |
diff --git a/test/tests.hs b/test/tests.hs index ba5dafd..85564f0 100644 --- a/test/tests.hs +++ b/test/tests.hs | |||
@@ -10,12 +10,14 @@ import Test.Tasty | |||
10 | import Test.Tasty.HUnit | 10 | import Test.Tasty.HUnit |
11 | 11 | ||
12 | import qualified Crypto.Macaroon.Tests | 12 | import qualified Crypto.Macaroon.Tests |
13 | import qualified Crypto.Macaroon.Serializer.Base64.Tests | ||
13 | 14 | ||
14 | main = defaultMain tests | 15 | main = defaultMain tests |
15 | 16 | ||
16 | tests :: TestTree | 17 | tests :: TestTree |
17 | tests = testGroup "Tests" [ sanityCheck | 18 | tests = testGroup "Tests" [ sanityCheck |
18 | , Crypto.Macaroon.Tests.tests | 19 | , Crypto.Macaroon.Tests.tests |
20 | , Crypto.Macaroon.Serializer.Base64.Tests.tests | ||
19 | ] | 21 | ] |
20 | 22 | ||
21 | sanityCheck :: TestTree | 23 | sanityCheck :: TestTree |