Skip to content

Commit 07b6c8f

Browse files
committed
Duplicate tests from NetworkDirectionProcessor into NetworkDirectionutils tests
1 parent 13fe69d commit 07b6c8f

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.common.network;
11+
12+
import org.elasticsearch.test.ESTestCase;
13+
14+
import java.util.List;
15+
16+
import static org.hamcrest.Matchers.equalTo;
17+
18+
public class NetworkDirectionUtilsTests extends ESTestCase {
19+
public void testCIDR() {
20+
testNetworkDirectionUtils("10.0.1.1", "192.168.1.2", List.of("10.0.0.0/8"), "outbound");
21+
testNetworkDirectionUtils("192.168.1.2", "10.0.1.1", List.of("10.0.0.0/8"), "inbound");
22+
}
23+
24+
public void testUnspecified() {
25+
testNetworkDirectionUtils("0.0.0.0", "0.0.0.0", List.of("unspecified"), "internal");
26+
testNetworkDirectionUtils("::", "::", List.of("unspecified"), "internal");
27+
}
28+
29+
public void testNetworkPrivate() {
30+
testNetworkDirectionUtils("192.168.1.1", "192.168.1.2", List.of("private"), "internal");
31+
testNetworkDirectionUtils("10.0.1.1", "192.168.1.2", List.of("private"), "internal");
32+
testNetworkDirectionUtils("192.168.1.1", "172.16.0.1", List.of("private"), "internal");
33+
testNetworkDirectionUtils("192.168.1.1", "fd12:3456:789a:1::1", List.of("private"), "internal");
34+
}
35+
36+
public void testNetworkPublic() {
37+
testNetworkDirectionUtils("192.168.1.1", "192.168.1.2", List.of("public"), "external");
38+
testNetworkDirectionUtils("10.0.1.1", "192.168.1.2", List.of("public"), "external");
39+
testNetworkDirectionUtils("192.168.1.1", "172.16.0.1", List.of("public"), "external");
40+
testNetworkDirectionUtils("192.168.1.1", "fd12:3456:789a:1::1", List.of("public"), "external");
41+
}
42+
43+
private void testNetworkDirectionUtils(String source, String destination, List<String> networks, String expectedDirection) {
44+
boolean sourceInternal = NetworkDirectionUtils.isInternal(networks, source);
45+
boolean destinationInternal = NetworkDirectionUtils.isInternal(networks, destination);
46+
assertThat(expectedDirection, equalTo(NetworkDirectionUtils.getDirection(sourceInternal, destinationInternal)));
47+
}
48+
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,24 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
126126
}
127127

128128
@Evaluator
129-
static BytesRef process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef scratch, BytesRef sourceIp, BytesRef destinationIp, @Position int position, BytesRefBlock internalNetworks) {
129+
static BytesRef process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef scratch, BytesRef sourceIp, BytesRef destinationIp, @Position int position, BytesRefBlock networks) {
130130
// Pulling the bytes out directly using InetAddress.getByAddress() requires error handling TODO
131131
InetAddress sourceIpAddress = InetAddresses.forString(sourceIp.utf8ToString());
132132
InetAddress destinationIpAddress = InetAddresses.forString(destinationIp.utf8ToString());
133133
boolean sourceInternal = false;
134134
boolean destinationInternal = false;
135135

136-
int valueCount = internalNetworks.getValueCount(position);
137-
int first = internalNetworks.getFirstValueIndex(position);
136+
int valueCount = networks.getValueCount(position);
137+
int first = networks.getFirstValueIndex(position);
138138

139139
for (int i = first; i < first + valueCount; i++) {
140-
if (NetworkDirectionUtils.inNetwork(sourceIpAddress, internalNetworks.getBytesRef(i, scratch).utf8ToString())) {
140+
if (NetworkDirectionUtils.inNetwork(sourceIpAddress, networks.getBytesRef(i, scratch).utf8ToString())) {
141141
sourceInternal = true;
142142
break;
143143
}
144144
}
145145
for (int i = first; i < first + valueCount; i++) {
146-
if (NetworkDirectionUtils.inNetwork(destinationIpAddress, internalNetworks.getBytesRef(i, scratch).utf8ToString())) {
146+
if (NetworkDirectionUtils.inNetwork(destinationIpAddress, networks.getBytesRef(i, scratch).utf8ToString())) {
147147
destinationInternal = true;
148148
break;
149149
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirectionTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@
77

88
package org.elasticsearch.xpack.esql.expression.function.scalar.ip;
99

10+
import com.carrotsearch.randomizedtesting.annotations.Name;
11+
12+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
13+
14+
import org.apache.lucene.util.BytesRef;
15+
import org.elasticsearch.common.network.NetworkDirectionUtils;
16+
import org.elasticsearch.test.ESTestCase;
1017
import org.elasticsearch.xpack.esql.core.expression.Expression;
1118
import org.elasticsearch.xpack.esql.core.tree.Source;
19+
import org.elasticsearch.xpack.esql.core.type.DataType;
1220
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
21+
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
22+
import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter;
1323

1424
import java.util.List;
25+
import java.util.function.Supplier;
1526

1627
public class NetworkDirectionTests extends AbstractScalarFunctionTestCase {
28+
public NetworkDirectionTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
29+
this.testCase = testCaseSupplier.get();
30+
}
31+
1732
@Override
1833
protected Expression build(Source source, List<Expression> args) {
19-
return null;
34+
return new NetworkDirection(source, args.get(0), args.get(1), args.get(2));
2035
}
2136
}

0 commit comments

Comments
 (0)