Skip to content

Commit

Permalink
🧪 Add some failing unit tests and improve the existing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
YvanMazy committed Feb 28, 2024
1 parent 9755452 commit ab11e11
Show file tree
Hide file tree
Showing 33 changed files with 1,044 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

package be.darkkraft.transferproxy.api.profile;

record ClientInformationImpl(String locale, byte viewDistance, ChatVisibility chatVisibility, boolean chatColors,
byte displayedSkinParts, MainHand mainHand, boolean enableTextFiltering,
boolean allowServerListing) implements ClientInformation {
record ClientInformationImpl(String locale, byte viewDistance, ChatVisibility chatVisibility, boolean chatColors, byte displayedSkinParts,
MainHand mainHand, boolean enableTextFiltering, boolean allowServerListing) implements ClientInformation {

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class ChatVisibilityTest {

@Test
void testIdEqualsOrdinal() {
for (final ChatVisibility result : ChatVisibility.values()) {
assertEquals(result.ordinal(), result.getId());
for (final ChatVisibility visibility : ChatVisibility.values()) {
assertEquals(visibility.ordinal(), visibility.getId());
}
}

@Test
void testEqualityWithFromId() {
for (final ChatVisibility result : ChatVisibility.values()) {
assertSame(ChatVisibility.fromId(result.getId()), result);
for (final ChatVisibility visibility : ChatVisibility.values()) {
assertSame(visibility, ChatVisibility.fromId(visibility.getId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class MainHandTest {

@Test
void testIdEqualsOrdinal() {
for (final MainHand result : MainHand.values()) {
assertEquals(result.ordinal(), result.getId());
for (final MainHand hand : MainHand.values()) {
assertEquals(hand.ordinal(), hand.getId());
}
}

@Test
void testEqualityWithFromId() {
for (final MainHand result : MainHand.values()) {
assertSame(MainHand.fromId(result.getId()), result);
for (final MainHand hand : MainHand.values()) {
assertSame(hand, MainHand.fromId(hand.getId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void testIdEqualsOrdinal() {
@Test
void testEqualityWithFromId() {
for (final ResourcePackResult result : ResourcePackResult.values()) {
assertSame(ResourcePackResult.fromId(result.getId()), result);
assertSame(result, ResourcePackResult.fromId(result.getId()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public record ClientInformationPacket(String locale, byte viewDistance, ChatVisi
byte displayedSkinParts, MainHand mainHand, boolean enableTextFiltering,
boolean allowServerListing) implements ServerboundPacket, ClientInformation {

public ClientInformationPacket(final @NotNull ClientInformation information) {
this(information.locale(),
information.viewDistance(),
information.chatVisibility(),
information.chatColors(),
information.displayedSkinParts(),
information.mainHand(),
information.enableTextFiltering(),
information.allowServerListing());
}

public ClientInformationPacket(final @NotNull ByteBuf buf) {
this(readString(buf, 16),
buf.readByte(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public interface PacketProvider {
PacketProvider[] HANDSHAKE = {HandshakePacket::new};
PacketProvider[] STATUS = {StatusRequestPacket::new, PingPongPacket::new};
PacketProvider[] LOGIN = {LoginStartPacket::new, null, null, LoginAcknowledgedPacket::new, LoginCookieResponsePacket::new};
PacketProvider[] CONFIG = {ClientInformationPacket::new, ConfigCookieResponsePacket::new, PluginMessagePacket::from, FinishConfigurationPacket::new, KeepAlivePacket::new, null, ResourcePackResponsePacket::new};
PacketProvider[] CONFIG =
{ClientInformationPacket::new, ConfigCookieResponsePacket::new, PluginMessagePacket::from, FinishConfigurationPacket::new, KeepAlivePacket::new, null, ResourcePackResponsePacket::new};

Packet provide(final @NotNull ByteBuf buf);

Expand All @@ -71,7 +72,7 @@ static Packet buildPacket(final @NotNull ConnectionState state, final @NotNull B
return packet;
}

private static PacketProvider[] getProviders(final @NotNull ConnectionState state) {
static PacketProvider[] getProviders(final @NotNull ConnectionState state) {
return switch (state) {
case HANDSHAKE -> HANDSHAKE;
case STATUS -> STATUS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* MIT License
*
* Copyright (c) 2024 Darkkraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.darkkraft.transferproxy.network.packet;

import be.darkkraft.transferproxy.api.network.packet.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class PacketTestBase {

private ByteBuf buf;

@BeforeEach
void setUp() {
this.buf = Unpooled.buffer();
}

protected <T extends Packet> void test(final T packet, final Function<ByteBuf, T> builder) {
packet.write(this.buf);
assertEquals(packet, builder.apply(this.buf));
assertEquals(0, this.buf.readableBytes());
}

@AfterEach
void tearDown() {
this.buf.release();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* MIT License
*
* Copyright (c) 2024 Darkkraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.darkkraft.transferproxy.network.packet.config;

import be.darkkraft.transferproxy.network.packet.PacketTestBase;
import org.junit.jupiter.api.Test;

class FinishConfigurationPacketTest extends PacketTestBase {

@Test
void testWriteReadConsistency() {
this.test(new FinishConfigurationPacket(), FinishConfigurationPacket::new);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2024 Darkkraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.darkkraft.transferproxy.network.packet.config;

import be.darkkraft.transferproxy.network.packet.PacketTestBase;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class KeepAlivePacketTest extends PacketTestBase {

@ParameterizedTest
@ValueSource(longs = {0L, 5L, Long.MIN_VALUE, Long.MAX_VALUE})
void testWriteReadConsistency(final long payload) {
this.test(new KeepAlivePacket(payload), KeepAlivePacket::new);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2024 Darkkraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.darkkraft.transferproxy.network.packet.config;

import be.darkkraft.transferproxy.network.packet.PacketTestBase;
import be.darkkraft.transferproxy.network.packet.config.payload.BrandPayload;
import org.junit.jupiter.api.Test;

class PluginMessagePacketTest extends PacketTestBase {

@Test
void testWriteReadConsistency() {
this.test(new PluginMessagePacket("minecraft:brand", new BrandPayload("vanilla")), PluginMessagePacket::from);
this.test(new PluginMessagePacket("minecraft:unknown", null), PluginMessagePacket::from);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2024 Darkkraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.darkkraft.transferproxy.network.packet.config.clientbound;

import be.darkkraft.transferproxy.network.packet.PacketTestBase;
import be.darkkraft.transferproxy.util.test.TestGenerationUtil;
import io.netty.handler.codec.DecoderException;
import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertThrows;

class AddResourcePackPacketTest extends PacketTestBase {

@Test
void testWriteReadConsistency() {
this.test(new AddResourcePackPacket(UUID.randomUUID(), "url", "hash", true, TestGenerationUtil.generateComplexComponent()),
AddResourcePackPacket::new);
}

@Test
void testWriteTooLongUrl() {
assertThrows(DecoderException.class,
() -> this.test(new AddResourcePackPacket(UUID.randomUUID(),
"a".repeat(Short.MAX_VALUE + 1),
"hash",
true,
TestGenerationUtil.generateComplexComponent()), AddResourcePackPacket::new));
}

@Test
void testWriteTooLongHash() {
assertThrows(DecoderException.class,
() -> this.test(new AddResourcePackPacket(UUID.randomUUID(),
"url",
"a".repeat(41),
true,
TestGenerationUtil.generateComplexComponent()), AddResourcePackPacket::new));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* MIT License
*
* Copyright (c) 2024 Darkkraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package be.darkkraft.transferproxy.network.packet.config.clientbound;

import be.darkkraft.transferproxy.network.packet.PacketTestBase;
import org.junit.jupiter.api.Test;

class ConfigCookieRequestPacketTest extends PacketTestBase {

@Test
void testWriteReadConsistency() {
this.test(new ConfigCookieRequestPacket("minecraft:cookie_key"), ConfigCookieRequestPacket::new);
}

}
Loading

0 comments on commit ab11e11

Please sign in to comment.