Browse Source

Add a test on child executable embeding

Yann Weber 1 year ago
parent
commit
a51d69eeff
3 changed files with 73 additions and 1 deletions
  1. 16
    1
      shell.c
  2. 43
    0
      tests/tests_asm_env.c
  3. 14
    0
      tests/tests_shell.c

+ 16
- 1
shell.c View File

@@ -29,8 +29,23 @@ int asmsh_init(asmsh_t *sh, const char *child_path)
29 29
 		asmsh_env_free(sh->env);
30 30
 		goto err;
31 31
 	}
32
-	sh->child_path = strdup(child_path); // TODO check error
32
+	if(child_path)
33
+	{
34
+		if(!(sh->child_path = strdup(child_path)))
35
+		{
36
+			perror("Unable to copy child path");
37
+			goto err_all;
38
+		}
39
+	}
40
+	else
41
+	{
42
+		sh->child_path = NULL;
43
+	}
33 44
 	return 0;
45
+
46
+err_all:
47
+	asmsh_asmc_ctx_free(sh->cctx);
48
+	asmsh_env_free(sh->env);
34 49
 err:
35 50
 	bzero(sh, sizeof(asmsh_t));
36 51
 	return -1;

+ 43
- 0
tests/tests_asm_env.c View File

@@ -222,6 +222,48 @@ START_TEST(test_env_regs)
222 222
 END_TEST
223 223
 
224 224
 
225
+START_TEST(test_embed_env_exit42)
226
+{
227
+	const char childpath[] = REAL_CHILD;
228
+
229
+	asmsh_asmc_ctx_t *cctx;
230
+	asmsh_bytecode_t bcode;
231
+	int ret, status;
232
+
233
+	asmsh_env_t *env = asmsh_env(NULL);
234
+	ck_assert_ptr_nonnull(env);
235
+
236
+	cctx = asmsh_asmc_ctx_default();
237
+	ck_assert_ptr_nonnull(cctx);
238
+
239
+	ck_assert_int_ge(asmsh_asmc_compile(cctx, "mov $60, %rax", &bcode), 0);
240
+	ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
241
+	ck_assert_int_ge(asmsh_asmc_compile(cctx, "mov $42, %rdi", &bcode), 0);
242
+	ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
243
+	ck_assert_int_ge(asmsh_asmc_compile(cctx, "syscall", &bcode), 0);
244
+	ck_assert_int_ge(asmsh_env_write_code(env, &bcode), 0);
245
+
246
+	asmsh_asmc_ctx_free(cctx);
247
+
248
+	for(int i=0; i<2; i++)
249
+	{
250
+		ret = asmsh_env_step(env, &status);
251
+		ck_assert_int_eq(ret, 0);
252
+		ck_assert_msg(WCOREDUMP(status) ==  0, "SEGFAULT");
253
+		ck_assert_int_ne(WIFSTOPPED(status), 0);
254
+		ck_assert_int_eq(status>>8, 5);
255
+	}
256
+	ret = asmsh_env_step(env, &status);
257
+	ck_assert_int_eq(ret, 1);
258
+	ck_assert(WIFEXITED(status));
259
+	ck_assert_int_eq(WEXITSTATUS(status), 42);
260
+
261
+	asmsh_env_free(env);
262
+
263
+}
264
+END_TEST
265
+
266
+
225 267
 ASMSH_CHECK_START("Testing asm env", "testing asm environment")
226 268
 	ASMSH_ADD_TEST(test_env);
227 269
 	ASMSH_ADD_TEST(test_asm_stamps);
@@ -229,5 +271,6 @@ ASMSH_CHECK_START("Testing asm env", "testing asm environment")
229 271
 	ASMSH_ADD_TEST(test_env_exit42);
230 272
 	ASMSH_ADD_TEST(test_env_steps);
231 273
 	ASMSH_ADD_TEST(test_env_regs);
274
+	ASMSH_ADD_TEST(test_embed_env_exit42);
232 275
 ASMSH_CHECK_END
233 276
 

+ 14
- 0
tests/tests_shell.c View File

@@ -44,9 +44,23 @@ START_TEST(test_cmd)
44 44
 }
45 45
 END_TEST
46 46
 
47
+START_TEST(test_embed)
48
+{
49
+	asmsh_t sh;
50
+	ck_assert_int_eq(asmsh_init(&sh, NULL), 0);
51
+	ck_assert_int_eq(asmsh_exec(&sh, "mov $60, %rax"), 0);
52
+	ck_assert_int_eq(asmsh_exec(&sh, "xor %rdi, %rdi"), 0);
53
+	ck_assert_int_eq(asmsh_exec(&sh, "syscall"), 1);
54
+	asmsh_cleanup(&sh);
55
+
56
+
57
+}
58
+END_TEST
59
+
47 60
 ASMSH_CHECK_START("Testing shell", "testing shell init/exec functions")
48 61
 	ASMSH_ADD_TEST(test_init);
49 62
 	ASMSH_ADD_TEST(test_exit);
50 63
 	ASMSH_ADD_TEST(test_cmd);
64
+	ASMSH_ADD_TEST(test_embed);
51 65
 ASMSH_CHECK_END
52 66
 

Loading…
Cancel
Save