소스 검색

[TEST] Prevent duplicate key in XContent test (#36581)

It was possible to generate a response that would output duplicate
keys

Closes: #36473
Tim Vernum 6 년 전
부모
커밋
c650be72cf

+ 3 - 1
x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/user/HasPrivilegesResponseTests.java

@@ -11,6 +11,7 @@ import org.elasticsearch.common.bytes.BytesReference;
 import org.elasticsearch.common.collect.MapBuilder;
 import org.elasticsearch.common.io.stream.BytesStreamOutput;
 import org.elasticsearch.common.io.stream.StreamInput;
+import org.elasticsearch.common.util.set.Sets;
 import org.elasticsearch.common.xcontent.ToXContent;
 import org.elasticsearch.common.xcontent.XContentBuilder;
 import org.elasticsearch.common.xcontent.XContentParser;
@@ -155,7 +156,8 @@ public class HasPrivilegesResponseTests
 
     private Collection<HasPrivilegesResponse.ResourcePrivileges> randomResourcePrivileges() {
         final Collection<HasPrivilegesResponse.ResourcePrivileges> list = new ArrayList<>();
-        for (String resource : randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(2, 6))) {
+        // Use hash set to force a unique set of resources
+        for (String resource : Sets.newHashSet(randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(2, 6)))) {
             final Map<String, Boolean> privileges = new HashMap<>();
             for (String priv : randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) {
                 privileges.put(priv, randomBoolean());