Skip to content

Commit

Permalink
cucumber test
Browse files Browse the repository at this point in the history
  • Loading branch information
ahangsu committed Aug 23, 2022
1 parent 7551d32 commit cc0632b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .test-env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Configs for testing repo download:
SDK_TESTING_URL="https://github.com/algorand/algorand-sdk-testing"
SDK_TESTING_BRANCH="master"
# TODO change back to master when sdk-testing is merged
SDK_TESTING_BRANCH="deprecate-langspec"
SDK_TESTING_HARNESS="test-harness"

VERBOSE_HARNESS=0
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/algorand/algosdk/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static void sanityCheckProgram(final byte[] program) {
throw new IllegalArgumentException("cannot check if program bytes are Algorand address" + e);
}
if (isAddress)
throw new IllegalArgumentException("requesting program bytes, but get Algorand address");
throw new IllegalArgumentException("requesting program bytes, get Algorand address");

// or maybe these bytes are some B64 encoded bytes representation
if (Base64.isBase64(program))
Expand Down
33 changes: 0 additions & 33 deletions src/test/java/com/algorand/algosdk/logic/TestLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,37 +449,4 @@ public void testCheckProgramV6() throws Exception {
assertThat(valid).isTrue();
}
}

@Test
public void testSanityCheckProgram() {
{
// pass in address
String addressNotProg = "DN7MBMCL5JQ3PFUQS7TMX5AH4EEKOBJVDUF4TCV6WERATKFLQF4MQUPZTA";
assertThatThrownBy(() -> sanityCheckProgram(addressNotProg.getBytes(StandardCharsets.UTF_8)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("requesting program bytes, but get Algorand address");
}

{
// pass in b64 encoded msg
String exampleB64 = "gqRtc2lng6ZzdWJzaWeTgqJwa8QgG37AsEvqYbeWkJfmy/QH4QinBTUdC8mKvrEiCairgXihc8RAdvZ3y9GsInBPutdwKc7Jy+an13CcjSV1lcvRAYQKYOxXwfgT5B/mK14R57ueYJTYyoDO8zBY6kQmBalWkm95AIGicGvEIAljMglTc4nwdWcRdzmRx9A+G3PIxPUr9q/wGqJc+cJxgaJwa8Qg5/D4TQaBHfnzHI2HixFV9GcdUaGFwgCQhmf0SVhwaKGjdGhyAqF2AaN0eG6Jo2FtdM0TiKNmZWXOAANPqKJmds4ADtbco2dlbq10ZXN0bmV0LXYzMS4womx2zgAO2sSkbm90ZcQItFF5Ofz60nGjcmN2xCAbfsCwS+pht5aQl+bL9AfhCKcFNR0LyYq+sSIJqKuBeKNzbmTEII2StImQAXOgTfpDWaNmamr86ixCoF3Zwfc+66VHgDfppHR5cGWjcGF5";
assertThatThrownBy(() -> sanityCheckProgram(exampleB64.getBytes(StandardCharsets.UTF_8)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("program should not be b64 encoded");
}

{
String readableStuff = "Cast a cold eye\non life, on death.\nHorseman, pass by";
assertThatThrownBy(() -> sanityCheckProgram(readableStuff.getBytes(StandardCharsets.UTF_8)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("program bytes are all ASCII printable characters, not looking like Teal byte code");
}

{
byte[] program = {
0x06, 0x31, 0x3f, 0x15, (byte) 0x81, 0x40, 0x12, 0x33, 0x00, 0x3e, 0x15, (byte) 0x81, 0x0a, 0x12, 0x10
};
assertDoesNotThrow(() -> sanityCheckProgram(program));
}
}
}
37 changes: 37 additions & 0 deletions src/test/java/com/algorand/algosdk/unit/ProgramSanityCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.algorand.algosdk.unit;

import com.algorand.algosdk.logic.Logic;
import com.algorand.algosdk.util.Encoder;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

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

public class ProgramSanityCheck {
byte[] seeminglyProgram;
String actualErrMsg;

@Given("a base64 encoded program bytes for heuristic sanity check {string}")
public void takeB64encodedBytes(String b64encodedBytes) {
seeminglyProgram = Encoder.decodeFromBase64(b64encodedBytes);
}

@When("I start heuristic sanity check over the bytes")
public void heuristicCheckOverBytes() {
try {
Logic.sanityCheckProgram(seeminglyProgram);
} catch (Exception e) {
actualErrMsg = e.getMessage();
}
}

@Then("if there exists an error, the error contains {string}")
public void checkErrorIfMatching(String errMsg) {
if (errMsg != null && !errMsg.isEmpty())
assertThat(actualErrMsg).contains(errMsg);
else
assertThat(actualErrMsg).isNullOrEmpty();
}
}
1 change: 1 addition & 0 deletions src/test/unit.tags
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@unit.indexer.logs
@unit.indexer.rekey
@unit.offline
@unit.program_sanity_check
@unit.rekey
@unit.responses
@unit.responses.231
Expand Down

0 comments on commit cc0632b

Please sign in to comment.