Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FLIP 242 #3252

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions migrations/entitlements/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ func TestConvertToEntitledValue(t *testing.T) {
wrap: func(staticType interpreter.StaticType) interpreter.Value {
return interpreter.NewCapabilityValue(
nil,
0,
1,
interpreter.AddressValue{},
staticType,
)
Expand All @@ -1241,7 +1241,7 @@ func TestConvertToEntitledValue(t *testing.T) {
interpreter.AddressValue{},
interpreter.NewCapabilityValue(
nil,
0,
1,
interpreter.AddressValue{},
staticType,
),
Expand Down Expand Up @@ -1453,7 +1453,7 @@ func TestMigrateSimpleContract(t *testing.T) {

unentitledRCap := interpreter.NewCapabilityValue(
inter,
0,
1,
interpreter.NewAddressValue(inter, account),
unentitledRRefStaticType,
)
Expand All @@ -1475,7 +1475,7 @@ func TestMigrateSimpleContract(t *testing.T) {
entitledRRefStaticType := entitledRRef.StaticType(inter)
entitledRCap := interpreter.NewCapabilityValue(
inter,
0,
1,
interpreter.NewAddressValue(inter, account),
entitledRRefStaticType,
)
Expand All @@ -1492,7 +1492,7 @@ func TestMigrateSimpleContract(t *testing.T) {
storedValue: unentitledRCap.Clone(inter),
expectedValue: interpreter.NewCapabilityValue(
inter,
0,
1,
interpreter.NewAddressValue(inter, account),
entitledRRefStaticType,
),
Expand Down
54 changes: 42 additions & 12 deletions runtime/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testR() {
let path = /public/r
let cap = self.account.capabilities.get<&R>(path)!
let cap = self.account.capabilities.get<&R>(path)

assert(self.account.capabilities.exists(path))

Expand Down Expand Up @@ -162,7 +162,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testRAsR2() {
let path = /public/rAsR2
let cap = self.account.capabilities.get<&R2>(path)!
let cap = self.account.capabilities.get<&R2>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -185,7 +185,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testRAsS() {
let path = /public/rAsS
let cap = self.account.capabilities.get<&S>(path)!
let cap = self.account.capabilities.get<&S>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -208,7 +208,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistentTarget() {
let path = /public/nonExistentTarget
let cap = self.account.capabilities.get<&R>(path)!
let cap = self.account.capabilities.get<&R>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -231,13 +231,28 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistent() {
let path = /public/nonExistent
assert(self.account.capabilities.get<&AnyResource>(path) == nil)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved

let cap = self.account.capabilities.get<&R>(path)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(cap.id == 0)
assert(cap as? Capability<&R> != nil)
assert(cap as? Capability<&AnyResource> != nil)
assert(cap.borrow() == nil)
assert(cap.address == 0x1)
assert(cap.check() == false)

let cap2 = self.account.capabilities.get<&AnyResource>(path)
assert(cap2.id == 0)
assert(cap2 as? Capability<&AnyResource> != nil)
assert(cap2.borrow() == nil)
assert(cap2.address == 0x1)
assert(cap2.check() == false)

assert(!self.account.capabilities.exists(path))
}

access(all)
fun testSwap(): Int {
let ref = self.account.capabilities.get<&R>(/public/r)!.borrow()!
let ref = self.account.capabilities.get<&R>(/public/r).borrow()!

let r <- self.account.storage.load<@R>(from: /storage/r)
destroy r
Expand Down Expand Up @@ -385,7 +400,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testS() {
let path = /public/s
let cap = self.account.capabilities.get<&S>(path)!
let cap = self.account.capabilities.get<&S>(path)

assert(self.account.capabilities.exists(path))

Expand Down Expand Up @@ -414,7 +429,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testSAsS2() {
let path = /public/sAsS2
let cap = self.account.capabilities.get<&S2>(path)!
let cap = self.account.capabilities.get<&S2>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -437,7 +452,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testSAsR() {
let path = /public/sAsR
let cap = self.account.capabilities.get<&R>(path)!
let cap = self.account.capabilities.get<&R>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -460,7 +475,7 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistentTarget() {
let path = /public/nonExistentTarget
let cap = self.account.capabilities.get<&S>(path)!
let cap = self.account.capabilities.get<&S>(path)

assert(self.account.capabilities.exists(path))

Expand All @@ -483,13 +498,28 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
access(all)
fun testNonExistent() {
let path = /public/nonExistent
assert(self.account.capabilities.get<&AnyStruct>(path) == nil)

let cap = self.account.capabilities.get<&S>(path)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(cap.id == 0)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(cap as? Capability<&S> != nil)
assert(cap as? Capability<&AnyStruct> != nil)
assert(cap.borrow() == nil)
assert(cap.address == 0x1)
assert(cap.check() == false)

let cap2 = self.account.capabilities.get<&AnyStruct>(path)
assert(cap2.id == 0)
assert(cap2 as? Capability<&AnyStruct> != nil)
assert(cap2.borrow() == nil)
assert(cap2.address == 0x1)
assert(cap2.check() == false)

assert(!self.account.capabilities.exists(path))
}

access(all)
fun testSwap(): Int {
let ref = self.account.capabilities.get<&S>(/public/s)!.borrow()!
let ref = self.account.capabilities.get<&S>(/public/s).borrow()!

self.account.storage.load<S>(from: /storage/s)

Expand Down
57 changes: 39 additions & 18 deletions runtime/capabilitycontrollers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,15 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
let path = /public/x

// Act
let gotCap: Capability<&AnyStruct>? =
let gotCap: Capability<&AnyStruct> =
%[1]s.capabilities.get<&AnyStruct>(path)

// Assert
assert(!%[1]s.capabilities.exists(path))
assert(gotCap == nil)
assert(gotCap.id == 0)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
assert(gotCap.borrow() == nil)
assert(gotCap.check() == false)
assert(gotCap.address == 0x1)
}
}
`,
Expand Down Expand Up @@ -274,7 +277,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)!
%[1]s.capabilities.get<&Test.R>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
Expand Down Expand Up @@ -308,7 +311,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Account> =
%[1]s.capabilities.get<&Account>(publicPath)!
%[1]s.capabilities.get<&Account>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
Expand Down Expand Up @@ -353,7 +356,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)!
%[1]s.capabilities.get<&Test.R>(publicPath)
let ref: &Test.R = gotCap.borrow()!

// Assert
Expand Down Expand Up @@ -390,7 +393,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

// Act
let gotCap: Capability<&Account> =
%[1]s.capabilities.get<&Account>(publicPath)!
%[1]s.capabilities.get<&Account>(publicPath)
let ref: &Account = gotCap.borrow()!

// Assert
Expand Down Expand Up @@ -436,13 +439,16 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<auth(Test.X) &Test.R>? =
let gotCap: Capability<auth(Test.X) &Test.R> =
%[1]s.capabilities.get<auth(Test.X) &Test.R>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
assert(gotCap.borrow() == nil)
assert(gotCap.check() == false)
assert(gotCap.address == 0x1)
}
}
`,
Expand Down Expand Up @@ -472,13 +478,16 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<&Test.R>? =
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
assert(gotCap.borrow() == nil)
assert(gotCap.check() == false)
assert(gotCap.address == 0x1)
}
}
`,
Expand Down Expand Up @@ -516,13 +525,16 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<&Test.S>? =
let gotCap: Capability<&Test.S> =
%[1]s.capabilities.get<&Test.S>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
assert(gotCap.borrow() == nil)
assert(gotCap.check() == false)
assert(gotCap.address == 0x1)
}
}
`,
Expand Down Expand Up @@ -550,13 +562,16 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
signer.capabilities.publish(issuedCap, at: publicPath)

// Act
let gotCap: Capability<&AnyResource>? =
let gotCap: Capability<&AnyResource> =
%[1]s.capabilities.get<&AnyResource>(publicPath)

// Assert
assert(%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
assert(gotCap.borrow() == nil)
assert(gotCap.check() == false)
assert(gotCap.address == 0x1)
}
}
`,
Expand Down Expand Up @@ -594,14 +609,17 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
let unpublishedcap = signer.capabilities.unpublish(publicPath)

// Act
let gotCap: Capability<&Test.R>? =
let gotCap: Capability<&Test.R> =
%[1]s.capabilities.get<&Test.R>(publicPath)

// Assert
assert(!%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(unpublishedcap!.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
assert(gotCap.borrow() == nil)
assert(gotCap.check() == false)
assert(gotCap.address == 0x1)
}
}
`,
Expand Down Expand Up @@ -629,14 +647,17 @@ func TestRuntimeCapabilityControllers(t *testing.T) {
let unpublishedcap = signer.capabilities.unpublish(publicPath)

// Act
let gotCap: Capability<&Account>? =
let gotCap: Capability<&Account> =
%[1]s.capabilities.get<&Account>(publicPath)

// Assert
assert(!%[1]s.capabilities.exists(publicPath))
assert(issuedCap.id == expectedCapID)
assert(unpublishedcap!.id == expectedCapID)
assert(gotCap == nil)
assert(gotCap.id == 0)
assert(gotCap.borrow() == nil)
assert(gotCap.check() == false)
assert(gotCap.address == 0x1)
}
}
`,
Expand Down
Loading
Loading