Squash commits for public release

This commit is contained in:
2025-02-12 09:54:05 -05:00
commit 7118adc514
1108 changed files with 80873 additions and 0 deletions

8
test/all.sh Normal file
View File

@@ -0,0 +1,8 @@
# Run from the root dir.
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]:-$0}"; )" &> /dev/null && pwd 2> /dev/null; )";
$SCRIPT_DIR/x86.sh
$SCRIPT_DIR/x86_64.sh
$SCRIPT_DIR/arm.sh
$SCRIPT_DIR/arm64.sh
$SCRIPT_DIR/riscv64.sh

17
test/arm.sh Normal file
View File

@@ -0,0 +1,17 @@
# Run from the root dir.
gn clean out
./gn_gen.sh -y --target_arch arm --host gnu --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out
./gn_gen.sh -y --target_arch arm --host llvm --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out

17
test/arm64.sh Normal file
View File

@@ -0,0 +1,17 @@
# Run from the root dir.
gn clean out
./gn_gen.sh -y --target_arch arm64 --target_board qemu_virt --host gnu --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out
./gn_gen.sh -y --target_arch arm64 --target_board qemu_virt --host llvm --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out

17
test/bench/BUILD.gn Normal file
View File

@@ -0,0 +1,17 @@
import("//build/userland/TEMPLATE.gni")
xOS_executable("launch_server") {
signexec = true
install_path = "System/"
sources = [
"main.cpp",
"pngloader.cpp",
]
configs = [ "//build/userland:userland_flags" ]
deplibs = [
"libcxx",
"libfoundation",
"libg",
"libui",
]
}

21
test/bench/common.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <ctime>
#include <sys/time.h>
#define RUN_BENCH(name, x) for (bench_run = 0, bench_pno = bench_no, gettimeofday(&tv, &tz); bench_run < x; gettimeofday(&ttv, &tz), printf("[BENCH][%s] %d (usec)\n", name, to_usec()), fflush(stdout), bench_run++, gettimeofday(&tv, &tz))
extern int bench_pno;
extern int bench_no;
extern int bench_run;
extern timeval_t tv, ttv;
extern timezone_t tz;
static inline int to_usec()
{
int sec = ttv.tv_sec - tv.tv_sec;
int diff = ttv.tv_usec - tv.tv_usec;
return sec * 1000000 + diff;
}
void bench_pngloader();

39
test/bench/main.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include "common.h"
#include <cstdio>
#include <cstdlib>
#include <sys/wait.h>
#include <unistd.h>
char* bench_name;
int bench_pno = -1;
int bench_no = 0;
int bench_run = 0;
timeval_t tv, ttv;
timezone_t tz;
void bench_kernel()
{
RUN_BENCH("FORK", 3)
{
for (int i = 0; i < 20; i++) {
int pid = fork();
if (pid < 0) {
return;
}
if (pid) {
wait(pid);
} else {
exit(0);
}
}
}
}
int main(int argc, char** argv)
{
bench_kernel();
bench_pngloader();
printf("[BENCH END]\n\n");
fflush(stdout);
return 0;
}

13
test/bench/pngloader.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "common.h"
#include <libg/ImageLoaders/PNGLoader.h>
LG::PixelBitmap bitmap;
void bench_pngloader()
{
RUN_BENCH("PNG LOADER", 5)
{
LG::PNG::PNGLoader loader;
bitmap = loader.load_from_file("/res/wallpapers/island.png");
}
}

7
test/kernel/BUILD.gn Normal file
View File

@@ -0,0 +1,7 @@
group("test_kernel") {
deps = [
"//test/kernel/env:env",
"//test/kernel/fs:fs",
"//test/kernel/signal:signal",
]
}

