Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 124 lines (109 sloc) 4.698 kb
59697b4 @ahyatt Initial checkin of files. These files were initially part of the
authored
1 ;;; websocket-test.el --- Unit tests for the websocket layer
2
3 ;; Copyright (c) 2010 Andrew Hyatt
4 ;;
5 ;; Author: Andrew Hyatt <ahyatt at gmail dot com>
6 ;; Maintainer: Andrew Hyatt <ahyatt at gmail dot com>
7 ;;
8 ;; This program is free software; you can redistribute it and/or
9 ;; modify it under the terms of the GNU General Public License as
10 ;; published by the Free Software Foundation; either version 2 of the
11 ;; License, or (at your option) any later version.
12 ;;
13 ;; This program is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program; if not, write to the Free Software
20 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 ;; 02110-1301, USA.
22
23 ;;; Commentary:
24 ;; This defines and runs ert unit tests. You can download ert from:
25 ;; http://github.com/ohler/ert, it also comes with Emacs 24 and above.
26
27 (require 'ert)
9d8794a @tkf Suppress compilation warning in websocket-test.el
tkf authored
28 (require 'websocket)
29 (eval-when-compile (require 'cl))
59697b4 @ahyatt Initial checkin of files. These files were initially part of the
authored
30
31 (defun websocket-test-get-filtered-response (outputs)
9d8794a @tkf Suppress compilation warning in websocket-test.el
tkf authored
32 (let* ((packet-data nil)
33 (websocket
34 (make-websocket :conn "fake-conn"
35 :filter (lambda (packet) (push packet packet-data))
36 :close-callback (lambda (not-called) (assert nil))
763cb60 @ahyatt Get rid of all mentions of version 75.
authored
37 :url "ws://foo/bar")))
59697b4 @ahyatt Initial checkin of files. These files were initially part of the
authored
38 (dolist (output outputs)
39 (websocket-outer-filter websocket output))
40 (nreverse packet-data)))
41
45245a4 @tkf Add test websocket-genbytes-length
tkf authored
42 (ert-deftest websocket-genbytes-length ()
43 (loop repeat 100
07050bc @ahyatt Get opening handshake to work correctly. Framing does not yet work,
authored
44 do (should (= (string-bytes (websocket-genbytes)) 16))))
45245a4 @tkf Add test websocket-genbytes-length
tkf authored
45
59697b4 @ahyatt Initial checkin of files. These files were initially part of the
authored
46 (ert-deftest websocket-filter-basic ()
47 (should (equal
48 '("foo")
49 (websocket-test-get-filtered-response '("\0foo\377"))))
50 (should (equal
51 '("foo" "bar")
52 (websocket-test-get-filtered-response
53 '("\0foo\377\0bar\377"))))
54 (should (equal
55 '("foo" "bar")
56 (websocket-test-get-filtered-response
57 '("\0foo\377" "\0bar\377")))))
58
59 (ert-deftest websocket-filter-inflight-packets ()
60 (should (equal
61 '("foo" "bar")
62 (websocket-test-get-filtered-response
63 '("\0foo\377\0b" "a" "r\377"))))
64 (should (equal
65 '("foo" "bar")
66 (websocket-test-get-filtered-response
67 '("\0foo\377\0ba" "r\377baz")))))
68
69 (ert-deftest websocket-filter-first-response ()
70 (should (equal
71 '("foo" "bar")
72 (websocket-test-get-filtered-response
73 '("HTTP 1.1\0foo\377\0bar\377"))))
74 (should (equal
75 '("foo")
76 (websocket-test-get-filtered-response
77 '("HTTP 1.1" "\0foo\377")))))
78
b5bbce1 @ahyatt Add a function to calculate the accept header expected.
authored
79 (ert-deftest websocket-calculate-accept ()
80 ;; This example comes straight from RFC 6455
81 (should
82 (equal "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
83 (websocket-calculate-accept "dGhlIHNhbXBsZSBub25jZQ=="))))
84
2a1518b @ahyatt Implementation of websocket-get-bits
authored
85 (ert-deftest websocket-get-bits ()
86 ;; 0 1 2 3
87 ;; 01234567890123456789012345678901
88 ;; For testing, let's use this fairly random sequence
89 ;; 01010001000100101011011110010101
90 ;; =
91 (let ((test-num 1360181141))
92 (should (equal 1 (websocket-get-bits test-num 0 1)))
93 (should (equal 1 (websocket-get-bits test-num 30 31)))
94 ;; 16-20 = 10110
95 (should (equal 22 (websocket-get-bits test-num 16 20)))))
96
f4e1aa8 @ahyatt Add helper functions for websocket-get-opcode and
authored
97 (defconst websocket-test-hello "\x81\x05\x48\x65\x6c\x6c\x6f"
98 "'Hello' string example, taken from the RFC.")
99
100 (ert-deftest websocket-get-opcode ()
101 (should (equal 'text (websocket-get-opcode websocket-test-hello))))
102
103 (ert-deftest websocket-get-payload-len ()
104 (should (equal '(5 . 0)
105 (websocket-get-payload-len websocket-test-hello)))
106 (should (equal '(200 . 3)
107 (websocket-get-payload-len
108 (bindat-pack '((:len u32) (:val u16))
109 `((:len . ,(lsh 126 16))
110 (:val . 200))))))
111 ;; we don't want to hit up any limits even on strange emacs builds,
112 ;; so this test has a pretty small test value
113 (should (equal '(70000 . 9)
114 (websocket-get-payload-len
115 (bindat-pack '((:len u32) (:val vec 2 u32))
116 `((:len . ,(lsh 127 16))
117 (:val . [0 70000])))))))
118
45245a4 @tkf Add test websocket-genbytes-length
tkf authored
119 (ert-run-tests-interactively 'websocket-genbytes-length)
59697b4 @ahyatt Initial checkin of files. These files were initially part of the
authored
120 (ert-run-tests-interactively 'websocket-filter-basic)
121 (ert-run-tests-interactively 'websocket-filter-inflight-packets)
122 (ert-run-tests-interactively 'websocket-filter-first-response)
b5bbce1 @ahyatt Add a function to calculate the accept header expected.
authored
123 (ert-run-tests-interactively 'websocket-calculate-accept)
Something went wrong with that request. Please try again.