| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.util; |
| 12 |
|
| 13 |
import static org.junit.jupiter.api.Assertions.assertTrue; |
| 14 |
import static org.junit.jupiter.api.Assertions.fail; |
| 15 |
|
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.List; |
| 18 |
|
| 19 |
import org.junit.jupiter.api.Test; |
| 20 |
|
| 21 |
import org.minidns.record.SRV; |
| 22 |
|
| 23 |
public class SrvUtilTest { |
| 24 |
|
| 25 |
@Test |
| 26 |
public void sortSRVlowestPrioFirstTest() { |
| 27 |
List<SRV> sortedRecords = SrvUtil.sortSrvRecords(createSRVRecords()); |
| 28 |
assertTrue(sortedRecords.get(0).target.ace.equals("0.20.foo.bar")); |
| 29 |
} |
| 30 |
|
| 31 |
@Test |
| 32 |
public void sortSRVdistributeOverWeights() { |
| 33 |
int weight50 = 0, weight20one = 0, weight20two = 0, weight10 = 0; |
| 34 |
for (int i = 0; i < 1000; i++) { |
| 35 |
List<SRV> sortedRecords = SrvUtil.sortSrvRecords(createSRVRecords()); |
| 36 |
String host = sortedRecords.get(1).target.ace; |
| 37 |
if (host.equals("5.20.one.foo.bar")) { |
| 38 |
weight20one++; |
| 39 |
} else if (host.equals("5.20.two.foo.bar")) { |
| 40 |
weight20two++; |
| 41 |
} else if (host.equals("5.10.foo.bar")) { |
| 42 |
weight10++; |
| 43 |
} else if (host.equals("5.50.foo.bar")) { |
| 44 |
weight50++; |
| 45 |
} else { |
| 46 |
fail("Wrong host after SRVRecord sorting"); |
| 47 |
} |
| 48 |
} |
| 49 |
assertTrue(weight50 > 400 && weight50 < 600); |
| 50 |
assertTrue(weight20one > 100 && weight20one < 300); |
| 51 |
assertTrue(weight20two > 100 && weight20two < 300); |
| 52 |
assertTrue(weight10 > 0 && weight10 < 200); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void sortSRVdistributeZeroWeights() { |
| 57 |
int weightZeroOne = 0, weightZeroTwo = 0; |
| 58 |
for (int i = 0; i < 1000; i++) { |
| 59 |
List<SRV> sortedRecords = SrvUtil.sortSrvRecords(createSRVRecords()); |
| 60 |
// Remove the first 5 records with a lower priority |
| 61 |
for (int j = 0; j < 5; j++) { |
| 62 |
sortedRecords.remove(0); |
| 63 |
} |
| 64 |
String host = sortedRecords.remove(0).target.ace; |
| 65 |
if (host.equals("10.0.one.foo.bar")) { |
| 66 |
weightZeroOne++; |
| 67 |
} else if (host.endsWith("10.0.two.foo.bar")) { |
| 68 |
weightZeroTwo++; |
| 69 |
} else { |
| 70 |
fail("Wrong host after SRVRecord sorting"); |
| 71 |
} |
| 72 |
} |
| 73 |
assertTrue(weightZeroOne > 400 && weightZeroOne < 600); |
| 74 |
assertTrue(weightZeroTwo > 400 && weightZeroTwo < 600); |
| 75 |
} |
| 76 |
|
| 77 |
private static List<SRV> createSRVRecords() { |
| 78 |
List<SRV> records = new ArrayList<>(); |
| 79 |
|
| 80 |
// We create one record with priority 0 that should also be tried first |
| 81 |
// Then 4 records with priority 5 and different weights (50, 20, 20, 10) |
| 82 |
// Then 2 records with priority 10 and weight 0 which should be treated equal |
| 83 |
// These records are added in a 'random' way to the list |
| 84 |
records.add(new SRV(5, 20, 42, "5.20.one.foo.bar")); // Priority 5, Weight 20 |
| 85 |
records.add(new SRV(10, 0, 42, "10.0.one.foo.bar")); // Priority 10, Weight 0 |
| 86 |
records.add(new SRV(5, 10, 42, "5.10.foo.bar")); // Priority 5, Weight 10 |
| 87 |
records.add(new SRV(10, 0, 42, "10.0.two.foo.bar")); // Priority 10, Weight 0 |
| 88 |
records.add(new SRV(5, 20, 42, "5.20.two.foo.bar")); // Priority 5, Weight 20 |
| 89 |
records.add(new SRV(0, 20, 42, "0.20.foo.bar")); // Priority 0, Weight 20 |
| 90 |
records.add(new SRV(5, 50, 42, "5.50.foo.bar")); // Priority 5, Weight 50 |
| 91 |
|
| 92 |
return records; |
| 93 |
} |
| 94 |
} |