chore: 并入 iotlcm-upstream-chan 历史到 upstream-chan/(原提交 1e74497)

This commit is contained in:
MiFaZhan
2026-11-02 13:28:02 +08:00
192 changed files with 33119 additions and 0 deletions
@@ -0,0 +1,84 @@
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")
}
}
}