NAME='Import project'
FILE=malloc://512
CMDS=<<EXPECT
e dir.projects = ../bins/other/projects
Po test > /dev/null
C*
EXPECT=<<RUN
CCu base64:U29tZSB0ZXN0 @ 0x00404890
RUN

NAME='Import project with overwriting'
FILE=malloc://512
CMDS=<<EXPECT
e asm.bits=64
e asm.arch = x86
e anal.arch = x86
e dir.projects = ../bin/
CC Some test @ 0x00404890
Po import2 > /dev/null
CC*
EXPECT=<<RUN
CCu base64:U29tZSB0ZXN0 @ 0x00404890
RUN

NAME='Import project with overwriting'
FILE=malloc://512
CMDS=<<EXPECT
t-*
e asm.arch = x86
e anal.arch = x86
e dir.projects = .tmp/
e asm.bits=64
wx 31ed4989d15e4889e24883e4f0505449c7c09024410048c7c12024410048c7c7a0284000e857dcfffff4
af test
afF @ test
pdf
Ps testw > /dev/null
Pc testw~afF
rm .tmp/testw
EXPECT=<<RUN
/ (fcn) test
\           0x00000000 (42 byte folded function)
afF @ 0x00000000
RUN

NAME='Export project'
FILE=malloc://512
CMDS=<<EXPECT
e dir.projects = .tmp/
CC "Some other test" @ 0x00404890
Ps testexp > /dev/null
cat .tmp/testexp/rc | grep base64
rmdir -p .tmp/testexp
EXPECT=<<RUN
"e bin.debase64 = false"
CCu base64:IlNvbWUgb3RoZXIgdGVzdCI= @ 0x00404890
RUN

NAME='Set, export, unset and import'
FILE=malloc://512
CMDS=<<EXPECT
e dir.projects = .tmp/
t-*
CC "First comment" @ 0x00404890
CC "Second comment" @ 0x00404800
Ps testset > /dev/null
C- @ 0x00404890
C- @ 0x00404800
Po project.r2
cat .tmp/testset/rc | grep CC
rmdir -p .tmp/testset
EXPECT=<<RUN
CCu base64:IlNlY29uZCBjb21tZW50Ig== @ 0x00404800
CCu base64:IkZpcnN0IGNvbW1lbnQi @ 0x00404890
RUN

NAME='xrefs'
FILE=malloc://512
CMDS=<<EXPECT
ax 1
e dir.projects = .tmp/
axq
Ps xrefs > /dev/null
axq
EXPECT=<<RUN
0x00000000 -> 0x00000001  UNKNOWN
0x00000000 -> 0x00000001  UNKNOWN
RUN

NAME='Import mapped project'
FILE=malloc://512
CMDS=<<EXPECT
e dir.projects = ../bins/other/projects/
Po resetc64 > /dev/null
pu 3 @0xc000
EXPECT=<<RUN
%4c%e2%fc
RUN

NAME='asm.emu in project'
FILE=../bins/elf/analysis/simpleARM.elf
CMDS=<<EXPECT
e asm.flags = false
e asm.emu = true
e dir.projects = .tmp/
Ps emu > /dev/null
Po emu
CC-
om `o~[0]` 0
pd 1
rm .tmp/emu
EXPECT=<<RUN
            0x08000060      0d20a0e3       mov r2, 0xd                 ; r2=0xd -> 0x2000000
RUN

NAME='Variables in project file'
FILE=../bins/efi/bootia32.efi
CMDS=<<EXPECT
e dir.projects = .tmp/
af
afvr edi blahblah typeR @ 0x10002d8d
afvb 8 ImageHandle blah @ 0x10002d8d
Ps var > /dev/null
Pc var~ImageHandle
Pc var~blahblah
rm .tmp/var
EXPECT=<<RUN
afvb 8 ImageHandle blah @ 0x10002d8d
afvr edi blahblah typeR @ 0x10002d8d
RUN


NAME='Types and link in project file'
FILE=-
CMDS=<<EXPECT
e dir.projects = .tmp/
tk zoby  =  type
tl char @ 0x0
Ps types > /dev/null
Pc types~?link.,symlink,unlink
Pc types~?zoby
EXPECT=<<RUN
10
1
RUN

NAME='Breakpoints in project file'
FILE=../bins/elf/analysis/main
BROKEN=1
CMDS=<<EXPECT
e dbg.bpinmaps = false
e dir.projects = .tmp/
db 0x1234567
Ps breakpoints > /dev/null
Pc breakpoints~?0x1234567
EXPECT=<<RUN
1
RUN

