Skip to content

Commit

Permalink
chore: examples overhaul (#473)
Browse files Browse the repository at this point in the history
* make assertions

* pin btc-canister

* gitignore

* comment

* asset_storage

* fix management

* chess

* counter

* print

* simplify profile

* edition 2021 & resolver 2
  • Loading branch information
lwshang authored Mar 14, 2024
1 parent 08cbe39 commit ab458c4
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 94 deletions.
5 changes: 2 additions & 3 deletions examples/asset_storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[workspace]
members = [
"src/asset_storage_rs",
]
members = ["src/asset_storage_rs"]
resolver = "2"

[profile.release]
lto = true
Expand Down
2 changes: 1 addition & 1 deletion examples/asset_storage/src/asset_storage_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "asset_storage_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
88 changes: 42 additions & 46 deletions examples/asset_storage/tests/basic.bats
Original file line number Diff line number Diff line change
@@ -1,70 +1,66 @@
load ../../bats/bats-assert/load.bash

# Executed before each test.
setup() {
cd examples/asset_storage
# Make sure the directory is clean.
dfx start --clean --background
dfx start --clean --background

run dfx identity new alice --disable-encryption
run dfx identity new bob --disable-encryption
run dfx identity new charlie --disable-encryption
x=$(mktemp -d -t cdk-XXXXXXXX)
export DFX_CONFIG_ROOT="$x"
dfx identity new alice --storage-mode=plaintext
dfx identity new bob --storage-mode=plaintext
dfx identity new charlie --storage-mode=plaintext
}

# executed after each test
teardown() {
dfx stop
rm -rf "$DFX_CONFIG_ROOT"
}

@test "Can store and restore assets" {
dfx deploy
dfx canister call asset_storage store '("asset_name", vec { 1; 2; 3; })'
dfx canister call asset_storage retrieve '("asset_name")'
run dfx canister call asset_storage retrieve '("unknown")'
# As of dfx 0.8.1, above command results in following error message:
# > The Replica returned an error: code 5, message: "IC0502: Canister rwlgt-iiaaa-aaaaa-aaaaa-cai trapped: unreachable"
[ "$status" != 0 ]
}

@test "Will fails on invalid identities" {
dfx identity use alice
dfx deploy
dfx canister call asset_storage store '("asset_name", vec { 1; 2; 3; })'
dfx canister call asset_storage retrieve '("asset_name")'

dfx canister call asset_storage add_user "(principal \"$(dfx --identity charlie identity get-principal)\")"

dfx identity use bob
dfx canister call asset_storage retrieve '("asset_name")'
dfx --identity alice deploy
run dfx --identity alice canister call asset_storage store '("asset_name", vec { 1; 2; 3; })'
assert_success
run dfx --identity alice canister call asset_storage retrieve '("asset_name")'
assert_success

# Test that an unknown asset fails.
run dfx canister call asset_storage retrieve '("unknown")'
[ "$status" != 0 ]
run dfx --identity alice canister call asset_storage retrieve '("unknown")'
assert_failure
}

# Test that cannot upload assets as bob.
run dfx canister call asset_storage store '("asset_name", vec { 1; })'
[ "$status" != 0 ]
@test "Unauthorized identity cannot store" {
dfx --identity alice deploy
dfx --identity alice canister call asset_storage store '("asset_name", vec { 1; 2; 3; })'
dfx --identity alice canister call asset_storage retrieve '("asset_name")'

# Test we can upload assets as charlie.
dfx identity use charlie
run dfx canister call asset_storage store '("asset_name_2", vec { 1; 2; 3; })'
[ "$status" == 0 ]
}
# add charlie
run dfx --identity alice canister call asset_storage add_user "(principal \"$(dfx --identity charlie identity get-principal)\")"
assert_success

@test "Can upgrade and keep ACLs" {
dfx identity use alice
dfx deploy
# bob cannot upload assets
run dfx --identity bob canister call asset_storage store '("asset_name", vec { 1; })'
assert_failure

dfx canister call asset_storage store '("asset_name", vec { 1; 2; 3; })'
dfx identity use bob
run dfx canister call asset_storage retrieve '("unknown")'
[ "$status" != 0 ]
# charlie can upload assets
run dfx --identity charlie canister call asset_storage store '("asset_name_2", vec { 1; 2; 3; })'
assert_success
}

dfx identity use alice
dfx canister call asset_storage add_user "(principal \"$(dfx --identity charlie identity get-principal)\")"
@test "Can upgrade and keep the access control list" {
dfx --identity alice deploy
dfx --identity alice canister call asset_storage store '("asset_name", vec { 1; 2; 3; })'
dfx --identity alice canister call asset_storage add_user "(principal \"$(dfx --identity charlie identity get-principal)\")"

dfx build
dfx canister install --all --mode=upgrade

dfx identity use charlie
run dfx canister call asset_storage store '("asset_name_2", vec { 1; 2; 3; })'
[ "$status" == 0 ]
# bob still cannot upload assets
run dfx --identity bob canister call asset_storage store '("asset_name", vec { 1; })'
assert_failure

# charlie still can upload assets
run dfx --identity charlie canister call asset_storage store '("asset_name_2", vec { 1; 2; 3; })'
assert_success
}
5 changes: 2 additions & 3 deletions examples/chess/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[workspace]
members = [
"src/chess_rs",
]
members = ["src/chess_rs"]
resolver = "2"

[profile.release]
lto = true
Expand Down
6 changes: 4 additions & 2 deletions examples/chess/src/chess_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "chess_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -14,4 +14,6 @@ candid = "0.10"
ic-cdk = { path = "../../../../src/ic-cdk" }
serde = "1.0.111"
tanton = "1.0.0"
getrandom = { version = "0.2", features = ["custom"] } # tanton requires this to compile on wasm target
getrandom = { version = "0.2", features = [
"custom",
] } # tanton requires this to compile on wasm target
10 changes: 6 additions & 4 deletions examples/chess/tests/basic.bats
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load ../../bats/bats-assert/load.bash

# Executed before each test.
setup() {
cd examples/chess
Expand All @@ -13,11 +15,11 @@ teardown() {
@test "Can play chess against AI" {
dfx deploy
run dfx canister call chess_rs new '("test", true)'
[ "$output" == "()" ]
assert_output '()'
run dfx canister call chess_rs move '("test", "e2e4")'
[ "$output" == "(true)" ]
assert_output '(true)'
run dfx canister call chess_rs move '("test", "d2d3")'
[ "$output" == "(true)" ]
assert_output '(true)'
run dfx canister call chess_rs getFen '("test")'
[ "$output" == '(opt "rnb1kbnr/pp1ppppp/1qp5/8/4P3/3P4/PPP2PPP/RNBQKBNR w KQkq - 1 3")' ]
assert_output '(opt "rnb1kbnr/pp1ppppp/1qp5/8/4P3/3P4/PPP2PPP/RNBQKBNR w KQkq - 1 3")'
}
1 change: 1 addition & 0 deletions examples/counter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["src/counter_rs", "src/inter_rs", "src/inter2_rs"]
resolver = "2"

[workspace.dependencies]
candid = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion examples/counter/src/counter_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "counter_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion examples/counter/src/inter2_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "inter2_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion examples/counter/src/inter_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "inter_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
31 changes: 18 additions & 13 deletions examples/counter/tests/basic.bats
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load ../../bats/bats-assert/load.bash

# Executed before each test.
setup() {
cd examples/counter
Expand All @@ -13,52 +15,55 @@ teardown() {
@test "Can counter (counter_rs)" {
dfx deploy
run dfx canister call counter_rs read
[ "$output" == '(0 : nat)' ]
assert_output '(0 : nat)'
dfx canister call counter_rs inc
run dfx canister call counter_rs read
[ "$output" == '(1 : nat)' ]
assert_output '(1 : nat)'
run dfx canister call counter_rs write '(5)'
dfx canister call counter_rs inc
run dfx canister call counter_rs read
[ "$output" == '(6 : nat)' ]
assert_output '(6 : nat)'
}

@test "Can counter (inter_rs)" {
dfx deploy
run dfx canister call inter_rs read
[ "$output" == '(0 : nat)' ]
assert_output '(0 : nat)'
dfx canister call inter_rs inc
run dfx canister call inter_rs read
[ "$output" == '(1 : nat)' ]
assert_output '(1 : nat)'
run dfx canister call inter_rs write '(5)'
dfx canister call inter_rs inc
run dfx canister call inter_rs read
[ "$output" == '(6 : nat)' ]
assert_output '(6 : nat)'
}

@test "Can counter (inter2_rs)" {
dfx deploy

run dfx canister call counter_rs read
[ "$output" == '(0 : nat)' ]
assert_output '(0 : nat)'

run dfx canister call inter2_rs read
[ "$output" == '(0 : nat)' ]
assert_output '(0 : nat)'
dfx canister call inter2_rs inc
run dfx canister call inter2_rs read
[ "$output" == '(1 : nat)' ]
assert_output '(1 : nat)'
run dfx canister call inter2_rs write '(5)'
dfx canister call inter2_rs inc
run dfx canister call inter2_rs read
[ "$output" == '(6 : nat)' ]
assert_output '(6 : nat)'

# Check that counter_rs has 6 too.
run dfx canister call counter_rs read
[ "$output" == '(6 : nat)' ]
assert_output '(6 : nat)'
}

@test "counter_rs generated Candid excludes hidden methods" {
dfx build --check counter_rs
! grep -q update_hidden src/counter_rs/counter.did
! grep -q query_hidden src/counter_rs/counter.did
run grep -q update_hidden src/counter_rs/counter.did
assert_failure

run grep -q query_hidden src/counter_rs/counter.did
assert_failure
}
1 change: 1 addition & 0 deletions examples/management_canister/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ic-btc-canister.wasm.gz
5 changes: 2 additions & 3 deletions examples/management_canister/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[workspace]
members = [
"src/caller",
]
members = ["src/caller"]
resolver = "2"
22 changes: 17 additions & 5 deletions examples/management_canister/tests/basic.bats
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load ../../bats/bats-assert/load.bash

# Executed before each test.
setup() {
cd examples/management_canister
Expand All @@ -9,21 +11,31 @@ teardown() {
}

@test "http_request example succeed" {
dfx start --clean --background --enable-canister-http
dfx start --clean --background # canister-http default on
dfx deploy
dfx canister call caller http_request_example
run dfx canister call caller http_request_example
assert_success
}

@test "ecdsa methods succeed" {
dfx start --clean --background
dfx deploy
dfx canister call caller execute_ecdsa_methods
run dfx canister call caller execute_ecdsa_methods
assert_success
}

@test "bitcoin methods succeed" {
bitcoind -regtest -daemonwait
dfx start --clean --background --enable-bitcoin

# The bitcoin canister bundled with dfx 0.18.0 doesn't work with application replica
# As a temporary remedy, we download a previous release.
# TODO: remove when dfx fix, SDKTG-296
wget -O ic-btc-canister.wasm.gz https://github.com/dfinity/bitcoin-canister/releases/download/release%2F2023-10-13/ic-btc-canister.wasm.gz
DFX_BITCOIN_WASM=ic-btc-canister.wasm.gz dfx start --clean --background --enable-bitcoin

dfx deploy
dfx canister call caller execute_bitcoin_methods
run dfx canister call caller execute_bitcoin_methods
assert_success

bitcoin-cli -regtest stop
}
1 change: 1 addition & 0 deletions examples/print/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["src/print_rs"]
resolver = "2"

[workspace.dependencies]
ic-cdk = { path = "../../src/ic-cdk" }
Expand Down
2 changes: 1 addition & 1 deletion examples/print/src/print_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "print_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
5 changes: 4 additions & 1 deletion examples/print/tests/basic.bats
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load ../../bats/bats-assert/load.bash

# Executed before each test.
setup() {
cd examples/print
Expand All @@ -13,5 +15,6 @@ teardown() {
@test "Can print" {
dfx deploy

dfx canister call print print
run dfx canister call print print
assert_success
}
1 change: 1 addition & 0 deletions examples/profile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["src/profile_rs", "src/profile_inter_rs"]
resolver = "2"

[workspace.dependencies]
candid = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion examples/profile/src/profile_inter_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "profile_inter_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
2 changes: 1 addition & 1 deletion examples/profile/src/profile_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "profile_rs"
version = "0.1.0"
edition = "2018"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
Loading

0 comments on commit ab458c4

Please sign in to comment.