1/*
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
22#include "utils.h"
23
24TEST(pty, openpty) {
25  int master, slave;
26  char name[32];
27  struct winsize w = { 123, 456, 9999, 999 };
28  ASSERT_EQ(0, openpty(&master, &slave, name, NULL, &w));
29  ASSERT_NE(-1, master);
30  ASSERT_NE(-1, slave);
31  ASSERT_NE(master, slave);
32
33  char tty_name[32];
34  ASSERT_EQ(0, ttyname_r(slave, tty_name, sizeof(tty_name)));
35  ASSERT_STREQ(tty_name, name);
36
37  struct winsize w_actual;
38  ASSERT_EQ(0, ioctl(slave, TIOCGWINSZ, &w_actual));
39  ASSERT_EQ(w_actual.ws_row, w.ws_row);
40  ASSERT_EQ(w_actual.ws_col, w.ws_col);
41  ASSERT_EQ(w_actual.ws_xpixel, w.ws_xpixel);
42  ASSERT_EQ(w_actual.ws_ypixel, w.ws_ypixel);
43
44  close(master);
45  close(slave);
46}
47
48TEST(pty, forkpty) {
49  pid_t sid = getsid(0);
50
51  int master;
52  pid_t pid = forkpty(&master, NULL, NULL, NULL);
53  ASSERT_NE(-1, pid);
54
55  if (pid == 0) {
56    // We're the child.
57    ASSERT_NE(sid, getsid(0));
58    _exit(0);
59  }
60
61  ASSERT_EQ(sid, getsid(0));
62
63  AssertChildExited(pid, 0);
64
65  close(master);
66}
67