NAME='Visual Marks in project file'
FILE=../bins/elf/analysis/main
CMDS=<<EXPECT
fV 49 0x400410
fV 50 0x400412
Ps vmarks > /dev/null
Pc vmarks~fV
Pd vmarks > /dev/null
EXPECT=<<RUN
fV 49 0x400410
fV 50 0x400412
RUN

NAME='Custom types in project file'
BROKEN=1
FILE=../bins/elf/analysis/main
CMDS=<<EXPECT
tk char **=type
tk type.char **=**z
tk type.char **.size=64
Ps custom_types > /dev/null
Pc custom_types~?char **
"td struct Foo { int bar; int x; int y; };"
Ps custom_types > /dev/null
Pc custom_types~?char **
Pc custom_types~?Foo
Pd custom_types > /dev/null
EXPECT=<<RUN
9
9
8
RUN

NAME='Export custom types and import again'
FILE=../bins/elf/analysis/main
BROKEN=1
CMDS=<<EXPECT
tk char **=type
tk type.char **=**z
tk type.char **.size=64
"td struct Foo { int bar; int x; int y; };"
Ps custom_types > /dev/null
t- char **
t- Foo
tk~?char **
tk~?Foo
Po custom_types > /dev/null
tk~?char **
tk~?Foo
Pd custom_types > /dev/null
EXPECT=<<RUN
5
0
7
5
RUN

NAME='Save project for a binary and checking opened files'
FILE='../bins/elf/analysis/main'
BROKEN=1
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
Po blah > /dev/null
o*~?
EXPECT=<<RUN
1
RUN


NAME='Save project for a binary without saving its info'
FILE='../bins/elf/analysis/main'
ARGS='-n'
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
Po blah > /dev/null
i~format
EXPECT=<<RUN
RUN

NAME='Save project for a binary after analysis'
FILE='../bins/elf/analysis/main'
ARGS=''
CMDS=<<EXPECT
e dir.projects = .tmp/
aaa
Ps blah
  # Following command hangs:
  # Po blah
y
EXPECT=<<RUN
RUN

NAME='Save project for a binary without saving its info at a specific map offset'
FILE='../bins/elf/analysis/main'
BROKEN=1
ARGS='-n -m 0x1337'
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
Po blah > /dev/null
om*~?
om*~[2]
EXPECT=<<RUN
1
0x00001337
RUN

NAME='Delete a saved project (with Pd)'
FILE='../bins/elf/analysis/main'
ARGS='-n'
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
Pd blah > /dev/null
Pl~blah

EXPECT=<<RUN
RUN

NAME='Delete a saved project (with P-)'
FILE='../bins/elf/analysis/main'
ARGS='-n'
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
P- blah > /dev/null
Pl~blah
EXPECT=<<RUN
RUN

NAME='Delete a saved project and used directory (with Pd)'
FILE='../bins/elf/analysis/main'
ARGS='-n'
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
Pd blah > /dev/null
Pl~blah
ls -l `e dir.projects`~blah
EXPECT=<<RUN
RUN


NAME='Delete a saved project and used directory (with P-)'
FILE='../bins/elf/analysis/main'
ARGS='-n'
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
P- blah > /dev/null
Pl~blah
ls -l `e dir.projects`~blah
EXPECT=<<RUN
RUN

NAME='Save project for a file with asm.bits=32 and check the value after loading again'
FILE='../bins/src/hello.c'
BROKEN=1
ARGS=''
CMDS=<<EXPECT
e dir.projects = .tmp/
e asm.bits=16
Ps blah > /dev/null
Po blah > /dev/null
e asm.bits
EXPECT=<<RUN
16
RUN

NAME='prj.name saved in project'
FILE=malloc://512
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps named_prj > /dev/null
e prj.name=
Po named_prj > /dev/null
e prj.name
rm .tmp/named_prj
EXPECT=<<RUN
named_prj
RUN

NAME='Checking e prj.name after saving again'
BROKEN=1
FILE='../bins/elf/analysis/main'
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps blah > /dev/null
Po blah > /dev/null
Ps blah > /dev/null
Po blah > /dev/null
e prj.name
EXPECT=<<RUN
blah
RUN

NAME='Restoring xrefs'
FILE=malloc://512
CMDS=<<EXPECT
e asm.arch = x86
e anal.arch = x86
e asm.bits=32
e dir.projects = .tmp/
wx 5031c0e801000000c3b801000000c3
af
Ps blah > /dev/null
Po blah > /dev/null
axq
EXPECT=<<RUN
0x00000003 -> 0x00000009  CALL
RUN

NAME='Reopening projects bin info loading'
FILE=../bins/elf/analysis/main
BROKEN=1
CMDS=<<EXPECT
e dir.projects = .tmp/
Ps bininfo > /dev/null
Po bininfo
i~?
ie~?
ii~?
EXPECT=<<RUN
37
3
5
RUN
