@@ -113,7 +113,7 @@ fn test_valid_packet_counter() -> Result<()> {
113
113
0xcf , 0x90 , 0x1e , 0xa5 , 0xda , 0xd3 , 0x2c , 0x15 , 0x00 , 0xa2 , 0x24 , 0xae , 0xae , 0xaf , 0x00 ,
114
114
0x00 ,
115
115
] ;
116
- let counter = generate_counter ( 32846 , s . rollover_counter , s. ssrc , & srtp_session_salt) ;
116
+ let counter = generate_counter ( 32846 , ( s . index >> 16 ) as _ , s. ssrc , & srtp_session_salt) ;
117
117
assert_eq ! (
118
118
counter, expected_counter,
119
119
"Session Key {counter:?} does not match expected {expected_counter:?}" ,
@@ -124,15 +124,13 @@ fn test_valid_packet_counter() -> Result<()> {
124
124
125
125
#[ test]
126
126
fn test_rollover_count ( ) -> Result < ( ) > {
127
- let mut s = SrtpSsrcState {
128
- ssrc : DEFAULT_SSRC ,
129
- ..Default :: default ( )
130
- } ;
127
+ let mut s = SrtpSsrcState :: default ( ) ;
131
128
132
129
// Set initial seqnum
133
- let roc = s. next_rollover_count ( 65530 ) ;
130
+ let ( roc, diff , ovf ) = s. next_rollover_count ( 65530 ) ;
134
131
assert_eq ! ( roc, 0 , "Initial rolloverCounter must be 0" ) ;
135
- s. update_rollover_count ( 65530 ) ;
132
+ assert ! ( !ovf, "Should not overflow" ) ;
133
+ s. update_rollover_count ( 65530 , diff) ;
136
134
137
135
// Invalid packets never update ROC
138
136
s. next_rollover_count ( 0 ) ;
@@ -142,64 +140,148 @@ fn test_rollover_count() -> Result<()> {
142
140
s. next_rollover_count ( 0 ) ;
143
141
144
142
// We rolled over to 0
145
- let roc = s. next_rollover_count ( 0 ) ;
143
+ let ( roc, diff , ovf ) = s. next_rollover_count ( 0 ) ;
146
144
assert_eq ! ( roc, 1 , "rolloverCounter was not updated after it crossed 0" ) ;
147
- s. update_rollover_count ( 0 ) ;
145
+ assert ! ( !ovf, "Should not overflow" ) ;
146
+ s. update_rollover_count ( 0 , diff) ;
148
147
149
- let roc = s. next_rollover_count ( 65530 ) ;
148
+ let ( roc, diff , ovf ) = s. next_rollover_count ( 65530 ) ;
150
149
assert_eq ! (
151
150
roc, 0 ,
152
151
"rolloverCounter was not updated when it rolled back, failed to handle out of order"
153
152
) ;
154
- s. update_rollover_count ( 65530 ) ;
153
+ assert ! ( !ovf, "Should not overflow" ) ;
154
+ s. update_rollover_count ( 65530 , diff) ;
155
155
156
- let roc = s. next_rollover_count ( 5 ) ;
156
+ let ( roc, diff , ovf ) = s. next_rollover_count ( 5 ) ;
157
157
assert_eq ! (
158
158
roc, 1 ,
159
159
"rolloverCounter was not updated when it rolled over initial, to handle out of order"
160
160
) ;
161
- s. update_rollover_count ( 5 ) ;
162
-
163
- s. next_rollover_count ( 6 ) ;
164
- s. update_rollover_count ( 6 ) ;
165
-
166
- s. next_rollover_count ( 7 ) ;
167
- s. update_rollover_count ( 7 ) ;
168
-
169
- let roc = s. next_rollover_count ( 8 ) ;
161
+ assert ! ( !ovf, "Should not overflow" ) ;
162
+ s. update_rollover_count ( 5 , diff) ;
163
+
164
+ let ( _, diff, _) = s. next_rollover_count ( 6 ) ;
165
+ s. update_rollover_count ( 6 , diff) ;
166
+ let ( _, diff, _) = s. next_rollover_count ( 7 ) ;
167
+ s. update_rollover_count ( 7 , diff) ;
168
+ let ( roc, diff, _) = s. next_rollover_count ( 8 ) ;
170
169
assert_eq ! (
171
170
roc, 1 ,
172
171
"rolloverCounter was improperly updated for non-significant packets"
173
172
) ;
174
- s. update_rollover_count ( 8 ) ;
173
+ s. update_rollover_count ( 8 , diff ) ;
175
174
176
175
// valid packets never update ROC
177
- let roc = s. next_rollover_count ( 0x4000 ) ;
176
+ let ( roc, diff , ovf ) = s. next_rollover_count ( 0x4000 ) ;
178
177
assert_eq ! (
179
178
roc, 1 ,
180
179
"rolloverCounter was improperly updated for non-significant packets"
181
180
) ;
182
- s . update_rollover_count ( 0x4000 ) ;
183
-
184
- let roc = s. next_rollover_count ( 0x8000 ) ;
181
+ assert ! ( !ovf , "Should not overflow" ) ;
182
+ s . update_rollover_count ( 0x4000 , diff ) ;
183
+ let ( roc, diff , ovf ) = s. next_rollover_count ( 0x8000 ) ;
185
184
assert_eq ! (
186
185
roc, 1 ,
187
186
"rolloverCounter was improperly updated for non-significant packets"
188
187
) ;
189
- s . update_rollover_count ( 0x8000 ) ;
190
-
191
- let roc = s. next_rollover_count ( 0xFFFF ) ;
188
+ assert ! ( !ovf , "Should not overflow" ) ;
189
+ s . update_rollover_count ( 0x8000 , diff ) ;
190
+ let ( roc, diff , ovf ) = s. next_rollover_count ( 0xFFFF ) ;
192
191
assert_eq ! (
193
192
roc, 1 ,
194
193
"rolloverCounter was improperly updated for non-significant packets"
195
194
) ;
196
- s . update_rollover_count ( 0xFFFF ) ;
197
-
198
- let roc = s. next_rollover_count ( 0 ) ;
195
+ assert ! ( !ovf , "Should not overflow" ) ;
196
+ s . update_rollover_count ( 0xFFFF , diff ) ;
197
+ let ( roc, _ , ovf ) = s. next_rollover_count ( 0 ) ;
199
198
assert_eq ! (
200
199
roc, 2 ,
201
200
"rolloverCounter must be incremented after wrapping, got {roc}"
202
201
) ;
202
+ assert ! ( !ovf, "Should not overflow" ) ;
203
+
204
+ Ok ( ( ) )
205
+ }
206
+
207
+ #[ test]
208
+ fn test_rollover_count_overflow ( ) -> Result < ( ) > {
209
+ let mut s = SrtpSsrcState {
210
+ index : ( MAX_ROC as u64 ) << 16 ,
211
+ ..Default :: default ( )
212
+ } ;
213
+ s. update_rollover_count ( 0xFFFF , 0 ) ;
214
+ let ( _, _, ovf) = s. next_rollover_count ( 0 ) ;
215
+ assert ! ( ovf, "Should overflow" ) ;
216
+
217
+ Ok ( ( ) )
218
+ }
219
+
220
+ #[ test]
221
+ fn test_rollover_count_2 ( ) -> Result < ( ) > {
222
+ let mut s = SrtpSsrcState :: default ( ) ;
223
+
224
+ let ( roc, diff, ovf) = s. next_rollover_count ( 30123 ) ;
225
+ assert_eq ! ( roc, 0 , "Initial rolloverCounter must be 0" ) ;
226
+ assert ! ( !ovf, "Should not overflow" ) ;
227
+ s. update_rollover_count ( 30123 , diff) ;
228
+
229
+ // 62892 = 30123 + (1 << 15) + 1
230
+ let ( roc, diff, ovf) = s. next_rollover_count ( 62892 ) ;
231
+ assert_eq ! ( roc, 0 , "Initial rolloverCounter must be 0" ) ;
232
+ assert ! ( !ovf, "Should not overflow" ) ;
233
+ s. update_rollover_count ( 62892 , diff) ;
234
+ let ( roc, diff, ovf) = s. next_rollover_count ( 204 ) ;
235
+ assert_eq ! ( roc, 1 , "rolloverCounter was not updated after it crossed 0" ) ;
236
+ assert ! ( !ovf, "Should not overflow" ) ;
237
+ s. update_rollover_count ( 62892 , diff) ;
238
+ let ( roc, diff, ovf) = s. next_rollover_count ( 64535 ) ;
239
+ assert_eq ! (
240
+ roc, 0 ,
241
+ "rolloverCounter was not updated when it rolled back, failed to handle out of order"
242
+ ) ;
243
+ assert ! ( !ovf, "Should not overflow" ) ;
244
+ s. update_rollover_count ( 64535 , diff) ;
245
+ let ( roc, diff, ovf) = s. next_rollover_count ( 205 ) ;
246
+ assert_eq ! (
247
+ roc, 1 ,
248
+ "rolloverCounter was improperly updated for non-significant packets"
249
+ ) ;
250
+ assert ! ( !ovf, "Should not overflow" ) ;
251
+ s. update_rollover_count ( 205 , diff) ;
252
+ let ( roc, diff, ovf) = s. next_rollover_count ( 1 ) ;
253
+ assert_eq ! (
254
+ roc, 1 ,
255
+ "rolloverCounter was improperly updated for non-significant packets"
256
+ ) ;
257
+ assert ! ( !ovf, "Should not overflow" ) ;
258
+ s. update_rollover_count ( 1 , diff) ;
259
+
260
+ let ( roc, diff, ovf) = s. next_rollover_count ( 64532 ) ;
261
+ assert_eq ! (
262
+ roc, 0 ,
263
+ "rolloverCounter was improperly updated for non-significant packets"
264
+ ) ;
265
+ assert ! ( !ovf, "Should not overflow" ) ;
266
+ s. update_rollover_count ( 64532 , diff) ;
267
+ let ( roc, diff, ovf) = s. next_rollover_count ( 64534 ) ;
268
+ assert_eq ! (
269
+ roc, 0 ,
270
+ "index was improperly updated for non-significant packets"
271
+ ) ;
272
+ assert ! ( !ovf, "Should not overflow" ) ;
273
+ s. update_rollover_count ( 64534 , diff) ;
274
+ let ( roc, diff, ovf) = s. next_rollover_count ( 64532 ) ;
275
+ assert_eq ! (
276
+ roc, 0 ,
277
+ "index was improperly updated for non-significant packets"
278
+ ) ;
279
+ assert ! ( !ovf, "Should not overflow" ) ;
280
+ s. update_rollover_count ( 64532 , diff) ;
281
+ let ( roc, diff, ovf) = s. next_rollover_count ( 205 ) ;
282
+ assert_eq ! ( roc, 1 , "index was not updated after it crossed 0" ) ;
283
+ assert ! ( !ovf, "Should not overflow" ) ;
284
+ s. update_rollover_count ( 205 , diff) ;
203
285
204
286
Ok ( ( ) )
205
287
}
0 commit comments