Skip to content

Commit dd1de65

Browse files
committed
Improved JWT Token utils lib
1 parent bdcfc0a commit dd1de65

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

xds/src/test/java/io/grpc/xds/JwtTokenFileCallCredentialsTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
import java.util.Date;
3131
import java.util.concurrent.TimeUnit;
3232
import org.junit.Before;
33+
import org.junit.Rule;
3334
import org.junit.Test;
3435
import org.junit.experimental.runners.Enclosed;
36+
import org.junit.rules.TemporaryFolder;
3537
import org.junit.runner.RunWith;
3638
import org.junit.runners.JUnit4;
3739

@@ -40,12 +42,15 @@
4042
public class JwtTokenFileCallCredentialsTest {
4143
@RunWith(JUnit4.class)
4244
public static class WithEmptyJwtTokenTest {
45+
@Rule
46+
public TemporaryFolder tempFolder = new TemporaryFolder();
47+
4348
private File jwtTokenFile;
4449
private JwtTokenFileCallCredentials unit;
4550

4651
@Before
4752
public void setUp() throws Exception {
48-
this.jwtTokenFile = JwtTokenFileTestUtils.createEmptyJwtToken();
53+
this.jwtTokenFile = tempFolder.newFile("empty_jwt.token");
4954

5055
Constructor<JwtTokenFileCallCredentials> ctor =
5156
JwtTokenFileCallCredentials.class.getDeclaredConstructor(String.class);
@@ -63,12 +68,16 @@ public void givenJwtTokenFileEmpty_WhenTokenRefreshed_ExpectException() {
6368

6469
@RunWith(JUnit4.class)
6570
public static class WithInvalidJwtTokenTest {
71+
@Rule
72+
public TemporaryFolder tempFolder = new TemporaryFolder();
73+
6674
private File jwtTokenFile;
6775
private JwtTokenFileCallCredentials unit;
6876

6977
@Before
7078
public void setUp() throws Exception {
71-
this.jwtTokenFile = JwtTokenFileTestUtils.createJwtTokenWithoutExpiration();
79+
this.jwtTokenFile = tempFolder.newFile("invalid_jwt.token");
80+
JwtTokenFileTestUtils.writeJwtTokenContentWithoutExpiration(jwtTokenFile);
7281

7382
Constructor<JwtTokenFileCallCredentials> ctor =
7483
JwtTokenFileCallCredentials.class.getDeclaredConstructor(String.class);
@@ -92,15 +101,19 @@ public void givenJwtTokenFileWithoutExpiration_WhenTokenRefreshed_ExpectExceptio
92101

93102
@RunWith(JUnit4.class)
94103
public static class WithValidJwtTokenTest {
104+
@Rule
105+
public TemporaryFolder tempFolder = new TemporaryFolder();
106+
95107
private File jwtTokenFile;
96108
private JwtTokenFileCallCredentials unit;
97109
private Long givenExpTimeInSeconds;
98110

99111
@Before
100112
public void setUp() throws Exception {
113+
this.jwtTokenFile = tempFolder.newFile("jwt.token");
101114
this.givenExpTimeInSeconds = Instant.now().getEpochSecond() + TimeUnit.HOURS.toSeconds(1);
102-
103-
this.jwtTokenFile = JwtTokenFileTestUtils.createValidJwtToken(givenExpTimeInSeconds);
115+
116+
JwtTokenFileTestUtils.writeValidJwtTokenContent(jwtTokenFile, givenExpTimeInSeconds);
104117

105118
Constructor<JwtTokenFileCallCredentials> ctor =
106119
JwtTokenFileCallCredentials.class.getDeclaredConstructor(String.class);

xds/src/test/java/io/grpc/xds/JwtTokenFileTestUtils.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,7 @@
2323
import java.nio.charset.StandardCharsets;
2424

2525
public class JwtTokenFileTestUtils {
26-
public static File createEmptyJwtToken() throws IOException {
27-
File jwtToken = File.createTempFile(new String("jwt.token"), "");
28-
jwtToken.deleteOnExit();
29-
return jwtToken;
30-
}
31-
32-
public static File createJwtTokenWithoutExpiration() throws IOException {
33-
File jwtToken = File.createTempFile(new String("jwt.token"), "");
34-
jwtToken.deleteOnExit();
26+
public static void writeJwtTokenContentWithoutExpiration(File jwtToken) throws IOException {
3527
FileOutputStream outputStream = new FileOutputStream(jwtToken);
3628
String content =
3729
BaseEncoding.base64().encode(
@@ -43,13 +35,10 @@ public static File createJwtTokenWithoutExpiration() throws IOException {
4335
+ BaseEncoding.base64().encode(new String("signature").getBytes(StandardCharsets.UTF_8));
4436
outputStream.write(content.getBytes(StandardCharsets.UTF_8));
4537
outputStream.close();
46-
return jwtToken;
4738
}
4839

49-
public static File createValidJwtToken(long expTime)
40+
public static void writeValidJwtTokenContent(File jwtToken, long expTime)
5041
throws Exception {
51-
File jwtToken = File.createTempFile(new String("jwt.token"), "");
52-
jwtToken.deleteOnExit();
5342
FileOutputStream outputStream = new FileOutputStream(jwtToken);
5443
String content =
5544
BaseEncoding.base64().encode(
@@ -61,6 +50,5 @@ public static File createValidJwtToken(long expTime)
6150
+ BaseEncoding.base64().encode(new String("signature").getBytes(StandardCharsets.UTF_8));
6251
outputStream.write(content.getBytes(StandardCharsets.UTF_8));
6352
outputStream.close();
64-
return jwtToken;
6553
}
6654
}

xds/src/test/java/io/grpc/xds/SharedXdsClientPoolProviderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.concurrent.TimeUnit;
5959
import org.junit.Rule;
6060
import org.junit.Test;
61+
import org.junit.rules.TemporaryFolder;
6162
import org.junit.runner.RunWith;
6263
import org.junit.runners.JUnit4;
6364
import org.mockito.Mock;
@@ -70,6 +71,8 @@ public class SharedXdsClientPoolProviderTest {
7071

7172
private static final String SERVER_URI = "trafficdirector.googleapis.com";
7273
@Rule
74+
public TemporaryFolder tempFolder = new TemporaryFolder();
75+
@Rule
7376
public final MockitoRule mocks = MockitoJUnit.rule();
7477
private final Node node = Node.newBuilder().setId("SharedXdsClientPoolProviderTest").build();
7578
private final MetricRecorder metricRecorder = new MetricRecorder() {};
@@ -225,7 +228,8 @@ public void xdsClient_usesJwtTokenFileCallCredentials() throws Exception {
225228
GrpcBootstrapperImpl.xdsBootstrapCallCredsEnabled = true;
226229

227230
Long givenExpTimeInSeconds = Instant.now().getEpochSecond() + TimeUnit.HOURS.toSeconds(1);
228-
File jwtToken = JwtTokenFileTestUtils.createValidJwtToken(givenExpTimeInSeconds);
231+
File jwtToken = tempFolder.newFile("jwt.token");
232+
JwtTokenFileTestUtils.writeValidJwtTokenContent(jwtToken, givenExpTimeInSeconds);
229233
String jwtTokenContent = new String(
230234
Files.readAllBytes(jwtToken.toPath()),
231235
StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)