Add (basic) tests for stat(), fgettype(), fgetmod()

This commit is contained in:
2025-07-27 14:49:24 +02:00
parent efd35b2957
commit 40583103a1
2 changed files with 116 additions and 6 deletions

View File

@@ -20,10 +20,10 @@ section .text
; Used registers: ; Used registers:
; rax* (ret) ; rax* (ret)
; rdi* (arg) Pointer to file ; rdi* (arg) Pointer to file
; rsi* (arg) Pointer to statBuffer ; rsi* arg for syscall NR_newfstatat
; rdx* arg for syscall NR_newfstatat ; rdx* arg for syscall NR_newfstatat
; r10* arg(flag) for syscall NR_newfstatat ; r10* arg(flag) for syscall NR_newfstatat
fstatat: stat:
sub rsp, SIZE_QWORD sub rsp, SIZE_QWORD
mov rax, NR_newfstatat mov rax, NR_newfstatat
mov rdx, rsi mov rdx, rsi
@@ -46,7 +46,7 @@ fgettype:
mov rax, -EINVAL mov rax, -EINVAL
ret ret
.buffok: .buffok:
mov eax, dword [rdi + ST_MODE] mov eax, [rdi + ST_MODE]
and eax, 0xF000 and eax, 0xF000
ret ret

View File

