From 100efe095767491131ffcf69684b1b66ec6192fb Mon Sep 17 00:00:00 2001 From: Anyexyz Date: Mon, 18 Nov 2024 13:21:07 +0800 Subject: [PATCH 1/5] feat: add support for OpenHarmony in device OS detection --- .../java/run/halo/app/security/device/DeviceServiceImpl.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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..e38a57e6be 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,13 @@ 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) { From 4d1e13ebafaaec5cfd8fddca8db02539be586433 Mon Sep 17 00:00:00 2001 From: Anyexyz Date: Mon, 18 Nov 2024 14:58:23 +0800 Subject: [PATCH 2/5] test: add unit test for OpenHarmony device info parsing --- .../halo/app/security/device/DeviceServiceImplTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) 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..fc6983e0ec 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 @@ -21,4 +21,13 @@ void deviceInfoParseTest() { assertThat(info.browser()).isEqualTo("Chrome 126.0"); } + @Test + void deviceInfoParseOpenHarmonyTest() { + var info = DeviceServiceImpl.DeviceInfo.parse( + "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"); + assertThat(info.os()).isEqualTo("OpenHarmony 5.0"); + assertThat(info.browser()).isEqualTo("Chrome 114.0"); + } + } \ No newline at end of file From e8d0190ba2ac2b85a206ba23b96b7fbabd1e058e Mon Sep 17 00:00:00 2001 From: Anyexyz Date: Mon, 18 Nov 2024 15:16:32 +0800 Subject: [PATCH 3/5] test: refactor device info parsing tests to use parameterized tests --- .../device/DeviceServiceImplTest.java | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) 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 fc6983e0ec..60b71b482e 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 @@ -1,8 +1,11 @@ package run.halo.app.security.device; -import static org.assertj.core.api.Assertions.assertThat; +import java.util.stream.Stream; -import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +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,22 +15,26 @@ */ 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" + ) + ); } - @Test - void deviceInfoParseOpenHarmonyTest() { - var info = DeviceServiceImpl.DeviceInfo.parse( - "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"); - assertThat(info.os()).isEqualTo("OpenHarmony 5.0"); - assertThat(info.browser()).isEqualTo("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 From 0cedd8a9f3a49e4200607cef07e39901acbf908d Mon Sep 17 00:00:00 2001 From: Anyexyz Date: Mon, 18 Nov 2024 15:35:26 +0800 Subject: [PATCH 4/5] test: refactor device info parsing tests for better readability --- .../app/security/device/DeviceServiceImplTest.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 60b71b482e..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 @@ -1,8 +1,8 @@ package run.halo.app.security.device; -import java.util.stream.Stream; - import static org.assertj.core.api.Assertions.assertThat; + +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; @@ -18,12 +18,15 @@ class DeviceServiceImplTest { 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", + "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", + "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" ) From 60ec325b2a1df48a03f7d06043b158faefe768c0 Mon Sep 17 00:00:00 2001 From: Anyexyz Date: Mon, 18 Nov 2024 18:03:55 +0800 Subject: [PATCH 5/5] fix: format OS regex pattern for improved readability --- .../java/run/halo/app/security/device/DeviceServiceImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 e38a57e6be..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,7 +192,8 @@ 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|OpenHarmony)"); + 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+)?)"),