Compare commits
2 Commits
1741134819
...
fdd3a2e7c7
Author | SHA1 | Date |
---|---|---|
|
fdd3a2e7c7 | |
|
c70cfad89c |
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"server": {
|
||||
"host": "0.0.0.0",
|
||||
"port": 27017
|
||||
},
|
||||
"storage": {
|
||||
"engine": "memory",
|
||||
"dataPath": "/var/lib/goaidb"
|
||||
},
|
||||
"log": {
|
||||
"appenders": {
|
||||
"console": {
|
||||
"type": "console",
|
||||
"formatter": "text",
|
||||
"options": {}
|
||||
}
|
||||
},
|
||||
"categories": {
|
||||
"default": {
|
||||
"appenders": ["console"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -45,6 +45,29 @@
|
|||
- 返回格式错误响应
|
||||
- 服务器不崩溃
|
||||
|
||||
## OP_UPDATE扩展测试用例
|
||||
|
||||
### TC001-UPDATE-004: $inc操作符验证
|
||||
**描述**: 验证数值递增操作符功能
|
||||
**前置条件**:
|
||||
- 文档存在且包含数值字段
|
||||
**步骤**:
|
||||
1. 构造包含$inc操作符的更新请求
|
||||
2. 发送请求并检查存储层处理
|
||||
3. 验证字段值是否正确递增
|
||||
**预期结果**:
|
||||
- 字段值正确递增
|
||||
- 返回成功状态码
|
||||
|
||||
### TC001-UPDATE-005: 权限不足处理
|
||||
**描述**: 验证无更新权限时的错误处理
|
||||
**步骤**:
|
||||
1. 使用无权限账户发送更新请求
|
||||
2. 检查服务器响应
|
||||
**预期结果**:
|
||||
- 返回权限拒绝错误
|
||||
- 服务器保持正常运行
|
||||
|
||||
## OP_DELETE测试用例
|
||||
|
||||
### TC001-DELETE-001: 单文档删除
|
||||
|
@ -72,6 +95,27 @@
|
|||
- 所有匹配文档被删除
|
||||
- 返回正确的删除计数
|
||||
|
||||
## OP_DELETE扩展测试用例
|
||||
|
||||
### TC001-DELETE-003: 删除不存在的文档
|
||||
**描述**: 验证删除不存在文档的处理
|
||||
**步骤**:
|
||||
1. 构造针对不存在文档的删除请求
|
||||
2. 发送请求
|
||||
3. 检查存储层调用情况
|
||||
**预期结果**:
|
||||
- DeleteFunc被调用
|
||||
- 返回删除计数0
|
||||
|
||||
### TC001-DELETE-004: 权限不足处理
|
||||
**描述**: 验证无删除权限时的错误处理
|
||||
**步骤**:
|
||||
1. 使用无权限账户发送删除请求
|
||||
2. 检查服务器响应
|
||||
**预期结果**:
|
||||
- 返回权限拒绝错误
|
||||
- 文档未被删除
|
||||
|
||||
## OP_COMMAND测试用例
|
||||
|
||||
### TC001-COMMAND-001: isMaster命令执行
|
||||
|
@ -96,6 +140,20 @@
|
|||
- CreateCollFunc被正确调用
|
||||
- 返回成功状态码
|
||||
|
||||
## OP_COMMAND扩展测试用例
|
||||
|
||||
### TC001-COMMAND-003: dropCollection命令
|
||||
**描述**: 验证集合删除命令
|
||||
**前置条件**:
|
||||
- 目标集合存在
|
||||
**步骤**:
|
||||
1. 构造dropCollection命令请求
|
||||
2. 发送请求
|
||||
3. 检查存储层DropCollFunc调用
|
||||
**预期结果**:
|
||||
- DropCollFunc被正确调用
|
||||
- 返回成功状态码
|
||||
|
||||
## OP_GET_MORE测试用例
|
||||
|
||||
### TC001-GETMORE-001: 基本分页获取
|
||||
|
@ -116,4 +174,15 @@
|
|||
1. 创建游标后延迟一段时间
|
||||
2. 尝试使用过期游标获取数据
|
||||
**预期结果**:
|
||||
- 返回游标不存在或已超时错误
|
||||
- 返回游标不存在或已超时错误
|
||||
|
||||
## OP_GET_MORE扩展测试用例
|
||||
|
||||
### TC001-GETMORE-003: 无效游标ID处理
|
||||
**描述**: 验证无效游标ID的错误处理
|
||||
**步骤**:
|
||||
1. 构造使用无效游标ID的GET_MORE请求
|
||||
2. 发送请求
|
||||
**预期结果**:
|
||||
- 返回游标不存在错误
|
||||
- 服务器保持正常运行
|
||||
|
|
24
go.mod
24
go.mod
|
@ -7,12 +7,26 @@ go 1.23
|
|||
// require github.com/mongodb/mongo-go-driver/v2 v2.0.0
|
||||
|
||||
require (
|
||||
git.pyer.club/kingecg/gologger v1.0.9 // indirect
|
||||
git.pyer.club/kingecg/gologger v1.0.9
|
||||
github.com/fsnotify/fsnotify v1.9.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
go.mongodb.org/mongo-driver v1.17.4
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/klauspost/compress v1.16.7 // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
golang.org/x/sys v0.13.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
|
||||
golang.org/x/crypto v0.26.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.23.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
57
go.sum
57
go.sum
|
@ -1,19 +1,64 @@
|
|||
git.pyer.club/kingecg/gologger v1.0.7 h1:sMrz+F806Whon6kzxVPYYMqB5frUvvJQEWa2zevRvX8=
|
||||
git.pyer.club/kingecg/gologger v1.0.7/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY=
|
||||
git.pyer.club/kingecg/gologger v1.0.8 h1:DaPDIsn0Jc+hF97+MRuG//W9zuXdPR7VTc+nPkXvym0=
|
||||
git.pyer.club/kingecg/gologger v1.0.8/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY=
|
||||
git.pyer.club/kingecg/gologger v1.0.9 h1:DWQBtbl0o0U3Kk0/vOdreUwv3IbFGSrFAcZWCDlHI8I=
|
||||
git.pyer.club/kingecg/gologger v1.0.9/go.mod h1:SNSl2jRHPzIpHSzdKOoVG798rtYMjPDPFyxUrEgivkY=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
|
||||
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
|
||||
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw=
|
||||
go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
|
|
|
@ -47,6 +47,7 @@ func (s *Server) handleConnection(conn net.Conn) {
|
|||
return
|
||||
}
|
||||
|
||||
log.WriteOpMsgToFile("opbin.log", uint8(2), buffer[:n])
|
||||
// 解析MongoDB协议消息
|
||||
message, err := protocol.ParseMessage(buffer[:n])
|
||||
if err != nil {
|
||||
|
|
|
@ -174,6 +174,20 @@ func parseBSONValue(elementType byte, data []byte, pos int) (interface{}, int, e
|
|||
}
|
||||
|
||||
return subDoc, docLength, nil
|
||||
case 0x04: // Array
|
||||
if len(data) < 4 {
|
||||
return nil, 0, fmt.Errorf("data too short for Array length")
|
||||
}
|
||||
// 读取数组长度
|
||||
arrayLength := int(binary.LittleEndian.Uint32(data[0:4]))
|
||||
if len(data) < arrayLength {
|
||||
return nil, 0, fmt.Errorf("data too short for Array")
|
||||
}
|
||||
subDoc, _, err := parseBSON(data[0:arrayLength])
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to parse array: %v", err)
|
||||
}
|
||||
return subDoc, arrayLength, nil
|
||||
|
||||
default:
|
||||
return nil, 0, fmt.Errorf("unsupported BSON element type: 0x%02X", elementType)
|
||||
|
@ -183,10 +197,10 @@ func parseBSONValue(elementType byte, data []byte, pos int) (interface{}, int, e
|
|||
// BsonMarshal 将map转换为BSON格式的字节流
|
||||
func BsonMarshal(doc map[string]interface{}) ([]byte, error) {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
|
||||
// 写入占位符长度(4字节)
|
||||
buf.Write(make([]byte, 4))
|
||||
|
||||
|
||||
// 遍历文档元素
|
||||
for key, value := range doc {
|
||||
// 写入元素类型和键名
|
||||
|
@ -194,24 +208,24 @@ func BsonMarshal(doc map[string]interface{}) ([]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
buf.WriteByte(elementType)
|
||||
buf.WriteString(key)
|
||||
buf.WriteByte(0x00) // 键名终止符
|
||||
|
||||
|
||||
// 写入值数据
|
||||
if err := writeBSONValue(buf, elementType, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 写入文档结束符
|
||||
buf.WriteByte(0x00)
|
||||
|
||||
|
||||
// 回填文档长度
|
||||
length := uint32(buf.Len())
|
||||
binary.LittleEndian.PutUint32(buf.Bytes(), length)
|
||||
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
|
@ -248,7 +262,7 @@ func writeBSONValue(buf *bytes.Buffer, elementType byte, value interface{}) erro
|
|||
b := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(b, uint32(v))
|
||||
buf.Write(b)
|
||||
|
||||
|
||||
case 0x12: // Int64
|
||||
v, ok := value.(int64)
|
||||
if !ok {
|
||||
|
@ -257,7 +271,7 @@ func writeBSONValue(buf *bytes.Buffer, elementType byte, value interface{}) erro
|
|||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, uint64(v))
|
||||
buf.Write(b)
|
||||
|
||||
|
||||
case 0x01: // Double
|
||||
v, ok := value.(float64)
|
||||
if !ok {
|
||||
|
@ -266,7 +280,7 @@ func writeBSONValue(buf *bytes.Buffer, elementType byte, value interface{}) erro
|
|||
b := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(b, math.Float64bits(v))
|
||||
buf.Write(b)
|
||||
|
||||
|
||||
case 0x02: // String
|
||||
v, ok := value.(string)
|
||||
if !ok {
|
||||
|
@ -281,7 +295,7 @@ func writeBSONValue(buf *bytes.Buffer, elementType byte, value interface{}) erro
|
|||
// 写入字符串内容和终止符
|
||||
buf.Write(strBytes)
|
||||
buf.WriteByte(0x00)
|
||||
|
||||
|
||||
case 0x08: // Boolean
|
||||
v, ok := value.(bool)
|
||||
if !ok {
|
||||
|
@ -292,10 +306,10 @@ func writeBSONValue(buf *bytes.Buffer, elementType byte, value interface{}) erro
|
|||
} else {
|
||||
buf.WriteByte(0x00)
|
||||
}
|
||||
|
||||
|
||||
case 0x0A: // Null
|
||||
// 不需要写入任何数据
|
||||
|
||||
|
||||
case 0x03: // EmbeddedDocument
|
||||
v, ok := value.(map[string]interface{})
|
||||
if !ok {
|
||||
|
@ -307,7 +321,7 @@ func writeBSONValue(buf *bytes.Buffer, elementType byte, value interface{}) erro
|
|||
}
|
||||
// 直接写入子文档数据(包含完整的长度信息)
|
||||
buf.Write(subDoc)
|
||||
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unsupported BSON element type: 0x%02X", elementType)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.mongodb.org/mongo-driver/mongo/readpref"
|
||||
)
|
||||
|
||||
func TestTC001_Connection_001_Connect(t *testing.T) {
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTC001_UPDATE_004_IncOperator(t *testing.T) {
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
coll := client.Database("testdb").Collection("testcoll")
|
||||
|
||||
// 初始化文档
|
||||
_, err = coll.InsertOne(context.TODO(), bson.D{{"name", "counter"}, {"value", 0}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 执行$inc更新
|
||||
update := bson.D{{"$inc", bson.D{{"value", 1}}}}
|
||||
result := coll.FindOneAndUpdate(context.TODO(), bson.D{{"name", "counter"}}, update)
|
||||
if result.Err() != nil {
|
||||
t.Fatal(result.Err())
|
||||
}
|
||||
|
||||
// 验证更新结果
|
||||
var updated bson.D
|
||||
if err := result.Decode(&updated); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value := updated.Map()["value"]
|
||||
if value.(int32) != 1 {
|
||||
t.Errorf("Expected value 1, got %v", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTC001_DELETE_003_DeleteNonExistent(t *testing.T) {
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
coll := client.Database("testdb").Collection("testcoll")
|
||||
|
||||
// 尝试删除不存在的文档
|
||||
res, err := coll.DeleteMany(context.TODO(), bson.D{{"name", "nonexistent"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 验证删除计数
|
||||
if res.DeletedCount != 0 {
|
||||
t.Errorf("Expected 0 deleted docs, got %d", res.DeletedCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTC001_COMMAND_003_DropCollection(t *testing.T) {
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db := client.Database("testdb")
|
||||
coll := db.Collection("testcoll")
|
||||
|
||||
// 创建集合
|
||||
if err := db.CreateCollection(context.TODO(), "testcoll"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 删除集合
|
||||
if err := coll.Drop(context.TODO()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 验证集合不存在
|
||||
collections, err := db.ListCollectionNames(context.TODO(), bson.D{{"name", "testcoll"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(collections) > 0 {
|
||||
t.Error("Collection was not dropped")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTC001_GETMORE_003_InvalidCursor(t *testing.T) {
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
coll := client.Database("testdb").Collection("testcoll")
|
||||
|
||||
// 插入测试数据
|
||||
for i := 0; i < 100; i++ {
|
||||
coll.InsertOne(context.TODO(), bson.D{{"i", i}})
|
||||
}
|
||||
|
||||
// 创建游标并获取ID
|
||||
cur, err := coll.Find(context.TODO(), bson.D{}, options.Find().SetLimit(10))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cursorID := cur.ID()
|
||||
cur.Close(context.TODO())
|
||||
|
||||
// 使用无效游标ID
|
||||
invalidCursor := cursorID + 1
|
||||
opt := options.Database().SetReadPreference(readpref.PrimaryPreferred())
|
||||
cmd := bson.D{
|
||||
primitive.E{Key: "getMore", Value: invalidCursor},
|
||||
primitive.E{Key: "collection", Value: "testcoll"},
|
||||
}
|
||||
err = coll.Database().Client().Database("testdb", opt).RunCommand(context.TODO(), cmd).Err()
|
||||
if err == nil {
|
||||
t.Error("Expected command error for invalid cursor")
|
||||
} else {
|
||||
_, isCmdErr := err.(mongo.CommandError)
|
||||
if !isCmdErr {
|
||||
t.Errorf("Expected command error but got %v", err)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,11 +5,12 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UpdateFlags are the flags for OP_UPDATE
|
||||
const (
|
||||
Upsert = 1 << iota
|
||||
Upsert = 1 << iota
|
||||
MultiUpdate // 标志位用于多文档更新
|
||||
)
|
||||
|
||||
|
@ -97,11 +98,11 @@ func parseUpdate(data []byte) (interface{}, error) {
|
|||
|
||||
return &UpdateMessage{
|
||||
Body: struct {
|
||||
Flags UpdateFlags
|
||||
Flags UpdateFlags
|
||||
DatabaseName string
|
||||
CollName string
|
||||
Query map[string]interface{}
|
||||
UpdateSpec map[string]interface{}
|
||||
CollName string
|
||||
Query map[string]interface{}
|
||||
UpdateSpec map[string]interface{}
|
||||
}{
|
||||
Flags: flags,
|
||||
DatabaseName: dbName,
|
||||
|
@ -190,29 +191,41 @@ func parseQuery(data []byte) (interface{}, error) {
|
|||
if dbEnd == -1 {
|
||||
return nil, fmt.Errorf("database name not null terminated")
|
||||
}
|
||||
dbName := string(data[4 : dbEnd+4])
|
||||
dbcolName := string(data[4 : dbEnd+4])
|
||||
dcnames := strings.Split(dbcolName, ".")
|
||||
dbName := dcnames[0]
|
||||
collName := dcnames[1]
|
||||
|
||||
// 剩余数据包含集合名和查询条件
|
||||
remaining := data[dbEnd+5:] // 跳过终止符
|
||||
|
||||
var numberToSkip, numberToReturn int32
|
||||
binary.Read(bytes.NewReader(remaining[0:4]), binary.LittleEndian, &numberToSkip)
|
||||
binary.Read(bytes.NewReader(remaining[4:8]), binary.LittleEndian, &numberToReturn)
|
||||
// 提取集合名
|
||||
collEnd := bytes.IndexByte(remaining, 0)
|
||||
if collEnd == -1 {
|
||||
return nil, fmt.Errorf("collection name not null terminated")
|
||||
}
|
||||
collName := string(remaining[:collEnd])
|
||||
// collEnd := bytes.IndexByte(remaining, 0)
|
||||
// if collEnd == -1 {
|
||||
// return nil, fmt.Errorf("collection name not null terminated")
|
||||
// }
|
||||
// if collEnd == 0 {
|
||||
// collEnd = bytes.IndexFunc(remaining, func(r rune) bool {
|
||||
// return r != 0
|
||||
// })
|
||||
// }
|
||||
// collName := string(remaining[:collEnd])
|
||||
|
||||
// 解析查询条件
|
||||
queryDoc, _, err := parseBSON(remaining[collEnd+1:])
|
||||
queryDoc, _, err := parseBSON(remaining[8:])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse query conditions: %v", err)
|
||||
}
|
||||
|
||||
return &QueryMessage{
|
||||
Flags: flags,
|
||||
DatabaseName: dbName,
|
||||
CollName: collName,
|
||||
Query: queryDoc,
|
||||
Flags: flags,
|
||||
DatabaseName: dbName,
|
||||
CollName: collName,
|
||||
NumberToSkip: numberToSkip,
|
||||
NumberToReturn: numberToReturn,
|
||||
Query: queryDoc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -265,10 +278,12 @@ func parseInsert(data []byte) (interface{}, error) {
|
|||
|
||||
// QueryMessage OP_QUERY消息体结构
|
||||
type QueryMessage struct {
|
||||
Flags uint32 // 查询标志
|
||||
DatabaseName string // 数据库名称
|
||||
CollName string // 集合名称
|
||||
Query map[string]interface{} // 查询条件
|
||||
Flags uint32 // 查询标志
|
||||
DatabaseName string // 数据库名称
|
||||
CollName string // 集合名称
|
||||
NumberToSkip int32
|
||||
NumberToReturn int32
|
||||
Query map[string]interface{} // 查询条件
|
||||
}
|
||||
|
||||
// InsertMessage OP_INSERT消息体结构
|
||||
|
@ -281,8 +296,8 @@ type InsertMessage struct {
|
|||
|
||||
// UpdateMessage represents an OP_UPDATE message
|
||||
type UpdateMessage struct {
|
||||
Header Header
|
||||
Body struct {
|
||||
Header Header
|
||||
Body struct {
|
||||
Flags UpdateFlags
|
||||
DatabaseName string
|
||||
CollName string
|
||||
|
|
|
@ -2,8 +2,22 @@ package protocol
|
|||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.pyer.club/kingecg/goaidb/log"
|
||||
)
|
||||
|
||||
func TestParseQuery(t *testing.T) {
|
||||
_, data, err := log.ReadOpMsgFromFile("/home/kingecg/code/goaidb/opbin.log")
|
||||
if err != nil {
|
||||
t.Fatalf("ReadOpMsgFromFile failed: %v", err)
|
||||
}
|
||||
msg, err := ParseMessage(data)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseMessage failed: %v", err)
|
||||
}
|
||||
log.Info(msg.Body)
|
||||
}
|
||||
|
||||
func TestParseUpdate(t *testing.T) {
|
||||
// 构造测试数据(最小有效Update消息)
|
||||
data := []byte{
|
||||
|
|
Loading…
Reference in New Issue