Skip to content

Commit 2e6a151

Browse files
Merge pull request #116 from AikidoSec/AIK-4395
Add more test coverage
2 parents d59a197 + 0639b46 commit 2e6a151

File tree

8 files changed

+474
-0
lines changed

8 files changed

+474
-0
lines changed

agent_api/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ test {
6161
events "passed", "skipped", "failed"
6262
exceptionFormat "full"
6363
}
64+
jacoco {
65+
enabled = true
66+
}
6467
}
6568

6669
jacocoTestReport {

agent_api/src/main/java/dev/aikido/agent_api/vulnerabilities/sql_injection/GetBinaryPath.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ private static String getFileName() {
2121
fileName.append("aarch64-"); // Add architecture to file name
2222
} else if (architecture.contains("64")) {
2323
fileName.append("x86_64-"); // Add architecture to file name
24+
} else {
25+
fileName.append("x86_64-"); // Default to x86-64
2426
}
2527

2628
if (os.contains("win")) {
@@ -29,6 +31,8 @@ private static String getFileName() {
2931
fileName.append("apple-darwin.dylib"); // macOS
3032
} else if (os.contains("nix") || os.contains("nux")) {
3133
fileName.append("unknown-linux-gnu.so"); // Linux
34+
} else {
35+
fileName.append("unknown-linux-gnu.so"); // Default to linux.
3236
}
3337
return fileName.toString();
3438
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import dev.aikido.agent_api.helpers.logging.LogLevel;
2+
import org.junit.jupiter.api.Test;
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
public class LogLevelTest {
7+
8+
@Test
9+
public void testLogLevelValues() {
10+
assertEquals(0, LogLevel.TRACE.getLevel());
11+
assertEquals(1, LogLevel.DEBUG.getLevel());
12+
assertEquals(2, LogLevel.INFO.getLevel());
13+
assertEquals(3, LogLevel.WARN.getLevel());
14+
assertEquals(4, LogLevel.ERROR.getLevel());
15+
assertEquals(5, LogLevel.FATAL.getLevel());
16+
}
17+
18+
@Test
19+
public void testLogLevelToString() {
20+
assertEquals("TRACE", LogLevel.TRACE.toString());
21+
assertEquals("DEBUG", LogLevel.DEBUG.toString());
22+
assertEquals("INFO", LogLevel.INFO.toString());
23+
assertEquals("WARN", LogLevel.WARN.toString());
24+
assertEquals("ERROR", LogLevel.ERROR.toString());
25+
assertEquals("FATAL", LogLevel.FATAL.toString());
26+
}
27+
}

agent_api/src/test/java/ShouldBlockRequestTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import dev.aikido.agent_api.SetUser;
22
import dev.aikido.agent_api.ShouldBlockRequest;
33
import dev.aikido.agent_api.background.Endpoint;
4+
import dev.aikido.agent_api.background.ipc_commands.ShouldRateLimitCommand;
5+
import dev.aikido.agent_api.background.utilities.ThreadIPCClient;
46
import dev.aikido.agent_api.context.Context;
57
import dev.aikido.agent_api.context.ContextObject;
68
import dev.aikido.agent_api.context.User;
9+
import dev.aikido.agent_api.ratelimiting.ShouldRateLimit;
710
import dev.aikido.agent_api.storage.routes.Routes;
811
import dev.aikido.agent_api.thread_cache.ThreadCache;
912
import dev.aikido.agent_api.thread_cache.ThreadCacheObject;
@@ -17,7 +20,11 @@
1720
import java.sql.SQLException;
1821
import java.util.*;
1922

23+
import static dev.aikido.agent_api.background.utilities.ThreadIPCClientFactory.getDefaultThreadIPCClient;
2024
import static org.junit.jupiter.api.Assertions.*;
25+
import static org.mockito.ArgumentMatchers.any;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
2128
import static utils.EmtpyThreadCacheObject.getEmptyThreadCacheObject;
2229

2330
public class ShouldBlockRequestTest {
@@ -209,4 +216,62 @@ public void testThreadClientInvalid2() throws SQLException {
209216
assertFalse(res1.block());
210217
}
211218

219+
@Test
220+
@SetEnvironmentVariable(key = "AIKIDO_TOKEN", value = "valid-token")
221+
public void testNoEndpointsConfigured() throws SQLException {
222+
// Set up context with a user
223+
ContextObject ctx = new SampleContextObject();
224+
ctx.setUser(new User("ID3", "Alice", "192.168.1.3", 100));
225+
Context.set(ctx);
226+
227+
// Set up thread cache with no endpoints
228+
ThreadCache.set(new ThreadCacheObject(List.of(), Set.of(), Set.of(), new Routes(), Optional.empty()));
229+
230+
// Call the method
231+
var res = ShouldBlockRequest.shouldBlockRequest();
232+
233+
// Assert that the request is not blocked
234+
assertFalse(res.block());
235+
}
236+
@Test
237+
@SetEnvironmentVariable(key = "AIKIDO_TOKEN", value = "valid-token")
238+
public void testBlockedUserWithMultipleEndpoints() throws SQLException {
239+
// Set up context with a blocked user
240+
ContextObject ctx = new SampleContextObject();
241+
ctx.setUser(new User("ID1", "John Doe", "192.168.1.1", 100));
242+
Context.set(ctx);
243+
244+
// Set up thread cache with multiple endpoints and a blocked user
245+
ThreadCache.set(new ThreadCacheObject(List.of(
246+
new Endpoint("GET", "/api/resource", 1, 1000, Collections.emptyList(), false, false, true),
247+
new Endpoint("POST", "/api/resource", 1, 1000, Collections.emptyList(), false, false, true)
248+
), Set.of("ID1"), Set.of(), new Routes(), Optional.empty()));
249+
250+
// Call the method
251+
var res = ShouldBlockRequest.shouldBlockRequest();
252+
253+
// Assert that the request is blocked due to the user being blocked
254+
assertTrue(res.block());
255+
assertEquals("user", res.data().trigger());
256+
assertEquals("blocked", res.data().type());
257+
assertEquals("192.168.1.1", res.data().ip());
258+
}
259+
260+
@Test
261+
@SetEnvironmentVariable(key = "AIKIDO_TOKEN", value = "valid-token")
262+
public void testNoUserWithEndpoints() throws SQLException {
263+
// Set up context without a user
264+
Context.set(new SampleContextObject());
265+
266+
// Set up thread cache with endpoints
267+
ThreadCache.set(new ThreadCacheObject(List.of(
268+
new Endpoint("GET", "/api/resource", 1, 1000, Collections.emptyList(), false, false, true)
269+
), Set.of(), Set.of(), new Routes(), Optional.empty()));
270+
271+
// Call the method
272+
var res = ShouldBlockRequest.shouldBlockRequest();
273+
274+
// Assert that the request is not blocked
275+
assertFalse(res.block());
276+
}
212277
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package helpers.env;
2+
3+
import dev.aikido.agent_api.helpers.env.BooleanEnv;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
7+
8+
import static org.junit.jupiter.api.Assertions.assertFalse;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
public class BooleanEnvTest {
12+
13+
@Test
14+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "1")
15+
public void testBooleanEnv_WithValueOne() {
16+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", false);
17+
assertTrue(booleanEnv.getValue());
18+
}
19+
20+
@Test
21+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "true")
22+
public void testBooleanEnv_WithValueTrue() {
23+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", false);
24+
assertTrue(booleanEnv.getValue());
25+
}
26+
27+
@Test
28+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "TRUE")
29+
public void testBooleanEnv_WithValueTrueUppercase() {
30+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", false);
31+
assertTrue(booleanEnv.getValue());
32+
}
33+
34+
@Test
35+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "0")
36+
public void testBooleanEnv_WithValueZero() {
37+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", true);
38+
assertFalse(booleanEnv.getValue());
39+
}
40+
41+
@Test
42+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "false")
43+
public void testBooleanEnv_WithValueFalse() {
44+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", true);
45+
assertFalse(booleanEnv.getValue());
46+
}
47+
48+
@Test
49+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "FALSE")
50+
public void testBooleanEnv_WithValueFalseUppercase() {
51+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", true);
52+
assertFalse(booleanEnv.getValue());
53+
}
54+
55+
@Test
56+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "")
57+
public void testBooleanEnv_WithEmptyString() {
58+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", true);
59+
assertTrue(booleanEnv.getValue());
60+
}
61+
62+
@Test
63+
public void testBooleanEnv_WithNullEnvironmentVariable() {
64+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", true);
65+
assertTrue(booleanEnv.getValue());
66+
}
67+
68+
@Test
69+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "randomString")
70+
public void testBooleanEnv_WithRandomString() {
71+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", false);
72+
assertFalse(booleanEnv.getValue());
73+
}
74+
75+
@Test
76+
@SetEnvironmentVariable(key = "TEST_BOOLEAN_ENV", value = "TrUe")
77+
public void testBooleanEnv_WithMixedCaseTrue() {
78+
BooleanEnv booleanEnv = new BooleanEnv("TEST_BOOLEAN_ENV", false);
79+
assertTrue(booleanEnv.getValue());
80+
}
81+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package helpers.env;
2+
3+
import dev.aikido.agent_api.helpers.env.Endpoints;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.AfterEach;
9+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class EndpointsTest {
14+
@Test
15+
@SetEnvironmentVariable(key = "AIKIDO_ENDPOINT", value = "https://custom.aikido.dev")
16+
public void testGetAikidoAPIEndpoint_WithCustomEndpoint() {
17+
String result = Endpoints.getAikidoAPIEndpoint();
18+
assertEquals("https://custom.aikido.dev/", result);
19+
}
20+
21+
@Test
22+
@SetEnvironmentVariable(key = "AIKIDO_ENDPOINT", value = "https://custom.aikido.dev")
23+
public void testGetAikidoAPIEndpoint_WithCustomEndpointWithoutTrailingSlash() {
24+
String result = Endpoints.getAikidoAPIEndpoint();
25+
assertEquals("https://custom.aikido.dev/", result);
26+
}
27+
28+
@Test
29+
@SetEnvironmentVariable(key = "AIKIDO_ENDPOINT", value = "")
30+
public void testGetAikidoAPIEndpoint_WithEmptyEnvironmentVariable() {
31+
String result = Endpoints.getAikidoAPIEndpoint();
32+
assertEquals("https://guard.aikido.dev/", result);
33+
}
34+
35+
@Test
36+
public void testGetAikidoAPIEndpoint_WithNullEnvironmentVariable() {
37+
// No environment variable set, should return default
38+
String result = Endpoints.getAikidoAPIEndpoint();
39+
assertEquals("https://guard.aikido.dev/", result);
40+
}
41+
42+
@Test
43+
@SetEnvironmentVariable(key = "AIKIDO_REALTIME_ENDPOINT", value = "https://realtime.aikido.dev")
44+
public void testGetAikidoRealtimeEndpoint_WithCustomEndpoint() {
45+
String result = Endpoints.getAikidoRealtimeEndpoint();
46+
assertEquals("https://realtime.aikido.dev/", result);
47+
}
48+
49+
@Test
50+
@SetEnvironmentVariable(key = "AIKIDO_REALTIME_ENDPOINT", value = "https://realtime.aikido.dev")
51+
public void testGetAikidoRealtimeEndpoint_WithCustomEndpointWithoutTrailingSlash() {
52+
String result = Endpoints.getAikidoRealtimeEndpoint();
53+
assertEquals("https://realtime.aikido.dev/", result);
54+
}
55+
56+
@Test
57+
public void testGetAikidoRealtimeEndpoint_WithNullEnvironmentVariable() {
58+
// No environment variable set, should return default
59+
String result = Endpoints.getAikidoRealtimeEndpoint();
60+
assertEquals("https://runtime.aikido.dev/", result);
61+
}
62+
@Test
63+
@SetEnvironmentVariable(key = "AIKIDO_REALTIME_ENDPOINT", value = "https://realtime.aikido.dev/")
64+
public void testGetAikidoRealtimeEndpoint_WithCustomEndpointWithTrailingSlash() {
65+
String result = Endpoints.getAikidoRealtimeEndpoint();
66+
assertEquals("https://realtime.aikido.dev/", result);
67+
}
68+
69+
@Test
70+
@SetEnvironmentVariable(key = "AIKIDO_REALTIME_ENDPOINT", value = "")
71+
public void testGetAikidoRealtimeEndpoint_WithEmptyEnvironmentVariable() {
72+
String result = Endpoints.getAikidoRealtimeEndpoint();
73+
assertEquals("https://runtime.aikido.dev/", result);
74+
}
75+
76+
// Additional tests
77+
@Test
78+
@SetEnvironmentVariable(key = "AIKIDO_ENDPOINT", value = "https://missing-slash.aikido.dev")
79+
public void testGetAikidoAPIEndpoint_WithMissingSlash() {
80+
String result = Endpoints.getAikidoAPIEndpoint();
81+
assertEquals("https://missing-slash.aikido.dev/", result);
82+
}
83+
84+
@Test
85+
@SetEnvironmentVariable(key = "AIKIDO_ENDPOINT", value = "https://missing-slash.aikido.dev/")
86+
public void testGetAikidoAPIEndpoint_WithTrailingSlashAlreadyPresent() {
87+
String result = Endpoints.getAikidoAPIEndpoint();
88+
assertEquals("https://missing-slash.aikido.dev/", result);
89+
}
90+
91+
@Test
92+
@SetEnvironmentVariable(key = "AIKIDO_ENDPOINT", value = "https://another-custom.aikido.dev")
93+
public void testGetAikidoAPIEndpoint_WithAnotherCustomEndpoint() {
94+
String result = Endpoints.getAikidoAPIEndpoint();
95+
assertEquals("https://another-custom.aikido.dev/", result);
96+
}
97+
98+
@Test
99+
@SetEnvironmentVariable(key = "AIKIDO_ENDPOINT", value = "https://another-custom.aikido.dev/")
100+
public void testGetAikidoAPIEndpoint_WithAnotherCustomEndpointWithTrailingSlash() {
101+
String result = Endpoints.getAikidoAPIEndpoint();
102+
assertEquals("https://another-custom.aikido.dev/", result);
103+
}
104+
}

0 commit comments

Comments
 (0)