refactor: 重构trustlog-sdk目录结构到trustlog/go-trustlog
- 将所有trustlog-sdk文件移动到trustlog/go-trustlog/目录 - 更新README中所有import路径从trustlog-sdk改为go-trustlog - 更新cookiecutter配置文件中的项目名称 - 更新根目录.lefthook.yml以引用新位置的配置 - 添加go.sum文件到版本控制 - 删除过时的示例文件 这次重构与trustlog-server保持一致的目录结构, 为未来支持多语言SDK(Python、Java等)预留空间。
This commit is contained in:
349
api/model/proof_test.go
Normal file
349
api/model/proof_test.go
Normal file
@@ -0,0 +1,349 @@
|
||||
package model_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.yandata.net/iod/iod/trustlog-sdk/api/grpc/pb"
|
||||
"go.yandata.net/iod/iod/trustlog-sdk/api/model"
|
||||
)
|
||||
|
||||
func TestProofFromProtobuf_Nil(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result := model.ProofFromProtobuf(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_Empty(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Empty(t, result.Sign)
|
||||
assert.Empty(t, result.Version)
|
||||
assert.Nil(t, result.ColItems)
|
||||
assert.Nil(t, result.RawItems)
|
||||
assert.Nil(t, result.ColRootItem)
|
||||
assert.Nil(t, result.RawRootItem)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_WithSign(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
Sign: "test-signature",
|
||||
}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "test-signature", result.Sign)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_WithVersion(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
Version: "v1.0.0",
|
||||
}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "v1.0.0", result.Version)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_WithColItems(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
ColItems: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 1, Hash: "hash1", Left: true},
|
||||
{Floor: 2, Hash: "hash2", Left: false},
|
||||
},
|
||||
}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.ColItems, 2)
|
||||
assert.Equal(t, uint32(1), result.ColItems[0].Floor)
|
||||
assert.Equal(t, "hash1", result.ColItems[0].Hash)
|
||||
assert.True(t, result.ColItems[0].Left)
|
||||
assert.Equal(t, uint32(2), result.ColItems[1].Floor)
|
||||
assert.Equal(t, "hash2", result.ColItems[1].Hash)
|
||||
assert.False(t, result.ColItems[1].Left)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_WithRawItems(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
RawItems: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 3, Hash: "hash3", Left: true},
|
||||
},
|
||||
}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.RawItems, 1)
|
||||
assert.Equal(t, uint32(3), result.RawItems[0].Floor)
|
||||
assert.Equal(t, "hash3", result.RawItems[0].Hash)
|
||||
assert.True(t, result.RawItems[0].Left)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_WithColRootItem(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
ColRootItem: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 4, Hash: "hash4", Left: false},
|
||||
},
|
||||
}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.ColRootItem, 1)
|
||||
assert.Equal(t, uint32(4), result.ColRootItem[0].Floor)
|
||||
assert.Equal(t, "hash4", result.ColRootItem[0].Hash)
|
||||
assert.False(t, result.ColRootItem[0].Left)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_WithRawRootItem(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
RawRootItem: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 5, Hash: "hash5", Left: true},
|
||||
},
|
||||
}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.RawRootItem, 1)
|
||||
assert.Equal(t, uint32(5), result.RawRootItem[0].Floor)
|
||||
assert.Equal(t, "hash5", result.RawRootItem[0].Hash)
|
||||
assert.True(t, result.RawRootItem[0].Left)
|
||||
}
|
||||
|
||||
func TestProofFromProtobuf_Full(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
Sign: "full-signature",
|
||||
Version: "v1.0.0",
|
||||
ColItems: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 1, Hash: "col1", Left: true},
|
||||
},
|
||||
RawItems: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 2, Hash: "raw1", Left: false},
|
||||
},
|
||||
ColRootItem: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 3, Hash: "colroot1", Left: true},
|
||||
},
|
||||
RawRootItem: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 4, Hash: "rawroot1", Left: false},
|
||||
},
|
||||
}
|
||||
result := model.ProofFromProtobuf(pbProof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "full-signature", result.Sign)
|
||||
assert.Equal(t, "v1.0.0", result.Version)
|
||||
assert.Len(t, result.ColItems, 1)
|
||||
assert.Len(t, result.RawItems, 1)
|
||||
assert.Len(t, result.ColRootItem, 1)
|
||||
assert.Len(t, result.RawRootItem, 1)
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_Nil(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result := model.ProofToProtobuf(nil)
|
||||
assert.Nil(t, result)
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_Empty(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Empty(t, result.GetSign())
|
||||
assert.Empty(t, result.GetVersion())
|
||||
assert.Nil(t, result.GetColItems())
|
||||
assert.Nil(t, result.GetRawItems())
|
||||
assert.Nil(t, result.GetColRootItem())
|
||||
assert.Nil(t, result.GetRawRootItem())
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_WithSign(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{
|
||||
Sign: "test-signature",
|
||||
}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "test-signature", result.GetSign())
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_WithVersion(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{
|
||||
Version: "v1.0.0",
|
||||
}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "v1.0.0", result.GetVersion())
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_WithColItems(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{
|
||||
ColItems: []*model.MerkleTreeProofItem{
|
||||
{Floor: 1, Hash: "hash1", Left: true},
|
||||
{Floor: 2, Hash: "hash2", Left: false},
|
||||
},
|
||||
}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.GetColItems(), 2)
|
||||
assert.Equal(t, uint32(1), result.GetColItems()[0].GetFloor())
|
||||
assert.Equal(t, "hash1", result.GetColItems()[0].GetHash())
|
||||
assert.True(t, result.GetColItems()[0].GetLeft())
|
||||
assert.Equal(t, uint32(2), result.GetColItems()[1].GetFloor())
|
||||
assert.Equal(t, "hash2", result.GetColItems()[1].GetHash())
|
||||
assert.False(t, result.GetColItems()[1].GetLeft())
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_WithRawItems(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{
|
||||
RawItems: []*model.MerkleTreeProofItem{
|
||||
{Floor: 3, Hash: "hash3", Left: true},
|
||||
},
|
||||
}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.GetRawItems(), 1)
|
||||
assert.Equal(t, uint32(3), result.GetRawItems()[0].GetFloor())
|
||||
assert.Equal(t, "hash3", result.GetRawItems()[0].GetHash())
|
||||
assert.True(t, result.GetRawItems()[0].GetLeft())
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_WithColRootItem(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{
|
||||
ColRootItem: []*model.MerkleTreeProofItem{
|
||||
{Floor: 4, Hash: "hash4", Left: false},
|
||||
},
|
||||
}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.GetColRootItem(), 1)
|
||||
assert.Equal(t, uint32(4), result.GetColRootItem()[0].GetFloor())
|
||||
assert.Equal(t, "hash4", result.GetColRootItem()[0].GetHash())
|
||||
assert.False(t, result.GetColRootItem()[0].GetLeft())
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_WithRawRootItem(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{
|
||||
RawRootItem: []*model.MerkleTreeProofItem{
|
||||
{Floor: 5, Hash: "hash5", Left: true},
|
||||
},
|
||||
}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.GetRawRootItem(), 1)
|
||||
assert.Equal(t, uint32(5), result.GetRawRootItem()[0].GetFloor())
|
||||
assert.Equal(t, "hash5", result.GetRawRootItem()[0].GetHash())
|
||||
assert.True(t, result.GetRawRootItem()[0].GetLeft())
|
||||
}
|
||||
|
||||
func TestProofToProtobuf_Full(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proof := &model.Proof{
|
||||
Sign: "full-signature",
|
||||
Version: "v1.0.0",
|
||||
ColItems: []*model.MerkleTreeProofItem{
|
||||
{Floor: 1, Hash: "col1", Left: true},
|
||||
},
|
||||
RawItems: []*model.MerkleTreeProofItem{
|
||||
{Floor: 2, Hash: "raw1", Left: false},
|
||||
},
|
||||
ColRootItem: []*model.MerkleTreeProofItem{
|
||||
{Floor: 3, Hash: "colroot1", Left: true},
|
||||
},
|
||||
RawRootItem: []*model.MerkleTreeProofItem{
|
||||
{Floor: 4, Hash: "rawroot1", Left: false},
|
||||
},
|
||||
}
|
||||
result := model.ProofToProtobuf(proof)
|
||||
|
||||
require.NotNil(t, result)
|
||||
assert.Equal(t, "full-signature", result.GetSign())
|
||||
assert.Equal(t, "v1.0.0", result.GetVersion())
|
||||
assert.Len(t, result.GetColItems(), 1)
|
||||
assert.Len(t, result.GetRawItems(), 1)
|
||||
assert.Len(t, result.GetColRootItem(), 1)
|
||||
assert.Len(t, result.GetRawRootItem(), 1)
|
||||
}
|
||||
|
||||
func TestProofRoundTrip(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
original := &pb.Proof{
|
||||
Sign: "round-trip-signature",
|
||||
Version: "v1.0.0",
|
||||
ColItems: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 1, Hash: "col1", Left: true},
|
||||
{Floor: 2, Hash: "col2", Left: false},
|
||||
},
|
||||
RawItems: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 3, Hash: "raw1", Left: true},
|
||||
},
|
||||
ColRootItem: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 4, Hash: "colroot1", Left: false},
|
||||
},
|
||||
RawRootItem: []*pb.MerkleTreeProofItem{
|
||||
{Floor: 5, Hash: "rawroot1", Left: true},
|
||||
},
|
||||
}
|
||||
|
||||
// Convert to model
|
||||
modelProof := model.ProofFromProtobuf(original)
|
||||
require.NotNil(t, modelProof)
|
||||
|
||||
// Convert back to protobuf
|
||||
pbProof := model.ProofToProtobuf(modelProof)
|
||||
require.NotNil(t, pbProof)
|
||||
|
||||
// Verify round trip
|
||||
assert.Equal(t, original.GetSign(), pbProof.GetSign())
|
||||
assert.Equal(t, original.GetVersion(), pbProof.GetVersion())
|
||||
assert.Len(t, pbProof.GetColItems(), 2)
|
||||
assert.Len(t, pbProof.GetRawItems(), 1)
|
||||
assert.Len(t, pbProof.GetColRootItem(), 1)
|
||||
assert.Len(t, pbProof.GetRawRootItem(), 1)
|
||||
|
||||
assert.Equal(t, original.GetColItems()[0].GetFloor(), pbProof.GetColItems()[0].GetFloor())
|
||||
assert.Equal(t, original.GetColItems()[0].GetHash(), pbProof.GetColItems()[0].GetHash())
|
||||
assert.Equal(t, original.GetColItems()[0].GetLeft(), pbProof.GetColItems()[0].GetLeft())
|
||||
}
|
||||
Reference in New Issue
Block a user