@@ -18,6 +18,8 @@
%endmacro %endmacro
;assert value, jmp instruction, testMsg_X ;assert value, jmp instruction, testMsg_X
%macro assert 3 %macro assert 3
sub rsp, SIZE_QWORD
push rax
inc r13 inc r13
mov rsi, %1 mov rsi, %1
mov rdx, %1 mov rdx, %1
@@ -37,17 +39,18 @@
lea rdi, [rel %3] lea rdi, [rel %3]
call printf call printf
%%end: %%end:
nop pop rax
add rsp, SIZE_QWORD
%endmacro %endmacro
%define assert_eq(val) assert val, je, testMsg_assertIEqual %define assert_eq(val) assert val, je, testMsg_assertIEqual
%define assert_neq(val) assert val, jne, testMsg_assertINequal %define assert_neq(val) assert val, jne, testMsg_assertINEqual
%define assert_less(val) assert val, jl, testMsg_assertILess %define assert_less(val) assert val, jl, testMsg_assertILess
%define assert_less_eq(val) assert val, jle, testMsg_assertILessE %define assert_less_eq(val) assert val, jle, testMsg_assertILessE
%define assert_more(val) assert val, jg, testMsg_assertIMore %define assert_more(val) assert val, jg, testMsg_assertIMore
%define assert_more_eq(val) assert val, jge, testMsg_assertIMoreE %define assert_more_eq(val) assert val, jge, testMsg_assertIMoreE
%define assert_u_eq(val) assert val, je, testMsg_assertUEqual %define assert_u_eq(val) assert val, je, testMsg_assertUEqual
%define assert_u_neq(val) assert val, jne, testMsg_assertUNequal %define assert_u_neq(val) assert val, jne, testMsg_assertUNEqual
%define assert_u_less(val) assert val, jb, testMsg_assertULess %define assert_u_less(val) assert val, jb, testMsg_assertULess
%define assert_u_less_eq(val) assert val, jbe, testMsg_assertULessE %define assert_u_less_eq(val) assert val, jbe, testMsg_assertULessE
%define assert_u_more(val) assert val, ja, testMsg_assertUMore %define assert_u_more(val) assert val, ja, testMsg_assertUMore
@@ -344,11 +347,45 @@ section .rodata
addTest(fwrite3, "fwrite(fp1, 'Howdy environment!') //mode 'w'") addTest(fwrite3, "fwrite(fp1, 'Howdy environment!') //mode 'w'")
addTest(fwrite4, "fwrite(fp1, ''%c%c%c%c%c%c%c%c\n'', 'H', 'e', 'l', 'l', 'o', '!', '?', '!')") addTest(fwrite4, "fwrite(fp1, ''%c%c%c%c%c%c%c%c\n'', 'H', 'e', 'l', 'l', 'o', '!', '?', '!')")
fwriteStr1 db "%c%c%c%c%c%c%c%c\n",EOS fwriteStr1 db "%c%c%c%c%c%c%c%c\n",EOS
;stat()
addTestHeader(_stat, "stat")
addTest(stat1, "stat(file1)")
statStr db "\t## Stats of file: %s ##\n"
db "\tDevice: %u\n"
db "\tInode: %u\n"
db "\tNumber of links: %u\n"
db "\tMode: %u\n"
db "\tUID: %u\n"
db "\tGID: %u\n"
db "\tDevice number: %u\n"
db "\tFile size: %d\n"
db "\tBlock size: %d\n"
db "\tBlocks: %d\n"
db "\tatime: %d.%u\n"
db "\tmtime: %d.%u\n"
db "\tctime: %d.%u\n",EOS
;fgettype()
addTestHeader(_fgettype, "fgettype")
addTest(fgettype1, "fgettype(statBuffer)")
fgettypeStr db "\t- '%s' is filetype: %s\n",EOS
ftype_dir db "Directory",EOS
ftype_char db "Character Device",EOS
ftype_blk db "Block Device",EOS
ftype_reg db "Regular File",EOS
ftype_fifo db "FIFO",EOS
ftype_link db "Symbolic Link",EOS
ftype_sock db "Socket",EOS
ftype_unknown db "Unknown (function fgettype() or stat() failed succesfully)",EOS
;fgetmod()
addTestHeader(_fgetmod, "fgetmod")
addTest(fgetmod1, "fgetmod(statBuffer)")
fgetmodStr db "\t- File permissions of file '%s': %04o\n",EOS
section .bss section .bss
strBuff1 resb 64 strBuff1 resb 64
strBuff2 resb 64 strBuff2 resb 64
fp1 resq 1 fp1 resq 1
statBuff resb STATBUFF_SIZE
section .text section .text
global _start global _start
@@ -1244,14 +1281,87 @@ _start:
;--- stat() ;--- stat()
%if TEST_stat %if TEST_stat
printTestHeader(_stat)
printTest(stat1)
lea rdi, [rel file1]
lea rsi, [rel statBuff]
call stat
assert_eq(0)
lea rdi, [rel statStr]
lea rsi, [rel file1]
mov rdx, [statBuff + ST_DEV]
mov rcx, [statBuff + ST_INO]
mov r8, [statBuff + ST_NLINK]
mov r9d, [statBuff + ST_MODE]
push qword [statBuff + ST_CTIME_NSEC]
push qword [statBuff + ST_CTIME]
push qword [statBuff + ST_MTIME_NSEC]
push qword [statBuff + ST_MTIME]
push qword [statBuff + ST_ATIME_NSEC]
push qword [statBuff + ST_ATIME]
push qword [statBuff + ST_BLOCKS]
push qword [statBuff + ST_BLKSIZE]
push qword [statBuff + ST_SIZE]
push qword [statBuff + ST_RDEV]
mov r10d, [statBuff + ST_GID]
push r10
mov r10d, [statBuff + ST_UID]
push r10
call printf
add rsp, SIZE_QWORD * 12
%endif %endif
;--- fgettype() ;--- fgettype()
%if TEST_fgettype %if TEST_fgettype
printTestHeader(_fgettype)
printTest(fgettype1)
lea rdi, [rel statBuff]
call fgettype
assert_neq(-EINVAL)
lea rdx, [rel ftype_unknown]
lea rsi, [rel ftype_dir]
cmp rax, S_IFDIR
cmove rdx, rsi
lea rsi, [rel ftype_char]
cmp rax, S_IFCHR
cmove rdx, rsi
lea rsi, [rel ftype_blk]
cmp rax, S_IFBLK
cmove rdx, rsi
lea rsi, [rel ftype_reg]
cmp rax, S_IFREG
cmove rdx, rsi
lea rsi, [rel ftype_fifo]
cmp rax, S_IFIFO
cmove rdx, rsi
lea rsi, [rel ftype_link]
cmp rax, S_IFLNK
cmove rdx, rsi
lea rsi, [rel ftype_sock]
cmp rax, S_IFSOCK
cmove rdx, rsi
lea rdi, [rel fgettypeStr]
lea rsi, [rel file1]
call printf
%endif %endif
;--- fgetmod() ;--- fgetmod()
%if TEST_fgetmod %if TEST_fgetmod
printTestHeader(_fgetmod)
printTest(fgetmod1)
lea rdi, [rel statBuff]
call fgetmod
assert_neq(-EINVAL)
lea rdi, [rel fgetmodStr]
lea rsi, [rel file1]
mov rdx, rax
call printf
%endif %endif
;;; ;;;