Skip to content

Commit

Permalink
prepare tlb and cell deserialization tests - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
neodiX committed Aug 4, 2024
1 parent ddd7788 commit 68943cd
Show file tree
Hide file tree
Showing 5 changed files with 2,045 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.ton.java;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.ton.java.cell.Cell;
import org.ton.java.cell.CellBuilder;
import org.ton.java.cell.CellSlice;
import org.ton.java.tlb.types.InternalMessageInfo;

import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
@RunWith(JUnit4.class)
public class TestTonSdkTestCasesTlbDeserialization {
public static final String numbersTestFileUrl = "https://raw.githubusercontent.com/neodix42/ton-sdk-test-cases/main/tlb-deserialization.json";
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
String fileContentWithUseCases = IOUtils.toString(new URL(numbersTestFileUrl), Charset.defaultCharset());
TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

public TestTonSdkTestCasesTlbDeserialization() throws IOException {
}

@Test
public void testTlbDeserialization1() {

String testId = "tlb-deserialization-1";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

String bocAsHex = testCase.getInput().get("bocAsHex").toString();

Cell c = CellBuilder.beginCell().fromBoc("b5ee9c724101020100860001b36800bf4c6bdca25797e55d700c1a5448e2af5d1ac16f9a9628719a4e1eb2b44d85e33fd104a366f6fb17799871f82e00e4f2eb8ae6aaf6d3e0b3fb346cd0208e23725e14094ba15d20071f12260000446ee17a9b0cc8c028d8c001004d8002b374733831aac3455708e8f1d2c7f129540b982d3a5de8325bf781083a8a3d2a04a7f943813277f3ea").endCell();
InternalMessageInfo internalMessageInfo = InternalMessageInfo.deserialize(CellSlice.beginParse(c));
log.info("internalMessage {}", internalMessageInfo);

Boolean ihrDisabled = Boolean.valueOf(testCase.getExpectedOutput().get("ihrDisabled").toString());
Boolean bounce = Boolean.valueOf(testCase.getExpectedOutput().get("bounce").toString());
Boolean bounced = Boolean.valueOf(testCase.getExpectedOutput().get("bounced").toString());
String sourceAddress = testCase.getExpectedOutput().get("sourceAddress").toString();
String destinationAddress = testCase.getExpectedOutput().get("destinationAddress").toString();
BigInteger valueCoins = new BigInteger(testCase.getExpectedOutput().get("valueCoins").toString());
BigInteger ihrFee = new BigInteger(testCase.getExpectedOutput().get("ihrFee").toString());
BigInteger fwdFee = new BigInteger(testCase.getExpectedOutput().get("fwdFee").toString());
BigInteger createdLt = new BigInteger(testCase.getExpectedOutput().get("createdLt").toString());
BigInteger createdAt = new BigInteger(testCase.getExpectedOutput().get("createdAt").toString());

assertThat(internalMessageInfo.getIHRDisabled()).isEqualTo(ihrDisabled);
assertThat(internalMessageInfo.getBounce()).isEqualTo(bounce);
assertThat(internalMessageInfo.getBounced()).isEqualTo(bounced);
assertThat(internalMessageInfo.getSrcAddr().toAddress().toRaw()).isEqualTo(sourceAddress);
assertThat(internalMessageInfo.getDstAddr().toAddress().toRaw()).isEqualTo(destinationAddress);
assertThat(internalMessageInfo.getValue().getCoins()).isEqualTo(valueCoins);
assertThat(internalMessageInfo.getIHRFee()).isEqualTo(ihrFee);
assertThat(internalMessageInfo.getFwdFee()).isEqualTo(fwdFee);
assertThat(internalMessageInfo.getCreatedLt()).isEqualTo(createdLt);
assertThat(internalMessageInfo.getCreatedAt()).isEqualTo(createdAt.longValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.ton.java;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.ton.java.tlb.types.MsgAddress;
import org.ton.java.tlb.types.MsgAddressExtNone;
import org.ton.java.tlb.types.MsgAddressExternal;
import org.ton.java.utils.Utils;

import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.Charset;

import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
@RunWith(JUnit4.class)
public class TestTonSdkTestCasesTlbSerialization {
public static final String numbersTestFileUrl = "https://raw.githubusercontent.com/neodix42/ton-sdk-test-cases/main/tlb-serialization.json";
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
String fileContentWithUseCases = IOUtils.toString(new URL(numbersTestFileUrl), Charset.defaultCharset());
TonSdkTestCases tonSdkTestCases = gson.fromJson(fileContentWithUseCases, TonSdkTestCases.class);

public TestTonSdkTestCasesTlbSerialization() throws IOException {
}

@Test
public void testTlbSerialization1() {

String testId = "tlb-serialization-1";
TonSdkTestCases.TestCase testCase = tonSdkTestCases.getTestCases().get(testId);

String description = testCase.getDescription();

log.info("testId: {}", testId);
log.info("description: {}", description);

String bocAsHex = testCase.getInput().get("addr1_none").toString();
MsgAddress addr1 = MsgAddressExtNone.builder().build();

BigInteger addr2_external_len = new BigInteger(testCase.getInput().get("addr2_external_len").toString());
String addr2_external_bits = testCase.getInput().get("addr2_external_bits").toString();

MsgAddress addr2 = MsgAddressExternal.builder()
.len(addr2_external_len.intValue())
.externalAddress(new BigInteger(Utils.bitStringToHex(addr2_external_bits), 16))
.build();
String bocAsHexAddr1 = testCase.getExpectedOutput().get("bocAsHexAddr1").toString();
String bocAsHexAddr2 = testCase.getExpectedOutput().get("bocAsHexAddr2").toString();

assertThat(addr1.toCell().toHex(true)).isEqualTo(bocAsHexAddr1);
assertThat(addr2.toCell().toHex(true)).isEqualTo(bocAsHexAddr2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -52,10 +53,13 @@ public void testBocDeserializationIssue70() {
@Test
public void testBocDeserializationAllConfig() throws IOException {
String newBlockOutput = IOUtils.toString(Objects.requireNonNull(getClass().getResourceAsStream("/allconfig-boc-as-hex.txt")), StandardCharsets.UTF_8);
String cellOutput = IOUtils.toString(Objects.requireNonNull(getClass().getResourceAsStream("/allconfig-cell-print.txt")), StandardCharsets.UTF_8);
Cell cell = CellBuilder.beginCell().fromBoc(newBlockOutput).endCell();
log.info("CellType {}", cell.getCellType());
log.info(cell.print());
log.info(Utils.bytesToHex(cell.toBoc(true)));

assertThat(StringUtils.trim(cell.print())).isEqualTo(cellOutput);
}


Expand Down
5 changes: 5 additions & 0 deletions cell/src/test/java/org/ton/java/tlb/TestTlbMessageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public void testCornerInternalMessage() {
InternalMessageInfo internalMessageInfo = InternalMessageInfo.deserialize(CellSlice.beginParse(c));
log.info("internalMessage {}", internalMessageInfo);
assertThat(internalMessageInfo.getIHRDisabled()).isTrue();
assertThat(internalMessageInfo.getBounce()).isTrue();
assertThat(internalMessageInfo.getBounced()).isFalse();
assertThat(internalMessageInfo.getSrcAddr().toAddress().toRaw()).isEqualTo("0:5fa635ee512bcbf2aeb8060d2a247157ae8d60b7cd4b1438cd270f595a26c2f1");
assertThat(internalMessageInfo.getDstAddr().toAddress().toRaw()).isEqualTo("-1:44128d9bdbec5de661c7e0b80393cbae2b9aabdb4f82cfecd1b34082388dc978");
assertThat(internalMessageInfo.getValue().getCoins()).isEqualTo(BigInteger.valueOf(9980893000L));
assertThat(internalMessageInfo.getIHRFee()).isEqualTo(BigInteger.ZERO);
assertThat(internalMessageInfo.getFwdFee()).isEqualTo(BigInteger.valueOf(9406739L));
assertThat(internalMessageInfo.getCreatedAt()).isEqualTo(1684018284L);
assertThat(internalMessageInfo.getCreatedLt()).isEqualTo(BigInteger.valueOf(37621510000006L));
Expand Down
Loading

0 comments on commit 68943cd

Please sign in to comment.