NAME=write nofile error
FILE=--
ARGS=-w
CMDS=<<EOF
e io.voidwrites = false
w hello
x 32
EOF
EXPECT_ERR=<<EOF
ERROR: Cannot write. Use `omp`, `io.cache` or reopen the file in rw with `oo+`
EOF
EXPECT=<<EOF
- offset -   0 1  2 3  4 5  6 7  8 9  A B  C D  E F  0123456789ABCDEF
0x00000000  ffff ffff ffff ffff ffff ffff ffff ffff  ................
0x00000010  ffff ffff ffff ffff ffff ffff ffff ffff  ................
EOF
RUN

NAME=write
FILE=.newfile
ARGS=-w
CMDS=<<EOF
e io.va=0
w jeje
i~size[1]
p8 4
rm ./.newfile
EOF
EXPECT=<<EOF
0x4
6a656a65
EOF
RUN

NAME=write-on-ro
FILE=bins/elf/analysis/pie
ARGS=-e log.level=10 -e log.filter=core.bin
CMDS=<<EOF
e log.level=0
e log.source=true
w hello
ps
EOF
EXPECT=<<EOF
1
EOF
EXPECT_ERR=<<EOF
DEBUG: (section .dynsym) Cd 4[48] @ 0x1c0
DEBUG: (section .dynstr) Css 194 @ 0x280
DEBUG: (section .rel.plt) Cd 4[6] @ 0x3d4
DEBUG: (section .init_array) Cd 4[1] @ 0x173c
DEBUG: (section .fini_array) Cd 4[1] @ 0x1740
DEBUG: (section .dynamic) Cd 4[58] @ 0x1748
DEBUG: (section .got) Cd 4[6] @ 0x1830
DEBUG: (section .dynsym) Cd 4[48] @ 0x1c0
DEBUG: (section .dynstr) Css 194 @ 0x280
DEBUG: (section .rel.plt) Cd 4[6] @ 0x3d4
DEBUG: (section .init_array) Cd 4[1] @ 0x173c
DEBUG: (section .fini_array) Cd 4[1] @ 0x1740
DEBUG: (section .dynamic) Cd 4[58] @ 0x1748
DEBUG: (section .got) Cd 4[6] @ 0x1830
WARN: Relocs has not been applied. Please use `-e bin.relocs.apply=true` or `-e bin.cache=true` next time
DEBUG: Cannot resolve symbol address _ITM_deregisterTMCloneTable
DEBUG: Cannot resolve symbol address _ITM_deregisterTMCloneTable
DEBUG: Cannot resolve symbol address _Jv_RegisterClasses
DEBUG: Cannot resolve symbol address _Jv_RegisterClasses
DEBUG: Cannot resolve symbol address _ITM_registerTMCloneTable
DEBUG: Cannot resolve symbol address _ITM_registerTMCloneTable
EOF
RUN

NAME=write-on-ro-then-cache
FILE=bins/elf/analysis/pie
CMDS=<<EOF
e log.origin=true
w hello
ps
e io.cache=1
w hello\x00
ps
EOF
EXPECT=<<EOF
1
hello
EOF
EXPECT_ERR=<<EOF
WARN: Relocs has not been applied. Please use `-e bin.relocs.apply=true` or `-e bin.cache=true` next time
ERROR: [cmd_write_fail] Cannot write. Use `omp`, `io.cache` or reopen the file in rw with `oo+`
EOF
RUN
