@@ -113,7 +113,7 @@ fn test_valid_packet_counter() -> Result<()> {
113113 0xcf , 0x90 , 0x1e , 0xa5 , 0xda , 0xd3 , 0x2c , 0x15 , 0x00 , 0xa2 , 0x24 , 0xae , 0xae , 0xaf , 0x00 ,
114114 0x00 ,
115115 ] ;
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) ;
117117 assert_eq ! (
118118 counter, expected_counter,
119119 "Session Key {counter:?} does not match expected {expected_counter:?}" ,
@@ -124,15 +124,13 @@ fn test_valid_packet_counter() -> Result<()> {
124124
125125#[ test]
126126fn test_rollover_count ( ) -> Result < ( ) > {
127- let mut s = SrtpSsrcState {
128- ssrc : DEFAULT_SSRC ,
129- ..Default :: default ( )
130- } ;
127+ let mut s = SrtpSsrcState :: default ( ) ;
131128
132129 // Set initial seqnum
133- let roc = s. next_rollover_count ( 65530 ) ;
130+ let ( roc, diff , ovf ) = s. next_rollover_count ( 65530 ) ;
134131 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) ;
136134
137135 // Invalid packets never update ROC
138136 s. next_rollover_count ( 0 ) ;
@@ -142,64 +140,148 @@ fn test_rollover_count() -> Result<()> {
142140 s. next_rollover_count ( 0 ) ;
143141
144142 // We rolled over to 0
145- let roc = s. next_rollover_count ( 0 ) ;
143+ let ( roc, diff , ovf ) = s. next_rollover_count ( 0 ) ;
146144 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) ;
148147
149- let roc = s. next_rollover_count ( 65530 ) ;
148+ let ( roc, diff , ovf ) = s. next_rollover_count ( 65530 ) ;
150149 assert_eq ! (
151150 roc, 0 ,
152151 "rolloverCounter was not updated when it rolled back, failed to handle out of order"
153152 ) ;
154- s. update_rollover_count ( 65530 ) ;
153+ assert ! ( !ovf, "Should not overflow" ) ;
154+ s. update_rollover_count ( 65530 , diff) ;
155155
156- let roc = s. next_rollover_count ( 5 ) ;
156+ let ( roc, diff , ovf ) = s. next_rollover_count ( 5 ) ;
157157 assert_eq ! (
158158 roc, 1 ,
159159 "rolloverCounter was not updated when it rolled over initial, to handle out of order"
160160 ) ;
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 ) ;
170169 assert_eq ! (
171170 roc, 1 ,
172171 "rolloverCounter was improperly updated for non-significant packets"
173172 ) ;
174- s. update_rollover_count ( 8 ) ;
173+ s. update_rollover_count ( 8 , diff ) ;
175174
176175 // valid packets never update ROC
177- let roc = s. next_rollover_count ( 0x4000 ) ;
176+ let ( roc, diff , ovf ) = s. next_rollover_count ( 0x4000 ) ;
178177 assert_eq ! (
179178 roc, 1 ,
180179 "rolloverCounter was improperly updated for non-significant packets"
181180 ) ;
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 ) ;
185184 assert_eq ! (
186185 roc, 1 ,
187186 "rolloverCounter was improperly updated for non-significant packets"
188187 ) ;
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 ) ;
192191 assert_eq ! (
193192 roc, 1 ,
194193 "rolloverCounter was improperly updated for non-significant packets"
195194 ) ;
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 ) ;
199198 assert_eq ! (
200199 roc, 2 ,
201200 "rolloverCounter must be incremented after wrapping, got {roc}"
202201 ) ;
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) ;
203285
204286 Ok ( ( ) )
205287}
0 commit comments