PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test/darkexchange/model/security.clj

https://github.com/vtcex/Dark-Exchange
Clojure | 86 lines | 77 code | 9 blank | 0 comment | 22 complexity | d3fa8fb87fbcb726496a1cfbde83f453 MD5 | raw file
  1. (ns test.darkexchange.model.security
  2. (:require [clojure.contrib.logging :as logging])
  3. (:use [darkexchange.model.security])
  4. (:use [clojure.test])
  5. (:import [java.security KeyPair]))
  6. (deftest test-encrypt-decrypt
  7. (let [key-pair (generate-key-pair)
  8. data "secret text"]
  9. (is key-pair "key-pair is nil, but expected non-nil.")
  10. (let [encrypted-text (encrypt key-pair data)]
  11. (is encrypted-text "encrypted-text is nil, but expected non-nil.")
  12. (let [decrypted-text (decrypt key-pair encrypted-text)]
  13. (is decrypted-text "decrypted-text is nil, but expected non-nil.")
  14. (is (= decrypted-text data) (str "Text not decrypted."))))))
  15. (deftest password-encrypt-decrypt
  16. (let [password "password"
  17. data "secret text"
  18. algorithm des-algorithm
  19. encrypted-data (password-encrypt password data algorithm)]
  20. (is (not (= data encrypted-data)) "Text not encrypted.")
  21. (is (= data (password-decrypt password encrypted-data algorithm)) "Text not decrypted."))
  22. (let [password "password blah blah blah blah blah blah blah blah"
  23. data "secret text"
  24. algorithm des-algorithm
  25. encrypted-data (password-encrypt password data algorithm)]
  26. (is (not (= data encrypted-data)) "Text not encrypted.")
  27. (is (= data (password-decrypt password encrypted-data algorithm)) "Text not decrypted.")))
  28. (deftest basic-password-protection
  29. (let [password "password"
  30. salt 2079324
  31. algorithm default-encrypt-password-algorithm
  32. n default-encrypt-password-n
  33. encrypted-password (encrypt-password-string password salt algorithm n)]
  34. (is (not (= encrypted-password password)) "Password not encrypted")
  35. (is (= encrypted-password (encrypt-password-string password salt algorithm n)) "Password check not valid.")
  36. (is (not (= encrypted-password (encrypt-password-string password salt algorithm 1))) "Multiple hash iterations do not help.")
  37. (is (not (= encrypted-password (encrypt-password-string (.substring password 0 4) salt algorithm n))) "Short password works when it shouldn't.")))
  38. (defn byte-not-equals [byte1 byte2]
  39. (not (= byte1 byte2)))
  40. (defn byte-array-equals [byte-array1 byte-array2]
  41. (and
  42. (= (count byte-array1) (count byte-array2))
  43. (nil? (some identity (map byte-not-equals byte-array1 byte-array2)))))
  44. (defn key-equals? [key1 key2]
  45. (and
  46. (= (.getAlgorithm key1) (.getAlgorithm key2))
  47. (= (.getFormat key1) (.getFormat key2))
  48. (byte-array-equals (.getEncoded key1) (.getEncoded key2))))
  49. (defn key-pair-equals? [key-pair1 key-pair2]
  50. (and
  51. (key-equals? (.getPublic key-pair1) (.getPublic key-pair2))
  52. (key-equals? (.getPrivate key-pair1) (.getPrivate key-pair2))))
  53. (deftest save-load-key-pairs
  54. (let [key-pair (generate-key-pair)
  55. key-pair-map (get-key-pair-map key-pair)]
  56. (is key-pair-map "Expected non-nil key-pair-map.")
  57. (is (map? key-pair-map) "key-pair-map must be a map")
  58. (is (contains? key-pair-map :public-key) "key-pair-map must contain the :public-key key")
  59. (is (contains? key-pair-map :private-key) "key-pair-map must contain the :private-key key")
  60. (let [public-key (:public-key key-pair-map)
  61. private-key (:private-key key-pair-map)]
  62. (is (map? public-key) "Public key must be a map.")
  63. (is (map? private-key) "private key must be a map.")
  64. (is (= (:algorithm public-key) default-algorithm) "The public key algorithm must be the default algorithm.")
  65. (is (= (:algorithm private-key) default-algorithm) "The private key algorithm must be the default algorithm.")
  66. (is (contains? public-key :bytes) "The public key map must contain the :bytes key.")
  67. (is (contains? private-key :bytes) "The private key map must contain the :bytes key."))
  68. (let [decoded-key-pair (decode-key-pair key-pair-map)]
  69. (is decoded-key-pair "Expected non-nil decoded-key-pair")
  70. (is (instance? KeyPair decoded-key-pair) "decode-key-pair must return an object of type KeyPair.")
  71. (is (key-pair-equals? key-pair decoded-key-pair) "The decoded key is not equal to the original key."))))
  72. (deftest signature
  73. (let [test-data "Test data to sign"
  74. key-pair (generate-key-pair)
  75. signature (sign key-pair test-data)]
  76. (is (verify-signature key-pair test-data signature))
  77. (is (not (verify-signature (generate-key-pair) test-data signature)))))