Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / syscalls / getpid_test.cc
1 /*
2  * Copyright 2010 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
7
8 /*
9  * Test for getpid syscall.
10  */
11
12 #ifdef USE_RAW_SYSCALLS
13 #include "native_client/src/untrusted/nacl/syscall_bindings_trampoline.h"
14 #endif
15 #include "native_client/tests/syscalls/test.h"  //NOLINT
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #ifdef USE_RAW_SYSCALLS
23 #define GETPID NACL_SYSCALL(getpid)
24 #else
25 #define GETPID getpid
26 #endif
27
28 bool TestGetPid() {
29   bool test_status;
30   const char *testname = "getpid_test";
31   pid_t pid_one = GETPID();
32   pid_t pid_two = GETPID();
33
34   // check if it's greater than 0.
35   if ((pid_one > 0) && (pid_one == pid_two)) {
36     test_status =
37         test::Passed(testname,
38                      "getpid returned what appears to be a valid pid.");
39   } else if (pid_one == pid_two) {
40     // a buffer size that should be sufficient
41     char buffer[256];
42     snprintf(buffer,
43              255,
44              "getpid returned an invalid pid for this test: %d",
45              pid_one);
46     buffer[255] = 0;
47     test_status = test::Failed(testname, buffer, __FILE__, __LINE__);
48   } else {
49     char buffer[256];
50     snprintf(buffer,
51              255,
52              "getpid returned different pids for the same process.  First "
53              "time it was called: %d Second time: %d",
54              pid_one,
55              pid_two);
56     buffer[255] = 0;
57     test_status = test::Failed(testname, buffer, __FILE__, __LINE__);
58   }
59   return test_status;
60 }
61
62 /*
63  * function testSuite()
64  *
65  *   Run through a complete sequence of file tests.
66  *
67  * returns true if all tests succeed.  false if one or more fail.
68  */
69 bool TestSuite() {
70   bool ret = true;
71   ret &= TestGetPid();
72   return ret;
73 }
74
75 /*
76  * Main entry point.
77  *
78  * Runs all tests and calls system exit with appropriate value
79  *   0 - success, all tests passed.
80  *  -1 - one or more tests failed.
81  */
82 int main(const int argc, const char *argv[]) {
83   bool passed;
84
85   // run the full test suite
86   passed = TestSuite();
87
88   if (passed) {
89     printf("All tests PASSED\n");
90     exit(0);
91   } else {
92     printf("One or more tests FAILED\n");
93     exit(-1);
94   }
95 }