package atspace import ( "crypto/sha256" "encoding/json" "testing" "github.com/bluesky-social/indigo/atproto/atcrypto" "github.com/bluesky-social/indigo/atproto/syntax" "tangled.org/bnewbold.net/cobalt/atspace/xsyntax" "github.com/stretchr/testify/assert" ) func TestSpaceCommitBasic(t *testing.T) { assert := assert.New(t) hash1 := sha256.Sum256([]byte("dummy1")) privkey, _ := atcrypto.GeneratePrivateKeyP256() pubkey, _ := privkey.PublicKey() space := xsyntax.SpaceRef("at://did:plc:asdf123/space/com.example.space/skey") space2 := xsyntax.SpaceRef("at://did:plc:asdf123/space/com.example.space/skey2") author := syntax.DID("did:web:author.example.com") author2 := syntax.DID("did:web:other.example.com") rev := syntax.TID("3mtwwsccuqy22") c1 := NewSpaceCommit(hash1, rev, space, author) assert.NoError(c1.Sign(space, author, privkey)) assert.NoError(c1.VerifyMAC(space, author)) assert.NoError(c1.VerifySignature(space, author, pubkey)) // check that all the fields are checked assert.Error(c1.VerifyMAC(space2, author)) assert.Error(c1.VerifyMAC(space, author2)) assert.Error(c1.VerifySignature(space2, author, pubkey)) assert.Error(c1.VerifySignature(space, author2, pubkey)) // corrupt mac c2 := NewSpaceCommit(hash1, rev, space, author) assert.NoError(c2.VerifyMAC(space, author)) c2.MAC[0] = c2.MAC[0] + 1 assert.Error(c2.VerifyMAC(space, author)) // corrupt sig c3 := NewSpaceCommit(hash1, rev, space, author) assert.NoError(c3.Sign(space, author, privkey)) assert.NoError(c3.VerifySignature(space, author, pubkey)) c3.Signature[0] = c3.Signature[0] + 1 assert.Error(c3.VerifySignature(space, author, pubkey)) // round-trip through JSON json1, err := json.Marshal(c1) if err != nil { t.Fail() } var c4 SpaceCommit if err := json.Unmarshal(json1, &c4); err != nil { t.Fail() } assert.NoError(c4.VerifyMAC(space, author)) assert.NoError(c4.VerifySignature(space, author, pubkey)) }