1 /*
2  * Copyright 2018 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 <audio_utils/FdToString.h>
18 
19 #include <signal.h>
20 #include <chrono>
21 
22 #include <gtest/gtest.h>
23 
24 using namespace android::audio_utils;
25 
TEST(audio_utils_fdtostring,basic)26 TEST(audio_utils_fdtostring, basic) {
27     signal(SIGPIPE, SIG_IGN);
28     const std::string PREFIX{"aa "};
29     const std::string TEST_STRING{"hello world\n"};
30 
31     auto writer_opt = FdToString::createWriter(PREFIX);
32     ASSERT_TRUE(writer_opt.has_value());
33     FdToString::Writer& writer = *writer_opt;
34     const int fd = writer.borrowFdUnsafe();
35     ASSERT_TRUE(fd >= 0);
36 
37     write(fd, TEST_STRING.c_str(), TEST_STRING.size());
38 
39     const std::string result = FdToString::closeWriterAndGetString(std::move(writer));
40     ASSERT_EQ((PREFIX + TEST_STRING), result);
41 }
42 
TEST(audio_utils_fdtostring,multiline)43 TEST(audio_utils_fdtostring, multiline) {
44     signal(SIGPIPE, SIG_IGN);
45     const std::string PREFIX{"aa "};
46     const std::string INPUT[] = {"hello\n", "pt1", "pt2 ", "\n", "\n", "pt3\n", "pt4"};
47     const std::string GOLDEN = "aa hello\naa pt1pt2 \naa \naa pt3\npt4";
48 
49     auto writer_opt = FdToString::createWriter(PREFIX);
50     ASSERT_TRUE(writer_opt.has_value());
51     FdToString::Writer& writer = *writer_opt;
52     const int fd = writer.borrowFdUnsafe();
53     ASSERT_TRUE(fd >= 0);
54 
55     for (const auto& str : INPUT) {
56         write(fd, str.c_str(), str.size());
57     }
58 
59     ASSERT_EQ(FdToString::closeWriterAndGetString(std::move(writer)), GOLDEN);
60 }
61 
TEST(audio_utils_fdtostring,blocking)62 TEST(audio_utils_fdtostring, blocking) {
63     signal(SIGPIPE, SIG_IGN);
64     const std::string PREFIX{"- "};
65     const std::string INPUT[] = {"1\n", "2\n", "3\n", "4\n", "5\n"};
66     const std::string GOLDEN = "- 1\n- 2\n- 3\n- 4\n- 5\n";
67 
68     auto writer_opt = FdToString::createWriter(PREFIX, std::chrono::milliseconds{200});
69 
70     ASSERT_TRUE(writer_opt.has_value());
71     FdToString::Writer& writer = *writer_opt;
72     const int fd = writer.borrowFdUnsafe();
73     ASSERT_TRUE(fd >= 0);
74 
75     // Chosen so that we shouldn't finish the entire array before the timeout
76     constexpr auto WAIT = std::chrono::milliseconds{90};
77 
78     int count = 0;
79     for (const auto& str : INPUT) {
80         ASSERT_LT(count, 4) << "The reader has timed out, write should have failed by now";
81         if (write(fd, str.c_str(), str.size()) < 0) break;
82         std::this_thread::sleep_for(WAIT);
83         count++;
84     }
85 
86     ASSERT_EQ(FdToString::closeWriterAndGetString(std::move(writer)).substr(0, 8),
87             GOLDEN.substr(0, 8)) << "Format mistake";
88 }
89