interplane.Get Omega
Read in your language

omega-office

Omega office documents: read and write office files in Omega

Brought by drakestapleton

branch main Branches Tags History

omega-office / tests / gate.om

1534 lines · 27794 bytes Raw Blame History
1om2
2note tests/gate.om: the omega-office gate. Run from the repository root:
3note OMC < tests/gate.om > build/gate-bin && chmod +x build/gate-bin && build/gate-bin OMC
4note 1. Refuses (NOT_RUN, exit 3) unless the compiler's SHA-256 is the pin PIN below (and in the file
5note PIN); SHA-256 (FIPS 180-4) is computed here.
6note 2. Builds officeread from UNITS with that compiler (build/gate/officeread).
7note 3. Runs every case of tests/cases.txt: exit status, stdout against the oracle's bytes, and stderr
8note (empty after success, exactly one line "officeread: ..." or "ozip: ..." after a refusal).
9note 4. Builds every mutant of tests/mutants.txt (lines replaced) and requires some case to fail on it.
10note 5. Robustness (rob lines): each input cut at 64 points and changed at 256 single bytes. Every run must
11note end with exit 0 or a documented refusal code, never a trap (101..114), a signal or the time
12note limit (10 s each). An exit 0 must print the unchanged input's output, except for a plain XML file
13note (no checksum: a changed byte can be a changed character), counted as "changed-plain".
14note 6. Before step 3, builds tests/docs.om with the same compiler and runs it: publication checks of the
15note repository's own text (rules tests/docrules.txt, its own mutants tests/docmutants.txt). Its
16note output is copied here, and any exit but 0 fails the gate.
17note Exit 0 with the last line "OFFICEREADGATE PASS ...", 1 FAIL, 3 NOT_RUN. Scratch: build/gate (recreated).
18
19text PIN "6d1b1cf5a44803389a831b67f750ea4cd1c9f53edfde9d6d9241d7352887f1f8"
20text UNITS "src/head.om vendor/omega-zip/vendor/omega-mv/seed/text/text.om vendor/omega-zip/src/io.om vendor/omega-zip/src/crc32.om vendor/omega-zip/src/inflate.om vendor/omega-zip/src/unzip.om src/xml.om src/sink.om src/opc.om src/docx.om src/xlsx.om src/main.om"
21const TLIM long 10000000000
22const CLIM long 300000000000
23const GXH ulong 8388608
24
25global cs uchar 262144
26global csn ulong 1
27global mu uchar 65536
28global mun ulong 1
29global sr uchar 1048576
30global srn ulong 1
31global ub uchar 262144
32global ca uchar 65536
33global cb uchar 65536
34global rb uchar 1048576
35global gx uchar 16777216
36global pk uchar 4096
37global pkn ulong 1
38global pkc ulong 1
39global pa uchar 4096
40global pan ulong 1
41global pb uchar 4096
42global pbn ulong 1
43global om uchar 4096
44global omn ulong 1
45global fs ulong 32
46global fe ulong 32
47global nf ulong 1
48note cnt: 0..3 passing runs by oracle (expat, LibreOffice text, LibreOffice CSV, exact expected file),
49note 8..11 refusals by code range (XML 60..75, package 80..89, zip engine 2..53, usage 1); 20 mutants, 21
50note killed; 30 robustness runs, 31 refused, 32 exact, 33 changed-plain, 34 failures, 35 files,
51note 36 accepted after a change (a refused plain XML file that a change made well-formed); 64 + code: runs
52note refused with that code.
53global cnt ulong 256
54global stb uchar 8
55global wp uchar 4096
56global hk uint 64
57global hh uint 8
58global hv uint 8
59global hw uint 64
60global hbk uchar 64
61note a mutant: up to 4 replacements r: unit mru[r], old line mu[mro[4r] .. mro[4r+1]), new line
62note mu[mro[4r+2] .. mro[4r+3]), lines replaced mrh[r]; mrc replacements in all.
63global mru ulong 4
64global mro ulong 16
65global mrh ulong 4
66global mrc ulong 1
67global failn ulong 1
68
69text tnl "\n"
70text tsp " "
71text tdash "-"
72text tbuild "build"
73text tgdir "build/gate"
74text tooff "build/gate/officeread"
75text tmut "build/gate/mutant"
76text tout "build/gate/out"
77text terr "build/gate/err"
78text trob "build/gate/rob.in"
79text tref "build/gate/rob.ref"
80text tnull "/dev/null"
81text tcases "tests/cases.txt"
82text tmutf "tests/mutants.txt"
83text tdotom ".om"
84text tpre1 "officeread: "
85text tpre2 "ozip: "
86text trobw "rob"
87text txmlev "xmlev"
88text tzip "zip"
89text tfail "FAIL "
90text tnotrun "OFFICEREADGATE NOT_RUN: "
91text tbadpin "compiler sha256 is not the pin "
92text tnoomc "usage: gate OMC (run from the repository root)"
93text tcompile "build: compile failed: "
94text tio "gate: I/O error on "
95text twait "gate: fork or wait failed "
96text tshort "malformed case line "
97text tbig "gate: a file is above the comparison buffer "
98
99fn say t view uchar * to void
100r = write 1 t 0 len t
101end
102
103fn saypa to void
104r = write 1 pa 0 get pan 0
105end
106
107fn saynum v ulong to void
108b = new uchar 24 0
109x = v
110for i ulong 20
111put b sub 19 i uchar add rem x 10 48
112x = div x 10
113end
114k = ulong 0
115while lt k 19 bound 20
116if ne get b k 48
117break
118end
119k = add k 1
120end
121r = write 1 b k sub 20 k
122end
123
124fn die t view uchar * to void
125say t
126saypa
127say tnl
128exit 1
129end
130
131note ---- paths: pa and pb are built from texts and case fields ----
132fn pat t view uchar * to void
133for i ulong len t bound 4096
134put pa i get t i
135end
136put pan 0 len t
137end
138
139fn paadd t view uchar * to void
140n = get pan 0
141for i ulong len t bound 4096
142put pa add n i get t i
143end
144put pan 0 add n len t
145end
146
147fn pacs s ulong e ulong to void
148n = get pan 0
149for i ulong sub e s bound 4096
150put pa add n i get cs add s i
151end
152put pan 0 add n sub e s
153end
154
155fn pbt t view uchar * to void
156for i ulong len t bound 4096
157put pb i get t i
158end
159put pbn 0 len t
160end
161
162fn pbcs s ulong e ulong to void
163for i ulong sub e s bound 4096
164put pb i get cs add s i
165end
166put pbn 0 sub e s
167end
168
169note ---- argument packet for execprog: u16 count, then u16 length + bytes per argument ----
170fn pkreset to void
171put pkn 0 2
172put pkc 0 0
173end
174
175fn pkhdr k ulong to ulong
176n = get pkn 0
177put pk n uchar and k 255
178put pk add n 1 uchar shr k 8
179c = add get pkc 0 1
180put pkc 0 c
181put pk 0 uchar and c 255
182put pk 1 uchar shr c 8
183ret add n 2
184end
185
186fn pkt t view uchar * to void
187n = pkhdr len t
188for i ulong len t bound 4096
189put pk add n i get t i
190end
191put pkn 0 add n len t
192end
193
194fn pkpa to void
195k = get pan 0
196n = pkhdr k
197for i ulong k bound 4096
198put pk add n i get pa i
199end
200put pkn 0 add n k
201end
202
203fn pkcs s ulong e ulong to void
204n = pkhdr sub e s
205for i ulong sub e s bound 4096
206put pk add n i get cs add s i
207end
208put pkn 0 add n sub e s
209end
210
211fn pkom to void
212k = get omn 0
213n = pkhdr k
214for i ulong k bound 4096
215put pk add n i get om i
216end
217put pkn 0 add n k
218end
219
220note ---- processes ----
221fn openout to int
222fd = fopen pa 0 get pan 0 577 420
223if lt fd 0
224die tio
225end
226ret int fd
227end
228
229note reap: wait for pid at most lim ns; the status as spawn describes it.
230fn reap pid long lim long to ulong
231t0 = monons
232while true bound 100000000
233w = waitpid int pid stb 0 4 1
234if eq w pid
235s = or ulong get stb 0 shl ulong get stb 1 8
236if eq and s 127 0
237ret and shr s 8 255
238end
239ret add 1000 and s 127
240end
241if lt w 0
242die twait
243end
244if gt sub monons t0 lim
245r = killpid int pid 9
246w = waitpid int pid stb 0 4 0
247ret 2000
248end
249r = sleepns 50000
250end
251ret 2000
252end
253
254note spawn: run the packet with stdin pb (/dev/null when pbn is 0), stdout pa, stderr ep. The
255note result: exit status 0..255, 1000 + signal, or 2000 when the time limit lim (ns) was reached
256note (the child is then killed with SIGKILL and reaped).
257fn spawn ep view uchar * lim long to ulong
258fi = long -1
259if gt get pbn 0 0
260fi = fopen pb 0 get pbn 0 0 0
261else
262fi = fopen tnull 0 len tnull 0 0
263end
264if lt fi 0
265pat tnull
266die tio
267end
268fo = openout
269fx = fopen ep 0 len ep 577 420
270if lt fx 0
271die tio
272end
273pid = fork
274if lt pid 0
275die twait
276end
277if eq pid 0
278r = dupfd int fi 0 0
279r = dupfd fo 1 0
280r = dupfd int fx 2 0
281r = execprog pk 0 get pkn 0
282exit 127
283end
284r = fclose int fi
285r = fclose fo
286r = fclose int fx
287ret reap pid lim
288end
289
290note ---- files ----
291note rdinto: file pa into a big buffer (0 cs, 1 mu, 2 ub, 3 rb); the byte count.
292fn rdinto which ulong to ulong
293fd = fopen pa 0 get pan 0 0 0
294if lt fd 0
295die tio
296end
297n = ulong 0
298while true bound 1000
299r = long 0
300if eq which 0
301r = read int fd cs n sub len cs n
302end
303if eq which 1
304r = read int fd mu n sub len mu n
305end
306if eq which 2
307r = read int fd ub n sub len ub n
308end
309if eq which 3
310r = read int fd rb n sub len rb n
311end
312if lt r 0
313die tio
314end
315if eq r 0
316break
317end
318n = add n ulong r
319end
320r = fclose int fd
321ret n
322end
323
324note rdgx: file pa into gx[o .. o + GXH); the byte count (a larger file stops the gate).
325fn rdgx o ulong to ulong
326fd = fopen pa 0 get pan 0 0 0
327if lt fd 0
328die tio
329end
330n = ulong 0
331while true bound 100000
332if eq n GXH
333die tbig
334end
335r = read int fd gx add o n sub GXH n
336if lt r 0
337die tio
338end
339if eq r 0
340break
341end
342n = add n ulong r
343end
344r = fclose int fd
345ret n
346end
347
348note same: files pa and pb have equal bytes.
349fn same to bool
350fa = fopen pa 0 get pan 0 0 0
351if lt fa 0
352ret false
353end
354fb = fopen pb 0 get pbn 0 0 0
355if lt fb 0
356r = fclose int fa
357ret false
358end
359ok = true
360while true bound 1000000
361xa = read int fa ca 0 65536
362xb = read int fb cb 0 65536
363if ne xa xb
364ok = false
365break
366end
367if le xa 0
368if lt xa 0
369ok = false
370end
371break
372end
373for i ulong ulong xa
374if ne get ca i get cb i
375ok = false
376end
377end
378if not ok
379break
380end
381end
382r = fclose int fa
383r = fclose int fb
384ret ok
385end
386
387note ---- the stated normalisation for the LibreOffice oracles ----
388note gnorm: gx[o .. o+n) in place; the new length. Always: CR LF becomes LF. Text mode t also: a leading
389note UTF-8 byte order mark is removed, and spaces and tabs at the end of each line and of the file.
390fn gnorm o ulong n ulong t bool to ulong
391e = add o n
392i = o
393if and t ge n 3
394b0 = eq get gx o 0xef
395b1 = eq get gx add o 1 0xbb
396if and b0 and b1 eq get gx add o 2 0xbf
397i = add o 3
398end
399end
400w = o
401ls = o
402while lt i e bound 16777217
403c = get gx i
404i = add i 1
405if eq c 13
406if lt i e
407if eq get gx i 10
408continue
409end
410end
411end
412if and t eq c 10
413while gt w ls bound 16777217
414d = get gx sub w 1
415if and ne d 32 ne d 9
416break
417end
418w = sub w 1
419end
420put gx w c
421w = add w 1
422ls = w
423continue
424end
425put gx w c
426w = add w 1
427end
428if t
429while gt w ls bound 16777217
430d = get gx sub w 1
431if and ne d 32 ne d 9
432break
433end
434w = sub w 1
435end
436end
437ret sub w o
438end
439
440note ncmp: stdout (build/gate/out) and the oracle file pb, both normalised (t: text mode), are equal.
441fn ncmp t bool to bool
442pat tout
443na = rdgx 0
444for i ulong get pbn 0 bound 4096
445put pa i get pb i
446end
447put pan 0 get pbn 0
448nb = rdgx GXH
449na = gnorm 0 na t
450nb = gnorm GXH nb t
451if ne na nb
452ret false
453end
454for i ulong na bound 8388608
455if ne get gx i get gx add GXH i
456ret false
457end
458end
459ret true
460end
461
462note errok: the stderr file: empty after exit 0, else exactly one line starting "officeread: " or "ozip: ".
463fn errok code ulong to bool
464fd = fopen terr 0 len terr 0 0
465if lt fd 0
466ret false
467end
468r = read int fd ca 0 65536
469c = fclose int fd
470if lt r 0
471ret false
472end
473n = ulong r
474if eq code 0
475ret eq n 0
476end
477if lt n 8
478ret false
479end
480k1 = len tpre1
481p1 = ge n k1
482for i ulong k1
483if ne get ca i get tpre1 i
484p1 = false
485end
486end
487p2 = true
488for i ulong 6
489if ne get ca i get tpre2 i
490p2 = false
491end
492end
493if not or p1 p2
494ret false
495end
496for i ulong sub n 1
497if eq get ca i 10
498ret false
499end
500end
501ret eq get ca sub n 1 10
502end
503
504fn wrrob n ulong to void
505fd = fopen trob 0 len trob 577 420
506if lt fd 0
507pat trob
508die tio
509end
510if gt n 0
511r = write int fd rb 0 n
512if ne r long n
513pat trob
514die tio
515end
516end
517r = fclose int fd
518end
519
520note ---- directories ----
521note wipe: remove the tree wp[0 .. n) (directories at most 8 deep) like rm -rf; O_DIRECTORY and
522note O_NOFOLLOW (AArch64 values 16384 and 32768), so a symbolic link is removed, never followed.
523fn wipe depth ulong n ulong to void
524fd = fopen wp 0 n 49152 0
525if lt fd 0
526r = unlink wp 0 n
527ret
528end
529d = new uchar 8192 0
530while true bound 100000
531k = getdnts int fd d 0 8192
532if le k 0
533break
534end
535o = ulong 0
536while lt o ulong k bound 8192
537dent d o n depth
538rl = or ulong get d add o 16 shl ulong get d add o 17 8
539o = add o rl
540end
541end
542r = fclose int fd
543r = rmdir wp 0 n
544end
545
546note dent: one getdents64 record at d[o ..] (name at 19, type at 18) below wp[0 .. n).
547fn dent d view uchar * o ulong n ulong depth ulong to void
548m = ulong 0
549while ne get d add add o 19 m 0 bound 256
550m = add m 1
551end
552if eq m 1
553if eq get d add o 19 46
554ret
555end
556end
557if eq m 2
558if eq get d add o 19 46
559if eq get d add o 20 46
560ret
561end
562end
563end
564put wp n 47
565b = add n 1
566c = add o 19
567for j ulong m bound 256
568put wp add b j get d add c j
569end
570q = add b m
571if and eq get d add o 18 4 lt depth 8
572wipe add depth 1 q
573ret
574end
575r = unlink wp 0 q
576end
577
578fn mkd to void
579r = makedir pa 0 get pan 0 493
580end
581
582fn wipepa to void
583for i ulong get pan 0 bound 4096
584put wp i get pa i
585end
586wipe 0 get pan 0
587end
588
589note ---- SHA-256 (FIPS 180-4 section 6.2) of the compiler ----
590text KTAB "428a2f98 71374491 b5c0fbcf e9b5dba5 3956c25b 59f111f1 923f82a4 ab1c5ed5 d807aa98 12835b01 243185be 550c7dc3 72be5d74 80deb1fe 9bdc06a7 c19bf174 e49b69c1 efbe4786 0fc19dc6 240ca1cc 2de92c6f 4a7484aa 5cb0a9dc 76f988da 983e5152 a831c66d b00327c8 bf597fc7 c6e00bf3 d5a79147 06ca6351 14292967 27b70a85 2e1b2138 4d2c6dfc 53380d13 650a7354 766a0abb 81c2c92e 92722c85 a2bfe8a1 a81a664b c24b8b70 c76c51a3 d192e819 d6990624 f40e3585 106aa070 19a4c116 1e376c08 2748774c 34b0bcb5 391c0cb3 4ed8aa4a 5b9cca4f 682e6ff3 748f82ee 78a5636f 84c87814 8cc70208 90befffa a4506ceb bef9a3f7 c67178f2"
591text IVTAB "6a09e667 bb67ae85 3c6ef372 a54ff53a 510e527f 9b05688c 1f83d9ab 5be0cd19"
592
593fn hexv c uchar to uint
594if le c 57
595ret uint sub c 48
596end
597ret uint sub c 87
598end
599
600fn shainit to void
601for i ulong 64
602v = uint 0
603for j ulong 8
604v = or shl v 4 hexv get KTAB add mul i 9 j
605end
606put hk i v
607end
608for i ulong 8
609v = uint 0
610for j ulong 8
611v = or shl v 4 hexv get IVTAB add mul i 9 j
612end
613put hh i v
614end
615end
616
617fn shablk to void
618for t ulong 16
619b = mul t 4
620x = or shl uint get hbk b 24 shl uint get hbk add b 1 16
621y = or shl uint get hbk add b 2 8 uint get hbk add b 3
622put hw t or x y
623end
624for t ulong 48
625u = add t 16
626x = get hw sub u 15
627y = get hw sub u 2
628s0 = xor xor rotr x 7 rotr x 18 shr x 3
629s1 = xor xor rotr y 17 rotr y 19 shr y 10
630z = add get hw sub u 16 s0
631z = add z add get hw sub u 7 s1
632put hw u z
633end
634for i ulong 8
635put hv i get hh i
636end
637for t ulong 64
638e = get hv 4
639s1 = xor xor rotr e 6 rotr e 11 rotr e 25
640ch = xor and e get hv 5 and not e get hv 6
641t1 = add get hv 7 s1
642t1 = add t1 ch
643t1 = add t1 add get hk t get hw t
644a = get hv 0
645s0 = xor xor rotr a 2 rotr a 13 rotr a 22
646b1 = get hv 1
647c1 = get hv 2
648mj = xor xor and a b1 and a c1 and b1 c1
649put hv 7 get hv 6
650put hv 6 get hv 5
651put hv 5 e
652put hv 4 add get hv 3 t1
653put hv 3 get hv 2
654put hv 2 get hv 1
655put hv 1 a
656put hv 0 add t1 add s0 mj
657end
658for i ulong 8
659put hh i add get hh i get hv i
660end
661end
662
663fn shafile to bool
664shainit
665fd = fopen om 0 get omn 0 0 0
666if lt fd 0
667ret false
668end
669tot = ulong 0
670k = ulong 0
671while true bound 10000000
672r = read int fd ca 0 65536
673if lt r 0
674c = fclose int fd
675ret false
676end
677if eq r 0
678break
679end
680for i ulong ulong r
681put hbk k get ca i
682k = add k 1
683if eq k 64
684shablk
685k = 0
686end
687end
688tot = add tot ulong r
689end
690c = fclose int fd
691shapad k tot
692ret true
693end
694
695note shapad: the final padding (FIPS 180-4 5.1.1) after k bytes of the last block, tot bytes in all.
696fn shapad k0 ulong tot ulong to void
697k = k0
698put hbk k 128
699k = add k 1
700if gt k 56
701while lt k 64 bound 64
702put hbk k 0
703k = add k 1
704end
705shablk
706k = 0
707end
708while lt k 56 bound 64
709put hbk k 0
710k = add k 1
711end
712bl = mul tot 8
713for i ulong 8
714put hbk add 56 i uchar and shr bl mul sub 7 i 8 255
715end
716shablk
717end
718
719note pinok: the SHA-256 of the file om[0 .. omn) in lowercase hex equals PIN.
720fn pinok to bool
721if not shafile
722ret false
723end
724for i ulong 8
725v = get hh i
726for j ulong 8
727d = and shr v uint mul sub 7 j 4 15
728c = uchar add d 48
729if gt d 9
730c = uchar add d 87
731end
732if ne c get PIN add mul i 8 j
733ret false
734end
735end
736end
737ret true
738end
739
740note ---- build: UNITS concatenated (lines replaced in a mutant) and compiled ----
741note lineeq: ub[s .. e) equals the old line of replacement r.
742fn lineeq r ulong s ulong e ulong to bool
743a = get mro mul r 4
744if ne sub e s sub get mro add mul r 4 1 a
745ret false
746end
747for i ulong sub e s bound 4096
748if ne get ub add s i get mu add a i
749ret false
750end
751end
752ret true
753end
754
755note emitub: ub[s .. e) and a newline appended to sr at o; emitmu: the new line of replacement r.
756fn emitub o ulong s ulong e ulong to ulong
757for i ulong sub e s bound 65536
758put sr add o i get ub add s i
759end
760put sr add o sub e s 10
761ret add add o sub e s 1
762end
763
764fn emitmu o ulong r ulong to ulong
765c = get mro add mul r 4 2
766k = sub get mro add mul r 4 3 c
767for i ulong k bound 4096
768put sr add o i get mu add c i
769end
770put sr add o k 10
771ret add add o k 1
772end
773
774note repl: the replacement of unit u whose old line is ub[s .. e), or 99.
775fn repl u ulong s ulong e ulong to ulong
776for r ulong get mrc 0 bound 4
777if eq get mru r u
778if lineeq r s e
779ret r
780end
781end
782end
783ret 99
784end
785
786note addunit: unit file pa (index u) appended to sr, replacing the lines a replacement names.
787fn addunit u ulong to void
788n = rdinto 2
789o = get srn 0
790s = ulong 0
791while lt s n bound 262144
792e = s
793while lt e n bound 262144
794if eq get ub e 10
795break
796end
797e = add e 1
798end
799r = repl u s e
800if eq r 99
801o = emitub o s e
802else
803put mrh r add get mrh r 1
804o = emitmu o r
805end
806s = add e 1
807end
808put srn 0 o
809end
810
811fn unitpa s ulong e ulong to void
812for j ulong sub e s bound 4096
813put pa j get UNITS add s j
814end
815put pan 0 sub e s
816end
817
818note units: every unit of UNITS into sr; true when each replacement replaced exactly one line.
819fn units to bool
820put srn 0 0
821for r ulong 4
822put mrh r 0
823end
824k = ulong 0
825s = ulong 0
826n = len UNITS
827for i ulong add n 1 bound 4096
828c = uchar 32
829if lt i n
830c = get UNITS i
831end
832if eq c 32
833if gt i s
834unitpa s i
835addunit k
836k = add k 1
837end
838s = add i 1
839end
840end
841for r ulong get mrc 0 bound 4
842if ne get mrh r 1
843ret false
844end
845end
846ret true
847end
848
849note build: UNITS (with the current replacements, none when mrc = 0) into build/gate/NAME.om, compiled to
850note build/gate/NAME (the text bn).
851fn build bn view uchar * to bool
852if not units
853ret false
854end
855pat bn
856paadd tdotom
857fd = openout
858r = write fd sr 0 get srn 0
859c = fclose fd
860if ne r long get srn 0
861die tio
862end
863for i ulong get pan 0 bound 4096
864put pb i get pa i
865end
866put pbn 0 get pan 0
867pkreset
868pkom
869pat bn
870st = spawn terr CLIM
871if ne st 0
872ret false
873end
874r = chmod pa 0 get pan 0 493
875ret true
876end
877
878note ---- cases ----
879fn fields s ulong e ulong to void
880k = ulong 0
881i = s
882while lt i e bound 65536
883while lt i e bound 65536
884if ne get cs i 32
885break
886end
887i = add i 1
888end
889if ge i e
890break
891end
892if ge k 32
893break
894end
895put fs k i
896while lt i e bound 65536
897if eq get cs i 32
898break
899end
900i = add i 1
901end
902put fe k i
903k = add k 1
904end
905put nf 0 k
906end
907
908fn fis k ulong t view uchar * to bool
909s = get fs k
910n = sub get fe k s
911if ne n len t
912ret false
913end
914for i ulong n bound 4096
915if ne get cs add s i get t i
916ret false
917end
918end
919ret true
920end
921
922fn fnum k ulong to ulong
923v = ulong 0
924s = get fs k
925n = sub get fe k s
926for i ulong n bound 20
927c = ulong get cs add s i
928v = add mul v 10 sub c 48
929end
930ret v
931end
932
933note fmode: field k begins "t:" (1, LibreOffice text oracle), "c:" (2, LibreOffice CSV oracle), else 0.
934fn fmode k ulong to ulong
935s = get fs k
936if lt sub get fe k s 3
937ret 0
938end
939if ne get cs add s 1 58
940ret 0
941end
942c = get cs s
943if eq c 116
944ret 1
945end
946if eq c 99
947ret 2
948end
949ret 0
950end
951
952fn fail1 f ulong to void
953put failn 0 add get failn 0 1
954say tfail
955r = write 1 cs get fs f sub get fe f get fs f
956say tsp
957end
958
959text texit "exit "
960text twant " want "
961text tbytes "stdout differs from "
962text tstderr "stderr is not one officeread: or ozip: line"
963
964note runcase: one run line ID EXIT STDIN EXPECT ARGS... ("-" for no stdin, or for no stdout check; EXPECT
965note "t:FILE" or "c:FILE" compares after the stated normalisation). True when exit, stdout, stderr are right.
966fn runcase bin view uchar * quiet bool to bool
967want = fnum 1
968pkreset
969pkt bin
970for k ulong sub get nf 0 4 bound 28
971f = add k 4
972pkcs get fs f get fe f
973end
974put pbn 0 0
975if not fis 2 tdash
976pbcs get fs 2 get fe 2
977end
978pat tout
979st = spawn terr TLIM
980if ne st want
981if not quiet
982fail1 0
983say texit
984saynum st
985say twant
986saynum want
987say tnl
988end
989ret false
990end
991if not errok st
992if not quiet
993fail1 0
994say tstderr
995say tnl
996end
997ret false
998end
999if fis 3 tdash
1000ret true
1001end
1002m = fmode 3
1003ok = false
1004if eq m 0
1005pbcs get fs 3 get fe 3
1006pat tout
1007ok = same
1008else
1009pbcs add get fs 3 2 get fe 3
1010ok = ncmp eq m 1
1011end
1012if not ok
1013if not quiet
1014fail1 0
1015say tbytes
1016r = write 1 pb 0 get pbn 0
1017say tnl
1018end
1019ret false
1020end
1021ret true
1022end
1023
1024note ckind: the counter of a passing case: by oracle when it exits 0, else by refusal code range.
1025fn ckind to ulong
1026w = fnum 1
1027if eq w 0
1028m = fmode 3
1029if ne m 0
1030ret m
1031end
1032if fis 4 txmlev
1033ret 0
1034end
1035ret 3
1036end
1037if and ge w 60 le w 75
1038ret 8
1039end
1040if and ge w 80 le w 89
1041ret 9
1042end
1043if eq w 1
1044ret 11
1045end
1046ret 10
1047end
1048
1049note doline: the case line already split into fields; true when it passes (comments and rob lines pass
1050note here; rob lines run in robust).
1051fn doline bin view uchar * quiet bool count bool to bool
1052if eq get nf 0 0
1053ret true
1054end
1055if eq get cs get fs 0 35
1056ret true
1057end
1058if fis 0 trobw
1059ret true
1060end
1061if lt get nf 0 5
1062fail1 0
1063say tshort
1064say tnl
1065ret false
1066end
1067ok = runcase bin quiet
1068if and ok count
1069k = ckind
1070put cnt k add get cnt k 1
1071end
1072ret ok
1073end
1074
1075note runall: every case line against bin; the number of failures (stop: return at the first one, and
1076note the failing line stays in fs/fe for the report). With count the passing runs are counted.
1077fn runall bin view uchar * quiet bool stop bool count bool to ulong
1078bad = ulong 0
1079s = ulong 0
1080n = get csn 0
1081while lt s n bound 262144
1082e = s
1083while lt e n bound 262144
1084if eq get cs e 10
1085break
1086end
1087e = add e 1
1088end
1089fields s e
1090s = add e 1
1091if not doline bin quiet count
1092bad = add bad 1
1093if stop
1094ret bad
1095end
1096end
1097end
1098ret bad
1099end
1100
1101note ---- mutants: tests/mutants.txt lines ID|UNIT|OLD|NEW, UNIT the 0-based index in UNITS; consecutive
1102note lines with the same ID are one mutant (each of its lines must be found exactly once) ----
1103text tkilled "MUTANT killed "
1104text tsurv "MUTANT SURVIVED "
1105text tnobuild "MUTANT FAIL (a line not found exactly once, or no compile) "
1106text tby " by case "
1107text tnomut "OFFICEREADGATE mutants not run: the unmutated build failed cases\n"
1108
1109fn bar s ulong e ulong to ulong
1110p = s
1111while lt p e bound 65536
1112if eq get mu p 124
1113ret p
1114end
1115p = add p 1
1116end
1117ret e
1118end
1119
1120fn munum s ulong e ulong to ulong
1121u = ulong 0
1122for i ulong sub e s bound 4
1123c = ulong get mu add s i
1124u = add mul u 10 sub c 48
1125end
1126ret u
1127end
1128
1129note mreport: k 0 killed (with the case that failed), 1 survived, 2 not built; the id is mu[s .. e).
1130fn mreport k ulong s ulong e ulong to void
1131if eq k 0
1132put cnt 21 add get cnt 21 1
1133say tkilled
1134r = write 1 mu s sub e s
1135say tby
1136r = write 1 cs get fs 0 sub get fe 0 get fs 0
1137say tsp
1138r = write 1 cs get fs 1 sub get fe 1 get fs 1
1139say tnl
1140ret
1141end
1142put failn 0 add get failn 0 1
1143if eq k 1
1144say tsurv
1145else
1146say tnobuild
1147end
1148r = write 1 mu s sub e s
1149say tnl
1150end
1151
1152note mrun: build and test the mutant whose replacements are set; its id is mu[s .. e).
1153fn mrun s ulong e ulong to void
1154put cnt 20 add get cnt 20 1
1155k = ulong 2
1156if build tmut
1157k = 1
1158if gt runall tmut true true false 0
1159k = 0
1160end
1161end
1162mreport k s e
1163put mrc 0 0
1164end
1165
1166note mideq: the ids mu[a .. b) and mu[c .. d) are equal.
1167fn mideq a ulong b ulong c ulong d ulong to bool
1168if ne sub b a sub d c
1169ret false
1170end
1171for i ulong sub b a bound 4096
1172if ne get mu add a i get mu add c i
1173ret false
1174end
1175end
1176ret true
1177end
1178
1179fn mutants to void
1180pat tmutf
1181n = rdinto 1
1182put mun 0 n
1183put mrc 0 0
1184is = ulong 0
1185ie = ulong 0
1186s = ulong 0
1187while lt s n bound 65536
1188e = s
1189while lt e n bound 65536
1190if eq get mu e 10
1191break
1192end
1193e = add e 1
1194end
1195p1 = bar s e
1196p2 = bar add p1 1 e
1197p3 = bar add p2 1 e
1198if lt p3 e
1199if gt get mrc 0 0
1200if not mideq is ie s p1
1201mrun is ie
1202end
1203end
1204r = get mrc 0
1205if lt r 4
1206put mru r munum add p1 1 p2
1207put mro mul r 4 add p2 1
1208put mro add mul r 4 1 p3
1209put mro add mul r 4 2 add p3 1
1210put mro add mul r 4 3 e
1211put mrc 0 add r 1
1212end
1213is = s
1214ie = p1
1215end
1216s = add e 1
1217end
1218if gt get mrc 0 0
1219mrun is ie
1220end
1221end
1222
1223note ---- robustness: rob ID KIND FILE CMD [ARG], KIND zip or xml; runs officeread CMD build/gate/rob.in [ARG] ----
1224text tcut " cut at "
1225text tflip " flip at "
1226text tcode " exit "
1227text tbase " the unchanged input exits "
1228
1229fn allowed c ulong to bool
1230if eq c 0
1231ret true
1232end
1233if and ge c 10 le c 22
1234ret true
1235end
1236if and ge c 30 le c 32
1237ret true
1238end
1239if and ge c 40 le c 53
1240ret true
1241end
1242if and ge c 60 le c 75
1243ret true
1244end
1245ret and ge c 80 le c 89
1246end
1247
1248note robexec: run the case's command on build/gate/rob.in with stdout to pa; the status.
1249fn robexec to ulong
1250pkreset
1251pkt tooff
1252pkcs get fs 4 get fe 4
1253pkt trob
1254if gt get nf 0 5
1255pkcs get fs 5 get fe 5
1256end
1257put pbn 0 0
1258ret spawn terr TLIM
1259end
1260
1261note robrun: one changed input; base is the unchanged input's status (its stdout is build/gate/rob.ref).
1262fn robrun base ulong what view uchar * pos ulong to void
1263pat tout
1264st = robexec
1265put cnt 30 add get cnt 30 1
1266plain = not fis 2 tzip
1267bad = false
1268if not allowed st
1269bad = true
1270else
1271if eq st 0
1272same0 = false
1273if eq base 0
1274pbt tref
1275pat tout
1276same0 = same
1277end
1278if same0
1279put cnt 32 add get cnt 32 1
1280else
1281if not plain
1282bad = true
1283else
1284if eq base 0
1285put cnt 33 add get cnt 33 1
1286else
1287put cnt 36 add get cnt 36 1
1288end
1289end
1290end
1291else
1292put cnt 31 add get cnt 31 1
1293put cnt add 64 st add get cnt add 64 st 1
1294end
1295end
1296if bad
1297put cnt 34 add get cnt 34 1
1298fail1 1
1299say what
1300saynum pos
1301say tcode
1302saynum st
1303say tnl
1304end
1305end
1306
1307fn robline to void
1308put pan 0 0
1309pacs get fs 3 get fe 3
1310n = rdinto 3
1311wrrob n
1312pat tref
1313base = robexec
1314if not allowed base
1315fail1 1
1316say tbase
1317saynum base
1318say tnl
1319ret
1320end
1321for k ulong 64
1322c = div mul n k 64
1323wrrob c
1324robrun base tcut c
1325end
1326for k ulong 256
1327p = div mul n k 256
1328if lt p n
1329v = get rb p
1330m = uchar 255
1331if eq rem k 2 0
1332m = uchar shl ulong 1 rem div k 2 8
1333end
1334put rb p xor v m
1335wrrob n
1336robrun base tflip p
1337put rb p v
1338end
1339end
1340end
1341
1342fn robust to void
1343s = ulong 0
1344n = get csn 0
1345while lt s n bound 262144
1346e = s
1347while lt e n bound 262144
1348if eq get cs e 10
1349break
1350end
1351e = add e 1
1352end
1353fields s e
1354s = add e 1
1355if ge get nf 0 5
1356if fis 0 trobw
1357put cnt 35 add get cnt 35 1
1358robline
1359end
1360end
1361end
1362end
1363
1364note ---- report ----
1365text r1 "OFFICEREADGATE cases passed: expat="
1366text r2 " libreoffice-text="
1367text r3 " libreoffice-csv="
1368text r4 " exact="
1369text r5 "; refusals passed: xml="
1370text r6 " package="
1371text r7 " zip="
1372text r7b " usage="
1373text r8 "OFFICEREADGATE mutants="
1374text r9 " killed="
1375text r10 "OFFICEREADGATE robustness files="
1376text r11 " runs="
1377text r12 " refused="
1378text r13 " exact="
1379text r14 " changed-plain="
1380text r14b " accepted-after-change="
1381text r15 " failures="
1382text r16 "OFFICEREADGATE robustness exit codes:"
1383text r17 "OFFICEREADGATE PASS cases="
1384text r18 " mutants-killed="
1385text r19 " robust-runs="
1386text r20 "OFFICEREADGATE FAIL failures="
1387text tslsh "/"
1388text tcolon ":"
1389
1390fn report to int
1391say r1
1392saynum get cnt 0
1393say r2
1394saynum get cnt 1
1395say r3
1396saynum get cnt 2
1397say r4
1398saynum get cnt 3
1399say r5
1400saynum get cnt 8
1401say r6
1402saynum get cnt 9
1403say r7
1404saynum get cnt 10
1405say r7b
1406saynum get cnt 11
1407say tnl
1408say r8
1409saynum get cnt 20
1410say r9
1411saynum get cnt 21
1412say tnl
1413say r10
1414saynum get cnt 35
1415say r11
1416saynum get cnt 30
1417say r12
1418saynum get cnt 31
1419say r13
1420saynum get cnt 32
1421say r14
1422saynum get cnt 33
1423say r14b
1424saynum get cnt 36
1425say r15
1426saynum get cnt 34
1427say tnl
1428say r16
1429for c ulong 128
1430if gt get cnt add 64 c 0
1431say tsp
1432saynum c
1433say tcolon
1434saynum get cnt add 64 c
1435end
1436end
1437say tnl
1438t = add add add get cnt 0 get cnt 1 get cnt 2 get cnt 3
1439t = add t add add add get cnt 8 get cnt 9 get cnt 10 get cnt 11
1440if eq get failn 0 0
1441say r17
1442saynum t
1443say r18
1444saynum get cnt 21
1445say tslsh
1446saynum get cnt 20
1447say r19
1448saynum get cnt 30
1449say tnl
1450ret 0
1451end
1452say r20
1453saynum get failn 0
1454say tnl
1455ret 1
1456end
1457
1458text tdocsrc "tests/docs.om"
1459text tdocbin "build/gate/docs"
1460text tdocout "build/gate/docs.out"
1461text tdocfail "OFFICEREADGATE FAIL tests/docs.om (publication checks) exit "
1462
1463note docs: step 6 of the head note; a failure counts in failn.
1464fn docs to void
1465pbt tdocsrc
1466pkreset
1467pkom
1468pat tdocbin
1469st = spawn terr CLIM
1470if ne st 0
1471pat tdocbin
1472die tcompile
1473end
1474pat tdocbin
1475r = chmod pa 0 get pan 0 493
1476pkreset
1477pkpa
1478put pbn 0 0
1479pat tdocout
1480st2 = spawn terr TLIM
1481pat tdocout
1482n = rdinto 3
1483w = write 1 rb 0 n
1484if ne st2 0
1485put failn 0 add get failn 0 1
1486say tdocfail
1487saynum st2
1488say tnl
1489end
1490end
1491
1492fn main to int
1493if lt argcnt 2
1494say tnoomc
1495say tnl
1496ret 3
1497end
1498k = argread 1 om 0 4096
1499if le k 0
1500say tnoomc
1501say tnl
1502ret 3
1503end
1504put omn 0 ulong k
1505if not pinok
1506say tnotrun
1507say tbadpin
1508say PIN
1509say tnl
1510ret 3
1511end
1512pat tbuild
1513mkd
1514pat tgdir
1515wipepa
1516pat tgdir
1517mkd
1518docs
1519pat tcases
1520put csn 0 rdinto 0
1521put mrc 0 0
1522if not build tooff
1523pat tooff
1524die tcompile
1525end
1526bad = runall tooff false false true
1527if eq bad 0
1528mutants
1529else
1530say tnomut
1531end
1532robust
1533ret report
1534end