Skip to content

Commit

Permalink
fix test return value
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Aug 11, 2020
1 parent da8929e commit 9eaa3dc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
36 changes: 19 additions & 17 deletions linux-loader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,47 @@ fn init_logger() {
mod tests {
use super::*;
use std::fs;
use zircon_object::object::task::*;

/// test with cmd line
async fn test(cmdline: &str) {
async fn test(cmdline: &str) -> i64 {
kernel_hal_unix::init();

let args: Vec<String> = cmdline.split(' ').map(|s| s.into()).collect();
let envs =
vec!["PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/x86_64-alpine-linux-musl/bin".into()]; // TODO
let hostfs = HostFS::new("../rootfs");
let proc = run(args, envs, hostfs);
let proc: Arc<dyn KernelObject> = proc;
proc.wait_signal(Signal::PROCESS_TERMINATED).await;
let procobj: Arc<dyn KernelObject> = proc.clone();
procobj.wait_signal(Signal::PROCESS_TERMINATED).await;
if let Status::Exited(code) = proc.status() {
return code;
}
-1
}

// test using busybox

#[async_std::test]
async fn test_busybox() {
test("/bin/busybox").await;
assert_eq!(test("/bin/busybox").await, 0);
}

#[async_std::test]
async fn test_uname() {
test("/bin/busybox uname -a").await;
assert_eq!(test("/bin/busybox uname -a").await, 0);
}

#[async_std::test]
async fn test_date() {
test("/bin/busybox date").await;
assert_eq!(test("/bin/busybox date").await, 0);
}

#[async_std::test]
async fn test_dir() {
test("/bin/busybox pwd").await;
test("/bin/busybox ls -a").await;
test("/bin/busybox dirname /bin/busybox").await;
assert_eq!(test("/bin/busybox pwd").await, 0);
assert_eq!(test("/bin/busybox ls -a").await, 0);
assert_eq!(test("/bin/busybox dirname /bin/busybox").await, 0);
}

#[async_std::test]
Expand All @@ -114,8 +119,8 @@ mod tests {

#[async_std::test]
async fn test_readfile() {
test("/bin/busybox cat /etc/profile").await;
test("/bin/busybox cat /etc/profila").await; // can't open
assert_eq!(test("/bin/busybox cat /etc/profile").await, 0);
assert_eq!(test("/bin/busybox cat /etc/profila").await, 1); // can't open
}

#[async_std::test]
Expand All @@ -140,21 +145,18 @@ mod tests {

#[async_std::test]
async fn test_env() {
test("/bin/busybox env").await;
assert_eq!(test("/bin/busybox env").await, 0);
}

// syscall unit test

#[async_std::test]
async fn test_pipe() {
test("/bin/testpipe1").await;
let string = fs::read_to_string("../rootfs/testpipe.txt").unwrap();
assert_eq!(string, String::from("hello pipe\n"));
test("/bin/busybox rm testpipe.txt").await;
assert_eq!(test("/bin/testpipe1").await, 0);
}

#[async_std::test]
async fn test_time() {
test("/bin/testtime").await;
assert_eq!(test("/bin/testtime").await, 0);
}
}
37 changes: 20 additions & 17 deletions linux-syscall/test/testpipe1.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

int main()
Expand All @@ -12,31 +13,33 @@ int main()
int cnt = 0;
int pipefd[2];
char buf;
char received[20];
int receivep = 0;
char w[12];
char r[12];
if (pipe(pipefd) == -1) {
if (pipe(pipefd) == -1)
{
printf("pipe");
exit(-1);
}
sprintf(w,"%d",pipefd[1]);
sprintf(r,"%d",pipefd[0]);
sprintf(w, "%d", pipefd[1]);
sprintf(r, "%d", pipefd[0]);
pid = vfork();
if(pid<0)
if (pid < 0)
printf("error in fork!\n");
else if(pid == 0)
{
execl("/bin/testpipe2","/bin/testpipe2",r,w,NULL);
exit(0);
else if (pid == 0)
{
execl("/bin/testpipe2", "/bin/testpipe2", r, w, NULL);
exit(0);
}
else if(pid > 0)
{
close(pipefd[1]);
int fd = open("testpipe.txt", O_WRONLY | O_CREAT);
while (read(pipefd[0], &buf, 1) > 0)
write(fd, &buf, 1);
write(fd, "\n", 1);
close(pipefd[0]);
close(fd);
else if (pid > 0)
{
close(pipefd[1]);
while (read(pipefd[0], &buf, 1) > 0)
received[receivep++] = buf;
received[receivep] = 0;
assert(strcmp(received, "hello pipe") == 0);
close(pipefd[0]);
}
return 0;
}
12 changes: 6 additions & 6 deletions linux-syscall/test/testpipe2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <string.h>

int main(int argc, char *argv[])
{
{
int writefd, readfd;
sscanf(argv[2],"%d",&writefd);
sscanf(argv[1],"%d",&readfd);
sscanf(argv[2], "%d", &writefd);
sscanf(argv[1], "%d", &readfd);
close(readfd);
write(writefd, "hello pipe", strlen("hello pipe"));
close(writefd);
exit(0);
write(writefd, "hello pipe", strlen("hello pipe"));
close(writefd);
exit(0);
}
6 changes: 6 additions & 0 deletions linux-syscall/test/testtime.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
Expand All @@ -11,26 +12,31 @@ int main(int argc, char **argv)
struct timespec ts = {0, 0};
clock_gettime(CLOCK_REALTIME, &ts);
printf("timespec: %ld sec, %ld nsec\n", ts.tv_sec, ts.tv_nsec);
assert(ts.tv_sec != 0 && ts.tv_nsec != 0);

struct timeval tv;

// the musl-libc call clock_gettime instead..qwq
gettimeofday(&tv, NULL);
printf("timeval: %ld sec, %ld usec\n", tv.tv_sec, tv.tv_usec);
assert(tv.tv_sec != 0 && tv.tv_usec != 0);

// the musl-libc call clock_gettime instead..qwq
time_t seconds;
seconds = time(NULL);
printf("time: %ld\n", seconds);
assert(seconds != 0);

struct tms tmp;
clock_t t = times(&tmp);
printf("times return: %ld\n", t);
assert(times != 0);

struct rusage usage;
getrusage(0, &usage);
printf("timeval getrusage user: %ld sec, %ld usec\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
printf("timeval getrusage system: %ld sec, %ld usec\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
assert(usage.ru_utime.tv_sec != 0 && usage.ru_utime.tv_usec != 0);

return 0;
}

0 comments on commit 9eaa3dc

Please sign in to comment.