summaryrefslogtreecommitdiff
path: root/tests/frame.scm
blob: 5dd4813a198e4db3dae1357721b2f326f45daef4 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(define-module (test-frame)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-64)
  #:use-module (ice-9 match)
  #:use-module (rnrs bytevectors)
  #:use-module (rnrs io ports)
  #:use-module (web socket frame))

(define (hex-string->bytevector str)
  "Convert the hexadecimal encoded string STR to a bytevector."
  (define hex-char->int
    (match-lambda
     (#\0 0)
     (#\1 1)
     (#\2 2)
     (#\3 3)
     (#\4 4)
     (#\5 5)
     (#\6 6)
     (#\7 7)
     (#\8 8)
     (#\9 9)
     (#\a 10)
     (#\b 11)
     (#\c 12)
     (#\d 13)
     (#\e 14)
     (#\f 15)))

  (define (read-byte i)
    (let ((j (* 2 i)))
      (+ (hex-char->int (string-ref str (1+ j)))
         (* (hex-char->int (string-ref str j)) 16))))

  (let* ((len (/ (string-length str) 2))
         (bv  (make-bytevector len)))
    (let loop ((i 0))
      (if (= i len)
          bv
          (begin
            (bytevector-u8-set! bv i (read-byte i))
            (loop (1+ i)))))))

(define (bytevector->hex-string bv)
  (define int->hex-char
    (match-lambda
      (0 #\0)
      (1 #\1)
      (2 #\2)
      (3 #\3)
      (4 #\4)
      (5 #\5)
      (6 #\6)
      (7 #\7)
      (8 #\8)
      (9 #\9)
      (10 #\a)
      (11 #\b)
      (12 #\c)
      (13 #\d)
      (14 #\e)
      (15 #\f)))

  (list->string
   (append-map (lambda (x)
                 (let ((high (ash (logand x #xf0) -4))
                       (low  (logand x #x0f)))
                   (list (int->hex-char high)
                         (int->hex-char low))))
               (bytevector->u8-list bv))))

(define (call-with-input-bytevector bv proc)
  (let ((port (open-bytevector-input-port bv)))
    (dynamic-wind
      (const #t)
      (lambda ()
        (proc port))
      (lambda ()
        (close-port port)))))

(define (frame->hex-string frame)
  (call-with-values open-bytevector-output-port
    (lambda (port get-bytevector)
      (write-frame frame port)
      (bytevector->hex-string (get-bytevector)))))

(define (bytevector->frame bv)
  (call-with-input-bytevector bv read-frame))

(define (hex-string->frame str)
  (bytevector->frame (hex-string->bytevector str)))

(test-begin "frame")

(test-equal "read unmasked text message"
  (hex-string->frame "810548656c6c6f")
  (make-text-frame "Hello"))

(test-equal "write unmasked text message"
  "810548656c6c6f"
  (frame->hex-string (make-text-frame "Hello")))

(test-equal "read masked text message"
  (hex-string->frame "818537fa213d7f9f4d5158")
  (make-text-frame "Hello" #vu8(#x37 #xfa #x21 #x3d)))

(test-equal "write masked text message"
  "818537fa213d7f9f4d5158"
  (frame->hex-string (make-text-frame "Hello" #vu8(#x37 #xfa #x21 #x3d))))

(test-equal "read fragmented umasked text message"
  (list (hex-string->frame "010348656c")
        (hex-string->frame "80026c6f"))
  (list (make-frame #f 'text #f (string->utf8 "Hel"))
        (make-frame #t 'continuation #f (string->utf8 "lo"))))

(test-equal "read unmasked ping frame"
  (hex-string->frame "890548656c6c6f")
  (make-ping-frame (string->utf8 "Hello")))

(test-equal "write unmasked ping frame"
  "890548656c6c6f"
  (frame->hex-string (make-ping-frame (string->utf8 "Hello"))))

(test-equal "read masked pong frame"
  (hex-string->frame "8a8537fa213d7f9f4d5158")
  (make-pong-frame (string->utf8 "Hello") #vu8(#x37 #xfa #x21 #x3d)))

(test-equal "write masked pong frame"
  "8a8537fa213d7f9f4d5158"
  (frame->hex-string
   (make-pong-frame (string->utf8 "Hello") #vu8(#x37 #xfa #x21 #x3d))))

(test-equal "read 256 bytes binary message in a single unmasked frame"
  (let* ((header (hex-string->bytevector "827e0100"))
         (len    (bytevector-length header))
         (frame  (make-bytevector (+ len 256) 170)))
    (bytevector-copy! header 0 frame 0 len)
    (bytevector->frame frame))
  (make-binary-frame (make-bytevector 256 170)))

(test-equal "read 64KiB binary message in a single unmasked frame"
  (let* ((header (hex-string->bytevector "827f0000000000010000"))
         (len    (bytevector-length header))
         (frame  (make-bytevector (+ len 65536) 170)))
    (bytevector-copy! header 0 frame 0 len)
    (bytevector->frame frame))
  (make-binary-frame (make-bytevector 65536 170)))

(test-equal "frame-length"
  10
  (frame-length (make-binary-frame (make-bytevector 10))))

(test-equal "frame-concatenate"
  #vu8(1 2 3 4 5)
  (frame-concatenate
   (list (make-binary-frame #vu8(1 2 3))
         (make-binary-frame #vu8(4 5)))))

(test-equal "text-frame->string"
  "Hello"
  (text-frame->string (make-text-frame "Hello")))

(test-equal "text-frames->string"
  "Hello"
  (text-frames->string
   (list (make-text-frame "Hel")
         (make-text-frame "lo"))))

(test-end "frame")


(exit (= (test-runner-fail-count (test-runner-current)) 0))