Skip to content

Commit

Permalink
add unit test to prevent NPE when user agent string is null
Browse files Browse the repository at this point in the history
  • Loading branch information
ehrenkret-signal committed May 23, 2023
1 parent a005ba3 commit 8dc7bb0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Expand Up @@ -23,6 +23,9 @@ public class UserAgentUtil {
}

public static ClientPlatform getPlatformFromUserAgentString(final String userAgentString) throws UnrecognizedUserAgentException {
if (userAgentString == null) {
throw new UnrecognizedUserAgentException();
}
final Matcher standardUaMatcher = STANDARD_UA_PATTERN.matcher(userAgentString);

if (standardUaMatcher.matches()) {
Expand Down
@@ -0,0 +1,21 @@
package org.signal.storageservice.util.ua;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

class UserAgentUtilTest {
@Test
void testGetPlatformFromUserAgentString() throws UnrecognizedUserAgentException {
String userAgentString = "Signal-Android/12.35.42 Android/31";
ClientPlatform platform = UserAgentUtil.getPlatformFromUserAgentString(userAgentString);
assertThat(platform).isSameAs(ClientPlatform.ANDROID);
}

@Test
void testGetPlatformFromUserAgentString_null() {
String userAgentString = null;
assertThatThrownBy(() -> UserAgentUtil.getPlatformFromUserAgentString(userAgentString)).isInstanceOf(UnrecognizedUserAgentException.class);
}
}

0 comments on commit 8dc7bb0

Please sign in to comment.