• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2002
4  * Copyright (C) 2024 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Verify that write/read is properly working when master and slave
11  * pseudo terminals communicate with each other.
12  */
13 
14 #define _GNU_SOURCE
15 
16 #include "common.h"
17 
18 #define STRING "Linux Test Project\n"
19 
20 static size_t string_len;
21 static int masterfd = -1;
22 static int slavefd = -1;
23 
run(void)24 static void run(void)
25 {
26 	char buf[BUFSIZ];
27 
28 	tst_res(TINFO, "Send message to master and read it from slave");
29 
30 	memset(buf, 0, BUFSIZ);
31 	SAFE_WRITE(SAFE_WRITE_ALL, masterfd, STRING, string_len);
32 	SAFE_READ(0, slavefd, buf, string_len);
33 	TST_EXP_EQ_STRN(STRING, buf, string_len - 1);
34 
35 	tst_res(TINFO, "Send message to slave and read it from master");
36 
37 	memset(buf, 0, BUFSIZ);
38 	SAFE_WRITE(SAFE_WRITE_ALL, slavefd, STRING, string_len);
39 
40 	/* we need to write string_len + 1, because kernel converts newline
41 	 * into carriage return + newline
42 	 */
43 	SAFE_READ(0, masterfd, buf, string_len + 1);
44 	TST_EXP_EQ_STRN(STRING, buf, string_len - 1);
45 }
46 
setup(void)47 static void setup(void)
48 {
49 	masterfd = open_master();
50 	slavefd = open_slave(masterfd);
51 
52 	string_len = strlen(STRING);
53 }
54 
cleanup(void)55 static void cleanup(void)
56 {
57 	if (masterfd != -1)
58 		SAFE_CLOSE(masterfd);
59 
60 	if (slavefd != -1)
61 		SAFE_CLOSE(slavefd);
62 }
63 
64 static struct tst_test test = {
65 	.test_all = run,
66 	.setup = setup,
67 	.cleanup = cleanup,
68 };
69