-
Notifications
You must be signed in to change notification settings - Fork 12
fix: Bugfixes and cleanup #98
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package capability | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "cosmossdk.io/log" | ||
| "cosmossdk.io/store" | ||
| "cosmossdk.io/store/metrics" | ||
| storetypes "cosmossdk.io/store/types" | ||
| cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" | ||
| dbm "github.com/cosmos/cosmos-db" | ||
| "github.com/cosmos/cosmos-sdk/codec" | ||
| codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func setupCapKeeper(t *testing.T) (sdk.Context, *capabilitykeeper.ScopedKeeper, *capabilitykeeper.ScopedKeeper) { | ||
| capStoreKey := storetypes.NewKVStoreKey("capkeeper") | ||
| capMemStoreKey := storetypes.NewMemoryStoreKey("capkeepermem") | ||
|
|
||
| db := dbm.NewMemDB() | ||
| stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics()) | ||
| stateStore.MountStoreWithDB(capStoreKey, storetypes.StoreTypeDB, db) | ||
| stateStore.MountStoreWithDB(capMemStoreKey, storetypes.StoreTypeMemory, nil) | ||
| require.NoError(t, stateStore.LoadLatestVersion()) | ||
|
|
||
| registry := codectypes.NewInterfaceRegistry() | ||
| cdc := codec.NewProtoCodec(registry) | ||
| capKeeper := capabilitykeeper.NewKeeper(cdc, capStoreKey, capMemStoreKey) | ||
|
|
||
| acpScoped := capKeeper.ScopeToModule("acp") | ||
| otherScoped := capKeeper.ScopeToModule("other") | ||
|
|
||
| ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger()) | ||
| capKeeper.Seal() | ||
|
|
||
| return ctx, &acpScoped, &otherScoped | ||
| } | ||
|
|
||
| func TestNewPolicyCapabilityManager(t *testing.T) { | ||
| _, acpScoped, _ := setupCapKeeper(t) | ||
| mgr := NewPolicyCapabilityManager(acpScoped) | ||
| require.NotNil(t, mgr) | ||
| } | ||
|
|
||
| func TestIssueAndFetch(t *testing.T) { | ||
| ctx, acpScoped, _ := setupCapKeeper(t) | ||
| mgr := NewPolicyCapabilityManager(acpScoped) | ||
|
|
||
| cap, err := mgr.Issue(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
| require.NotNil(t, cap) | ||
| require.Equal(t, "policy-1", cap.GetPolicyId()) | ||
| require.NotNil(t, cap.GetCosmosCapability()) | ||
|
|
||
| fetched, err := mgr.Fetch(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
| require.NotNil(t, fetched) | ||
| require.Equal(t, "policy-1", fetched.GetPolicyId()) | ||
| } | ||
|
|
||
| func TestFetchNonExistent(t *testing.T) { | ||
| ctx, acpScoped, _ := setupCapKeeper(t) | ||
| mgr := NewPolicyCapabilityManager(acpScoped) | ||
|
|
||
| _, err := mgr.Fetch(ctx, "nonexistent") | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestClaimCapability(t *testing.T) { | ||
| ctx, acpScoped, otherScoped := setupCapKeeper(t) | ||
| acpMgr := NewPolicyCapabilityManager(acpScoped) | ||
| otherMgr := NewPolicyCapabilityManager(otherScoped) | ||
|
|
||
| // acp issues | ||
| cap, err := acpMgr.Issue(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
|
|
||
| // other module claims | ||
| err = otherMgr.Claim(ctx, cap) | ||
| require.NoError(t, err) | ||
|
|
||
| // other module can now fetch | ||
| fetched, err := otherMgr.Fetch(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
| require.NotNil(t, fetched) | ||
| } | ||
|
|
||
| func TestValidateCapability(t *testing.T) { | ||
| ctx, acpScoped, otherScoped := setupCapKeeper(t) | ||
| acpMgr := NewPolicyCapabilityManager(acpScoped) | ||
| otherMgr := NewPolicyCapabilityManager(otherScoped) | ||
|
|
||
| cap, err := acpMgr.Issue(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
|
|
||
| err = otherMgr.Claim(ctx, cap) | ||
| require.NoError(t, err) | ||
|
|
||
| // validate from the other module's perspective — acp is an owner, so it passes | ||
| err = otherMgr.Validate(ctx, cap) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestGetOwnerModule(t *testing.T) { | ||
| ctx, acpScoped, otherScoped := setupCapKeeper(t) | ||
| acpMgr := NewPolicyCapabilityManager(acpScoped) | ||
| otherMgr := NewPolicyCapabilityManager(otherScoped) | ||
|
|
||
| cap, err := acpMgr.Issue(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
|
|
||
| err = otherMgr.Claim(ctx, cap) | ||
| require.NoError(t, err) | ||
|
|
||
| owner, err := otherMgr.GetOwnerModule(ctx, cap) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "other", owner) | ||
| } | ||
|
|
||
| func TestGetOwnerModuleNoClaimer(t *testing.T) { | ||
| ctx, acpScoped, _ := setupCapKeeper(t) | ||
| acpMgr := NewPolicyCapabilityManager(acpScoped) | ||
|
|
||
| cap, err := acpMgr.Issue(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
|
|
||
| // only acp owns it, after filtering acp out, mods is empty | ||
| _, err = acpMgr.GetOwnerModule(ctx, cap) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestValidateAcpOnlyOwner(t *testing.T) { | ||
| ctx, acpScoped, _ := setupCapKeeper(t) | ||
| acpMgr := NewPolicyCapabilityManager(acpScoped) | ||
|
|
||
| cap, err := acpMgr.Issue(ctx, "policy-1") | ||
| require.NoError(t, err) | ||
|
|
||
| // Validate should pass since acp issued it | ||
| err = acpMgr.Validate(ctx, cap) | ||
| require.NoError(t, err) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package capability | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPolicyCapabilityGetCapabilityName(t *testing.T) { | ||
| cap := &PolicyCapability{policyId: "test-policy-123"} | ||
| require.Equal(t, "/acp/module_policies/test-policy-123", cap.GetCapabilityName()) | ||
| } | ||
|
|
||
| func TestPolicyCapabilityGetPolicyId(t *testing.T) { | ||
| cap := &PolicyCapability{policyId: "test-policy-123"} | ||
| require.Equal(t, "test-policy-123", cap.GetPolicyId()) | ||
| } | ||
|
|
||
| func TestPolicyCapabilityGetCosmosCapability(t *testing.T) { | ||
| cap := &PolicyCapability{policyId: "p1", capability: nil} | ||
| require.Nil(t, cap.GetCosmosCapability()) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package did | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestProduceDID(t *testing.T) { | ||
| did, signer, err := ProduceDID() | ||
| require.NoError(t, err) | ||
| require.NotEmpty(t, did) | ||
| require.NotNil(t, signer) | ||
| require.Contains(t, did, "did:key:") | ||
| require.NoError(t, IsValidDID(did)) | ||
| } | ||
|
|
||
| func TestProduceDIDUniqueness(t *testing.T) { | ||
| did1, _, err := ProduceDID() | ||
| require.NoError(t, err) | ||
| did2, _, err := ProduceDID() | ||
| require.NoError(t, err) | ||
| require.NotEqual(t, did1, did2) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,9 @@ func IssueDID(acc sdk.AccountI) (string, error) { | |
|
|
||
| // DIDFromPubKey constructs and returns a DID from a public key. | ||
| func DIDFromPubKey(pk cryptotypes.PubKey) (string, error) { | ||
| if pk == nil { | ||
| return "", fmt.Errorf("account public key is nil") | ||
| } | ||
|
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🌐 Web query:
💡 Result: In Go, if an interface value holds a typed nil pointer (e.g., var p *T = nil; var i I = p), then i == nil evaluates to false. Citations:
🏁 Script executed: cd x/acp/did && cat -n types.go | head -100Repository: sourcenetwork/sourcehub Length of output: 3328 Guard against typed-nil The 🔧 Suggested fix func DIDFromPubKey(pk cryptotypes.PubKey) (string, error) {
if pk == nil {
return "", fmt.Errorf("account public key is nil")
}
var keyType crypto.KeyType
switch t := pk.(type) {
case *secp256k1.PubKey:
+ if t == nil {
+ return "", fmt.Errorf("account public key is nil")
+ }
keyType = crypto.SECP256k1
case *secp256r1.PubKey:
+ if t == nil {
+ return "", fmt.Errorf("account public key is nil")
+ }
keyType = crypto.P256
case *ed25519.PubKey:
+ if t == nil {
+ return "", fmt.Errorf("account public key is nil")
+ }
keyType = crypto.Ed25519
default:
return "", fmt.Errorf(
"failed to issue did for key %v: account key type must be secp256k1, secp256r1, or ed25519, got %v",
pk.Bytes(), t,
)
}🤖 Prompt for AI Agents |
||
| var keyType crypto.KeyType | ||
| switch t := pk.(type) { | ||
| case *secp256k1.PubKey: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.