<commit_before>{-# LANGUAGE OverloadedStrings #-}
module CryptoTest (tests) where

import qualified Data.ByteString as BS
import Data.Maybe
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import Test.HUnit hiding (assert)
import Test.QuickCheck.Monadic
import Test.QuickCheck

import Crypto.Gpgme
import TestUtil

tests = [ testProperty "bob_encrypt_for_alice_decrypt"
            bob_encrypt_for_alice_decrypt
        , testProperty "bob_encrypt_sign_for_alice_decrypt_verify"
            bob_encrypt_sign_for_alice_decrypt_verify
        , testCase "decrypt_garbage"
            decrypt_garbage
         , testProperty "bob_encrypt_for_alice_decrypt_short"
             bob_encrypt_for_alice_decrypt_short
         , testProperty "bob_encrypt_sign_for_alice_decrypt_verify_short"
             bob_encrypt_sign_for_alice_decrypt_verify_short

        {- very annoying to run, as passphrase callbacks don't work:
        , testProperty "bob_encrypt_symmetrically" bob_encrypt_symmetrically
        -}
        ]

bob_encrypt_for_alice_decrypt plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- withCtx "test/bob" "C" openPGP $ \bCtx ->
                       withKey bCtx alice_pub_fpr noSecret $ \aPubKey ->
                           encrypt bCtx [aPubKey] noFlag plain

               -- decrypt
               dec <- withCtx "test/alice" "C" openPGP $ \aCtx ->
                       decrypt aCtx (fromJustAndRight enc)

               return $ fromRight dec

bob_encrypt_for_alice_decrypt_short plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- encrypt' "test/bob" alice_pub_fpr plain

               -- decrypt
               dec <- decrypt' "test/alice" (fromRight enc)

               return $ fromRight dec

bob_encrypt_sign_for_alice_decrypt_verify plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- withCtx "test/bob" "C" openPGP $ \bCtx ->
                       withKey bCtx alice_pub_fpr noSecret $ \aPubKey ->
                           encryptSign bCtx [aPubKey] noFlag plain

               -- decrypt
               dec <- withCtx "test/alice" "C" openPGP $ \aCtx ->
                       decryptVerify aCtx (fromJustAndRight enc)

               return $ fromRight dec

bob_encrypt_sign_for_alice_decrypt_verify_short plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- encryptSign' "test/bob" alice_pub_fpr plain

               -- decrypt
               dec <- decryptVerify' "test/alice" (fromRight enc)

               return $ fromRight dec

bob_encrypt_symmetrically plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let symmetric_key = "foo"

               -- encrypt
               enc <- withPWCtx symmetric_key "test/bob" "C" openPGP $ \bCtx ->
                           encrypt bCtx [] noFlag plain

               -- decrypt
               dec <- withPWCtx symmetric_key "test/alice" "C" openPGP $ \aCtx ->
                       decrypt aCtx (fromRight enc)

               return $ fromRight dec

decrypt_garbage = do
    val <- withCtx "test/bob" "C" openPGP $ \bCtx ->
              decrypt bCtx (BS.pack [1,2,3,4,5,6])
    isLeft val @? "should be left " ++ show val
<commit_msg>commented withPWCtx as it no longer is part of he public api<commit_after>{-# LANGUAGE OverloadedStrings #-}
module CryptoTest (tests) where

import qualified Data.ByteString as BS
import Data.Maybe
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import Test.HUnit hiding (assert)
import Test.QuickCheck.Monadic
import Test.QuickCheck

import Crypto.Gpgme
import TestUtil

tests = [ testProperty "bob_encrypt_for_alice_decrypt"
            bob_encrypt_for_alice_decrypt
        , testProperty "bob_encrypt_sign_for_alice_decrypt_verify"
            bob_encrypt_sign_for_alice_decrypt_verify
        , testCase "decrypt_garbage"
            decrypt_garbage
         , testProperty "bob_encrypt_for_alice_decrypt_short"
             bob_encrypt_for_alice_decrypt_short
         , testProperty "bob_encrypt_sign_for_alice_decrypt_verify_short"
             bob_encrypt_sign_for_alice_decrypt_verify_short

        {- very annoying to run, as passphrase callbacks don't work:
        , testProperty "bob_encrypt_symmetrically" bob_encrypt_symmetrically
        -}
        ]

bob_encrypt_for_alice_decrypt plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- withCtx "test/bob" "C" openPGP $ \bCtx ->
                       withKey bCtx alice_pub_fpr noSecret $ \aPubKey ->
                           encrypt bCtx [aPubKey] noFlag plain

               -- decrypt
               dec <- withCtx "test/alice" "C" openPGP $ \aCtx ->
                       decrypt aCtx (fromJustAndRight enc)

               return $ fromRight dec

bob_encrypt_for_alice_decrypt_short plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- encrypt' "test/bob" alice_pub_fpr plain

               -- decrypt
               dec <- decrypt' "test/alice" (fromRight enc)

               return $ fromRight dec

bob_encrypt_sign_for_alice_decrypt_verify plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- withCtx "test/bob" "C" openPGP $ \bCtx ->
                       withKey bCtx alice_pub_fpr noSecret $ \aPubKey ->
                           encryptSign bCtx [aPubKey] noFlag plain

               -- decrypt
               dec <- withCtx "test/alice" "C" openPGP $ \aCtx ->
                       decryptVerify aCtx (fromJustAndRight enc)

               return $ fromRight dec

bob_encrypt_sign_for_alice_decrypt_verify_short plain =
    not (BS.null plain) ==> monadicIO $ do
        dec <- run encr_and_decr
        assert $ dec == plain
  where encr_and_decr =
            do let alice_pub_fpr = "EAACEB8A"

               -- encrypt
               enc <- encryptSign' "test/bob" alice_pub_fpr plain

               -- decrypt
               dec <- decryptVerify' "test/alice" (fromRight enc)

               return $ fromRight dec

-- bob_encrypt_symmetrically plain =
--     not (BS.null plain) ==> monadicIO $ do
--         dec <- run encr_and_decr
--         assert $ dec == plain
--   where encr_and_decr =
--             do let symmetric_key = "foo"
-- 
--                -- encrypt
--                enc <- withPWCtx symmetric_key "test/bob" "C" openPGP $ \bCtx ->
--                            encrypt bCtx [] noFlag plain
-- 
--                -- decrypt
--                dec <- withPWCtx symmetric_key "test/alice" "C" openPGP $ \aCtx ->
--                        decrypt aCtx (fromRight enc)
-- 
--                return $ fromRight dec

decrypt_garbage = do
    val <- withCtx "test/bob" "C" openPGP $ \bCtx ->
              decrypt bCtx (BS.pack [1,2,3,4,5,6])
    isLeft val @? "should be left " ++ show val

{'fraction_non_alphanumeric': 0.07966248914257353, 'fraction_numerical': 0.0027298672291847623, 'mean_word_length': 1.8280701754385964, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '68137', 'n_tokens_mistral': 2619, 'n_tokens_neox': 2537, 'n_words': 593}