xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/testcases/ipc.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // A binary that uses comms and client, to receive FDs by name, communicate
16 // with them, sandboxed or not.
17 
18 #include <cstdio>
19 #include <cstdlib>
20 #include <string>
21 
22 #include "absl/strings/numbers.h"
23 #include "sandboxed_api/sandbox2/client.h"
24 #include "sandboxed_api/sandbox2/comms.h"
25 #include "sandboxed_api/util/raw_logging.h"
26 
main(int argc,char * argv[])27 int main(int argc, char* argv[]) {
28   if (argc < 2) {
29     printf("argc < 2\n");
30     return EXIT_FAILURE;
31   }
32 
33   sandbox2::Comms default_comms(sandbox2::Comms::kDefaultConnection);
34   sandbox2::Client client(&default_comms);
35 
36   int testno;
37   SAPI_RAW_CHECK(absl::SimpleAtoi(argv[1], &testno), "testno is not a number");
38   switch (testno) {
39     case 1:
40       break;
41     case 2:
42       client.SandboxMeHere();
43       break;
44     case 3:
45       // In case 3, we're running without a mapped fd. This is to test that the
46       // Client object parses the environment variable properly in that case.
47       return EXIT_SUCCESS;
48     default:
49       printf("Unknown test: %d\n", testno);
50       return EXIT_FAILURE;
51   }
52   int expected_fd = -1;
53   if (argc >= 3) {
54     SAPI_RAW_CHECK(absl::SimpleAtoi(argv[2], &expected_fd),
55                    "expected_fd is not a number");
56   }
57 
58   int fd = client.GetMappedFD("ipc_test");
59   if (expected_fd >= 0 && fd != expected_fd) {
60     fprintf(stderr, "error mapped fd not as expected, got: %d, want: %d", fd,
61             expected_fd);
62     return EXIT_FAILURE;
63   }
64   sandbox2::Comms comms(fd);
65   std::string resp;
66   if (!default_comms.SendString("start")) {
67     fputs("error on default_comms.RecvString(\"start\")", stderr);
68     return EXIT_FAILURE;
69   }
70   if (!default_comms.RecvString(&resp)) {
71     fputs("error on default_comms.RecvString(&resp)", stderr);
72     return EXIT_FAILURE;
73   }
74   if (resp != "started") {
75     fprintf(stderr, "unexpected response \"%s\" (expected \"started\")\n",
76             resp.c_str());
77     return EXIT_FAILURE;
78   }
79 
80   if (!comms.RecvString(&resp)) {
81     fputs("error on comms.RecvString(&resp)", stderr);
82     return EXIT_FAILURE;
83   }
84   if (resp != "hello") {
85     fprintf(stderr, "unexpected response \"%s\" (expected \"hello\")\n",
86             resp.c_str());
87     return EXIT_FAILURE;
88   }
89 
90   if (!comms.SendString("world")) {
91     fputs("error on comms.SendString(\"world\")", stderr);
92     return EXIT_FAILURE;
93   }
94   if (!default_comms.SendString("finish")) {
95     fputs("error on default_comms.RecvString(\"finish\")", stderr);
96     return EXIT_FAILURE;
97   }
98   if (!default_comms.RecvString(&resp)) {
99     fputs("error on default_comms.RecvString(&resp)", stderr);
100     return EXIT_FAILURE;
101   }
102   if (resp != "finished") {
103     fprintf(stderr, "unexpected response \"%s\" (expected \"finished\")\n",
104             resp.c_str());
105     return EXIT_FAILURE;
106   }
107 
108   printf("OK: All tests went OK\n");
109   return EXIT_SUCCESS;
110 }
111