diff --git a/application/src/main/java/run/halo/app/security/device/DeviceServiceImpl.java b/application/src/main/java/run/halo/app/security/device/DeviceServiceImpl.java index 001265a790..cc7a19a015 100644 --- a/application/src/main/java/run/halo/app/security/device/DeviceServiceImpl.java +++ b/application/src/main/java/run/halo/app/security/device/DeviceServiceImpl.java @@ -192,12 +192,14 @@ record DeviceInfo(String browser, String os) { Pattern.CASE_INSENSITIVE); static final Pattern OS_REGEX = - Pattern.compile("(Windows NT|Mac OS X|Android|Linux|iPhone|iPad|Windows Phone)"); + Pattern.compile( + "(Windows NT|Mac OS X|Android|Linux|iPhone|iPad|Windows Phone|OpenHarmony)"); static final Pattern[] osRegexes = { Pattern.compile("Windows NT (\\d+\\.\\d+)"), Pattern.compile("Mac OS X (\\d+[\\._]\\d+([\\._]\\d+)?)"), Pattern.compile("iPhone OS (\\d+_\\d+(_\\d+)?)"), - Pattern.compile("Android (\\d+\\.\\d+(\\.\\d+)?)") + Pattern.compile("Android (\\d+\\.\\d+(\\.\\d+)?)"), + Pattern.compile("OpenHarmony (\\d+\\.\\d+(\\.\\d+)?)") }; public static DeviceInfo parse(String userAgent) { diff --git a/application/src/test/java/run/halo/app/security/device/DeviceServiceImplTest.java b/application/src/test/java/run/halo/app/security/device/DeviceServiceImplTest.java index 58d82ca576..31feb25529 100644 --- a/application/src/test/java/run/halo/app/security/device/DeviceServiceImplTest.java +++ b/application/src/test/java/run/halo/app/security/device/DeviceServiceImplTest.java @@ -2,7 +2,10 @@ import static org.assertj.core.api.Assertions.assertThat; -import org.junit.jupiter.api.Test; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests for {@link DeviceServiceImpl}. @@ -12,13 +15,29 @@ */ class DeviceServiceImplTest { - @Test - void deviceInfoParseTest() { - var info = DeviceServiceImpl.DeviceInfo.parse( - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like " - + "Gecko) Chrome/126.0.0.0 Safari/537.36"); - assertThat(info.os()).isEqualTo("Mac OS X 10.15.7"); - assertThat(info.browser()).isEqualTo("Chrome 126.0"); + static Stream deviceInfoParseTest() { + return Stream.of( + Arguments.of( + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like " + + "Gecko) Chrome/126.0.0.0 Safari/537.36", + "Mac OS X 10.15.7", + "Chrome 126.0" + ), + Arguments.of( + "Mozilla/5.0 (Phone; OpenHarmony 5.0) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/114.0.0.0 Safari/537.36 ArkWeb/4.1.6.1 Mobile HuaweiBrowser/5.0.4" + + ".300", + "OpenHarmony 5.0", + "Chrome 114.0" + ) + ); } + @ParameterizedTest + @MethodSource + void deviceInfoParseTest(String userAgent, String expectedOs, String expectedBrowser) { + var info = DeviceServiceImpl.DeviceInfo.parse(userAgent); + assertThat(info.os()).isEqualTo(expectedOs); + assertThat(info.browser()).isEqualTo(expectedBrowser); + } } \ No newline at end of file