From 1fde9937d300c33a4eeec83193dd2b1ea8f54e87 Mon Sep 17 00:00:00 2001 From: yuanyao14 Date: Tue, 9 Sep 2025 20:12:13 +0800 Subject: [PATCH] SDK util.LRUCache exceptions addons Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICWWZ3 Signed-off-by: yuanyao14 --- .../plugins/ets/sdk/api/@ohos.util.ets | 18 +++++++++ .../api/@ohos/util/LRUCacheExceptionTest.ets | 40 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/static_core/plugins/ets/sdk/api/@ohos.util.ets b/static_core/plugins/ets/sdk/api/@ohos.util.ets index 4dae36d59e..affa48bc34 100644 --- a/static_core/plugins/ets/sdk/api/@ohos.util.ets +++ b/static_core/plugins/ets/sdk/api/@ohos.util.ets @@ -108,6 +108,15 @@ export namespace util { } public put(key: K, value: V): V | undefined { + if (key == undefined) { + throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of ${key} must be Object`); + } + if (value === undefined) { + throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of ${value} must be Object`); + } + if (key === null || value === null) { + throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of key and value must be Object`); + } let valueBefore: V | undefined = undefined; this.putCount++; if (this.cache.has(key)) { @@ -173,6 +182,9 @@ export namespace util { } public remove(key: K): V | undefined { + if (key == null) { + throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of ${key} must be Object`); + } if (this.cache.has(key)) { let former: V | undefined = this.cache.get(key); this.cache.delete(key); @@ -185,6 +197,9 @@ export namespace util { } public get(key: K): V | undefined { + if (key == null) { + throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of ${key} must be Object`); + } if (this.cache.has(key)) { return this.containsInner(key); } @@ -213,6 +228,9 @@ export namespace util { } public contains(key: K): boolean { + if (key == null) { + throw createBusinessError(TypeErrorCodeId, `Parameter error. The type of ${key} must be Object`); + } if (this.cache.has(key)) { this.containsInner(key); return true; diff --git a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets index 66dc86fb30..3824157ca9 100644 --- a/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets +++ b/static_core/plugins/ets/tests/ets_sdk/api/@ohos/util/LRUCacheExceptionTest.ets @@ -19,6 +19,10 @@ import { BusinessError } from "@ohos.base"; const success = 0 const fail = 1 +const expectedRangeErrors = (e: Error): boolean => { + return (e instanceof BusinessError); +} + function main(): int { const suite = new arktest.ArkTestsuite("LRUCache API Error tests") @@ -139,3 +143,39 @@ function testLRUCacheUpdateGetCapacity_NumberMin() { } arktest.assertTrue(false, "LRUCache should throw an error when capacity is not int"); } + +function testInvalidInputKeyAtPut() { + let that = new util.LRUCache(); + arktest.expectThrow(() => { that.put(undefined, 1) }, expectedRangeErrors) + arktest.expectThrow(() => { that.put(null, 1) }, expectedRangeErrors) +} + +function testInvalidInputValueAtPut() { + let that = new util.LRUCache(); + arktest.expectThrow(() => { that.put(1, undefined) }, expectedRangeErrors) + arktest.expectThrow(() => { that.put(1, null) }, expectedRangeErrors) +} + +function testInvalidInputKeyAtGet() { + let that = new util.LRUCache(); + that.put(1, 1); + that.put(2, 2); + arktest.expectThrow(() => { that.get(undefined) }, expectedRangeErrors) + arktest.expectThrow(() => { that.get(null) }, expectedRangeErrors) +} + +function testInvalidInputKeyAtContains() { + let that = new util.LRUCache(); + that.put(1, 1); + that.put(2, 2); + arktest.expectThrow(() => { that.contains(undefined) }, expectedRangeErrors) + arktest.expectThrow(() => { that.contains(null) }, expectedRangeErrors) +} + +function testInvalidInputKeyAtRemove() { + let that = new util.LRUCache(); + that.put(1, 1); + that.put(2, 2); + arktest.expectThrow(() => { that.remove(undefined) }, expectedRangeErrors) + arktest.expectThrow(() => { that.remove(null) }, expectedRangeErrors) +} -- Gitee