1 /*
2 * Copyright (C) 2018 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 <gtest/gtest.h>
18
19 #include "netdutils/BackoffSequence.h"
20
21 namespace android {
22 namespace netdutils {
23
TEST(BackoffSequence,defaults)24 TEST(BackoffSequence, defaults) {
25 BackoffSequence<uint32_t> backoff;
26
27 EXPECT_TRUE(backoff.hasNextTimeout());
28 EXPECT_EQ(0x00000001U, backoff.getNextTimeout());
29 EXPECT_EQ(0x00000002U, backoff.getNextTimeout());
30 EXPECT_EQ(0x00000004U, backoff.getNextTimeout());
31 EXPECT_EQ(0x00000008U, backoff.getNextTimeout());
32 EXPECT_EQ(0x00000010U, backoff.getNextTimeout());
33 EXPECT_EQ(0x00000020U, backoff.getNextTimeout());
34 EXPECT_EQ(0x00000040U, backoff.getNextTimeout());
35 EXPECT_EQ(0x00000080U, backoff.getNextTimeout());
36 EXPECT_EQ(0x00000100U, backoff.getNextTimeout());
37 EXPECT_EQ(0x00000200U, backoff.getNextTimeout());
38 EXPECT_EQ(0x00000400U, backoff.getNextTimeout());
39 EXPECT_EQ(0x00000800U, backoff.getNextTimeout());
40 EXPECT_EQ(0x00001000U, backoff.getNextTimeout());
41 EXPECT_EQ(0x00002000U, backoff.getNextTimeout());
42 EXPECT_EQ(0x00004000U, backoff.getNextTimeout());
43 EXPECT_EQ(0x00008000U, backoff.getNextTimeout());
44 EXPECT_EQ(0x00010000U, backoff.getNextTimeout());
45 EXPECT_EQ(0x00020000U, backoff.getNextTimeout());
46 EXPECT_EQ(0x00040000U, backoff.getNextTimeout());
47 EXPECT_EQ(0x00080000U, backoff.getNextTimeout());
48 EXPECT_EQ(0x00100000U, backoff.getNextTimeout());
49 EXPECT_EQ(0x00200000U, backoff.getNextTimeout());
50 EXPECT_EQ(0x00400000U, backoff.getNextTimeout());
51 EXPECT_EQ(0x00800000U, backoff.getNextTimeout());
52 EXPECT_EQ(0x01000000U, backoff.getNextTimeout());
53 EXPECT_EQ(0x02000000U, backoff.getNextTimeout());
54 EXPECT_EQ(0x04000000U, backoff.getNextTimeout());
55 EXPECT_EQ(0x08000000U, backoff.getNextTimeout());
56 EXPECT_EQ(0x10000000U, backoff.getNextTimeout());
57 EXPECT_EQ(0x20000000U, backoff.getNextTimeout());
58 EXPECT_EQ(0x40000000U, backoff.getNextTimeout());
59 EXPECT_EQ(0x80000000U, backoff.getNextTimeout());
60 // Maxes out, and stays there, ad infinitum.
61 for (int i = 0; i < 10; i++) {
62 EXPECT_TRUE(backoff.hasNextTimeout());
63 EXPECT_EQ(0xffffffffU, backoff.getNextTimeout());
64 }
65 }
66
TEST(BackoffSequence,backoffToOncePerHour)67 TEST(BackoffSequence, backoffToOncePerHour) {
68 auto backoff = BackoffSequence<uint32_t>::Builder()
69 .withInitialRetransmissionTime(1)
70 .withMaximumRetransmissionTime(3600)
71 .build();
72
73 EXPECT_TRUE(backoff.hasNextTimeout());
74 EXPECT_EQ(0x00000001U, backoff.getNextTimeout());
75 EXPECT_EQ(0x00000002U, backoff.getNextTimeout());
76 EXPECT_EQ(0x00000004U, backoff.getNextTimeout());
77 EXPECT_EQ(0x00000008U, backoff.getNextTimeout());
78 EXPECT_EQ(0x00000010U, backoff.getNextTimeout());
79 EXPECT_EQ(0x00000020U, backoff.getNextTimeout());
80 EXPECT_EQ(0x00000040U, backoff.getNextTimeout());
81 EXPECT_EQ(0x00000080U, backoff.getNextTimeout());
82 EXPECT_EQ(0x00000100U, backoff.getNextTimeout());
83 EXPECT_EQ(0x00000200U, backoff.getNextTimeout());
84 EXPECT_EQ(0x00000400U, backoff.getNextTimeout());
85 EXPECT_EQ(0x00000800U, backoff.getNextTimeout());
86 // Maxes out, and stays there, ad infinitum.
87 for (int i = 0; i < 10; i++) {
88 EXPECT_TRUE(backoff.hasNextTimeout());
89 EXPECT_EQ(3600U, backoff.getNextTimeout());
90 }
91 }
92
TEST(BackoffSequence,simpleMaxRetransCount)93 TEST(BackoffSequence, simpleMaxRetransCount) {
94 auto backoff = BackoffSequence<uint32_t>::Builder()
95 .withInitialRetransmissionTime(3)
96 .withMaximumRetransmissionCount(7)
97 .build();
98
99 EXPECT_TRUE(backoff.hasNextTimeout());
100 EXPECT_EQ(0x00000003U, backoff.getNextTimeout());
101 EXPECT_EQ(0x00000006U, backoff.getNextTimeout());
102 EXPECT_EQ(0x0000000cU, backoff.getNextTimeout());
103 EXPECT_EQ(0x00000018U, backoff.getNextTimeout());
104 EXPECT_EQ(0x00000030U, backoff.getNextTimeout());
105 EXPECT_EQ(0x00000060U, backoff.getNextTimeout());
106 EXPECT_EQ(0x000000c0U, backoff.getNextTimeout());
107
108 for (int i = 0; i < 10; i++) {
109 EXPECT_FALSE(backoff.hasNextTimeout());
110 EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
111 }
112 }
113
TEST(BackoffSequence,simpleMaxDuration)114 TEST(BackoffSequence, simpleMaxDuration) {
115 auto backoff = BackoffSequence<int>::Builder()
116 .withInitialRetransmissionTime(3)
117 .withMaximumRetransmissionDuration(7)
118 .withEndOfSequenceIndicator(-1)
119 .build();
120
121 EXPECT_TRUE(backoff.hasNextTimeout());
122 EXPECT_EQ(0x00000003, backoff.getNextTimeout());
123 EXPECT_EQ(0x00000004, backoff.getNextTimeout());
124
125 for (int i = 0; i < 10; i++) {
126 EXPECT_FALSE(backoff.hasNextTimeout());
127 EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
128 EXPECT_EQ(-1, backoff.getNextTimeout());
129 }
130 }
131
TEST(PathologicalBackoffSequence,ZeroInitialRetransTime)132 TEST(PathologicalBackoffSequence, ZeroInitialRetransTime) {
133 auto backoff = BackoffSequence<std::chrono::seconds>::Builder()
134 .withInitialRetransmissionTime(std::chrono::seconds(0))
135 .build();
136
137 for (int i = 0; i < 10; i++) {
138 // TODO: Decide whether this needs fixing, and how.
139 EXPECT_TRUE(backoff.hasNextTimeout());
140 EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
141 }
142 }
143
TEST(PathologicalBackoffSequence,MaxRetransDurationGreaterThanInitialRetransTime)144 TEST(PathologicalBackoffSequence, MaxRetransDurationGreaterThanInitialRetransTime) {
145 auto backoff = BackoffSequence<std::chrono::milliseconds>::Builder()
146 .withInitialRetransmissionTime(std::chrono::milliseconds(5))
147 .withMaximumRetransmissionDuration(std::chrono::milliseconds(3))
148 .build();
149
150 EXPECT_EQ(std::chrono::milliseconds(3), backoff.getNextTimeout());
151 for (int i = 0; i < 10; i++) {
152 EXPECT_FALSE(backoff.hasNextTimeout());
153 EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
154 }
155 }
156
TEST(PathologicalBackoffSequence,MaxRetransDurationEqualsInitialRetransTime)157 TEST(PathologicalBackoffSequence, MaxRetransDurationEqualsInitialRetransTime) {
158 auto backoff = BackoffSequence<std::chrono::hours>::Builder()
159 .withInitialRetransmissionTime(std::chrono::hours(5))
160 .withMaximumRetransmissionDuration(std::chrono::hours(5))
161 .build();
162
163 EXPECT_EQ(std::chrono::hours(5), backoff.getNextTimeout());
164 for (int i = 0; i < 10; i++) {
165 EXPECT_FALSE(backoff.hasNextTimeout());
166 EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
167 }
168 }
169
TEST(PathologicalBackoffSequence,MaxRetransTimeAndDurationGreaterThanInitialRetransTime)170 TEST(PathologicalBackoffSequence, MaxRetransTimeAndDurationGreaterThanInitialRetransTime) {
171 auto backoff = BackoffSequence<std::chrono::nanoseconds>::Builder()
172 .withInitialRetransmissionTime(std::chrono::nanoseconds(7))
173 .withMaximumRetransmissionTime(std::chrono::nanoseconds(3))
174 .withMaximumRetransmissionDuration(std::chrono::nanoseconds(5))
175 .build();
176
177 EXPECT_EQ(std::chrono::nanoseconds(3), backoff.getNextTimeout());
178 EXPECT_EQ(std::chrono::nanoseconds(2), backoff.getNextTimeout());
179 for (int i = 0; i < 10; i++) {
180 EXPECT_FALSE(backoff.hasNextTimeout());
181 EXPECT_EQ(backoff.getEndOfSequenceIndicator(), backoff.getNextTimeout());
182 }
183 }
184
185 } // namespace netdutils
186 } // namespace android
187