96b1edff45
iotlcm-upstream-chan:运营商上游通道适配服务(统一 /sim/* 协议)。 - 内联原私有依赖:yunze-public → internal/pub/(10 包 18 文件), yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方 gogf/gf/contrib/registry/consul/v2(API 兼容) - Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-chan - 修复 mingqian 包测试:实现重构后 `adapter.operations` 字段已移除, 测试断言按当前实现语义重写(不改产品行为) - 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、 内网域名、真实手机号、注释里的真实 token/transid - 移除内部资产:CI 触发器、生产通道种子 SQL、真实凭据测试资源文档 - Dockerfile 改用公共基础镜像,移除写死的 registry 凭据 - 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
_ "git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/logic/channelconfig"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/logic/upstreamapi"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model/entity"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/service"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
)
|
|
|
|
type sharedTokenGateConfig struct {
|
|
service.IChannelConfig
|
|
config *model.SimChannelConfigItemCache
|
|
}
|
|
|
|
func (c *sharedTokenGateConfig) RefreshChannel(context.Context, string) error { return nil }
|
|
func (c *sharedTokenGateConfig) GetChannel(string) (*model.SimChannelConfigItemCache, bool) {
|
|
return c.config, true
|
|
}
|
|
|
|
type sharedTokenGateTemplate struct{ calls int }
|
|
|
|
func (f *sharedTokenGateTemplate) NewTemp(context.Context, *model.SimChannelConfigItemCache) model.SimCardChannelI {
|
|
f.calls++
|
|
return nil
|
|
}
|
|
|
|
func TestSharedTokenCannotCreateIneligibleChannel(t *testing.T) {
|
|
// The endpoint uses the real RefreshChannel implementation; no database or carrier calls.
|
|
oldAPI := service.UpstreamAPI()
|
|
t.Cleanup(func() { service.RegisterUpstreamAPI(oldAPI) })
|
|
oldConfig := service.ChannelConfig()
|
|
config := &sharedTokenGateConfig{}
|
|
service.RegisterChannelConfig(config)
|
|
t.Cleanup(func() { service.RegisterChannelConfig(oldConfig) })
|
|
api := upstreamapi.NewUpstreamAPI()
|
|
service.RegisterUpstreamAPI(api)
|
|
factory := &sharedTokenGateTemplate{}
|
|
api.RegisterTemp("gate_test", factory)
|
|
server := ghttp.GetServer(t.Name())
|
|
server.SetAddr("127.0.0.1:0")
|
|
bindHTTPRoutes(server)
|
|
if err := server.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = server.Shutdown() })
|
|
one, unexpected := 1, 2
|
|
for _, value := range []*int{nil, &one, &unexpected} {
|
|
config.config = &model.SimChannelConfigItemCache{
|
|
Channel: &entity.TSimChannels{ChanKey: "gate_channel", TempKey: "gate_test"},
|
|
Template: &entity.TSimChannelTemplate{TempKey: "gate_test"},
|
|
IsNormal: value,
|
|
}
|
|
resp, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d/internal/channel/shared-token?chanKey=gate_channel", server.GetListenedPort()), "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var body struct {
|
|
Code int `json:"code"`
|
|
}
|
|
err = json.NewDecoder(resp.Body).Decode(&body)
|
|
resp.Body.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body.Code != 50 {
|
|
t.Fatalf("unexpected response: %+v", body)
|
|
}
|
|
if factory.calls != 0 {
|
|
t.Fatal("shared-token endpoint bypassed the normal gate")
|
|
}
|
|
if _, ok := api.GetChan("gate_channel"); ok {
|
|
t.Fatal("shared-token endpoint cached an ineligible instance")
|
|
}
|
|
}
|
|
}
|