summaryrefslogtreecommitdiff
path: root/test/lisp/net/hmac-md5-tests.el
blob: 8e01353fa3f30857ee2ce42979cd6425153deb02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;;; hmac-md5-tests.el --- Tests for hmac-md5.el  -*- lexical-binding:t -*-

;; Copyright (C) 2020-2021 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'ert)
(require 'hmac-md5)

;; Test cases from RFC 2202, "Test Cases for HMAC-MD5 and HMAC-SHA-1",
;; moved here from hmac-md5.el

(ert-deftest hmac-md5-test-encode-string ()
  ;; RFC 2202 -- test_case 1
  (should (equal (encode-hex-string
                  (hmac-md5 "Hi There" (make-string 16 ?\x0b)))
                 "9294727a3638bb1c13f48ef8158bfc9d"))

  ;; RFC 2202 -- test_case 2
  (should (equal (encode-hex-string
                  (hmac-md5 "what do ya want for nothing?" "Jefe"))
                 "750c783e6ab0b503eaa86e310a5db738"))

  ;; RFC 2202 -- test_case 3
  (should (equal (encode-hex-string
                  (hmac-md5 (decode-hex-string (make-string 100 ?d))
                            (decode-hex-string (make-string 32 ?a))))
                 "56be34521d144c88dbb8c733f0e8b3f6"))

  ;; RFC 2202 -- test_case 4
  (should (equal (encode-hex-string
                  (hmac-md5 (decode-hex-string
                             (mapconcat (lambda (c) (concat (list c) "d"))
                                        (make-string 50 ?c) ""))
                            (decode-hex-string "0102030405060708090a0b0c0d0e0f10111213141516171819")))
                 "697eaf0aca3a3aea3a75164746ffaa79"))

  ;; RFC 2202 -- test_case 5 (a)
  (should (equal (encode-hex-string
                  (hmac-md5 "Test With Truncation" (make-string 16 ?\x0c)))
                 "56461ef2342edc00f9bab995690efd4c"))

  ;; RFC 2202 -- test_case 5 (b)
  (should (equal (encode-hex-string
                  (hmac-md5-96 "Test With Truncation" (make-string 16 ?\x0c)))
                 "56461ef2342edc00f9bab995"))

  ;; RFC 2202 -- test_case 6
  (should (equal (encode-hex-string
                  (hmac-md5
                   "Test Using Larger Than Block-Size Key - Hash Key First"
                   (decode-hex-string (make-string 160 ?a))))
                 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd"))

  ;; RFC 2202 -- test_case 7
  (should (equal (encode-hex-string
                  (hmac-md5
                   "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
                   (decode-hex-string (make-string 160 ?a))))
                 "6f630fad67cda0ee1fb1f562db3aa53e")))

(provide 'hmac-md5-tests)
;;; hmac-md5-tests.el ends here