Amit Hilbuch | a201204 | 2018-12-03 11:35:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "pc/sdpserializer.h" |
| 15 | #include "rtc_base/gunit.h" |
| 16 | |
| 17 | using ::testing::ValuesIn; |
| 18 | using cricket::SimulcastDescription; |
| 19 | using cricket::SimulcastLayer; |
| 20 | using cricket::SimulcastLayerList; |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class SdpSerializerTest : public ::testing::TestWithParam<const char*> { |
| 25 | public: |
| 26 | // Runs a test for deserializing Simulcast. |
| 27 | // |str| - The serialized Simulcast to parse. |
| 28 | // |expected| - The expected output Simulcast to compare to. |
| 29 | void TestSimulcastDeserialization( |
| 30 | const std::string& str, |
| 31 | const SimulcastDescription& expected) const { |
| 32 | SdpSerializer deserializer; |
| 33 | auto result = deserializer.DeserializeSimulcastDescription(str); |
| 34 | EXPECT_TRUE(result.ok()); |
| 35 | ExpectEqual(expected, result.value()); |
| 36 | } |
| 37 | |
| 38 | // Runs a test for serializing Simulcast. |
| 39 | // |simulcast| - The Simulcast to serialize. |
| 40 | // |expected| - The expected output string to compare to. |
| 41 | void TestSimulcastSerialization(const SimulcastDescription& simulcast, |
| 42 | const std::string& expected) const { |
| 43 | SdpSerializer serializer; |
| 44 | auto result = serializer.SerializeSimulcastDescription(simulcast); |
| 45 | EXPECT_EQ(expected, result); |
| 46 | } |
| 47 | |
| 48 | // Checks that the two vectors of SimulcastLayer objects are equal. |
| 49 | void ExpectEqual(const std::vector<SimulcastLayer>& expected, |
| 50 | const std::vector<SimulcastLayer>& actual) const { |
| 51 | EXPECT_EQ(expected.size(), actual.size()); |
| 52 | for (size_t i = 0; i < expected.size(); i++) { |
| 53 | EXPECT_EQ(expected[i].rid, actual[i].rid); |
| 54 | EXPECT_EQ(expected[i].is_paused, actual[i].is_paused); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Checks that the two SimulcastLayerLists are equal. |
| 59 | void ExpectEqual(const SimulcastLayerList& expected, |
| 60 | const SimulcastLayerList& actual) const { |
| 61 | EXPECT_EQ(expected.size(), actual.size()); |
| 62 | for (size_t i = 0; i < expected.size(); i++) { |
| 63 | ExpectEqual(expected[i], actual[i]); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Checks that the two SimulcastDescriptions are equal. |
| 68 | void ExpectEqual(const SimulcastDescription& expected, |
| 69 | const SimulcastDescription& actual) const { |
| 70 | ExpectEqual(expected.send_layers(), actual.send_layers()); |
| 71 | ExpectEqual(expected.receive_layers(), actual.receive_layers()); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | // Test Cases |
| 76 | |
| 77 | // Test simple deserialization with no alternative streams. |
| 78 | TEST_F(SdpSerializerTest, DeserializeSimulcast_SimpleCaseNoAlternatives) { |
| 79 | std::string simulcast_str = "send 1;2 recv 3;4"; |
| 80 | SimulcastDescription expected; |
| 81 | expected.send_layers().AddLayer(SimulcastLayer("1", false)); |
| 82 | expected.send_layers().AddLayer(SimulcastLayer("2", false)); |
| 83 | expected.receive_layers().AddLayer(SimulcastLayer("3", false)); |
| 84 | expected.receive_layers().AddLayer(SimulcastLayer("4", false)); |
| 85 | TestSimulcastDeserialization(simulcast_str, expected); |
| 86 | } |
| 87 | |
| 88 | // Test simulcast deserialization with alternative streams. |
| 89 | TEST_F(SdpSerializerTest, DeserializeSimulcast_SimpleCaseWithAlternatives) { |
| 90 | std::string simulcast_str = "send 1,5;2,6 recv 3,7;4,8"; |
| 91 | SimulcastDescription expected; |
| 92 | expected.send_layers().AddLayerWithAlternatives( |
| 93 | {SimulcastLayer("1", false), SimulcastLayer("5", false)}); |
| 94 | expected.send_layers().AddLayerWithAlternatives( |
| 95 | {SimulcastLayer("2", false), SimulcastLayer("6", false)}); |
| 96 | expected.receive_layers().AddLayerWithAlternatives( |
| 97 | {SimulcastLayer("3", false), SimulcastLayer("7", false)}); |
| 98 | expected.receive_layers().AddLayerWithAlternatives( |
| 99 | {SimulcastLayer("4", false), SimulcastLayer("8", false)}); |
| 100 | TestSimulcastDeserialization(simulcast_str, expected); |
| 101 | } |
| 102 | |
| 103 | // Test simulcast deserialization when only some streams have alternatives. |
| 104 | TEST_F(SdpSerializerTest, DeserializeSimulcast_WithSomeAlternatives) { |
| 105 | std::string simulcast_str = "send 1;2,6 recv 3,7;4"; |
| 106 | SimulcastDescription expected; |
| 107 | expected.send_layers().AddLayer(SimulcastLayer("1", false)); |
| 108 | expected.send_layers().AddLayerWithAlternatives( |
| 109 | {SimulcastLayer("2", false), SimulcastLayer("6", false)}); |
| 110 | expected.receive_layers().AddLayerWithAlternatives( |
| 111 | {SimulcastLayer("3", false), SimulcastLayer("7", false)}); |
| 112 | expected.receive_layers().AddLayer(SimulcastLayer("4", false)); |
| 113 | TestSimulcastDeserialization(simulcast_str, expected); |
| 114 | } |
| 115 | |
| 116 | // Test simulcast deserialization when only send streams are specified. |
| 117 | TEST_F(SdpSerializerTest, DeserializeSimulcast_OnlySendStreams) { |
| 118 | std::string simulcast_str = "send 1;2,6;3,7;4"; |
| 119 | SimulcastDescription expected; |
| 120 | expected.send_layers().AddLayer(SimulcastLayer("1", false)); |
| 121 | expected.send_layers().AddLayerWithAlternatives( |
| 122 | {SimulcastLayer("2", false), SimulcastLayer("6", false)}); |
| 123 | expected.send_layers().AddLayerWithAlternatives( |
| 124 | {SimulcastLayer("3", false), SimulcastLayer("7", false)}); |
| 125 | expected.send_layers().AddLayer(SimulcastLayer("4", false)); |
| 126 | TestSimulcastDeserialization(simulcast_str, expected); |
| 127 | } |
| 128 | |
| 129 | // Test simulcast deserialization when only receive streams are specified. |
| 130 | TEST_F(SdpSerializerTest, DeserializeSimulcast_OnlyReceiveStreams) { |
| 131 | std::string simulcast_str = "recv 1;2,6;3,7;4"; |
| 132 | SimulcastDescription expected; |
| 133 | expected.receive_layers().AddLayer(SimulcastLayer("1", false)); |
| 134 | expected.receive_layers().AddLayerWithAlternatives( |
| 135 | {SimulcastLayer("2", false), SimulcastLayer("6", false)}); |
| 136 | expected.receive_layers().AddLayerWithAlternatives( |
| 137 | {SimulcastLayer("3", false), SimulcastLayer("7", false)}); |
| 138 | expected.receive_layers().AddLayer(SimulcastLayer("4", false)); |
| 139 | TestSimulcastDeserialization(simulcast_str, expected); |
| 140 | } |
| 141 | |
| 142 | // Test simulcast deserialization with receive streams before send streams. |
| 143 | TEST_F(SdpSerializerTest, DeserializeSimulcast_SendReceiveReversed) { |
| 144 | std::string simulcast_str = "recv 1;2,6 send 3,7;4"; |
| 145 | SimulcastDescription expected; |
| 146 | expected.receive_layers().AddLayer(SimulcastLayer("1", false)); |
| 147 | expected.receive_layers().AddLayerWithAlternatives( |
| 148 | {SimulcastLayer("2", false), SimulcastLayer("6", false)}); |
| 149 | expected.send_layers().AddLayerWithAlternatives( |
| 150 | {SimulcastLayer("3", false), SimulcastLayer("7", false)}); |
| 151 | expected.send_layers().AddLayer(SimulcastLayer("4", false)); |
| 152 | TestSimulcastDeserialization(simulcast_str, expected); |
| 153 | } |
| 154 | |
| 155 | // Test simulcast deserialization with some streams set to paused state. |
| 156 | TEST_F(SdpSerializerTest, DeserializeSimulcast_PausedStreams) { |
| 157 | std::string simulcast_str = "recv 1;~2,6 send 3,7;~4"; |
| 158 | SimulcastDescription expected; |
| 159 | expected.receive_layers().AddLayer(SimulcastLayer("1", false)); |
| 160 | expected.receive_layers().AddLayerWithAlternatives( |
| 161 | {SimulcastLayer("2", true), SimulcastLayer("6", false)}); |
| 162 | expected.send_layers().AddLayerWithAlternatives( |
| 163 | {SimulcastLayer("3", false), SimulcastLayer("7", false)}); |
| 164 | expected.send_layers().AddLayer(SimulcastLayer("4", true)); |
| 165 | TestSimulcastDeserialization(simulcast_str, expected); |
| 166 | } |
| 167 | |
| 168 | // Parameterized negative test case for deserialization with invalid inputs. |
| 169 | TEST_P(SdpSerializerTest, SimulcastDeserializationFailed) { |
| 170 | SdpSerializer deserializer; |
| 171 | auto result = deserializer.DeserializeSimulcastDescription(GetParam()); |
| 172 | EXPECT_FALSE(result.ok()); |
| 173 | } |
| 174 | |
| 175 | // The malformed Simulcast inputs to use in the negative test case. |
| 176 | const char* kSimulcastMalformedStrings[] = { |
| 177 | "send ", |
| 178 | "recv ", |
| 179 | "recv 1 send", |
| 180 | "receive 1", |
| 181 | "recv 1;~2,6 recv 3,7;~4", |
| 182 | "send 1;~2,6 send 3,7;~4", |
| 183 | "send ~;~2,6", |
| 184 | "send 1; ;~2,6", |
| 185 | "send 1,;~2,6", |
| 186 | "recv 1 send 2 3", |
| 187 | "", |
| 188 | }; |
| 189 | |
| 190 | INSTANTIATE_TEST_CASE_P(SimulcastDeserializationErrors, |
| 191 | SdpSerializerTest, |
| 192 | ValuesIn(kSimulcastMalformedStrings)); |
| 193 | |
| 194 | // Test a simple serialization scenario. |
| 195 | TEST_F(SdpSerializerTest, SerializeSimulcast_SimpleCase) { |
| 196 | SimulcastDescription simulcast; |
| 197 | simulcast.send_layers().AddLayer(SimulcastLayer("1", false)); |
| 198 | simulcast.receive_layers().AddLayer(SimulcastLayer("2", false)); |
| 199 | TestSimulcastSerialization(simulcast, "send 1 recv 2"); |
| 200 | } |
| 201 | |
| 202 | // Test serialization with only send streams. |
| 203 | TEST_F(SdpSerializerTest, SerializeSimulcast_OnlySend) { |
| 204 | SimulcastDescription simulcast; |
| 205 | simulcast.send_layers().AddLayer(SimulcastLayer("1", false)); |
| 206 | simulcast.send_layers().AddLayer(SimulcastLayer("2", false)); |
| 207 | TestSimulcastSerialization(simulcast, "send 1;2"); |
| 208 | } |
| 209 | |
| 210 | // Test serialization with only receive streams |
| 211 | TEST_F(SdpSerializerTest, SerializeSimulcast_OnlyReceive) { |
| 212 | SimulcastDescription simulcast; |
| 213 | simulcast.receive_layers().AddLayer(SimulcastLayer("1", false)); |
| 214 | simulcast.receive_layers().AddLayer(SimulcastLayer("2", false)); |
| 215 | TestSimulcastSerialization(simulcast, "recv 1;2"); |
| 216 | } |
| 217 | |
| 218 | // Test a complex serialization with multiple streams, alternatives and states. |
| 219 | TEST_F(SdpSerializerTest, SerializeSimulcast_ComplexSerialization) { |
| 220 | SimulcastDescription simulcast; |
| 221 | simulcast.send_layers().AddLayerWithAlternatives( |
| 222 | {SimulcastLayer("2", false), SimulcastLayer("1", true)}); |
| 223 | simulcast.send_layers().AddLayerWithAlternatives( |
| 224 | {SimulcastLayer("4", false), SimulcastLayer("3", false)}); |
| 225 | |
| 226 | simulcast.receive_layers().AddLayerWithAlternatives( |
| 227 | {SimulcastLayer("6", false), SimulcastLayer("7", false)}); |
| 228 | simulcast.receive_layers().AddLayer(SimulcastLayer("8", true)); |
| 229 | simulcast.receive_layers().AddLayerWithAlternatives( |
| 230 | {SimulcastLayer("9", false), SimulcastLayer("10", true), |
| 231 | SimulcastLayer("11", false)}); |
| 232 | TestSimulcastSerialization(simulcast, "send 2,~1;4,3 recv 6,7;~8;9,~10,11"); |
| 233 | } |
| 234 | |
| 235 | } // namespace webrtc |