8
test/kernel/env/BUILD.gn vendored Normal file
View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("env") {
test_bundle = "kernel/env"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

28
test/kernel/env/main.c vendored Normal file
View File

@@ -0,0 +1,28 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
if (getenv("OSTEST")) {
char* res = getenv("GO");
if (!res) {
TestErr("Empty GO env var.");
}
if (strncmp(res, "go", 2)) {
TestErr("Different value in GO env var.");
}
return 0;
}
if (argc > 1) {
TestErr("Loop while executing test.");
}
setenv("OSTEST", "1", 1);
setenv("GO", "go", 1);
execlp(argv[0], argv[0], "loop", NULL);
return 1;
}

9
test/kernel/fs/BUILD.gn Normal file
View File

@@ -0,0 +1,9 @@
group("fs") {
deps = [
"//test/kernel/fs/cwd:cwd",
"//test/kernel/fs/dirfile:dirfile",
"//test/kernel/fs/dup:dup",
"//test/kernel/fs/fourfiles:fourfiles",
"//test/kernel/fs/procfs:procfs",
]
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("cwd") {
test_bundle = "kernel/fs/cwd"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

25
test/kernel/fs/cwd/main.c Normal file
View File

@@ -0,0 +1,25 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char buf[256];
#define ANSWER "/test_bin/kernel$fs$cwd"
int main(int argc, char** argv)
{
int fd = 0;
fd = open("/proc/self/exe", O_RDONLY);
int rd = read(fd, buf, 256);
if (rd != strlen(ANSWER)) {
TestErr("Wrong len");
}
if (strncmp(buf, ANSWER, rd)) {
TestErr("Wrong path");
}
return 0;
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("dirfile") {
test_bundle = "kernel/fs/dirfile"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

View File

@@ -0,0 +1,39 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char buf[512];
int main(int argc, char** argv)
{
int fd;
fd = open("dirfile", 0);
if (fd >= 0) {
TestErr("opened dirfile which does not exist");
}
fd = open("dirfile", O_CREAT);
if (fd < 0) {
TestErr("cannot create dirfile");
}
if (chdir("dirfile") == 0) {
TestErr("chdir dirfile succeeded");
}
if (unlink("dirfile") != 0) {
TestErr("unlink dirfile succeeded");
}
fd = open(".", O_RDWR);
if (fd >= 0) {
TestErr("open . for writing succeeded");
}
fd = open(".", O_RDONLY);
if (write(fd, "x", 1) > 0) {
TestErr("write . succeeded");
}
close(fd);
return 0;
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("dup") {
test_bundle = "kernel/fs/dup"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

40
test/kernel/fs/dup/main.c Normal file
View File

@@ -0,0 +1,40 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char** argv)
{
int fd = -1;
fd = open("/boot/kernel.config", O_RDONLY);
if (fd < 0) {
TestErr("Can't open kernel.config");
}
int dupfd = dup(fd);
if (dupfd < 0) {
TestErr("Can't dup file");
}
int err = write(dupfd, "abcd", 5);
if (!err) {
TestErr("Succesfully write to read only file");
}
stat_t stat;
if (fstat(fd, &stat) < 0) {
TestErr("Can't read fstat of orig file");
}
stat_t newstat;
if (fstat(dupfd, &newstat) < 0) {
TestErr("Can't read fstat of dup file");
}
if (memcmp(&stat, &newstat, sizeof(stat_t)) != 0) {
TestErr("Different stat info of files");
}
return 0;
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("fourfiles") {
test_bundle = "kernel/fs/fourfiles"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

View File

@@ -0,0 +1,67 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
char buf[512];
int main(int argc, char** argv)
{
int fd, pid, i, j, n, total, pi;
char* names[] = { "f0.e", "f1.e", "f2.e", "f3.e" };
char* fname;
int pids[4];
for (pi = 0; pi < 4; pi++) {
fname = names[pi];
unlink(fname);
pid = fork();
pids[pi] = pid;
if (pid < 0) {
TestErr("fork failed");
}
if (pid == 0) {
fd = open(fname, O_CREAT | O_RDWR, 0600);
if (fd < 0) {
TestErr("create failed");
}
memset(buf, '0' + pi, 512);
for (i = 0; i < 12; i++) {
if ((n = write(fd, buf, 500)) != 500) {
TestErr("write failed");
}
}
exit(0);
}
}
for (pi = 0; pi < 4; pi++) {
wait(pids[pi]);
}
for (i = 0; i < 4; i++) {
fname = names[i];
fd = open(fname, O_RDONLY);
total = 0;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
for (j = 0; j < n; j++) {
if (buf[j] != '0' + i) {
TestErr("wrong char");
}
}
total += n;
}
close(fd);
if (total != 12 * 500) {
TestErr("wrong length");
}
unlink(fname);
}
return 0;
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("procfs") {
test_bundle = "kernel/fs/procfs"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

View File

@@ -0,0 +1,21 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv)
{
int fd = 0;
fd = open("/proc/stat", O_RDWR);
if (fd > 0) {
TestErr("Opened /proc/stat for write");
}
fd = open("/proc/stat", O_RDONLY);
if (fd <= 0) {
TestErr("Can't open /proc/stat for read");
}
return 0;
}

View File

@@ -0,0 +1,6 @@
group("signal") {
deps = [
"//test/kernel/signal/signalchild:signalchild",
"//test/kernel/signal/signalself:signalself",
]
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("signalchild") {
test_bundle = "kernel/signal/signalchild"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

View File

@@ -0,0 +1,36 @@
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
const int calls = 10;
volatile int ret = calls;
void save_on_terminate()
{
ret--;
}
int main(int argc, char** argv)
{
int pid = fork();
if (pid) {
for (int i = 0; i < 4; i++) {
// Waiting until child setup its sigaction.
sched_yield();
}
for (int i = 0; i < calls; i++) {
kill(pid, 3);
sched_yield();
}
waitpid(pid, (int*)&ret, 0);
} else {
sigaction(3, save_on_terminate);
while (ret) { }
}
return ret;
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("signalself") {
test_bundle = "kernel/signal/signalself"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

View File

@@ -0,0 +1,18 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int ret = 1;
void save_on_terminate()
{
ret = 0;
}
int main(int argc, char** argv)
{
sigaction(SIGTERM, save_on_terminate);
raise(SIGTERM);
return ret;
}

5
test/libc/BUILD.gn Normal file
View File

@@ -0,0 +1,5 @@
group("test_libc") {
deps = [
"//test/libc/stdlib:stdlib",
]
}

View File

@@ -0,0 +1,7 @@
import("//build/test/TEMPLATE.gni")
group("stdlib") {
deps = [
"//test/libc/stdlib/tools:tools",
]
}

View File

@@ -0,0 +1,8 @@
import("//build/test/TEMPLATE.gni")
xOS_test("tools") {
test_bundle = "libc/stdlib/tools"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

View File

@@ -0,0 +1,28 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int values[] = { -52, -1906, -363, -1985, -1407, 213, -25, -1712, -1008, -682, 2101, 639, 2076, 1261, -2009, -1448,
1774, 231, -209, 2038, -905, -958, 276, -687, -1789, 1837, -1872, 1243, -16, 603, 1548, -2184, 1972, -1518, 108,
-743, 328, 896, -1255, -1312, 897, 1136, 333, 1197, -984, 1453, 1564, 1565, -2208, 1282, 2408, -881, 772, -19,
-336, -1273, 1904, -1882, -1165, 1453, 1492, 971, -1758, -1570, -805, 1403, 2015, -2221, -1523, 967, -209, -1055,
2418, -344, -1907, 171, -1485, 920, 476, -1327, 204, 27, -1598, -1919, 1363, 448, 339, -1263, 404, 1552, -1699,
2118, 1515, -73, -417, -1095, 1933, 1489, 1679, 297 };
const int target_values[] = { -2221, -2208, -2184, -2009, -1985, -1919, -1907, -1906, -1882, -1872, -1789, -1758,
-1712, -1699, -1598, -1570, -1523, -1518, -1485, -1448, -1407, -1327, -1312, -1273, -1263, -1255, -1165, -1095,
-1055, -1008, -984, -958, -905, -881, -805, -743, -687, -682, -417, -363, -344, -336, -209, -209, -73, -52, -25,
-19, -16, 27, 108, 171, 204, 213, 231, 276, 297, 328, 333, 339, 404, 448, 476, 603, 639, 772, 896, 897, 920, 967,
971, 1136, 1197, 1243, 1261, 1282, 1363, 1403, 1453, 1453, 1489, 1492, 1515, 1548, 1552, 1564, 1565, 1679, 1774,
1837, 1904, 1933, 1972, 2015, 2038, 2076, 2101, 2118, 2408, 2418 };
int cmpfunc(const void* a, const void* b)
{
return (*(int*)a - *(int*)b);
}
int main()
{
qsort(values, sizeof(values) / sizeof(int), sizeof(int), cmpfunc);
return memcmp(values, target_values, sizeof(target_values));
}

17
test/riscv64.sh Normal file
View File

@@ -0,0 +1,17 @@
# Run from the root dir.
gn clean out
./gn_gen.sh -y --target_arch riscv64 --host gnu --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out
./gn_gen.sh -y --target_arch riscv64 --host llvm --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out

9
test/runner/BUILD.gn Normal file
View File

@@ -0,0 +1,9 @@
import("//build/userland/TEMPLATE.gni")
xOS_executable("launch_server") {
signexec = true
install_path = "System/"
sources = [ "main.c" ]
configs = [ "//build/userland:userland_flags" ]
deplibs = [ "libc" ]
}

84
test/runner/main.c Normal file
View File

@@ -0,0 +1,84 @@
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#define BUF_SIZE 4096
char buf[BUF_SIZE];
char namebuf[256];
struct linux_dirent {
uint32_t inode;
uint16_t rec_len;
uint8_t name_len;
uint8_t file_type;
char* name;
};
void test_failed(const char* reason)
{
printf("[FAILED] %s\n", reason);
fflush(stdout);
exit(1);
}
void test_ok(const char* name)
{
printf("[OK] %s\n", name);
fflush(stdout);
}
int main(int argc, char** argv)
{
int fd, nread;
struct linux_dirent* d;
int bpos;
setenv("PATH", "/bin:/usr/bin:/test_bin", 1);
fd = open("/test_bin", O_RDONLY | O_DIRECTORY);
if (fd < 0) {
test_failed("can't open /test_bin\n");
}
for (;;) {
nread = getdents(fd, buf, BUF_SIZE);
if (nread < 0) {
test_failed("can't read dir");
}
if (nread == 0)
break;
for (bpos = 0; bpos < nread;) {
d = (struct linux_dirent*)(buf + bpos);
if (((char*)&d->name)[0] != '.') {
int pid = fork();
if (pid) {
int out = 0;
waitpid(pid, &out, 0);
if (out != 0) {
test_failed((char*)&d->name);
} else {
test_ok((char*)&d->name);
}
} else {
setuid(10);
setgid(10);
sprintf(namebuf, "/test_bin/%s", (char*)&d->name);
execlp(namebuf, namebuf, NULL);
exit(0);
}
}
bpos += d->rec_len;
}
}
printf("[ALL TESTS PASSED]\n\n");
fflush(stdout);
return 0;
}

17
test/x86.sh Normal file
View File

@@ -0,0 +1,17 @@
# Run from the root dir.
gn clean out
./gn_gen.sh -y --target_arch x86 --host gnu --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out
./gn_gen.sh -y --target_arch x86 --host llvm --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out

17
test/x86_64.sh Normal file
View File

@@ -0,0 +1,17 @@
# Run from the root dir.
gn clean out
./gn_gen.sh -y --target_arch x86_64 --host gnu --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out
./gn_gen.sh -y --target_arch x86_64 --host llvm --test_method tests
cd out
./build.sh
./sync.sh
python3 ../utils/test/test.py
cd ..
gn clean out