aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/data/format/tar.lux
blob: a858db47394719ebf4863ee7d1243f780e61636e (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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
(.require
 [library
  [lux (.except)
   [abstract
    [monad (.only do)]]
   [control
    ["<>" parser (.only)]
    ["[0]" maybe]
    ["[0]" try]
    ["[0]" exception]]
   [data
    ["[0]" product]
    ["[0]" binary (.use "[1]#[0]" equivalence monoid)
     ["<b>" \\parser]
     ["[0]" \\format]]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]
     [encoding
      ["[0]" utf8]]
     ["[0]" unicode
      ["[1]" set]
      ["[1]/[0]" block]]]
    [collection
     ["[0]" sequence]
     ["[0]" list (.use "[1]#[0]" mix)]]]
   [math
    ["[0]" random (.only Random)]
    [number
     ["n" nat]
     ["i" int]]]
   [world
    [time
     ["[0]" instant (.only Instant)]
     ["[0]" duration]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" /]])

(def path
  Test
  (_.for [/.Path]
         (do [! random.monad]
           [expected (random.lower_case /.path_size)
            invalid (random.lower_case (++ /.path_size))
            not_ascii (random.text (random.char (unicode.set [unicode/block.katakana (list)]))
                                   /.path_size)]
           (`` (all _.and
                    (_.coverage [/.path /.from_path]
                      (when (/.path expected)
                        {try.#Success actual}
                        (text#= expected
                                (/.from_path actual))

                        {try.#Failure error}
                        false))
                    (_.coverage [/.no_path]
                      (text#= "" (/.from_path /.no_path)))
                    (_.coverage [/.path_size /.path_is_too_long]
                      (when (/.path invalid)
                        {try.#Success _}
                        false

                        {try.#Failure error}
                        (exception.match? /.path_is_too_long error)))
                    (_.coverage [/.not_ascii]
                      (when (/.path not_ascii)
                        {try.#Success actual}
                        false

                        {try.#Failure error}
                        (exception.match? /.not_ascii error)))
                    )))))

(def name
  Test
  (_.for [/.Name]
         (do [! random.monad]
           [expected (random.lower_case /.name_size)
            invalid (random.lower_case (++ /.name_size))
            not_ascii (random.text (random.char (unicode.set [unicode/block.katakana (list)]))
                                   /.name_size)]
           (`` (all _.and
                    (_.coverage [/.name /.from_name]
                      (when (/.name expected)
                        {try.#Success actual}
                        (text#= expected
                                (/.from_name actual))

                        {try.#Failure error}
                        false))
                    (_.coverage [/.name_size /.name_is_too_long]
                      (when (/.name invalid)
                        {try.#Success _}
                        false

                        {try.#Failure error}
                        (exception.match? /.name_is_too_long error)))
                    (_.coverage [/.not_ascii]
                      (when (/.name not_ascii)
                        {try.#Success actual}
                        false

                        {try.#Failure error}
                        (exception.match? /.not_ascii error)))
                    )))))

(def small
  Test
  (_.for [/.Small]
         (do [! random.monad]
           [expected (|> random.nat (at ! each (n.% /.small_limit)))
            invalid (|> random.nat (at ! each (n.max /.small_limit)))]
           (`` (all _.and
                    (_.coverage [/.small /.from_small]
                      (when (/.small expected)
                        {try.#Success actual}
                        (n.= expected
                             (/.from_small actual))

                        {try.#Failure error}
                        false))
                    (_.coverage [/.small_limit /.not_a_small_number]
                      (when (/.small invalid)
                        {try.#Success actual}
                        false

                        {try.#Failure error}
                        (exception.match? /.not_a_small_number error)))
                    )))))

(def big
  Test
  (_.for [/.Big]
         (do [! random.monad]
           [expected (|> random.nat (at ! each (n.% /.big_limit)))
            invalid (|> random.nat (at ! each (n.max /.big_limit)))]
           (`` (all _.and
                    (_.coverage [/.big /.from_big]
                      (when (/.big expected)
                        {try.#Success actual}
                        (n.= expected
                             (/.from_big actual))

                        {try.#Failure error}
                        false))
                    (_.coverage [/.big_limit /.not_a_big_number]
                      (when (/.big invalid)
                        {try.#Success actual}
                        false

                        {try.#Failure error}
                        (exception.match? /.not_a_big_number error)))
                    )))))

(def chunk_size 32)

(def entry
  Test
  (do [! random.monad]
    [expected_path (random.lower_case (-- /.path_size))
     expected_moment (at ! each (|>> (n.% 1,0,00,00,00,00,000) .int instant.of_millis)
                         random.nat)
     chunk (random.lower_case chunk_size)
     chunks (at ! each (n.% 100) random.nat)
     .let [content (|> chunk
                       (list.repeated chunks)
                       text.together
                       (at utf8.codec encoded))]]
    (`` (all _.and
             (,, (with_template [<type> <tag>]
                   [(_.coverage [<type>]
                      (|> (do try.monad
                            [expected_path (/.path expected_path)
                             tar (|> (sequence.sequence {<tag> expected_path})
                                     (\\format.result /.format)
                                     (<b>.result /.parser))]
                            (in (when (sequence.list tar)
                                  (list {<tag> actual_path})
                                  (text#= (/.from_path expected_path)
                                          (/.from_path actual_path))

                                  _
                                  false)))
                          (try.else false)))]

                   [/.Symbolic_Link /.#Symbolic_Link]
                   [/.Directory /.#Directory]
                   ))
             (_.for [/.File /.Content /.content /.data]
                    (all _.and
                         (,, (with_template [<type> <tag>]
                               [(_.coverage [<type>]
                                  (|> (do try.monad
                                        [expected_path (/.path expected_path)
                                         expected_content (/.content content)
                                         tar (|> (sequence.sequence {<tag> [expected_path
                                                                            expected_moment
                                                                            /.none
                                                                            [/.#user [/.#name /.anonymous
                                                                                      /.#id /.no_id]
                                                                             /.#group [/.#name /.anonymous
                                                                                       /.#id /.no_id]]
                                                                            expected_content]})
                                                 (\\format.result /.format)
                                                 (<b>.result /.parser))]
                                        (in (when (sequence.list tar)
                                              (list {<tag> [actual_path actual_moment actual_mode actual_ownership actual_content]})
                                              (let [seconds (is (-> Instant Int)
                                                                (|>> instant.relative (duration.ticks duration.second)))]
                                                (and (text#= (/.from_path expected_path)
                                                             (/.from_path actual_path))
                                                     (i.= (seconds expected_moment)
                                                          (seconds actual_moment))
                                                     (binary#= (/.data expected_content)
                                                               (/.data actual_content))))

                                              _
                                              false)))
                                      (try.else false)))]

                               [/.Normal /.#Normal]
                               [/.Contiguous /.#Contiguous]
                               ))))))))

(def random_mode
  (Random /.Mode)
  (do [! random.monad]
    []
    (random.either (random.either (random.either (in /.execute_by_other)
                                                 (in /.write_by_other))
                                  (random.either (in /.read_by_other)
                                                 (in /.execute_by_group)))
                   (random.either (random.either (random.either (in /.write_by_group)
                                                                (in /.read_by_group))
                                                 (random.either (in /.execute_by_owner)
                                                                (in /.write_by_owner)))
                                  (random.either (random.either (in /.read_by_owner)
                                                                (in /.save_text))
                                                 (random.either (in /.set_group_id_on_execution)
                                                                (in /.set_user_id_on_execution)))))))

(def mode
  Test
  (_.for [/.Mode /.mode]
         (do [! random.monad]
           [path (random.lower_case 10)
            modes (random.list 4 ..random_mode)
            .let [expected_mode (list#mix /.and /.none modes)]]
           (`` (all _.and
                    (_.coverage [/.and]
                      (|> (do try.monad
                            [path (/.path path)
                             content (/.content (binary.empty 0))
                             tar (|> (sequence.sequence {/.#Normal [path
                                                                    (instant.of_millis +0)
                                                                    expected_mode
                                                                    [/.#user [/.#name /.anonymous
                                                                              /.#id /.no_id]
                                                                     /.#group [/.#name /.anonymous
                                                                               /.#id /.no_id]]
                                                                    content]})
                                     (\\format.result /.format)
                                     (<b>.result /.parser))]
                            (in (when (sequence.list tar)
                                  (list {/.#Normal [_ _ actual_mode _ _]})
                                  (n.= (/.mode expected_mode)
                                       (/.mode actual_mode))

                                  _
                                  false)))
                          (try.else false)))
                    (,, (with_template [<expected_mode>]
                          [(_.coverage [<expected_mode>]
                             (|> (do try.monad
                                   [path (/.path path)
                                    content (/.content (binary.empty 0))
                                    tar (|> (sequence.sequence {/.#Normal [path
                                                                           (instant.of_millis +0)
                                                                           <expected_mode>
                                                                           [/.#user [/.#name /.anonymous
                                                                                     /.#id /.no_id]
                                                                            /.#group [/.#name /.anonymous
                                                                                      /.#id /.no_id]]
                                                                           content]})
                                            (\\format.result /.format)
                                            (<b>.result /.parser))]
                                   (in (when (sequence.list tar)
                                         (list {/.#Normal [_ _ actual_mode _ _]})
                                         (n.= (/.mode <expected_mode>)
                                              (/.mode actual_mode))

                                         _
                                         false)))
                                 (try.else false)))]

                          [/.none]

                          [/.execute_by_other]
                          [/.write_by_other]
                          [/.read_by_other]

                          [/.execute_by_group]
                          [/.write_by_group]
                          [/.read_by_group]

                          [/.execute_by_owner]
                          [/.write_by_owner]
                          [/.read_by_owner]

                          [/.save_text]
                          [/.set_group_id_on_execution]
                          [/.set_user_id_on_execution]
                          )))))))

(def ownership
  Test
  (do [! random.monad]
    [path (random.lower_case /.path_size)
     expected (random.lower_case /.name_size)
     invalid (random.lower_case (++ /.name_size))
     not_ascii (random.text (random.char (unicode.set [unicode/block.katakana (list)]))
                            /.name_size)]
    (_.for [/.Ownership /.Owner /.ID]
           (all _.and
                (_.coverage [/.name_size /.name_is_too_long]
                  (when (/.name invalid)
                    {try.#Success _}
                    false

                    {try.#Failure error}
                    (exception.match? /.name_is_too_long error)))
                (_.coverage [/.not_ascii]
                  (when (/.name not_ascii)
                    {try.#Success actual}
                    false

                    {try.#Failure error}
                    (exception.match? /.not_ascii error)))
                (_.coverage [/.Name /.name /.from_name]
                  (|> (do try.monad
                        [path (/.path path)
                         content (/.content (binary.empty 0))
                         expected (/.name expected)
                         tar (|> (sequence.sequence {/.#Normal [path
                                                                (instant.of_millis +0)
                                                                /.none
                                                                [/.#user [/.#name expected
                                                                          /.#id /.no_id]
                                                                 /.#group [/.#name /.anonymous
                                                                           /.#id /.no_id]]
                                                                content]})
                                 (\\format.result /.format)
                                 (<b>.result /.parser))]
                        (in (when (sequence.list tar)
                              (list {/.#Normal [_ _ _ actual_ownership _]})
                              (and (text#= (/.from_name expected)
                                           (/.from_name (the [/.#user /.#name] actual_ownership)))
                                   (text#= (/.from_name /.anonymous)
                                           (/.from_name (the [/.#group /.#name] actual_ownership))))
                              
                              _
                              false)))
                      (try.else false)))
                (_.coverage [/.anonymous /.no_id]
                  (|> (do try.monad
                        [path (/.path path)
                         content (/.content (binary.empty 0))
                         tar (|> (sequence.sequence {/.#Normal [path
                                                                (instant.of_millis +0)
                                                                /.none
                                                                [/.#user [/.#name /.anonymous
                                                                          /.#id /.no_id]
                                                                 /.#group [/.#name /.anonymous
                                                                           /.#id /.no_id]]
                                                                content]})
                                 (\\format.result /.format)
                                 (<b>.result /.parser))]
                        (in (when (sequence.list tar)
                              (list {/.#Normal [_ _ _ actual_ownership _]})
                              (and (text#= (/.from_name /.anonymous)
                                           (/.from_name (the [/.#user /.#name] actual_ownership)))
                                   (n.= (/.from_small /.no_id)
                                        (/.from_small (the [/.#user /.#id] actual_ownership)))
                                   (text#= (/.from_name /.anonymous)
                                           (/.from_name (the [/.#group /.#name] actual_ownership)))
                                   (n.= (/.from_small /.no_id)
                                        (/.from_small (the [/.#group /.#id] actual_ownership))))
                              
                              _
                              false)))
                      (try.else false)))
                ))))

(def .public test
  Test
  (<| (_.covering /._)
      (_.for [/.Tar])
      (do random.monad
        [_ (in [])]
        (all _.and
             (_.coverage [/.format /.parser]
               (|> sequence.empty
                   (\\format.result /.format)
                   (<b>.result /.parser)
                   (at try.monad each sequence.empty?)
                   (try.else false)))
             (_.coverage [/.invalid_end_of_archive]
               (let [dump (\\format.result /.format sequence.empty)]
                 (when (<b>.result /.parser (binary#composite dump dump))
                   {try.#Success _}
                   false
                   
                   {try.#Failure error}
                   (exception.match? /.invalid_end_of_archive error))))
             
             ..path
             ..name
             ..small
             ..big
             (_.for [/.Entry]
                    (all _.and
                         ..entry
                         ..mode
                         ..ownership
                         ))
             ))))