Files
MyGoNavi/third_party/highgo-pq/ssl_permissions_test.go
Syngnat de8fb60a30 feat(highgo-sm3): 增加瀚高SM3专用驱动并解耦PostgreSQL连接链路
- 引入 third_party/highgo-pq 作为 HighGo 专用驱动实现
- 调整驱动注册与连接入口,避免覆盖 postgres 驱动
- 保持 PG 数据源行为不变并补充接入文档
2026-02-10 17:42:28 +08:00

110 lines
1.9 KiB
Go

//go:build !windows
// +build !windows
package pq
import (
"os"
"syscall"
"testing"
"time"
)
type stat_t_wrapper struct {
stat syscall.Stat_t
}
func (stat_t *stat_t_wrapper) Name() string {
return "pem.key"
}
func (stat_t *stat_t_wrapper) Size() int64 {
return int64(100)
}
func (stat_t *stat_t_wrapper) Mode() os.FileMode {
return os.FileMode(stat_t.stat.Mode)
}
func (stat_t *stat_t_wrapper) ModTime() time.Time {
return time.Now()
}
func (stat_t *stat_t_wrapper) IsDir() bool {
return true
}
func (stat_t *stat_t_wrapper) Sys() interface{} {
return &stat_t.stat
}
func TestHasCorrectRootGroupPermissions(t *testing.T) {
currentUID := uint32(os.Getuid())
currentGID := uint32(os.Getgid())
testData := []struct {
expectedError error
stat syscall.Stat_t
}{
{
expectedError: nil,
stat: syscall.Stat_t{
Mode: 0600,
Uid: currentUID,
Gid: currentGID,
},
},
{
expectedError: nil,
stat: syscall.Stat_t{
Mode: 0640,
Uid: 0,
Gid: currentGID,
},
},
{
expectedError: errSSLKeyHasUnacceptableUserPermissions,
stat: syscall.Stat_t{
Mode: 0666,
Uid: currentUID,
Gid: currentGID,
},
},
{
expectedError: errSSLKeyHasUnacceptableRootPermissions,
stat: syscall.Stat_t{
Mode: 0666,
Uid: 0,
Gid: currentGID,
},
},
}
for _, test := range testData {
wrapper := &stat_t_wrapper{
stat: test.stat,
}
if test.expectedError != hasCorrectPermissions(wrapper) {
if test.expectedError == nil {
t.Errorf(
"file owned by %d:%d with %s should not have failed check with error \"%s\"",
test.stat.Uid,
test.stat.Gid,
wrapper.Mode(),
hasCorrectPermissions(wrapper),
)
continue
}
t.Errorf(
"file owned by %d:%d with %s, expected \"%s\", got \"%s\"",
test.stat.Uid,
test.stat.Gid,
wrapper.Mode(),
test.expectedError,
hasCorrectPermissions(wrapper),
)
}
}
}