| 1 |
|
package org.europa.together.application; |
| 2 |
|
|
| 3 |
|
import static org.junit.jupiter.api.Assertions.*; |
| 4 |
|
import static com.google.code.beanmatchers.BeanMatchers.*; |
| 5 |
|
import java.sql.SQLException; |
| 6 |
|
import java.util.ArrayList; |
| 7 |
|
import java.util.List; |
| 8 |
|
import org.europa.together.JUnit5Preperator; |
| 9 |
|
import org.europa.together.business.DatabaseActions; |
| 10 |
|
import org.europa.together.business.JsonWebToken; |
| 11 |
|
import org.europa.together.business.Logger; |
| 12 |
|
import org.europa.together.domain.ConfigurationDO; |
| 13 |
|
import org.europa.together.domain.LogLevel; |
| 14 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
| 15 |
|
import org.junit.jupiter.api.AfterAll; |
| 16 |
|
import org.junit.jupiter.api.AfterEach; |
| 17 |
|
import org.junit.jupiter.api.Assumptions; |
| 18 |
|
import org.junit.jupiter.api.BeforeAll; |
| 19 |
|
import org.junit.jupiter.api.BeforeEach; |
| 20 |
|
import org.junit.jupiter.api.Test; |
| 21 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
| 22 |
|
import org.springframework.beans.factory.annotation.Autowired; |
| 23 |
|
import org.springframework.test.context.ContextConfiguration; |
| 24 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 25 |
|
|
| 26 |
|
@SuppressWarnings("unchecked") |
| 27 |
|
@ExtendWith({SpringExtension.class, JUnit5Preperator.class}) |
| 28 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
| |
|
| 100% |
Uncovered Elements: 0 (70) |
Complexity: 15 |
Complexity Density: 0.27 |
|
| 29 |
|
public class NimbusJwtTest { |
| 30 |
|
|
| 31 |
|
private static final Logger LOGGER = new LogbackLogger(NimbusJwtTest.class); |
| 32 |
|
|
| 33 |
|
private static final String FLUSH_TABLE |
| 34 |
|
= "TRUNCATE TABLE " + ConfigurationDO.TABLE_NAME + ";"; |
| 35 |
|
private static final String FILE |
| 36 |
|
= "org/europa/together/sql/jwt-config.sql"; |
| 37 |
|
|
| 38 |
|
private static DatabaseActions jdbcActions |
| 39 |
|
= JUnit5Preperator.JDBC_CONNECTION; |
| 40 |
|
|
| 41 |
|
@Autowired |
| 42 |
|
JsonWebToken jsonWebToken; |
| 43 |
|
|
| 44 |
|
String validToken |
| 45 |
|
= "eyJhbGciOiJIUzUxMiJ9.e0hhbGxvIFdvcmxkIEpTT059.i2C8x1zuXKnLgorrthDLj9gHQppUo4WPzmK_kPXFoNa2mnsNa3nwle6X68cdyTVtdH-x5cLreuYI8A5PMqoLOQ"; |
| 46 |
|
|
| 47 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 48 |
1 |
@BeforeAll... |
| 49 |
|
static void setUp() { |
| 50 |
1 |
Assumptions.assumeTrue(JUnit5Preperator.isConnected(), "JDBC DBMS Connection failed."); |
| 51 |
1 |
jdbcActions.executeSqlFromClasspath(FILE); |
| 52 |
|
|
| 53 |
1 |
LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG); |
| 54 |
|
} |
| 55 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 56 |
1 |
@AfterAll... |
| 57 |
|
static void tearDown() throws SQLException { |
| 58 |
|
} |
| 59 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 60 |
11 |
@BeforeEach... |
| 61 |
|
void testCaseInitialization() { |
| 62 |
11 |
jdbcActions.executeSqlFromClasspath(FILE); |
| 63 |
|
} |
| 64 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 65 |
11 |
@AfterEach... |
| 66 |
|
void testCaseTermination() throws SQLException { |
| 67 |
11 |
jdbcActions.executeQuery(FLUSH_TABLE); |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 71 |
1 |
@Test... |
| 72 |
|
void constructor() { |
| 73 |
1 |
LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG); |
| 74 |
1 |
assertThat(NimbusJwt.class, hasValidBeanConstructor()); |
| 75 |
|
} |
| 76 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 77 |
1 |
@Test... |
| 78 |
|
void buildHMAC512SignedJws() throws Exception { |
| 79 |
1 |
LOGGER.log("TEST CASE: buildHMAC512SignedJws", LogLevel.DEBUG); |
| 80 |
|
|
| 81 |
1 |
String singedJws = jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}"); |
| 82 |
1 |
assertEquals(validToken.length(), singedJws.length()); |
| 83 |
1 |
assertNotEquals(validToken, singedJws); |
| 84 |
|
} |
| 85 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 86 |
1 |
@Test... |
| 87 |
|
void failBuildHMAC512SignedJwsEmptySecret() throws Exception { |
| 88 |
1 |
LOGGER.log("TEST CASE: failBuildHMAC512SignedJwsEmptySecret", LogLevel.DEBUG); |
| 89 |
|
|
| 90 |
1 |
jdbcActions.executeQuery(FLUSH_TABLE); |
| 91 |
1 |
jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql"); |
| 92 |
|
|
| 93 |
1 |
assertThrows(Exception.class, () -> { |
| 94 |
1 |
jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}"); |
| 95 |
|
}); |
| 96 |
|
} |
| 97 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 98 |
1 |
@Test... |
| 99 |
|
void parseHMAC512SingedJws() throws Exception { |
| 100 |
1 |
LOGGER.log("TEST CASE: parseHMAC512SingedJws", LogLevel.DEBUG); |
| 101 |
|
|
| 102 |
1 |
String singedJws = jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}"); |
| 103 |
1 |
assertEquals("{Hallo World JSON}", jsonWebToken.parseHMAC512SingedJws(singedJws)); |
| 104 |
|
} |
| 105 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 106 |
1 |
@Test... |
| 107 |
|
void failParseHMAC512SingedJws() throws Exception { |
| 108 |
1 |
LOGGER.log("TEST CASE: failParseHMAC512SingedJws", LogLevel.DEBUG); |
| 109 |
|
|
| 110 |
1 |
assertThrows(Exception.class, () -> { |
| 111 |
1 |
jsonWebToken.parseHMAC512SingedJws("fail parsing"); |
| 112 |
|
}); |
| 113 |
|
} |
| 114 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 115 |
1 |
@Test... |
| 116 |
|
void parseHMAC512invalidSigningJws() throws Exception { |
| 117 |
1 |
LOGGER.log("TEST CASE: parseHMAC512invalidSigningJws", LogLevel.DEBUG); |
| 118 |
|
|
| 119 |
1 |
jdbcActions.executeQuery(FLUSH_TABLE); |
| 120 |
1 |
jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql"); |
| 121 |
|
|
| 122 |
1 |
assertThrows(Exception.class, () -> { |
| 123 |
1 |
jsonWebToken.parseHMAC512SingedJws("invalid"); |
| 124 |
|
}); |
| 125 |
|
} |
| 126 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 127 |
1 |
@Test... |
| 128 |
|
void buildHMAC512SignedJwt() throws Exception { |
| 129 |
1 |
LOGGER.log("TEST CASE: buildHMAC512SignedJwt", LogLevel.DEBUG); |
| 130 |
|
|
| 131 |
1 |
List<String> audience = new ArrayList<>(); |
| 132 |
1 |
audience.add("https://app.togehter-platform.org"); |
| 133 |
1 |
String singedJwt = jsonWebToken.buildHMAC512SignedJwt(audience); |
| 134 |
|
|
| 135 |
1 |
assertEquals(364, singedJwt.length()); |
| 136 |
|
} |
| 137 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 138 |
1 |
@Test... |
| 139 |
|
void failBuildHMAC512SignedJwtEmptySecret() throws Exception { |
| 140 |
1 |
LOGGER.log("TEST CASE: failBuildHMAC512SignedJwtEmptySecret", LogLevel.DEBUG); |
| 141 |
|
|
| 142 |
1 |
jdbcActions.executeQuery(FLUSH_TABLE); |
| 143 |
1 |
jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql"); |
| 144 |
|
|
| 145 |
1 |
assertThrows(Exception.class, () -> { |
| 146 |
1 |
List<String> audience = new ArrayList<>(); |
| 147 |
1 |
audience.add("https://app.togehter-platform.org"); |
| 148 |
|
|
| 149 |
1 |
jsonWebToken.buildHMAC512SignedJwt(audience); |
| 150 |
|
}); |
| 151 |
|
} |
| 152 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 153 |
1 |
@Test... |
| 154 |
|
void parseHMAC512SingedJwt() throws Exception { |
| 155 |
1 |
LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG); |
| 156 |
|
|
| 157 |
1 |
List<String> audience = new ArrayList<>(); |
| 158 |
1 |
audience.add("https://app.togehter-platform.org"); |
| 159 |
1 |
String jwt = jsonWebToken.buildHMAC512SignedJwt(audience); |
| 160 |
|
|
| 161 |
1 |
String payload = jsonWebToken.parseHMAC512SingedJwt(jwt); |
| 162 |
1 |
LOGGER.log("JWT Payload: " + payload, LogLevel.DEBUG); |
| 163 |
1 |
assertEquals("{\"sub\":\"tp-jws\",", payload.substring(0, 16)); |
| 164 |
|
} |
| 165 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 166 |
1 |
@Test... |
| 167 |
|
void failparseHMAC512SingedJwt() throws Exception { |
| 168 |
1 |
LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG); |
| 169 |
|
|
| 170 |
1 |
assertThrows(Exception.class, () -> { |
| 171 |
1 |
String jwt = jsonWebToken.buildHMAC512SignedJwt("", "", null); |
| 172 |
1 |
jsonWebToken.parseHMAC512SingedJwt("invalid"); |
| 173 |
|
}); |
| 174 |
|
} |
| 175 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 176 |
1 |
@Test... |
| 177 |
|
void parseHMAC512invalidSigningJwt() throws Exception { |
| 178 |
1 |
LOGGER.log("TEST CASE: parseHMAC512invalidSigningJwt", LogLevel.DEBUG); |
| 179 |
|
|
| 180 |
1 |
jdbcActions.executeQuery(FLUSH_TABLE); |
| 181 |
1 |
jdbcActions.executeSqlFromClasspath("org/europa/together/sql/jwt-fail-test.sql"); |
| 182 |
|
|
| 183 |
1 |
assertThrows(Exception.class, () -> { |
| 184 |
1 |
jsonWebToken.parseHMAC512SingedJwt("null"); |
| 185 |
|
}); |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
} |