File: handle_tests.cpp

package info (click to toggle)
libnop 0.0~git20200728.45dfe0f-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,432 kB
  • sloc: cpp: 13,946; ansic: 3,537; makefile: 100; python: 73
file content (145 lines) | stat: -rw-r--r-- 3,784 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2017 The Native Object Protocols Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <errno.h>
#include <fcntl.h>

#include <array>

#include <nop/types/file_handle.h>
#include <nop/types/handle.h>

using nop::FileHandle;
using nop::UniqueFileHandle;
using nop::UniqueHandle;

namespace {

struct CountingHandlePolicy {
  using Type = int;
  enum : int { kEmptyValue = -1 };

  static int Default() { return kEmptyValue; }
  static void Close(int* value) {
    if (IsValid(*value))
      close_count++;
    *value = kEmptyValue;
  };
  static int Release(int* value) {
    int temp = kEmptyValue;
    std::swap(*value, temp);
    return temp;
  }

  static bool IsValid(int value) { return value != kEmptyValue; }

  static int close_count;
  static void ClearCount() { close_count = 0; }
};

int CountingHandlePolicy::close_count = 0;

}  // anonymous namespace

TEST(Handle, UniqueHandle) {
  using IntHandle = UniqueHandle<CountingHandlePolicy>;

  {
    CountingHandlePolicy::ClearCount();

    IntHandle handle;
    EXPECT_FALSE(handle);
    EXPECT_EQ(0, CountingHandlePolicy::close_count);
  }

  {
    CountingHandlePolicy::ClearCount();

    IntHandle handle{10};
    EXPECT_TRUE(handle);
    EXPECT_EQ(0, CountingHandlePolicy::close_count);
    handle.close();
    EXPECT_EQ(1, CountingHandlePolicy::close_count);
  }
  EXPECT_EQ(1, CountingHandlePolicy::close_count);

  {
    CountingHandlePolicy::ClearCount();

    IntHandle handle{10};
    EXPECT_TRUE(handle);
    EXPECT_EQ(10, handle.get());
    EXPECT_EQ(0, CountingHandlePolicy::close_count);
  }
  EXPECT_EQ(1, CountingHandlePolicy::close_count);

  {
    CountingHandlePolicy::ClearCount();

    IntHandle handle{10};
    IntHandle handle2 = std::move(handle);
    EXPECT_FALSE(handle);
    EXPECT_EQ(CountingHandlePolicy::kEmptyValue, handle.get());
    EXPECT_TRUE(handle2);
    EXPECT_EQ(10, handle2.get());
    EXPECT_EQ(0, CountingHandlePolicy::close_count);
  }
  EXPECT_EQ(1, CountingHandlePolicy::close_count);

  {
    CountingHandlePolicy::ClearCount();

    IntHandle handle{10};
    IntHandle handle2{std::move(handle)};
    EXPECT_FALSE(handle);
    EXPECT_TRUE(handle2);
    EXPECT_EQ(0, CountingHandlePolicy::close_count);
  }
  EXPECT_EQ(1, CountingHandlePolicy::close_count);
}

TEST(Handle, FileHandle) {
  int fd = open("/dev/zero", O_RDONLY);
  UniqueFileHandle file_handle{fd};
  ASSERT_TRUE(file_handle);
  EXPECT_EQ(fd, file_handle.get());

  UniqueFileHandle file_handle2 = std::move(file_handle);
  EXPECT_FALSE(file_handle);
  EXPECT_TRUE(file_handle2);

  std::array<char, 10> buffer;
  buffer.fill('\0');

  EXPECT_EQ(static_cast<ssize_t>(buffer.size()),
            read(file_handle2.get(), buffer.data(), buffer.size()));

  {
    FileHandle handle{file_handle2};
    EXPECT_EQ(static_cast<ssize_t>(buffer.size()),
              read(handle.get(), buffer.data(), buffer.size()));
  }
  EXPECT_EQ(static_cast<ssize_t>(buffer.size()),
            read(file_handle2.get(), buffer.data(), buffer.size()));

  {
    UniqueFileHandle file_handle3{std::move(file_handle2)};
    EXPECT_FALSE(file_handle2);
    EXPECT_TRUE(file_handle3);
  }
  EXPECT_EQ(-1, read(fd, buffer.data(), buffer.size()));
  EXPECT_EQ(EBADF, errno);
}