-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_pod.clj
executable file
·123 lines (103 loc) · 4.78 KB
/
test_pod.clj
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
#!/usr/bin/env bb
(ns test-pod
(:require [babashka.pods :as pods]
[babashka.process :as process]
[clojure.edn :as edn]
[clojure.string :as string]))
(def pod-cmd
; ["clojure" "-M" "-m" "brisk.main"]
["./brisk"])
(pods/load-pod pod-cmd)
(require '[pod.brisk :as brisk])
(defn test-pod
[]
(let [sample-data {:princess :leia}
save-file "test-pod.nippy"]
(println "testing pod...")
(or
(do (println "save/thaw file")
(brisk/freeze-to-file save-file sample-data)
(when-not (= sample-data (brisk/thaw-from-file save-file))
(println "Failed to thaw from file.")
1))
(do (println "save/thaw string")
(let [frozen (brisk/freeze-to-string sample-data)]
(when-not (= sample-data (brisk/thaw-from-string frozen))
(println "Failed to thaw from string.")
1)))
(do (println "save/thaw file (salted password)")
(brisk/freeze-to-file save-file sample-data {:password [:salted "my-password"]})
(when-not (= sample-data (brisk/thaw-from-file save-file {:password [:salted "my-password"]}))
(println "Failed to thaw from file.")
1))
(do (println "save/thaw string (salted password)")
(let [frozen (brisk/freeze-to-string sample-data {:password [:salted "my-password"]})]
(when-not (= sample-data (brisk/thaw-from-string frozen {:password [:salted "my-password"]}))
(println "Failed to thaw from string.")
1)))
(do (println "save/thaw file (cached password)")
(brisk/freeze-to-file save-file sample-data {:password [:cached "my-password"]})
(when-not (= sample-data (brisk/thaw-from-file save-file {:password [:cached "my-password"]}))
(println "Failed to thaw from file.")
1))
(do (println "save/thaw string (cached password)")
(let [frozen (brisk/freeze-to-string sample-data {:password [:cached "my-password"]})]
(when-not (= sample-data (brisk/thaw-from-string frozen {:password [:cached "my-password"]}))
(println "Failed to thaw from string.")
1)))
(do (println "save/thaw string (wrong password)")
(let [frozen (brisk/freeze-to-string sample-data {:password [:salted "my-password"]})]
(try
(brisk/thaw-from-string frozen {:password [:salted "wrong-password"]})
(println "Failed to thaw from string.")
1
(catch Exception e
(when-not (string/includes? (ex-message e) "decompression failure")
(println "Incorrect message")
1)))))
)))
(defn test-cli
[]
(let [sample-data {:han :solo}
save-file "test-cli.nippy"
password "super-secret"]
(println "testing cli...")
(or
(do (println "save/thaw file")
(-> (process/process (into pod-cmd ["-f" "-o" save-file]) {:in (pr-str sample-data)})
(process/check))
(let [thawed (-> (process/process (into pod-cmd ["-t" "-i" save-file]) {:out :string})
(process/check)
:out
(edn/read-string))]
(when-not (= thawed sample-data)
(println "Failed to thaw from file.")
1)))
(do (println "save/thaw file (salted password)")
(-> (process/process (into pod-cmd ["--salted-password" password "-f" "-o" save-file]) {:in (pr-str sample-data)})
(process/check))
(let [thawed (-> (process/process (into pod-cmd ["--salted-password" password "-t" "-i" save-file]) {:out :string})
(process/check)
:out
(edn/read-string))]
(when-not (= thawed sample-data)
(println "Failed to thaw from file.")
1)))
(do (println "save/thaw file (salted password from ENV)")
(-> (process/process (into pod-cmd ["-f" "-o" save-file]) {:in (pr-str sample-data)
:extra-env {"BRISK_SALTED_PASSWORD" password}})
(process/check))
(let [thawed (-> (process/process (into pod-cmd ["-t" "-i" save-file]) {:out :string
:extra-env {"BRISK_SALTED_PASSWORD" password}})
(process/check)
:out
(edn/read-string))]
(when-not (= thawed sample-data)
(println "Failed to thaw from file.")
1)))
)))
(when (= *file* (System/getProperty "babashka.file"))
(System/exit
(or (test-pod)
(test-cli)
0)))