File: bio.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (87 lines) | stat: -rw-r--r-- 2,765 bytes parent folder | download | duplicates (2)
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
;;;; -*- Mode: LISP; Syntax: COMMON-LISP; indent-tabs-mode: nil; coding: utf-8; show-trailing-whitespace: t -*-
;;;
;;; Copyright (C) 2021  Tomas Zellerin (zellerin@gmail.com, https://github.com/zellerin)
;;; Copyright (C) 2021  Anton Vodonosov (avodonosov@yandex.ru, https://github.com/avodonosov)
;;; Copyright (C) contributors as per cl+ssl git history
;;;
;;; See LICENSE for details.

(in-package :cl+ssl.test)

(def-suite :cl+ssl.bio :in :cl+ssl
  :description "Bio interface test")

(in-suite :cl+ssl.bio)

(cl+ssl::define-crypto-function ("BIO_write" bio-write)
  :int
  (bio :pointer)
  (text :string)
  (len :int))

(cl+ssl::define-crypto-function ("BIO_read" bio-read)
  :int
  (bio :pointer)
  (text :pointer)
  (len :int))

(cl+ssl::define-crypto-function ("BIO_gets" bio-gets)
  :int
  (bio :pointer)
  (text :pointer)
  (len :int))

(cl+ssl::define-crypto-function ("BIO_puts" bio-puts)
  :int
  (bio :pointer)
  (text :string))

(test bio-read
  (is (equalp
       '("Hel" "lo")
       (cl+ssl::with-bio-input-from-string (bio "Hello")
         (cffi:with-foreign-object (array :char 32)
           (flet ((bio-read-to-string (len)
                    (let ((size (bio-read bio array len)))
                      (assert (< size 31))
                      (setf (cffi:mem-ref array :unsigned-char size) 0)
                      (cffi:foreign-string-to-lisp array))))
             (list
              (bio-read-to-string 3)
              (bio-read-to-string 32))))))))

(test bio-gets
  (cffi:with-foreign-object (array :char 32)
    (is (equalp
         '(6 "Hello
" 3 "bar")
         (cl+ssl::with-bio-input-from-string (bio "Hello
bar")
           (list
            (bio-gets bio array 32)
            (cffi:foreign-string-to-lisp array)
            (bio-gets bio array 32)
            (cffi:foreign-string-to-lisp array)))
         ))

    ;; check that the array is zero terminated
    ;; and thus the max number of chars read is len - 1.
    (setf (cffi:mem-ref array :unsigned-char 4) 7) ; will be replaced by zero terminator
    (is (= 4 (cl+ssl::with-bio-input-from-string (bio "1234567")
               (bio-gets bio array 5))))
    (is (= 0 (cffi:mem-ref array :unsigned-char 4)))

    ;; when the len 0, the return value is 0, and the array is still
    (setf (cffi:mem-ref array :unsigned-char 0) 7) ; will be replaced by zero terminator
    (is (= 0 (cl+ssl::with-bio-input-from-string (bio "zzz")
               (bio-gets bio array 0))))
    (is (= 0 (cffi:mem-ref array :unsigned-char 0)))))

(test bio-write-puts
  (is (equalp
       "Hello Hi
Hallo"
       (cl+ssl::with-bio-output-to-string (bio)
         (bio-write bio  #1="Hello " (length #1#))
         (bio-puts bio "Hi")
         (bio-write bio  #2="Hallo" (length #2#))))))