forked from airsonic-advanced/airsonic-advanced
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from kagemomiji/issue355-fix-insecure-randomness
#355 Fix code scanning alert about Insecure Randomness
- Loading branch information
Showing
2 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
airsonic-main/src/test/java/org/airsonic/player/service/SecurityServiceTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.airsonic.player.service; | ||
|
||
import org.airsonic.player.domain.MusicFolder; | ||
import org.airsonic.player.domain.User; | ||
import org.airsonic.player.domain.UserCredential; | ||
import org.airsonic.player.repository.UserCredentialRepository; | ||
import org.airsonic.player.repository.UserRepository; | ||
import org.airsonic.player.security.GlobalSecurityConfig; | ||
import org.airsonic.player.security.PasswordDecoder; | ||
import org.junit.jupiter.api.RepeatedTest; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyCollection; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class SecurityServiceTestCase { | ||
|
||
@Mock | ||
private UserCredentialRepository userCredentialRepository; | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@Mock | ||
private SettingsService settingsService; | ||
|
||
@Mock | ||
private MediaFolderService mediaFolderService; | ||
|
||
@InjectMocks | ||
private SecurityService securityService; | ||
|
||
@RepeatedTest(10) | ||
public void testCreateGuestUserIfNotExists() throws Exception { | ||
|
||
ArgumentCaptor<UserCredential> argumentCaptor = ArgumentCaptor.forClass(UserCredential.class); | ||
|
||
// given | ||
when(userRepository.existsById(User.USERNAME_GUEST)).thenReturn(false); | ||
// set encoder to hex | ||
when(settingsService.getPreferNonDecodablePasswords()).thenReturn(false); | ||
when(settingsService.getDecodablePasswordEncoder()).thenReturn("hex"); | ||
when(userRepository.saveAndFlush(any(User.class))).thenReturn(null); | ||
when(userCredentialRepository.saveAndFlush(any(UserCredential.class))).thenReturn(null); | ||
when(mediaFolderService.getAllMusicFolders()).thenReturn(List.of(new MusicFolder())); | ||
|
||
// when | ||
securityService.createGuestUserIfNotExists(); | ||
|
||
// then | ||
verify(mediaFolderService).setMusicFoldersForUser(eq(User.USERNAME_GUEST), anyCollection()); | ||
verify(userCredentialRepository).saveAndFlush(argumentCaptor.capture()); | ||
UserCredential uc = argumentCaptor.getValue(); | ||
assertEquals(User.USERNAME_GUEST, uc.getUser().getUsername()); | ||
assertEquals("hex", uc.getEncoder()); | ||
PasswordDecoder decoder = (PasswordDecoder) GlobalSecurityConfig.ENCODERS.get("hex"); | ||
String credential = decoder.decode(uc.getCredential()); | ||
assertTrue(credential.matches("[0-9A-Za-z]{30}")); | ||
assertEquals("Autogenerated for " + User.USERNAME_GUEST + " user", uc.getComment()); | ||
} | ||
} |