@@ -1271,3 +1271,159 @@ func TestBaseCandidateExtensionsEqual(t *testing.T) {
1271
1271
})
1272
1272
}
1273
1273
}
1274
+
1275
+ func TestCandidateAddExtension (t * testing.T ) {
1276
+ t .Run ("Add extension" , func (t * testing.T ) {
1277
+ candidate , err := NewCandidateHost (& CandidateHostConfig {
1278
+ Network : NetworkTypeUDP4 .String (),
1279
+ Address : "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a" ,
1280
+ Port : 53987 ,
1281
+ Priority : 500 ,
1282
+ Foundation : "750" ,
1283
+ })
1284
+ if err != nil {
1285
+ t .Error (err )
1286
+ }
1287
+
1288
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"a" , "b" }))
1289
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"c" , "d" }))
1290
+
1291
+ extensions := candidate .Extensions ()
1292
+ require .Equal (t , []CandidateExtension {{"a" , "b" }, {"c" , "d" }}, extensions )
1293
+ })
1294
+
1295
+ t .Run ("Add extension with existing key" , func (t * testing.T ) {
1296
+ candidate , err := NewCandidateHost (& CandidateHostConfig {
1297
+ Network : NetworkTypeUDP4 .String (),
1298
+ Address : "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a" ,
1299
+ Port : 53987 ,
1300
+ Priority : 500 ,
1301
+ Foundation : "750" ,
1302
+ })
1303
+ if err != nil {
1304
+ t .Error (err )
1305
+ }
1306
+
1307
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"a" , "b" }))
1308
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"a" , "d" }))
1309
+
1310
+ extensions := candidate .Extensions ()
1311
+ require .Equal (t , []CandidateExtension {{"a" , "d" }}, extensions )
1312
+ })
1313
+
1314
+ t .Run ("Keep tcptype extension" , func (t * testing.T ) {
1315
+ candidate , err := NewCandidateHost (& CandidateHostConfig {
1316
+ Network : NetworkTypeTCP4 .String (),
1317
+ Address : "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a" ,
1318
+ Port : 53987 ,
1319
+ Priority : 500 ,
1320
+ Foundation : "750" ,
1321
+ TCPType : TCPTypeActive ,
1322
+ })
1323
+ if err != nil {
1324
+ t .Error (err )
1325
+ }
1326
+
1327
+ ext , ok := candidate .GetExtension ("tcptype" )
1328
+ require .True (t , ok )
1329
+ require .Equal (t , ext , CandidateExtension {"tcptype" , "active" })
1330
+ require .Equal (t , candidate .Extensions (), []CandidateExtension {{"tcptype" , "active" }})
1331
+
1332
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"a" , "b" }))
1333
+
1334
+ ext , ok = candidate .GetExtension ("tcptype" )
1335
+ require .True (t , ok )
1336
+ require .Equal (t , ext , CandidateExtension {"tcptype" , "active" })
1337
+ require .Equal (t , candidate .Extensions (), []CandidateExtension {{"tcptype" , "active" }, {"a" , "b" }})
1338
+ })
1339
+
1340
+ t .Run ("TcpType change extension" , func (t * testing.T ) {
1341
+ candidate , err := NewCandidateHost (& CandidateHostConfig {
1342
+ Network : NetworkTypeTCP4 .String (),
1343
+ Address : "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a" ,
1344
+ Port : 53987 ,
1345
+ Priority : 500 ,
1346
+ Foundation : "750" ,
1347
+ })
1348
+ if err != nil {
1349
+ t .Error (err )
1350
+ }
1351
+
1352
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"tcptype" , "active" }))
1353
+
1354
+ extensions := candidate .Extensions ()
1355
+ require .Equal (t , []CandidateExtension {{"tcptype" , "active" }}, extensions )
1356
+ require .Equal (t , TCPTypeActive , candidate .TCPType ())
1357
+
1358
+ require .Error (t , candidate .AddExtension (CandidateExtension {"tcptype" , "INVALID" }))
1359
+ })
1360
+ }
1361
+
1362
+ func TestCandidateRemoveExtension (t * testing.T ) {
1363
+ t .Run ("Remove extension" , func (t * testing.T ) {
1364
+ candidate , err := NewCandidateHost (& CandidateHostConfig {
1365
+ Network : NetworkTypeUDP4 .String (),
1366
+ Address : "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a" ,
1367
+ Port : 53987 ,
1368
+ Priority : 500 ,
1369
+ Foundation : "750" ,
1370
+ })
1371
+ if err != nil {
1372
+ t .Error (err )
1373
+ }
1374
+
1375
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"a" , "b" }))
1376
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"c" , "d" }))
1377
+
1378
+ require .True (t , candidate .RemoveExtension ("a" ))
1379
+
1380
+ extensions := candidate .Extensions ()
1381
+ require .Equal (t , []CandidateExtension {{"c" , "d" }}, extensions )
1382
+ })
1383
+
1384
+ t .Run ("Remove extension that does not exist" , func (t * testing.T ) {
1385
+ candidate , err := NewCandidateHost (& CandidateHostConfig {
1386
+ Network : NetworkTypeUDP4 .String (),
1387
+ Address : "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a" ,
1388
+ Port : 53987 ,
1389
+ Priority : 500 ,
1390
+ Foundation : "750" ,
1391
+ })
1392
+ if err != nil {
1393
+ t .Error (err )
1394
+ }
1395
+
1396
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"a" , "b" }))
1397
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"c" , "d" }))
1398
+
1399
+ require .False (t , candidate .RemoveExtension ("b" ))
1400
+
1401
+ extensions := candidate .Extensions ()
1402
+ require .Equal (t , []CandidateExtension {{"a" , "b" }, {"c" , "d" }}, extensions )
1403
+ })
1404
+
1405
+ t .Run ("Remove tcptype extension" , func (t * testing.T ) {
1406
+ candidate , err := NewCandidateHost (& CandidateHostConfig {
1407
+ Network : NetworkTypeTCP4 .String (),
1408
+ Address : "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a" ,
1409
+ Port : 53987 ,
1410
+ Priority : 500 ,
1411
+ Foundation : "750" ,
1412
+ TCPType : TCPTypeActive ,
1413
+ })
1414
+ if err != nil {
1415
+ t .Error (err )
1416
+ }
1417
+
1418
+ // tcptype extension should be removed, even if it's not in the extensions list (Not Parsed)
1419
+ require .True (t , candidate .RemoveExtension ("tcptype" ))
1420
+ require .Equal (t , TCPTypeUnspecified , candidate .TCPType ())
1421
+ require .Empty (t , candidate .Extensions ())
1422
+
1423
+ require .NoError (t , candidate .AddExtension (CandidateExtension {"tcptype" , "passive" }))
1424
+
1425
+ require .True (t , candidate .RemoveExtension ("tcptype" ))
1426
+ require .Equal (t , TCPTypeUnspecified , candidate .TCPType ())
1427
+ require .Empty (t , candidate .Extensions ())
1428
+ })
1429
+ }
0 commit comments