blob: 7fe97e9af9215a4649ea0f28d9354d69e1fc9a72 [file] [log] [blame]
Elliott Hughes65f0df72014-12-03 14:39:20 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <pty.h>
20#include <sys/ioctl.h>
21
22TEST(pty, openpty) {
23 int master, slave;
24 char name[32];
25 struct winsize w = { 123, 456, 9999, 999 };
26 ASSERT_EQ(0, openpty(&master, &slave, name, NULL, &w));
27 ASSERT_NE(-1, master);
28 ASSERT_NE(-1, slave);
29 ASSERT_NE(master, slave);
30
31 char tty_name[32];
32 ASSERT_EQ(0, ttyname_r(slave, tty_name, sizeof(tty_name)));
33 ASSERT_STREQ(tty_name, name);
34
35 struct winsize w_actual;
36 ASSERT_EQ(0, ioctl(slave, TIOCGWINSZ, &w_actual));
37 ASSERT_EQ(w_actual.ws_row, w.ws_row);
38 ASSERT_EQ(w_actual.ws_col, w.ws_col);
39 ASSERT_EQ(w_actual.ws_xpixel, w.ws_xpixel);
40 ASSERT_EQ(w_actual.ws_ypixel, w.ws_ypixel);
41
42 close(master);
43 close(slave);
44}
45
46TEST(pty, forkpty) {
47 pid_t sid = getsid(0);
48
49 int master;
50 pid_t pid = forkpty(&master, NULL, NULL, NULL);
51 ASSERT_NE(-1, pid);
52
53 if (pid == 0) {
54 // We're the child.
55 ASSERT_NE(sid, getsid(0));
56 _exit(0);
57 }
58
59 ASSERT_EQ(sid, getsid(0));
60
61 int status;
62 ASSERT_EQ(pid, waitpid(pid, &status, 0));
63 ASSERT_TRUE(WIFEXITED(status));
64 ASSERT_EQ(0, WEXITSTATUS(status));
65
66 close(master);
67}