Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 #include <string.h> | |
| 7 | |
| 8 #include "native_client/src/include/minsfi.h" | |
| 9 #include "native_client/src/include/nacl_assert.h" | |
| 10 | |
| 11 /* | |
| 12 * This tests whether arguments are correctly passed to the sandbox. We do | |
| 13 * this by passing it a series of strings containing integer numbers. The | |
| 14 * sandbox is expected to parse the arguments and return their sum. | |
| 15 */ | |
| 16 void test_valid_args(void) { | |
| 17 char *argv_99[] = { "99" }; | |
| 18 char *argv_1_22_333[] = { "1", "22", "333" }; | |
| 19 | |
| 20 /* Prepare the sandbox. */ | |
| 21 MinsfiInitializeSandbox(); | |
| 22 | |
| 23 /* Empty arguments. The sandbox should always receive at least one argument | |
| 24 * (the name of the binary) but we test this anyway. */ | |
| 25 ASSERT_EQ(0, MinsfiInvokeSandbox(0, NULL)); | |
| 26 | |
| 27 /* Single argument. */ | |
| 28 ASSERT_EQ(99, MinsfiInvokeSandbox(1, argv_99)); | |
| 29 | |
| 30 /* Multiple arguments. */ | |
| 31 ASSERT_EQ(356, MinsfiInvokeSandbox(3, argv_1_22_333)); | |
| 32 | |
| 33 /* Clean up. */ | |
| 34 MinsfiDestroySandbox(); | |
| 35 } | |
| 36 | |
| 37 /* Helper function which creates a NULL-terminated string that occupies | |
|
jvoung (off chromium)
2014/09/10 17:04:29
/*
* xyz
*/
dbrazdil
2014/09/10 18:49:48
Done. (and below...)
| |
| 38 * the given number of bytes. */ | |
| 39 static inline char *get_str(int bytes) { | |
| 40 char *str = (char*) malloc(bytes); | |
| 41 memset(str, 49, bytes - 1); | |
| 42 str[bytes - 1] = 0; | |
| 43 return str; | |
| 44 } | |
| 45 | |
| 46 void test_invalid_args(void) { | |
| 47 int page_size = getpagesize(); | |
| 48 char *argv_long_arg[] = { get_str(32 * page_size) }; | |
|
jvoung (off chromium)
2014/09/10 17:04:30
Maybe minsfi_priv.h should have a function or some
dbrazdil
2014/09/10 18:49:47
Queries the active sandbox. Now tests invoke CopyA
| |
| 49 char *argv_longer_arg[] = { get_str(15 * page_size), | |
| 50 get_str(10 * page_size), | |
| 51 get_str(8 * page_size) }; | |
| 52 | |
| 53 /* Prepare the sandbox. */ | |
| 54 MinsfiInitializeSandbox(); | |
| 55 | |
| 56 /* argc < 0 doesn't make sense */ | |
| 57 ASSERT_EQ(EXIT_FAILURE, MinsfiInvokeSandbox(-5, NULL)); | |
| 58 | |
| 59 /* The last argument will attempt to write beyong the limits of the stack. */ | |
| 60 ASSERT_EQ(EXIT_FAILURE, MinsfiInvokeSandbox(3, argv_longer_arg)); | |
| 61 | |
| 62 /* After copying the actual argument the algorithm will also try to write | |
| 63 * the header of the info data structure, but the stack is too small. */ | |
| 64 ASSERT_EQ(EXIT_FAILURE, MinsfiInvokeSandbox(1, argv_long_arg)); | |
| 65 | |
|
jvoung (off chromium)
2014/09/10 17:04:30
How about a test where it does barely fit and exit
dbrazdil
2014/09/10 18:49:48
Done.
| |
| 66 /* Clean up. */ | |
| 67 MinsfiDestroySandbox(); | |
| 68 free(argv_long_arg[0]); | |
| 69 free(argv_longer_arg[0]); | |
| 70 free(argv_longer_arg[1]); | |
| 71 free(argv_longer_arg[2]); | |
| 72 } | |
| 73 | |
| 74 int main(void) { | |
| 75 test_valid_args(); | |
| 76 test_invalid_args(); | |
| 77 return 0; | |
| 78 } | |
| OLD | NEW |