aboutsummaryrefslogtreecommitdiff
path: root/tests/test-dwim-shell-config-input-safety.el
blob: 2ff2edc91b360aafec6ffd92146506154be151e1 (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
;;; test-dwim-shell-config-input-safety.el --- Tests for input validators -*- lexical-binding: t; -*-

;;; Commentary:
;; Covers the pure input validators that guard user-controlled strings before
;; they reach a shell command: git clone URLs, ffmpeg timestamps, and rename
;; prefixes.  These reject shell metacharacters and malformed input so the
;; command-construction sites stay injection-safe.

;;; Code:

(when noninteractive
  (package-initialize))

(require 'ert)
(require 'dwim-shell-config)

;; ---------------------------------------------------------------------------
;;; cj/dwim-shell--valid-git-url-p
;; ---------------------------------------------------------------------------

(ert-deftest test-dwim-valid-git-url-accepts-common-forms ()
  "Normal: https, scp-style, ssh, and git URLs are accepted."
  (dolist (url '("https://github.com/user/repo.git"
                 "http://example.com/x"
                 "git@github.com:user/repo.git"
                 "ssh://git@host/path/repo.git"
                 "git://host/path"))
    (should (cj/dwim-shell--valid-git-url-p url))))

(ert-deftest test-dwim-valid-git-url-rejects-empty-and-junk ()
  "Boundary: empty string and non-URL text are rejected."
  (should-not (cj/dwim-shell--valid-git-url-p ""))
  (should-not (cj/dwim-shell--valid-git-url-p "not a url"))
  (should-not (cj/dwim-shell--valid-git-url-p "   ")))

(ert-deftest test-dwim-valid-git-url-rejects-shell-metacharacters ()
  "Error: URLs carrying shell metacharacters are rejected."
  (should-not (cj/dwim-shell--valid-git-url-p "https://x.com;reboot"))
  (should-not (cj/dwim-shell--valid-git-url-p "https://x.com; rm -rf /"))
  (should-not (cj/dwim-shell--valid-git-url-p "https://x.com$(whoami)")))

;; ---------------------------------------------------------------------------
;;; cj/dwim-shell--valid-ffmpeg-timestamp-p
;; ---------------------------------------------------------------------------

(ert-deftest test-dwim-valid-timestamp-accepts-seconds-and-clock ()
  "Normal: plain seconds and HH:MM:SS forms are accepted."
  (dolist (ts '("5" "0" "90.5" "00:05" "00:00:05" "1:02:03.5"))
    (should (cj/dwim-shell--valid-ffmpeg-timestamp-p ts))))

(ert-deftest test-dwim-valid-timestamp-rejects-bad-input ()
  "Error: non-numeric, negative, or metacharacter-bearing timestamps rejected."
  (dolist (ts '("" "abc" "-5" "5; rm" "00:00:05; reboot"))
    (should-not (cj/dwim-shell--valid-ffmpeg-timestamp-p ts))))

;; ---------------------------------------------------------------------------
;;; cj/dwim-shell--safe-rename-prefix-p
;; ---------------------------------------------------------------------------

(ert-deftest test-dwim-safe-rename-prefix-accepts-filename-safe ()
  "Normal/Boundary: alphanumerics, spaces, dot, dash, underscore, and empty."
  (dolist (p '("" "img_" "vacation 2026" "shot-01."))
    (should (cj/dwim-shell--safe-rename-prefix-p p))))

(ert-deftest test-dwim-safe-rename-prefix-rejects-unsafe ()
  "Error: quotes, slashes, semicolons, and newlines are rejected."
  (dolist (p '("a'b" "a/b" "a;b" "a\nb"))
    (should-not (cj/dwim-shell--safe-rename-prefix-p p))))

(provide 'test-dwim-shell-config-input-safety)
;;; test-dwim-shell-config-input-safety.el ends here