aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/security/integrity.lux
blob: ad9b67f4f458d1bf74d42a2c2ae1a4fe24acc49a (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [control
    [hash (#+ Hash)]
    [monad (#+ do)]
    {[0 #test]
     [/
      ["$." functor (#+ Injection Comparison)]
      ["$." apply]
      ["$." monad]]}]
   [data
    ["." error]
    ["." text ("#;." equivalence)
     format]]
   [math
    ["r" random]]]
  {1
   ["." / (#+ Dirty)]})

(def: injection
  (Injection Dirty)
  /.taint)

(def: comparison
  (Comparison Dirty)
  (function (_ == left right)
    (== (/.trust left)
        (/.trust right))))

(def: #export test
  Test
  (<| (_.context (%name (name-of /.Dirty)))
      (do r.monad
        [raw (r.ascii 10)
         #let [dirty (/.taint raw)]]
        ($_ _.and
            ($functor.spec ..injection ..comparison /.functor)
            ($apply.spec ..injection ..comparison /.apply)
            ($monad.spec ..injection ..comparison /.monad)

            (_.test "Can clean a dirty value by trusting it."
                    (text;= raw (/.trust dirty)))
            (_.test "Can validate a dirty value."
                    (case (/.validate (function (_ value)
                                        (if (|> value text.size (n/> 0))
                                          (#error.Success value)
                                          (#error.Failure "Empty text is invalid.")))
                                      dirty)
                      (#error.Success clean)
                      (text;= raw clean)

                      (#error.Failure error)
                      false))
            ))))