Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hendrik Ebbers <hendrik.ebbers@web.de>
- Loading branch information
1 parent
756c025
commit e447a2b
Showing
7 changed files
with
139 additions
and
1 deletion.
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
47 changes: 47 additions & 0 deletions
47
hedera-node/hedera-config/src/main/java/com/hedera/node/config/converter/BytesConverter.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,47 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed 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 com.hedera.node.config.converter; | ||
|
||
import static com.swirlds.common.utility.CommonUtils.unhex; | ||
|
||
import com.hedera.pbj.runtime.io.buffer.Bytes; | ||
import com.swirlds.config.api.converter.ConfigConverter; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
|
||
/** | ||
* Converts a string of hex digits into a {@link Bytes} object. | ||
*/ | ||
public class BytesConverter implements ConfigConverter<Bytes> { | ||
@Nullable | ||
@Override | ||
public Bytes convert(@NonNull String value) throws IllegalArgumentException, NullPointerException { | ||
if (value == null) { | ||
throw new NullPointerException("BytesConverter cannot convert null value"); | ||
} else if (!value.startsWith("0x")) { | ||
throw new IllegalArgumentException("Invalid format. Value must start with '0x': " + value); | ||
} else { | ||
try { | ||
final var hex = value.substring(2); | ||
final var bytes = unhex(hex); | ||
return Bytes.wrap(bytes); | ||
} catch (final Exception e) { | ||
throw new IllegalArgumentException("Invalid format: " + value, e); | ||
} | ||
} | ||
} | ||
} |
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
83 changes: 83 additions & 0 deletions
83
...node/hedera-config/src/test/java/com/hedera/node/config/converter/BytesConverterTest.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,83 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed 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 com.hedera.node.config.converter; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
|
||
import com.hedera.pbj.runtime.io.buffer.Bytes; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class BytesConverterTest { | ||
|
||
@Test | ||
void testNull() { | ||
// given | ||
final BytesConverter bytesConverter = new BytesConverter(); | ||
|
||
// then | ||
assertThatThrownBy(() -> bytesConverter.convert(null)).isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
void testConversionResult() throws IOException { | ||
// given | ||
final BytesConverter bytesConverter = new BytesConverter(); | ||
final String value = "0x1234567890abcdef"; | ||
final byte[] expected = new byte[] {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef}; | ||
|
||
// when | ||
final byte[] bytes = bytesConverter.convert(value).toInputStream().readAllBytes(); | ||
|
||
// then | ||
assertArrayEquals(expected, bytes); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource( | ||
strings = { | ||
"0x", | ||
"0xaa", | ||
"0x1234", | ||
"0x111111111111111111111111", | ||
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" | ||
}) | ||
void testValueValues(final String value) { | ||
// given | ||
final BytesConverter bytesConverter = new BytesConverter(); | ||
|
||
// when | ||
final Bytes bytes = bytesConverter.convert(value); | ||
|
||
// then | ||
assertThat(bytes).isNotNull(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"", " ", " ", "1", "123", "abc", "0xu", "0x1", "0x123", "0x12345"}) | ||
void testInvalidValue(final String value) { | ||
// given | ||
final BytesConverter bytesConverter = new BytesConverter(); | ||
|
||
// then | ||
assertThatThrownBy(() -> bytesConverter.convert(value)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
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