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