소스 검색

Adds tests on shell & shell commands

Yann Weber 1 년 전
부모
커밋
6570d7f3d9
5개의 변경된 파일133개의 추가작업 그리고 2개의 파일을 삭제
  1. 4
    1
      shell.c
  2. 2
    0
      tests/asmsh_check.h
  3. 1
    1
      tests/tests_compile.c
  4. 52
    0
      tests/tests_shell.c
  5. 74
    0
      tests/tests_shell_cmds.c

+ 4
- 1
shell.c 파일 보기

@@ -85,7 +85,10 @@ int asmsh_exec(asmsh_t *sh, const char *cmd)
85 85
 			ret = _compile_step(sh, cmd);
86 86
 			break;
87 87
 	}
88
-	asmsh_env_update_regs(sh->env);
88
+	if(!ret)
89
+	{
90
+		asmsh_env_update_regs(sh->env);
91
+	}
89 92
 	return ret;
90 93
 }
91 94
 

+ 2
- 0
tests/asmsh_check.h 파일 보기

@@ -10,6 +10,8 @@
10 10
 #include <sys/types.h>
11 11
 #include <sys/stat.h>
12 12
 
13
+#define CHILD_PATH "../child"
14
+
13 15
 /**@brief Start a asmsh test
14 16
  *@param const char* suite_str test suite string
15 17
  *@param const char* tc_str test case string

+ 1
- 1
tests/tests_compile.c 파일 보기

@@ -1,4 +1,4 @@
1
-#define _GNU_SOURCE
1
+#include "config.h"
2 2
 #include <check.h>
3 3
 #include <errno.h>
4 4
 #include <stdio.h>

+ 52
- 0
tests/tests_shell.c 파일 보기

@@ -0,0 +1,52 @@
1
+#include "config.h"
2
+#include <check.h>
3
+#include <errno.h>
4
+#include <stdio.h>
5
+#include <string.h>
6
+#include <unistd.h>
7
+
8
+#include "asmsh_check.h"
9
+#include "shell.h"
10
+
11
+START_TEST(test_init)
12
+{
13
+	asmsh_t sh;
14
+	ck_assert_int_eq(asmsh_init(&sh, CHILD_PATH), 0);
15
+	asmsh_cleanup(&sh);
16
+}
17
+END_TEST
18
+
19
+START_TEST(test_exit)
20
+{
21
+	asmsh_t sh;
22
+	ck_assert_int_eq(asmsh_init(&sh, CHILD_PATH), 0);
23
+	ck_assert_int_eq(asmsh_exec(&sh, "mov $60, %rax"), 0);
24
+	ck_assert_int_eq(asmsh_exec(&sh, "xor %rdi, %rdi"), 0);
25
+	ck_assert_int_eq(asmsh_exec(&sh, "syscall"), 1);
26
+	asmsh_cleanup(&sh);
27
+
28
+	ck_assert_int_eq(asmsh_init(&sh, CHILD_PATH), 0);
29
+	ck_assert_int_eq(asmsh_exec(&sh, "mov $60, %rax"), 0);
30
+	ck_assert_int_eq(asmsh_exec(&sh, "mov $0x29, %rdi"), 0);
31
+	ck_assert_int_eq(asmsh_exec(&sh, "syscall"), 42);
32
+	asmsh_cleanup(&sh);
33
+}
34
+END_TEST
35
+
36
+START_TEST(test_cmd)
37
+{
38
+	asmsh_t sh;
39
+	ck_assert_int_eq(asmsh_init(&sh, CHILD_PATH), 0);
40
+	ck_assert_int_eq(asmsh_exec(&sh, ".reg"), 0);
41
+	ck_assert_int_eq(asmsh_exec(&sh, ".q"), 1);
42
+	asmsh_cleanup(&sh);
43
+
44
+}
45
+END_TEST
46
+
47
+ASMSH_CHECK_START("Testing shell", "testing shell init/exec functions")
48
+	ASMSH_ADD_TEST(test_init);
49
+	ASMSH_ADD_TEST(test_exit);
50
+	ASMSH_ADD_TEST(test_cmd);
51
+ASMSH_CHECK_END
52
+

+ 74
- 0
tests/tests_shell_cmds.c 파일 보기

@@ -0,0 +1,74 @@
1
+#include "config.h"
2
+
3
+#include <check.h>
4
+#include <errno.h>
5
+#include <stdio.h>
6
+#include <unistd.h>
7
+
8
+#include "asmsh_check.h"
9
+#include "shell_cmds.h"
10
+#include "logger.h"
11
+
12
+typedef struct {
13
+	const char *cmd;
14
+	const char *parsed;
15
+	const int argc;
16
+	const char **args;
17
+} parse_cmds_t;
18
+
19
+static const char *args0[]={NULL};
20
+static const char *args1[]={"hello", "world", NULL};
21
+static const char *args2[]={"mov", "$0x42,", "%rax", NULL};
22
+static const parse_cmds_t parse_cmds[] = {
23
+	{".foo",
24
+	".foo", 0, args0},
25
+	{".a.b hello world",
26
+	 ".a.b", 2, args1},
27
+	{".b mov $0x42, %rax",
28
+	 ".b", 3, args2},
29
+};
30
+START_TEST(parse)
31
+{
32
+	asmsh_logger_t *log =  asmsh_logger_new(ASMSH_TRACE);
33
+	asmsh_logger_setup(log);
34
+	const parse_cmds_t *cmd = &parse_cmds[_i];
35
+	asmsh_cmd_args_t *args = asmsh_cmd_parse(cmd->cmd);
36
+	asmsh_logger_dprint(2, _default_logger);
37
+	ck_assert_ptr_nonnull(args);
38
+	ck_assert_str_eq(args->cmd, cmd->parsed);
39
+	ck_assert_int_eq(args->argc, cmd->argc);
40
+	for(int i=0; i<args->argc; i++)
41
+	{
42
+		ck_assert_str_eq(args->args[i], cmd->args[i]);
43
+	}
44
+	ck_assert_ptr_null(args->args[args->argc]);
45
+	asmsh_cmd_args_free(args);
46
+}
47
+END_TEST
48
+
49
+
50
+START_TEST(test_cmds)
51
+{
52
+	asmsh_t sh;
53
+	ck_assert_int_eq(asmsh_init(&sh, CHILD_PATH), 0);
54
+	ck_assert_int_eq(asmsh_exec(&sh, ".h"), 0);
55
+	ck_assert_int_eq(asmsh_exec(&sh, ".help"), 0);
56
+	ck_assert_int_eq(asmsh_exec(&sh, ".b"), -1);
57
+	ck_assert_int_eq(asmsh_exec(&sh, ".bytecode syscall"), 0);
58
+	ck_assert_int_eq(asmsh_exec(&sh, ".bytecode mov $42, %rax"), 0);
59
+	ck_assert_int_eq(asmsh_exec(&sh, "push %rax"), 0);
60
+	ck_assert_int_eq(asmsh_exec(&sh, ".b"), 0);
61
+	ck_assert_int_eq(asmsh_exec(&sh, ".flags"), 0);
62
+	ck_assert_int_eq(asmsh_exec(&sh, ".reset"), 0);
63
+	ck_assert_int_eq(asmsh_exec(&sh, ".b"), -1);
64
+	ck_assert_int_eq(asmsh_exec(&sh, ".xxdsdfsdfsdfsdf"), -1);
65
+	ck_assert_int_eq(asmsh_exec(&sh, ".q"), 1);
66
+	asmsh_cleanup(&sh);
67
+
68
+}
69
+END_TEST
70
+
71
+ASMSH_CHECK_START("shell commands tests", "testing shell commands")
72
+	ASMSH_ADD_LOOP(parse, parse_cmds);
73
+	ASMSH_ADD_TEST(test_cmds);
74
+ASMSH_CHECK_END

Loading…
취소
저장