summaryrefslogtreecommitdiffstats
path: root/third_party/mojo/src/mojo/edk/system/channel_endpoint_id_unittest.cc
blob: c238cb8cbeeb8c5d1ff0cd48251bc653b37c96b8 (plain)
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
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/mojo/src/mojo/edk/system/channel_endpoint_id.h"

#include <sstream>

#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace system {
namespace {

TEST(ChannelEndpointIdTest, Basic) {
  ChannelEndpointId invalid;
  ChannelEndpointId bootstrap(ChannelEndpointId::GetBootstrap());

  EXPECT_EQ(invalid, invalid);
  EXPECT_EQ(bootstrap, bootstrap);
  EXPECT_FALSE(invalid == bootstrap);

  EXPECT_FALSE(invalid != invalid);
  EXPECT_FALSE(bootstrap != bootstrap);
  EXPECT_NE(invalid, bootstrap);

  EXPECT_FALSE(invalid < invalid);
  EXPECT_LT(invalid, bootstrap);

  EXPECT_FALSE(invalid.is_valid());
  EXPECT_TRUE(bootstrap.is_valid());

  EXPECT_FALSE(invalid.is_remote());
  EXPECT_FALSE(bootstrap.is_remote());

  // Test assignment.
  ChannelEndpointId copy;
  copy = bootstrap;
  EXPECT_EQ(copy, bootstrap);
  copy = invalid;
  EXPECT_EQ(copy, invalid);
}

// Tests values of invalid and bootstrap IDs. (This tests implementation
// details.)
TEST(ChannelEndpointIdTest, Value) {
  EXPECT_EQ(0u, ChannelEndpointId().value());
  EXPECT_EQ(1u, ChannelEndpointId::GetBootstrap().value());
}

// Tests ostream output. (This tests implementation details.)
TEST(ChannelEndpointIdTest, Ostream) {
  {
    std::ostringstream stream;
    stream << ChannelEndpointId();
    EXPECT_EQ("0", stream.str());
  }
  {
    std::ostringstream stream;
    stream << ChannelEndpointId::GetBootstrap();
    EXPECT_EQ("1", stream.str());
  }
}

TEST(LocalChannelEndpointIdGeneratorTest, Basic) {
  LocalChannelEndpointIdGenerator gen;

  ChannelEndpointId id1;
  EXPECT_FALSE(id1.is_valid());  // Check sanity.

  id1 = gen.GetNext();
  EXPECT_TRUE(id1.is_valid());
  EXPECT_FALSE(id1.is_remote());

  EXPECT_EQ(ChannelEndpointId::GetBootstrap().value(), id1.value());

  ChannelEndpointId id2 = gen.GetNext();
  EXPECT_TRUE(id2.is_valid());
  EXPECT_FALSE(id2.is_remote());
  // Technically, nonequality here is an implementation detail, since, e.g.,
  // random generation of IDs would be a valid implementation.
  EXPECT_NE(id2, id1);
  // ... but right now we just increment to generate IDs.
  EXPECT_EQ(2u, id2.value());
}

// Note: LocalChannelEndpointIdGeneratorTest.WrapAround is defined further
// below, outside the anonymous namespace.

TEST(RemoteChannelEndpointIdGeneratorTest, Basic) {
  RemoteChannelEndpointIdGenerator gen;

  ChannelEndpointId id1;
  EXPECT_FALSE(id1.is_valid());  // Check sanity.

  id1 = gen.GetNext();
  EXPECT_TRUE(id1.is_valid());
  EXPECT_TRUE(id1.is_remote());

  // This tests an implementation detail.
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id1.value());

  ChannelEndpointId id2 = gen.GetNext();
  EXPECT_TRUE(id2.is_valid());
  EXPECT_TRUE(id2.is_remote());
  // Technically, nonequality here is an implementation detail, since, e.g.,
  // random generation of IDs would be a valid implementation.
  EXPECT_NE(id2, id1);
  // ... but right now we just increment to generate IDs.
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag + 1, id2.value());
}

// Note: RemoteChannelEndpointIdGeneratorTest.WrapAround is defined further
// below, outside the anonymous namespace.

}  // namespace

// Tests that |LocalChannelEndpointIdGenerator| handles wrap-around correctly.
// (This tests implementation details.) This test isn't in an anonymous
// namespace, since it needs to be friended.
TEST(LocalChannelEndpointIdGeneratorTest, WrapAround) {
  LocalChannelEndpointIdGenerator gen;
  gen.next_ = ChannelEndpointId(ChannelEndpointId::kRemoteFlag - 1);

  ChannelEndpointId id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_FALSE(id.is_remote());
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag - 1, id.value());

  id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_FALSE(id.is_remote());
  EXPECT_EQ(1u, id.value());
}

// Tests that |RemoteChannelEndpointIdGenerator| handles wrap-around correctly.
// (This tests implementation details.) This test isn't in an anonymous
// namespace, since it needs to be friended.
TEST(RemoteChannelEndpointIdGeneratorTest, WrapAround) {
  RemoteChannelEndpointIdGenerator gen;
  gen.next_ = ChannelEndpointId(~0u);

  ChannelEndpointId id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_TRUE(id.is_remote());
  EXPECT_EQ(~0u, id.value());

  id = gen.GetNext();
  EXPECT_TRUE(id.is_valid());
  EXPECT_TRUE(id.is_remote());
  EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id.value());
}

}  // namespace system
}  // namespace mojo