1 /*
2 * Copyright (C) 2022 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 "interprocess_fifo.h"
18
19 #include <android-base/result-gmock.h>
20 #include <gtest/gtest.h>
21
22 #define ASSERT_OK(e) ASSERT_THAT(e, Ok())
23 #define ASSERT_NOT_OK(e) ASSERT_THAT(e, Not(Ok()))
24
25 using ::android::base::Result;
26 using ::android::base::testing::Ok;
27 using ::testing::Not;
28
29 namespace android {
30 namespace init {
31
TEST(FifoTest,WriteAndRead)32 TEST(FifoTest, WriteAndRead) {
33 InterprocessFifo fifo;
34 ASSERT_OK(fifo.Initialize());
35 ASSERT_OK(fifo.Write('a'));
36 ASSERT_OK(fifo.Write('b'));
37 Result<uint8_t> result = fifo.Read();
38 ASSERT_OK(result);
39 EXPECT_EQ(*result, 'a');
40 result = fifo.Read();
41 ASSERT_OK(result);
42 EXPECT_EQ(*result, 'b');
43 InterprocessFifo fifo2 = std::move(fifo);
44 ASSERT_NOT_OK(fifo.Write('c'));
45 ASSERT_NOT_OK(fifo.Read());
46 ASSERT_OK(fifo2.Write('d'));
47 result = fifo2.Read();
48 ASSERT_OK(result);
49 EXPECT_EQ(*result, 'd');
50 }
51
52 } // namespace init
53 } // namespace android
54