⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fstest.c

📁 Simple Operating Systems (简称SOS)是一个可以运行在X86平台上(包括QEMU
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2005 David Decotigny   This program is free software; you can redistribute it and/or   modify it under the terms of the GNU General Public License   as published by the Free Software Foundation; either version 2   of the License, or (at your option) any later version.      This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,   USA. */#include <crt.h>#include <libc.h>#include <stdarg.h>#include <string.h>#include <debug.h>#include "fstest_utils.h"/** * @file fstest.c * * File-system tests */int main(){  int fd, len;  char buff[256];  bochs_printf("Hi from fstest\n");  ls("/", 1, 1);  ls(".", 1, 1);  ls("", 1, 1);  ls(0, 1, 1);  TEST_EXPECT_CONDITION(fd = open("", 0),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open(0, 0),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("/", O_RDWR),			RETVAL == 0);  TEST_EXPECT_CONDITION(fd = open("/", O_RDONLY),			RETVAL == 1);  TEST_EXPECT_CONDITION(fd = open("/", O_WRONLY),			RETVAL == 2);  TEST_EXPECT_CONDITION(close(1),			RETVAL == 0);    TEST_EXPECT_CONDITION(fd = open("/", O_WRONLY),			RETVAL == 1);  TEST_EXPECT_CONDITION(fd = open("//", O_WRONLY),			RETVAL == 3);  TEST_EXPECT_CONDITION(fd = open("////////", O_WRONLY),			RETVAL == 4);  TEST_EXPECT_CONDITION(fd = open("/does not exist", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("////does not exist", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("/does not exist/", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("////does not exist/", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("/does not exist////", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("////does not exist/////", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("does not exist", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("does not exist/", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("does not exist////", O_WRONLY),			RETVAL);  TEST_EXPECT_CONDITION(fd = open("/does not exist/ab c d", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("////does not exist/ab c d", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("/does not exist////ab c d", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("////does not exist/////ab c d", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("does not exist", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("does not exist/ab c d", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("does not exist////ab c d", O_WRONLY),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("/", O_RDWR),			RETVAL == 5);  TEST_EXPECT_CONDITION(fd = open("/tutu.txt", O_RDWR),			RETVAL < 0);  ls("/", 1, 1);  TEST_EXPECT_CONDITION(fd = open("/tutu.txt", O_RDWR | O_CREAT,				  S_IRUSR | S_IWUSR),			RETVAL == 6);    ls("/", 1, 1);  TEST_EXPECT_CONDITION(fd = open("tutu.txt", O_RDWR | O_CREAT),			RETVAL == 7);  /* O_EXCL with an already-existing file */  TEST_EXPECT_CONDITION(fd = open("tutu.txt", O_RDWR | O_CREAT | O_EXCL),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("/toto.txt", O_RDWR | O_CREAT | O_EXCL,				  S_IRWXALL),			RETVAL == 8);  /* O_EXCL with an already-existing file */  TEST_EXPECT_CONDITION(fd = open("toto.txt", O_RDWR | O_CREAT | O_EXCL),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("toto.txt", O_RDWR | O_CREAT,				  S_IRUSR | S_IWUSR),			RETVAL == 9);  /* Trailing slash on non-dir entries */  TEST_EXPECT_CONDITION(fd = open("toto.txt/", O_RDWR), RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("notdir/", O_RDWR | O_CREAT, S_IRWXALL),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("notdir/", O_RDWR), RETVAL < 0);  /* Substring match */  TEST_EXPECT_CONDITION(fd = open("toto1.txt", O_RDWR),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("toto1.tx", O_RDWR | O_CREAT, S_IWUSR),			RETVAL == 10);  /* Substring match */  TEST_EXPECT_CONDITION(fd = open("toto.tx", O_RDWR),			RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("toto.tx", O_RDWR | O_CREAT,				  S_IRUSR | S_IWUSR),			RETVAL == 11);  /*   * read/write/seek   */  TEST_EXPECT_CONDITION(len = read(fd, buff, 256),			RETVAL == 0);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET),			RETVAL == 0);  strzcpy(buff, "Bonjour !", 256);  TEST_EXPECT_CONDITION(len = write(fd, buff, 10),			RETVAL == 10);  ls("/", 1, 1);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET),			RETVAL == 0);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256),			RETVAL == 10);  bochs_printf("read s='%s'\n", buff);  TEST_EXPECT_CONDITION(strcmp("Bonjour !", buff), RETVAL ==0);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_CUR), RETVAL == 10);  /*   * truncate   */  TEST_EXPECT_CONDITION(ftruncate(fd, 3), RETVAL == 0);    /* The current position should not have changed */  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_CUR), RETVAL == 10);  /* Make sure we cannot read anything because we get past the end of     the file */  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 0);  /* Now get back at the begining of the file */  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);  /* Make sure that we can read something with the correct first 3     characters */  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 3);  bochs_printf("read s='%s'\n", buff);  TEST_EXPECT_CONDITION(strncmp("Bon", buff, len), RETVAL == 0);  /*   * open mode   */  ls("/", 1, 1);  /* Open r/w, create read-only */  TEST_EXPECT_CONDITION(fd = open("toto2.txt", O_RDWR | O_CREAT, S_IRUSR),			RETVAL == 12);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256),			RETVAL == 0);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);  strzcpy(buff, "Permission denied", 256);  TEST_EXPECT_CONDITION(len = write(fd, buff, 10),			RETVAL < 0); /* Permission denied ! */  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 0);  /* Open read-only, create r/w */  TEST_EXPECT_CONDITION(fd = open("toto3.txt", O_RDONLY | O_CREAT,				  S_IRUSR | S_IWUSR),			RETVAL == 13);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 0);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);    strzcpy(buff, "Permission denied 2", 256);  TEST_EXPECT_CONDITION(len = write(fd, buff, 10),			RETVAL < 0); /* Permission denied ! */  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);    strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 0);  /* Create another process that chdirs in it */  if (fork() == 0)    {      bochs_printf("Hello from child\n");      TEST_EXPECT_CONDITION(fd = open("shrd.txt", O_RDWR | O_CREAT, S_IRWXALL),			    RETVAL == 14);      strzcpy(buff, "Hello from child !", 256);      TEST_EXPECT_CONDITION(len = write(fd, buff, 19),			    RETVAL == 19);      TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);      bochs_printf("Bye from child\n");      return 0;    }  bochs_printf("Father sleeping\n");  nanosleep(1, 0);  ls("/", 1, 1);  TEST_EXPECT_CONDITION(fd = open("shrd.txt", O_RDONLY),			RETVAL == 14);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256),			RETVAL == 19);  bochs_printf("read s='%s'\n", buff);  TEST_EXPECT_CONDITION(strncmp("Hello from child !", buff, len),			RETVAL == 0);  TEST_EXPECT_CONDITION(close(fd), RETVAL == 0);  TEST_EXPECT_CONDITION(unlink("shrd.txt"), RETVAL == 0);  ls("/", 1, 1);  /*   * ioctl / fcntl   */  TEST_EXPECT_CONDITION(fcntl(fd, 2, 3), RETVAL < 0); /* Not supported							 by virtfs */  ls("/", 1, 1);  /*   * creat/link/unlink/symlink   */  TEST_EXPECT_CONDITION(creat("toto4.txt", S_IRUSR), RETVAL == 0);  TEST_EXPECT_CONDITION(creat("toto4.txt", S_IRWXALL), RETVAL < 0); /*EEXIST*/  TEST_EXPECT_CONDITION(link("toto4.txt", "toto5.txt"), RETVAL == 0);  TEST_EXPECT_CONDITION(link("toto4.txt", "toto5.txt"), RETVAL < 0); /*EEXIST*/  TEST_EXPECT_CONDITION(link("toto4.txt", "toto.txt"), RETVAL < 0); /*EEXIST*/  ls("/", 1, 1);  TEST_EXPECT_CONDITION(chmod("toto5.txt", S_IRUSR | S_IWUSR), RETVAL == 0);  ls("/", 1, 1);  TEST_EXPECT_CONDITION(fd = open("toto5.txt", O_RDWR), RETVAL == 14);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 0);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);    strzcpy(buff, "Hello world from toto5", 256);  TEST_EXPECT_CONDITION(len = write(fd, buff, 24), RETVAL == 24);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);    strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 24);  bochs_printf("read s='%s'\n", buff);  TEST_EXPECT_CONDITION(fd = open("toto4.txt", O_RDWR), RETVAL == 15);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 24);  bochs_printf("read s='%s'\n", buff);  ls("/", 1, 1);  TEST_EXPECT_CONDITION(link("dangling", "toto42.txt"), RETVAL < 0); /*ENOENT*/  TEST_EXPECT_CONDITION(unlink("toto4.txt"), RETVAL == 0);  TEST_EXPECT_CONDITION(unlink("toto42.txt"), RETVAL < 0); /* ENOENT */  TEST_EXPECT_CONDITION(unlink("toto4.txt"), RETVAL < 0); /* ENOENT */  TEST_EXPECT_CONDITION(creat("toto4.txt", S_IWUSR | S_IRUSR), RETVAL == 0);  TEST_EXPECT_CONDITION(unlink("toto5.txt/"), RETVAL < 0); /* EISDIR ? */  TEST_EXPECT_CONDITION(rmdir("toto5.txt/"), RETVAL < 0); /* ENOTDIR ? */  TEST_EXPECT_CONDITION(rmdir("toto5.txt"), RETVAL < 0); /* ENOTDIR ? */  TEST_EXPECT_CONDITION(unlink("toto5.txt"), RETVAL == 0);  TEST_EXPECT_CONDITION(unlink("toto5.txt"), RETVAL < 0); /* ENOENT */  TEST_EXPECT_CONDITION(creat("toto4.txt", S_IRWXALL), RETVAL < 0); /*EEXIST*/  ls("/", 1, 1);  /* Change symlinks */  TEST_EXPECT_CONDITION(symlink("toto4.txt", "toto5.txt"), RETVAL == 0);  TEST_EXPECT_CONDITION(symlink("toto4.txt", "toto5.txt"), RETVAL < 0); /*EEXIST*/  TEST_EXPECT_CONDITION(symlink("toto4.txt", "toto.txt"), RETVAL < 0); /*EEXIST*/  ls("/", 1, 1);  TEST_EXPECT_CONDITION(fd = open("toto5.txt", O_RDWR), RETVAL == 16);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 0);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);    strzcpy(buff, "Hello world from toto5", 256);  TEST_EXPECT_CONDITION(len = write(fd, buff, 24), RETVAL == 24);  TEST_EXPECT_CONDITION(lseek(fd, 0, SEEK_SET), RETVAL == 0);    strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 24);  bochs_printf("read s='%s'\n", buff);  TEST_EXPECT_CONDITION(strcmp(buff, "Hello world from toto5"), RETVAL == 0);  TEST_EXPECT_CONDITION(fd = open("toto4.txt", O_RDWR), RETVAL == 17);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 24);  bochs_printf("read s='%s'\n", buff);  TEST_EXPECT_CONDITION(strcmp(buff, "Hello world from toto5"), RETVAL == 0);  TEST_EXPECT_CONDITION(symlink("dangling", "toto6.txt"), RETVAL == 0);  TEST_EXPECT_CONDITION(symlink("dangling", "toto6.txt"), RETVAL < 0); /*EEXIST*/  TEST_EXPECT_CONDITION(fd = open("toto6.txt", O_RDWR), RETVAL < 0);  TEST_EXPECT_CONDITION(fd = open("toto6.txt", O_RDWR | O_NOFOLLOW), RETVAL == 18);  strzcpy(buff, "Garbage garbage garbage", 256);  TEST_EXPECT_CONDITION(len = read(fd, buff, 256), RETVAL == 8);  bochs_printf("read s='%s'\n", buff);  TEST_EXPECT_CONDITION(strncmp(buff, "dangling", len), RETVAL == 0);  ls("/", 1, 1);  /* mkdir/rmdir */  TEST_EXPECT_CONDITION(mkdir("yo1", S_IRUSR | S_IXUSR), RETVAL == 0);  TEST_EXPECT_CONDITION(mkdir("yo1", S_IRWXALL), RETVAL < 0); /*EEXIST*/  ls("/", 1, 1);  TEST_EXPECT_CONDITION(unlink("yo1"), RETVAL < 0); /*EISDIR*/  TEST_EXPECT_CONDITION(unlink("yo1/"), RETVAL < 0); /*EISDIR*/  TEST_EXPECT_CONDITION(rmdir("yo1"), RETVAL == 0);  TEST_EXPECT_CONDITION(rmdir("yo1"), RETVAL < 0); /*ENOENT*/  TEST_EXPECT_CONDITION(rmdir("yoda"), RETVAL < 0); /*ENOENT*/  ls("/", 1, 1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -