Skip to content

Commit

Permalink
Trim knox key
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerwowen committed Jan 26, 2024
1 parent 4f8ab95 commit 569609b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ private String getKeyInternal() {
LOG.warn("Using default key since knoxManager is null");
return defaultKeyContent;
}
return knoxManager.getKey();
return knoxManager.getKey().trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,51 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

public class KnoxKeyReaderTest {
private KnoxKeyReader sut;
private KnoxKeyManager mockKnoxKeyManager;

@Before
public void setUp() {
sut = new KnoxKeyReader();
mockKnoxKeyManager = mock(KnoxKeyManager.class);
}

@Test
public void testGetKey_noInit() {
KnoxKeyReader sut = new KnoxKeyReader();
assertEquals("defaultKeyContent", sut.getKey());
}

@Test
public void testGetKey_initialized() {
KnoxKeyReader sut = new KnoxKeyReader();
sut.init("someRandomKey");
assertNull(sut.getKey());
}

@Test
public void testGetKey_cacheIsWorking() {
KnoxKeyReader sut = new KnoxKeyReader();
KnoxKeyManager mockKnoxKeyManager = mock(KnoxKeyManager.class);

sut.init("someRandomKey");
sut.setKnoxManager(mockKnoxKeyManager);
sut.getKey();
sut.getKey();

verify(mockKnoxKeyManager, only()).getKey();
}

@Test
public void testGetKey_trimmed() {
String expected = "keyWith234!~@";
when(mockKnoxKeyManager.getKey()).thenReturn(String.format(" \n\r\t %s \n\r\t", expected));

sut.init("someRandomKey");
sut.setKnoxManager(mockKnoxKeyManager);
String trimmed = sut.getKey();

assertEquals(expected, trimmed);
}
}

0 comments on commit 569609b

Please sign in to comment.