Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Unified Diff: tests/minsfi/test_initializer.c

Issue 539683002: MinSFI: Add loader (Closed) Base URL: https://chromium.googlesource.com/native_client/src/native_client.git@master
Patch Set: Fixed nits Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/minsfi/sandbox_dummy.c ('k') | tests/minsfi/test_memory_layout.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/minsfi/test_initializer.c
diff --git a/tests/minsfi/test_initializer.c b/tests/minsfi/test_initializer.c
new file mode 100644
index 0000000000000000000000000000000000000000..5a37c75fea518937bf6e8f47f244b25b610815d5
--- /dev/null
+++ b/tests/minsfi/test_initializer.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2014 The Native Client Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/*
+ * This exercises the public interface of MinSFI.
+ *
+ * Internally, we verify that running the Initialize function changes the
+ * active sandbox and sets the memory base for the sandboxed code. Destroying
+ * the sandbox should reset both to NULL.
+ *
+ * For the public interface, we check that the Initialize, Invoke and Destroy
+ * functions behave correctly when called in the right context, and that they
+ * do not crash the program otherwise.
+ */
+
+#include "native_client/src/include/minsfi.h"
+#include "native_client/src/include/minsfi_priv.h"
+#include "native_client/src/include/nacl_assert.h"
+
+extern uint64_t __sfi_memory_base;
+
+void helper_initialize(void) {
+ ASSERT_EQ(true, MinsfiInitializeSandbox());
+ ASSERT_NE(NULL, MinsfiGetActiveSandbox());
+ ASSERT_NE(0, __sfi_memory_base);
+ ASSERT_EQ(__sfi_memory_base, (uintptr_t) MinsfiGetActiveSandbox()->mem_base);
+}
+
+void helper_invoke_success(void) {
+ ASSERT_EQ((int) 0xCAFEBABE, MinsfiInvokeSandbox());
+}
+
+void helper_invoke_error(void) {
+ ASSERT_EQ(EXIT_FAILURE, MinsfiInvokeSandbox());
+}
+
+void helper_destroy(void) {
+ ASSERT_EQ(true, MinsfiDestroySandbox());
+ ASSERT_EQ(NULL, MinsfiGetActiveSandbox());
+ ASSERT_EQ(0, __sfi_memory_base);
+}
+
+int main(void) {
+ int i;
+
+ /* Test preconditions. There should be no active sandbox. */
+ ASSERT_EQ(NULL, MinsfiGetActiveSandbox());
+ ASSERT_EQ(0, __sfi_memory_base);
+
+ /*
+ * First, try invoking the sandbox without having initialized it.
+ * It should fail but not crash.
+ */
+ helper_invoke_error();
+
+ /* Initialize, invoke, destroy a couple of times. This should succeed. */
+ for (i = 0; i < 3; i++) {
+ helper_initialize();
+ helper_invoke_success();
+ helper_destroy();
+ }
+
+ /*
+ * Multiple initializations and invokes without destroying. This will leave
+ * the sandbox initialized.
+ */
+ for (i = 0; i < 3; i++) {
+ helper_initialize();
+ helper_invoke_success();
+ }
+
+ /* Now try destroying it multiple times. */
+ for (i = 0; i < 3; i++)
+ helper_destroy();
+
+ /* Finally, invoking the sandbox after it's been destroyed should fail. */
+ helper_invoke_error();
+}
« no previous file with comments | « tests/minsfi/sandbox_dummy.c ('k') | tests/minsfi/test_memory_layout.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698