|
| 1 | +package org.arkecosystem.crypto.transactions.builder; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import org.arkecosystem.crypto.AbstractTest; |
| 6 | +import org.arkecosystem.crypto.encoding.Hex; |
| 7 | +import org.arkecosystem.crypto.enums.ContractAddresses; |
| 8 | +import org.arkecosystem.crypto.transactions.Deserializer; |
| 9 | +import org.arkecosystem.crypto.transactions.types.AbstractTransaction; |
| 10 | +import org.arkecosystem.crypto.transactions.types.UsernameRegistration; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +public class UsernameRegistrationBuilderTest extends AbstractTest { |
| 14 | + |
| 15 | + private static final String REGISTER_SELECTOR = "0x36a94134"; |
| 16 | + |
| 17 | + @Test |
| 18 | + public void it_should_default_to_the_usernames_well_known_contract() { |
| 19 | + UsernameRegistrationBuilder builder = new UsernameRegistrationBuilder(); |
| 20 | + |
| 21 | + assertEquals(ContractAddresses.USERNAMES.address(), builder.transaction.recipientAddress); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void it_should_encode_register_username_selector_and_argument() { |
| 26 | + UsernameRegistrationBuilder builder = |
| 27 | + new UsernameRegistrationBuilder() |
| 28 | + .gasPrice(5_000_000_000L) |
| 29 | + .gasLimit(200_000) |
| 30 | + .nonce(1L) |
| 31 | + .username("alice"); |
| 32 | + |
| 33 | + assertEquals("alice", builder.transaction.username); |
| 34 | + assertTrue( |
| 35 | + builder.transaction.data.startsWith(REGISTER_SELECTOR), |
| 36 | + "expected payload to start with register selector but was " |
| 37 | + + builder.transaction.data); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void it_should_sign_and_verify_a_username_registration_transaction() { |
| 42 | + UsernameRegistrationBuilder builder = |
| 43 | + new UsernameRegistrationBuilder() |
| 44 | + .gasPrice(5_000_000_000L) |
| 45 | + .gasLimit(200_000) |
| 46 | + .nonce(1L) |
| 47 | + .username("alice") |
| 48 | + .sign(this.passphrase); |
| 49 | + |
| 50 | + assertNotNull(builder.transaction.signature); |
| 51 | + assertNotNull(builder.transaction.id); |
| 52 | + assertTrue(builder.verify()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void it_should_round_trip_through_serialization() { |
| 57 | + UsernameRegistrationBuilder builder = |
| 58 | + new UsernameRegistrationBuilder() |
| 59 | + .gasPrice(5_000_000_000L) |
| 60 | + .gasLimit(200_000) |
| 61 | + .nonce(7L) |
| 62 | + .username("bob_42") |
| 63 | + .sign(this.passphrase); |
| 64 | + |
| 65 | + String serialized = Hex.encode(builder.transaction.serialize()); |
| 66 | + AbstractTransaction restored = Deserializer.newDeserializer(serialized).deserialize(); |
| 67 | + |
| 68 | + assertInstanceOf(UsernameRegistration.class, restored); |
| 69 | + UsernameRegistration registration = (UsernameRegistration) restored; |
| 70 | + assertEquals(builder.transaction.id, registration.id); |
| 71 | + assertEquals("bob_42", registration.username); |
| 72 | + assertEquals( |
| 73 | + builder.transaction.recipientAddress.toLowerCase(), |
| 74 | + registration.recipientAddress.toLowerCase()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void it_should_produce_empty_payload_when_no_username_is_set() { |
| 79 | + UsernameRegistrationBuilder builder = new UsernameRegistrationBuilder(); |
| 80 | + |
| 81 | + assertEquals("", builder.transaction.data); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void it_should_reject_empty_username() { |
| 86 | + IllegalArgumentException thrown = |
| 87 | + assertThrows( |
| 88 | + IllegalArgumentException.class, |
| 89 | + () -> new UsernameRegistrationBuilder().username("")); |
| 90 | + assertTrue(thrown.getMessage().contains("between 1 and 20")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void it_should_reject_username_longer_than_20_characters() { |
| 95 | + assertThrows( |
| 96 | + IllegalArgumentException.class, |
| 97 | + () -> new UsernameRegistrationBuilder().username("a_very_long_username_xx")); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void it_should_reject_uppercase_letters() { |
| 102 | + IllegalArgumentException thrown = |
| 103 | + assertThrows( |
| 104 | + IllegalArgumentException.class, |
| 105 | + () -> new UsernameRegistrationBuilder().username("Alice")); |
| 106 | + assertTrue(thrown.getMessage().contains("lowercase")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void it_should_reject_special_characters() { |
| 111 | + assertThrows( |
| 112 | + IllegalArgumentException.class, |
| 113 | + () -> new UsernameRegistrationBuilder().username("alice!")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void it_should_reject_leading_or_trailing_underscore() { |
| 118 | + assertThrows( |
| 119 | + IllegalArgumentException.class, |
| 120 | + () -> new UsernameRegistrationBuilder().username("_alice")); |
| 121 | + assertThrows( |
| 122 | + IllegalArgumentException.class, |
| 123 | + () -> new UsernameRegistrationBuilder().username("alice_")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void it_should_reject_consecutive_underscores() { |
| 128 | + assertThrows( |
| 129 | + IllegalArgumentException.class, |
| 130 | + () -> new UsernameRegistrationBuilder().username("al__ice")); |
| 131 | + } |
| 132 | +} |
0 commit comments