Skip to content

Commit

Permalink
harden tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mraszyk committed Dec 19, 2024
1 parent c3893ea commit fd5192c
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions e2e/tests-dfx/call.bash
Original file line number Diff line number Diff line change
Expand Up @@ -299,73 +299,73 @@ teardown() {
assert_match '(8 : nat)'
}

@test "impersonate sender" {
[[ ! "$USE_POCKETIC" ]] && skip "skipped for replica: impersonating sender is only supported for PocketIC"
function impersonate_sender() {
IDENTITY_PRINCIPAL="${1}"

dfx_start
assert_command dfx deploy hello_backend
CANISTER_ID="$(dfx canister id hello_backend)"

# set the management canister as the only controller
assert_command dfx canister update-settings hello_backend --set-controller aaaaa-aa --yes
assert_command dfx canister update-settings hello_backend --set-controller "${IDENTITY_PRINCIPAL}" --yes

# updating settings now fails because the default identity does not control the canister anymore
assert_command_fail dfx canister update-settings hello_backend --freezing-threshold 0
assert_contains "Only controllers of canister $CANISTER_ID can call ic00 method update_settings"

# updating settings succeeds when impersonating the management canister as the sender
assert_command dfx canister update-settings hello_backend --freezing-threshold 0 --impersonate aaaaa-aa
assert_command dfx canister update-settings hello_backend --freezing-threshold 0 --impersonate "${IDENTITY_PRINCIPAL}"

# test management canister call failure (setting memory allocation to a low value)
assert_command_fail dfx canister update-settings hello_backend --memory-allocation 1 --impersonate aaaaa-aa
assert_command_fail dfx canister update-settings hello_backend --memory-allocation 1 --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "Management canister call failed: IC0402: Canister was given 1 B memory allocation but at least"

# canister status fails because the default identity does not control the canister anymore
assert_command_fail dfx canister status hello_backend
assert_contains "Only controllers of canister $CANISTER_ID can call ic00 method canister_status"

# canister status succeeds when impersonating the management canister as the sender
assert_command dfx canister status hello_backend --impersonate aaaaa-aa
assert_contains "Controllers: aaaaa-aa"
assert_command dfx canister status hello_backend --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "Controllers: ${IDENTITY_PRINCIPAL}"
assert_contains "Freezing threshold: 0"

# freeze the canister
assert_command dfx canister update-settings hello_backend --freezing-threshold 9223372036854775808 --confirm-very-long-freezing-threshold --impersonate aaaaa-aa
assert_command dfx canister update-settings hello_backend --freezing-threshold 9223372036854775808 --confirm-very-long-freezing-threshold --impersonate "${IDENTITY_PRINCIPAL}"

# test management canister call submission failure
assert_command_fail dfx canister status hello_backend --impersonate aaaaa-aa
assert_command_fail dfx canister status hello_backend --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "Failed to submit management canister call: IC0207: Canister $CANISTER_ID is out of cycles"

# test update call submission failure
assert_command_fail dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --update --impersonate aaaaa-aa
assert_command_fail dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --update --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "Failed to submit canister call: IC0207: Canister $CANISTER_ID is out of cycles"

# test async call submission failure
assert_command_fail dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate aaaaa-aa
assert_command_fail dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "Failed to submit canister call: IC0207: Canister $CANISTER_ID is out of cycles"

# unfreeze the canister
assert_command dfx canister update-settings hello_backend --freezing-threshold 0 --impersonate aaaaa-aa
assert_command dfx canister update-settings hello_backend --freezing-threshold 0 --impersonate "${IDENTITY_PRINCIPAL}"

# test update call failure
assert_command_fail dfx canister call aaaaa-aa delete_canister "(record { canister_id=principal\"$CANISTER_ID\" })" --update --impersonate aaaaa-aa
assert_command_fail dfx canister call aaaaa-aa delete_canister "(record { canister_id=principal\"$CANISTER_ID\" })" --update --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "Canister call failed: IC0510: Canister $CANISTER_ID must be stopped before it is deleted."

# test update call
assert_command dfx canister call aaaaa-aa start_canister "(record { canister_id=principal\"$CANISTER_ID\" })" --update --impersonate aaaaa-aa
assert_command dfx canister call aaaaa-aa start_canister "(record { canister_id=principal\"$CANISTER_ID\" })" --update --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "()"

# test async call
assert_command dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate aaaaa-aa
assert_command dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "Request ID:"

# test request status failure
RID=$(dfx canister call aaaaa-aa delete_canister "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate aaaaa-aa)
RID=$(dfx canister call aaaaa-aa delete_canister "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate "${IDENTITY_PRINCIPAL}")
assert_command_fail dfx canister request-status "$RID" hello_backend
assert_contains "Canister call failed: IC0510: Canister $CANISTER_ID must be stopped before it is deleted."

# test request status
RID=$(dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate aaaaa-aa)
RID=$(dfx canister call aaaaa-aa canister_status "(record { canister_id=principal\"$CANISTER_ID\" })" --async --impersonate "${IDENTITY_PRINCIPAL}")
assert_command dfx canister request-status "$RID" hello_backend
assert_contains "record {"

Expand All @@ -374,6 +374,22 @@ teardown() {
assert_contains "Failed to perform query call: IC0406: Caller $CANISTER_ID is not allowed to query ic00 method fetch_canister_logs"

# test query call
assert_command dfx canister call aaaaa-aa fetch_canister_logs "(record { canister_id=principal\"$CANISTER_ID\" })" --query --impersonate aaaaa-aa
assert_command dfx canister call aaaaa-aa fetch_canister_logs "(record { canister_id=principal\"$CANISTER_ID\" })" --query --impersonate "${IDENTITY_PRINCIPAL}"
assert_contains "(record { 1_754_302_831 = vec {} })"
}

@test "impersonate management canister as sender" {
[[ ! "$USE_POCKETIC" ]] && skip "skipped for replica: impersonating sender is only supported for PocketIC"

impersonate_sender "aaaaa-aa"
}

@test "impersonate new random identity as sender" {
[[ ! "$USE_POCKETIC" ]] && skip "skipped for replica: impersonating sender is only supported for PocketIC"

dfx identity new impersonated_identity
IDENTITY_PRINCIPAL="$(dfx --identity impersonated_identity identity get-principal)"
dfx identity remove impersonated_identity

impersonate_sender "${IDENTITY_PRINCIPAL}"
}

0 comments on commit fd5192c

Please sign in to comment.