aboutsummaryrefslogtreecommitdiff
path: root/cast/common/channel/virtual_connection_manager_unittest.cc
blob: 963fcac728fa387029d96df2f0f495d6fd66b741 (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
// Copyright 2019 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 "cast/common/channel/virtual_connection_manager.h"

#include "cast/common/channel/proto/cast_channel.pb.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace openscreen {
namespace cast {
namespace {

static_assert(::cast::channel::CastMessage_ProtocolVersion_CASTV2_1_0 ==
                  static_cast<int>(VirtualConnection::ProtocolVersion::kV2_1_0),
              "V2 1.0 constants must be equal");
static_assert(::cast::channel::CastMessage_ProtocolVersion_CASTV2_1_1 ==
                  static_cast<int>(VirtualConnection::ProtocolVersion::kV2_1_1),
              "V2 1.1 constants must be equal");
static_assert(::cast::channel::CastMessage_ProtocolVersion_CASTV2_1_2 ==
                  static_cast<int>(VirtualConnection::ProtocolVersion::kV2_1_2),
              "V2 1.2 constants must be equal");
static_assert(::cast::channel::CastMessage_ProtocolVersion_CASTV2_1_3 ==
                  static_cast<int>(VirtualConnection::ProtocolVersion::kV2_1_3),
              "V2 1.3 constants must be equal");

using ::testing::_;
using ::testing::Invoke;

class VirtualConnectionManagerTest : public ::testing::Test {
 protected:
  VirtualConnectionManager manager_;

  VirtualConnection vc1_{"local1", "peer1", 75};
  VirtualConnection vc2_{"local2", "peer2", 76};
  VirtualConnection vc3_{"local1", "peer3", 75};
};

}  // namespace

TEST_F(VirtualConnectionManagerTest, NoConnections) {
  EXPECT_FALSE(manager_.GetConnectionData(vc1_));
  EXPECT_FALSE(manager_.GetConnectionData(vc2_));
  EXPECT_FALSE(manager_.GetConnectionData(vc3_));
}

TEST_F(VirtualConnectionManagerTest, AddConnections) {
  VirtualConnection::AssociatedData data1 = {};

  manager_.AddConnection(vc1_, std::move(data1));
  EXPECT_TRUE(manager_.GetConnectionData(vc1_));
  EXPECT_FALSE(manager_.GetConnectionData(vc2_));
  EXPECT_FALSE(manager_.GetConnectionData(vc3_));

  VirtualConnection::AssociatedData data2 = {};
  manager_.AddConnection(vc2_, std::move(data2));
  EXPECT_TRUE(manager_.GetConnectionData(vc1_));
  EXPECT_TRUE(manager_.GetConnectionData(vc2_));
  EXPECT_FALSE(manager_.GetConnectionData(vc3_));

  VirtualConnection::AssociatedData data3 = {};
  manager_.AddConnection(vc3_, std::move(data3));
  EXPECT_TRUE(manager_.GetConnectionData(vc1_));
  EXPECT_TRUE(manager_.GetConnectionData(vc2_));
  EXPECT_TRUE(manager_.GetConnectionData(vc3_));
}

TEST_F(VirtualConnectionManagerTest, RemoveConnections) {
  VirtualConnection::AssociatedData data1 = {};
  VirtualConnection::AssociatedData data2 = {};
  VirtualConnection::AssociatedData data3 = {};

  manager_.AddConnection(vc1_, std::move(data1));
  manager_.AddConnection(vc2_, std::move(data2));
  manager_.AddConnection(vc3_, std::move(data3));

  EXPECT_TRUE(manager_.RemoveConnection(
      vc1_, VirtualConnection::CloseReason::kClosedBySelf));
  EXPECT_FALSE(manager_.GetConnectionData(vc1_));
  EXPECT_TRUE(manager_.GetConnectionData(vc2_));
  EXPECT_TRUE(manager_.GetConnectionData(vc3_));

  EXPECT_TRUE(manager_.RemoveConnection(
      vc2_, VirtualConnection::CloseReason::kClosedBySelf));
  EXPECT_FALSE(manager_.GetConnectionData(vc1_));
  EXPECT_FALSE(manager_.GetConnectionData(vc2_));
  EXPECT_TRUE(manager_.GetConnectionData(vc3_));

  EXPECT_TRUE(manager_.RemoveConnection(
      vc3_, VirtualConnection::CloseReason::kClosedBySelf));
  EXPECT_FALSE(manager_.GetConnectionData(vc1_));
  EXPECT_FALSE(manager_.GetConnectionData(vc2_));
  EXPECT_FALSE(manager_.GetConnectionData(vc3_));

  EXPECT_FALSE(manager_.RemoveConnection(
      vc1_, VirtualConnection::CloseReason::kClosedBySelf));
  EXPECT_FALSE(manager_.RemoveConnection(
      vc2_, VirtualConnection::CloseReason::kClosedBySelf));
  EXPECT_FALSE(manager_.RemoveConnection(
      vc3_, VirtualConnection::CloseReason::kClosedBySelf));
}

TEST_F(VirtualConnectionManagerTest, RemoveConnectionsByIds) {
  VirtualConnection::AssociatedData data1 = {};
  VirtualConnection::AssociatedData data2 = {};
  VirtualConnection::AssociatedData data3 = {};

  manager_.AddConnection(vc1_, std::move(data1));
  manager_.AddConnection(vc2_, std::move(data2));
  manager_.AddConnection(vc3_, std::move(data3));

  EXPECT_EQ(manager_.RemoveConnectionsByLocalId(
                "local1", VirtualConnection::CloseReason::kClosedBySelf),
            2u);
  EXPECT_FALSE(manager_.GetConnectionData(vc1_));
  EXPECT_TRUE(manager_.GetConnectionData(vc2_));
  EXPECT_FALSE(manager_.GetConnectionData(vc3_));

  data1 = {};
  data2 = {};
  data3 = {};
  manager_.AddConnection(vc1_, std::move(data1));
  manager_.AddConnection(vc2_, std::move(data2));
  manager_.AddConnection(vc3_, std::move(data3));
  EXPECT_EQ(manager_.RemoveConnectionsBySocketId(
                76, VirtualConnection::CloseReason::kClosedBySelf),
            1u);
  EXPECT_TRUE(manager_.GetConnectionData(vc1_));
  EXPECT_FALSE(manager_.GetConnectionData(vc2_));
  EXPECT_TRUE(manager_.GetConnectionData(vc3_));

  EXPECT_EQ(manager_.RemoveConnectionsBySocketId(
                75, VirtualConnection::CloseReason::kClosedBySelf),
            2u);
  EXPECT_FALSE(manager_.GetConnectionData(vc1_));
  EXPECT_FALSE(manager_.GetConnectionData(vc2_));
  EXPECT_FALSE(manager_.GetConnectionData(vc3_));
}

}  // namespace cast
}  // namespace openscreen