-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize: increase seata-core module unit test coverage (#6250)
- Loading branch information
1 parent
cd5d816
commit 77bfc08
Showing
15 changed files
with
858 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
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
54 changes: 54 additions & 0 deletions
54
core/src/test/java/io/seata/core/auth/DefaultAuthSignerTest.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,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.core.auth; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* The DefaultAuthSigner Test | ||
*/ | ||
public class DefaultAuthSignerTest { | ||
@Test | ||
public void testGetRamSignNotNull() { | ||
String data = "testGroup,127.0.0.1,1702564471650"; | ||
String key = "exampleEncryptKey"; | ||
String expectedSign = "6g9nMk6BRLFxl7bf5ZfWaEZvGdho3JBmwvx5rqgSUCE="; | ||
DefaultAuthSigner signer = new DefaultAuthSigner(); | ||
String sign = signer.sign(data, key); | ||
Assertions.assertEquals(expectedSign, sign); | ||
} | ||
|
||
@Test | ||
public void testGetRamSignNull() { | ||
String data = null; | ||
String key = "exampleEncryptKey"; | ||
DefaultAuthSigner signer = new DefaultAuthSigner(); | ||
String sign = signer.sign(data, key); | ||
Assertions.assertNull(sign); | ||
} | ||
|
||
@Test | ||
public void testGetSignVersion() { | ||
DefaultAuthSigner signer = new DefaultAuthSigner(); | ||
String expectedVersion = "V4"; | ||
String actualVersion = signer.getSignVersion(); | ||
|
||
// Assert the returned version matches the expected version | ||
Assertions.assertEquals(expectedVersion, actualVersion); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
core/src/test/java/io/seata/core/auth/RamSignAdapterTest.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,88 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.core.auth; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* The RamSignAdapter Test | ||
*/ | ||
public class RamSignAdapterTest { | ||
@Test | ||
public void testGetDateSigningKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
String secret = "mySecret"; | ||
String date = "20220101"; | ||
String signMethod = "HmacSHA256"; | ||
byte[] expectArray = new byte[]{-96, 108, 42, 75, -59, 121, -63, 108, -3, -126, 67, 3, 118, 2, 39, 59, -68, -37, -98, 122, -25, -120, 77, 56, -70, 24, -115, 33, 125, -128, -10, -26}; | ||
|
||
RamSignAdapter adapter = new RamSignAdapter(); | ||
// Use reflection to access the private method | ||
Method getDateSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getDateSigningKey", String.class, String.class, String.class); | ||
getDateSigningKeyMethod.setAccessible(true); | ||
byte[] signingKey = (byte[]) getDateSigningKeyMethod.invoke(adapter, secret, date, signMethod); | ||
Assertions.assertEquals(32, signingKey.length); | ||
Assertions.assertArrayEquals(expectArray, signingKey); | ||
} | ||
|
||
@Test | ||
public void testGetRegionSigningKey() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { | ||
String secret = "mySecret"; | ||
String date = "20220101"; | ||
String region = "cn-beijing"; | ||
String signMethod = "HmacSHA256"; | ||
byte[] expectArray = new byte[]{-40, 5, 2, 41, -48, 82, 10, -102, 125, -24, -44, -83, 127, 6, -85, 93, -26, 88, -88, 65, 56, 79, -5, -66, 65, -106, 19, -64, -85, 103, -32, 110}; | ||
|
||
RamSignAdapter adapter = new RamSignAdapter(); | ||
// Use reflection to access the private method | ||
Method getRegionSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getRegionSigningKey", String.class, String.class, String.class, String.class); | ||
getRegionSigningKeyMethod.setAccessible(true); | ||
byte[] signingKey = (byte[]) getRegionSigningKeyMethod.invoke(adapter, secret, date, region, signMethod); | ||
Assertions.assertEquals(32, signingKey.length); | ||
Assertions.assertArrayEquals(expectArray, signingKey); | ||
} | ||
|
||
@Test | ||
public void testGetProductSigningKey() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
String secret = "mySecret"; | ||
String date = "20220101"; | ||
String region = "cn-beijing"; | ||
String productCode = "seata"; | ||
String signMethod = "HmacSHA256"; | ||
byte[] expectArray = new byte[]{62, 98, -65, 30, -8, -3, 66, -111, 0, 123, 126, 78, -30, -74, 55, -79, 101, -18, -97, -5, 78, -19, -17, 0, 88, 30, -92, 108, 103, 87, 49, -22}; | ||
|
||
RamSignAdapter adapter = new RamSignAdapter(); | ||
Method getProductSigningKeyMethod = RamSignAdapter.class.getDeclaredMethod("getProductSigningKey", String.class, String.class, String.class, String.class, String.class); | ||
getProductSigningKeyMethod.setAccessible(true); | ||
byte[] signingKey = (byte[]) getProductSigningKeyMethod.invoke(adapter, secret, date, region, productCode, signMethod); | ||
Assertions.assertEquals(32, signingKey.length); | ||
Assertions.assertArrayEquals(expectArray, signingKey); | ||
} | ||
|
||
@Test | ||
public void testGetRamSign() { | ||
String encryptText = "testGroup,127.0.0.1,1702564471650"; | ||
String encryptKey = "exampleEncryptKey"; | ||
String expectedSign = "6g9nMk6BRLFxl7bf5ZfWaEZvGdho3JBmwvx5rqgSUCE="; | ||
String actualSign = RamSignAdapter.getRamSign(encryptText, encryptKey); | ||
// Assert the generated sign matches the expected sign | ||
Assertions.assertEquals(expectedSign, actualSign); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
core/src/test/java/io/seata/core/compressor/CompressorTypeTest.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,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.core.compressor; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* The CompressorType Test | ||
*/ | ||
public class CompressorTypeTest { | ||
@Test | ||
public void testGetByCode() { | ||
int code = 1; | ||
CompressorType expectedType = CompressorType.GZIP; | ||
CompressorType actualType = CompressorType.getByCode(code); | ||
// Assert the returned type matches the expected type | ||
Assertions.assertEquals(expectedType, actualType); | ||
} | ||
@Test | ||
public void testGetByName() { | ||
String name = "gzip"; | ||
CompressorType expectedType = CompressorType.GZIP; | ||
CompressorType actualType = CompressorType.getByName(name); | ||
// Assert the returned type matches the expected type | ||
Assertions.assertEquals(expectedType, actualType); | ||
} | ||
} |
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
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
68 changes: 68 additions & 0 deletions
68
core/src/test/java/io/seata/core/context/ThreadLocalContextCoreTest.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,68 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.seata.core.context; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* The type Thread local context core test. | ||
*/ | ||
public class ThreadLocalContextCoreTest { | ||
private static ThreadLocalContextCore contextCore ; | ||
|
||
|
||
@BeforeAll | ||
public static void setUp() { | ||
contextCore = new ThreadLocalContextCore(); | ||
} | ||
@Test | ||
public void testPutAndGet() { | ||
// Test putting and getting a value | ||
contextCore.put("key", "value"); | ||
assertEquals("value", contextCore.get("key")); | ||
contextCore.remove("key"); | ||
} | ||
|
||
@Test | ||
public void testRemove() { | ||
// Test putting and removing a value | ||
contextCore.put("key", "value"); | ||
assertEquals("value", contextCore.remove("key")); | ||
assertNull(contextCore.get("key")); | ||
} | ||
|
||
@Test | ||
public void testEntries() { | ||
// Test getting all entries | ||
contextCore.put("key1", "value1"); | ||
contextCore.put("key2", "value2"); | ||
contextCore.put("key3", "value3"); | ||
assertEquals(3, contextCore.entries().size()); | ||
assertTrue(contextCore.entries().containsKey("key1")); | ||
assertTrue(contextCore.entries().containsKey("key2")); | ||
assertTrue(contextCore.entries().containsKey("key3")); | ||
contextCore.remove("key1"); | ||
contextCore.remove("key2"); | ||
contextCore.remove("key3"); | ||
assertNull(contextCore.get("key1")); | ||
assertNull(contextCore.get("key2")); | ||
assertNull(contextCore.get("key3")); | ||
} | ||
} |
Oops, something went wrong.