File: session.scm

package info (click to toggle)
guile-ssh 0.18.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,996 kB
  • sloc: ansic: 4,821; lisp: 4,171; makefile: 310; sh: 259
file content (270 lines) | stat: -rw-r--r-- 9,459 bytes parent folder | download
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
;;; session.scm -- Testing of session procedures without a connection.

;; Copyright (C) 2014-2024 Artyom V. Poptsov <poptsov.artyom@gmail.com>
;;
;; This file is a part of Guile-SSH.
;;
;; Guile-SSH 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.
;;
;; Guile-SSH 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 Guile-SSH.  If not, see <http://www.gnu.org/licenses/>.

(add-to-load-path (getenv "abs_top_srcdir"))

(use-modules (srfi srfi-64)
             (ice-9 regex)
             (ssh session)
             (ssh log)
             (ssh version)
             ;; Helper procedures
             (tests common))

(define %libssh-minor-version
  (string->number (cadr (string-split (get-libssh-version) #\.))))

(set-log-verbosity! 'functions)

(test-begin-with-log "session")

;;;

(test-assert "%make-session"
  (%make-session))

(test-assert-with-log "%make-session, gc test"
  (let ((max-sessions 1000))
    (do ((idx 1 (+ idx 1)))
        ((> idx max-sessions))
      (when (zero? (euclidean-remainder idx 100))
        (format-log/scm 'nolog "" (format #f "~d / ~d sessions created ..."
                                          idx max-sessions)))
      (%make-session))
    #t))

(test-assert "session?"
  (let ((session (%make-session))
        (x       "string"))
    (and (session? session)
         (not (session? x)))))



(test-assert "display, undefined values"
  (let* ((session (%make-session))
         (output  (with-output-to-string
                    (lambda ()
                      (display session)))))
    (if (string-match "#<session #<undefined>@#<undefined>:22 \\(disconnected\\) [0-9a-z]+>"
                      output)
        output
        (error "Wrong output" output))))

(test-assert "display, defined values"
  (let* ((session (make-session #:host "example.org"
                                #:user "alice"))
         (output  (with-output-to-string
                    (lambda ()
                      (display session)))))
    (if (string-match "#<session alice@example.org:22 \\(disconnected\\) [0-9a-z]+>"
                      output)
        output
        (error "Wrong output" output))))



(test-assert "comparison of sessions"
  (let ((s1 (%make-session))
        (s2 (%make-session)))
    (and (equal? s1 s1)
         (not (equal? s1 s2)))))

(test-assert "session-set!, valid values"
  (let ((session (%make-session))
        (options `((host         "localhost")
                   (port         22)
                   (bindaddr     "127.0.0.1")
                   (user         "Random J. User")
                   (timeout      15)    ;seconds
                   (timeout-usec 15000) ;milliseconds
                   (ssh1         #f #t)
                   (ssh2         #f #t)
                   (log-verbosity nolog rare protocol packet functions
                                  nolog)
                   (compression-level 1 2 3 4 5 6 7 8 9)
                   (compression   "yes" "no")
                   (callbacks     ((user-data . "hello")
                                   (global-request-callback . ,(const #f))))))
        (res #t))

    (if (>= %libssh-minor-version 8)
        (set! options (cons  '(nodelay #f #t) options)))

    (for-each
     (lambda (opt)
       (for-each
        (lambda (val)
          (session-set! session (car opt) val))
        (cdr opt)))
     options)
    res))

(unless (>= %libssh-minor-version 10)
  (test-skip "session-set!, rsa-min-size"))
(test-assert "session-set!, rsa-min-size"
  (let ((session (%make-session)))
    (session-set! session 'rsa-min-size 1024)))

(test-assert "session-set!, invalid values"
  (let ((session (%make-session))
        (options '((host              12345 #t)
                   (port              "string" -22)
                   (bindaddr          12345 -12345)
                   (user              12345 -12345)
                   (timeout           "string" -15)
                   (timeout-usec      "string" -15000)
                   (ssh1              12345 "string")
                   (ssh2              12345 "string")
                   (log-verbosity     "string" -1 0 1 2 3 4 5)
                   (compression       12345)
                   (compression-level -1 0 10)
                   (callbacks         "not a list"
                                      ((global-request-callback . #f)))))
        (res #t))

    (if (>= %libssh-minor-version 8)
        (set! options (cons '(nodelay 12345 "string") options)))

    (for-each
     (lambda (opt)
       (for-each
        (lambda (val)
          (catch #t
            (lambda ()
              (session-set! session (car opt) val)
              (let* ((r (test-runner-current))
                     (l (test-runner-aux-value r)))
                (format l "  opt: ~a, val: ~a -- passed mistakenly~%"
                        (car opt) val)
                (set! res #f)))
            (const #t)))
        (cdr opt)))
     options)
    res))

(test-error "session-set!, invalid option type"
  'wrong-type-arg
  (let ((session (%make-session)))
    (session-set! session "non-valid type" "value")))

(test-assert "session-get"
  (let* ((host         "example.com")
         (port         12345)
         (user         "alice")
         (proxycommand "test")
         (callbacks    '((user-data . "test")))
         (session      (make-session #:host         host
                                     #:port         port
                                     #:user         user
                                     #:identity     %rsakey
                                     #:proxycommand proxycommand
                                     #:callbacks    callbacks)))
    (and (string=? (session-get session 'host)         host)
         (=        (session-get session 'port)         port)
         (string=? (session-get session 'user)         user)
         (string=? (session-get session 'identity)     %rsakey)
         (string=? (session-get session 'proxycommand) proxycommand)
         (equal?   (session-get session 'callbacks)    callbacks)
         ;; Make sure that default callbacks value is '#f'.
         (equal?   (session-get (%make-session) 'callbacks) #f))))

(test-error "session-get, non-session object"
  'wrong-type-arg
  (session-get "non-session object" 'test))

(test-error "session-get, invalid option"
  'guile-ssh-error
  (let ((session (%make-session)))
    (session-get session 'wrong-option)))

(test-assert "session-parse-config!"
  (let ((session (make-session #:host "example")))
    (session-parse-config! session %config)
    (format (current-error-port) "session: ~a~%" session)
    (format (current-error-port) "host: ~a~%" (session-get session 'host))
    (format (current-error-port) "user: ~a~%" (session-get session 'user))
    (format (current-error-port) "port: ~a~%" (session-get session 'port))
    (and (string=? (session-get session 'host) "example.org")
         (string=? (session-get session 'user) "alice")
         (=        (session-get session 'port) 2222))))

(test-error "session-parse-config!, non-session object"
  'wrong-type-arg
  (session-parse-config! "non-session object" "wrong-value"))

(test-error "session-parse-config!, wrong config file"
  'guile-ssh-error
  (session-parse-config! (%make-session) "wrong-value"))

(test-assert "make-session"
  (make-session #:host "localhost"
                #:port 22
                #:user "Random J. User"))

(test-assert "make-session, '#:config' and '#:host' is specified"
  (make-session #:host   "localhost"
                #:config %config))

(test-assert "make-session, '#:config' set to '/dev/null'"
  (make-session #:host   "localhost"
                #:config "/dev/null"))

;; Setting '#:config' option to #f must set "process-config?" option to #f.
(test-assert "make-session, '#:config' as a boolean value: #f"
  (make-session #:host   "localhost"
                #:config #f))

;; Setting '#:config' option to #t must initiate parsing the default user
;; configuration file ('~/.ssh/config'.)
(test-assert "make-session, '#:config' as a boolean value: #t"
  (make-session #:host   "localhost"
                #:config #t))

(test-error "make-session, only '#:config' is specified"
  'guile-ssh-error
  (make-session #:config %config))

(test-equal "make-session: keywords must overwrite config options"
  22
  (let ((s (make-session #:host   "example"
                         #:port   22
                         ;; Configuration sets port to 2222
                         #:config %config)))
    (session-get s 'port)))

(test-equal-with-log "blocking-flush!"
  'ok
  (blocking-flush! (%make-session) 15))

(test-error "connected?, non-session object"
  'wrong-type-arg
  (connected? "non-session object"))

(test-assert "connected?, check that we are not connected"
  (let ((session (%make-session)))
    (not (connected? session))))

(define exit-status (test-runner-fail-count (test-runner-current)))

(test-end "session")

(exit (= 0 exit-status))

;;; session.scm ends here.