Compare commits
15 Commits
83d9098a98
...
playground
| Author | SHA1 | Date | |
|---|---|---|---|
| e80164f5be | |||
| ff4ddfacba | |||
| ffb3fc423e | |||
| b4474a160d | |||
| 6fbbfb698a | |||
| 42d018b854 | |||
| 2aa65de911 | |||
| 35de09aeb6 | |||
| 1178b974a1 | |||
| 156f07644d | |||
| d9709f61a5 | |||
| 59bc95ab21 | |||
| 4c7485f4ef | |||
| b0fce4380f | |||
| a92d1624c5 |
22
.github/codeql/codeql-config.yml
vendored
Normal file
22
.github/codeql/codeql-config.yml
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
name: "CodeQL Config"
|
||||
|
||||
# 指定要扫描的路径
|
||||
paths:
|
||||
- pkg
|
||||
- main
|
||||
|
||||
# 排除不需要扫描的路径
|
||||
paths-ignore:
|
||||
- '**/*_test.go'
|
||||
- 'html/**'
|
||||
- 'objs/**'
|
||||
- 'vendor/**'
|
||||
|
||||
# 使用的查询套件
|
||||
queries:
|
||||
- uses: security-extended
|
||||
- uses: security-and-quality
|
||||
|
||||
# 禁用默认查询(如果只想使用自定义查询)
|
||||
# disable-default-queries: true
|
||||
|
||||
82
.github/workflows/ci.yml
vendored
Normal file
82
.github/workflows/ci.yml
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
name: Build and Test
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.23'
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- name: Cache Go modules
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: ~/go/pkg/mod
|
||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-go-
|
||||
|
||||
- name: Cache Node modules
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: html/NextGB/node_modules
|
||||
key: ${{ runner.os }}-node-${{ hashFiles('html/NextGB/package-lock.json') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-node-
|
||||
|
||||
- name: Download Go dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Build Go application
|
||||
run: make build
|
||||
|
||||
- name: Run Go tests
|
||||
run: go test -v ./...
|
||||
|
||||
- name: Run Go tests with coverage
|
||||
run: go test ./pkg/... -coverprofile=coverage.out -covermode=atomic
|
||||
|
||||
- name: Display coverage report
|
||||
run: go tool cover -func=coverage.out
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v5
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
flags: unittests
|
||||
name: codecov-umbrella
|
||||
fail_ci_if_error: false
|
||||
|
||||
- name: Install Vue dependencies
|
||||
run: make vue-install
|
||||
|
||||
- name: Build Vue application
|
||||
run: make vue-build
|
||||
|
||||
- name: Upload build artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
if: success()
|
||||
with:
|
||||
name: srs-sip-build
|
||||
path: |
|
||||
objs/srs-sip
|
||||
html/NextGB/dist/
|
||||
retention-days: 7
|
||||
|
||||
52
.github/workflows/codeql.yml
vendored
Normal file
52
.github/workflows/codeql.yml
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
schedule:
|
||||
# 每周一凌晨2点运行
|
||||
- cron: '0 2 * * 1'
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze Go Code
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'go' ]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.23'
|
||||
cache: true
|
||||
|
||||
# 初始化 CodeQL
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v3
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
config-file: ./.github/codeql/codeql-config.yml
|
||||
|
||||
# 自动构建
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v3
|
||||
|
||||
# 执行 CodeQL 分析
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v3
|
||||
with:
|
||||
category: "/language:${{matrix.language}}"
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@ -23,3 +23,5 @@
|
||||
hs_err_pid*
|
||||
objs
|
||||
.idea
|
||||
|
||||
run
|
||||
|
||||
28
Dockerfile
28
Dockerfile
@ -1,16 +1,29 @@
|
||||
# 引入SRS
|
||||
FROM ossrs/srs:v6.0.155 AS srs
|
||||
FROM ossrs/srs:v6.0.184 AS srs
|
||||
|
||||
# 前端构建阶段
|
||||
FROM node:20-slim AS frontend-builder
|
||||
ARG HTTP_PROXY=
|
||||
ARG NO_PROXY=
|
||||
ENV http_proxy=${HTTP_PROXY} \
|
||||
https_proxy=${HTTP_PROXY} \
|
||||
no_proxy=${NO_PROXY}
|
||||
WORKDIR /app/frontend
|
||||
COPY html/NextGB/package*.json ./
|
||||
# RUN npm config set registry http://mirrors.cloud.tencent.com/npm/ \
|
||||
# && npm install
|
||||
RUN npm install
|
||||
COPY html/NextGB/ .
|
||||
RUN npm run build
|
||||
|
||||
# 后端构建阶段
|
||||
FROM golang:1.23 AS backend-builder
|
||||
ARG HTTP_PROXY=
|
||||
ARG NO_PROXY=
|
||||
ENV http_proxy=${HTTP_PROXY} \
|
||||
https_proxy=${HTTP_PROXY} \
|
||||
no_proxy=${NO_PROXY} \
|
||||
GOPROXY=https://goproxy.cn,direct
|
||||
WORKDIR /app
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
@ -19,11 +32,20 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o /app/srs-sip main/main.go
|
||||
|
||||
# 最终运行阶段
|
||||
FROM ubuntu:22.04
|
||||
ARG HTTP_PROXY=
|
||||
ARG NO_PROXY=
|
||||
ENV http_proxy=${HTTP_PROXY} \
|
||||
https_proxy=${HTTP_PROXY} \
|
||||
no_proxy=${NO_PROXY}
|
||||
WORKDIR /usr/local
|
||||
|
||||
# 设置时区
|
||||
ENV TZ=Asia/Shanghai
|
||||
RUN apt-get update && \
|
||||
RUN sed -i \
|
||||
-e 's@http://archive.ubuntu.com/ubuntu/@http://mirrors.ustc.edu.cn/ubuntu/@g' \
|
||||
-e 's@http://security.ubuntu.com/ubuntu/@http://mirrors.ustc.edu.cn/ubuntu/@g' \
|
||||
/etc/apt/sources.list && \
|
||||
apt-get update && \
|
||||
apt-get install -y ca-certificates tzdata supervisor && \
|
||||
ln -fs /usr/share/zoneinfo/$TZ /etc/localtime && \
|
||||
dpkg-reconfigure -f noninteractive tzdata && \
|
||||
@ -60,7 +82,7 @@ stderr_logfile=/dev/stderr\n\
|
||||
stderr_logfile_maxbytes=0\n\
|
||||
\n\
|
||||
[program:srs-sip]\n\
|
||||
command=/usr/local/srs-sip/srs-sip\n\
|
||||
command=/usr/local/srs-sip/srs-sip -c /usr/local/srs-sip/config.yaml\n\
|
||||
directory=/usr/local/srs-sip\n\
|
||||
autostart=true\n\
|
||||
autorestart=true\n\
|
||||
|
||||
29
README.md
29
README.md
@ -1,5 +1,11 @@
|
||||
# SRS-SIP
|
||||
|
||||
[](https://github.com/ossrs/srs-sip/actions/workflows/ci.yml)
|
||||
[](https://github.com/ossrs/srs-sip/actions/workflows/codeql.yml)
|
||||
[](https://codecov.io/gh/ossrs/srs-sip)
|
||||
[](https://goreportcard.com/report/github.com/ossrs/srs-sip)
|
||||
[](https://github.com/ossrs/srs-sip/blob/main/LICENSE)
|
||||
|
||||
## Usage
|
||||
|
||||
Pre-requisites:
|
||||
@ -24,6 +30,29 @@ Run the program:
|
||||
./objs/srs-sip -c conf/config.yaml
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
Run all tests:
|
||||
|
||||
```bash
|
||||
go test -v ./pkg/...
|
||||
```
|
||||
|
||||
Run tests with coverage:
|
||||
|
||||
```bash
|
||||
go test ./pkg/... -coverprofile=coverage.out
|
||||
go tool cover -func=coverage.out
|
||||
```
|
||||
|
||||
For more details, see [Testing Guide](docs/TESTING.md).
|
||||
|
||||
## Security
|
||||
|
||||
This project uses CodeQL for automated security scanning. For more information about security practices and how to report vulnerabilities, see [Security Guide](docs/SECURITY.md).
|
||||
|
||||
## Docker
|
||||
|
||||
Use docker
|
||||
```
|
||||
docker run -id -p 1985:1985 -p 5060:5060 -p 8025:8025 -p 9000:9000 -p 5060:5060/udp -p 8000:8000/udp --name srs-sip --env CANDIDATE=your_ip ossrs/srs-sip:alpha
|
||||
|
||||
78
README_cross.md
Normal file
78
README_cross.md
Normal file
@ -0,0 +1,78 @@
|
||||
# note to me
|
||||
|
||||
```bash
|
||||
docker compose build --network host
|
||||
```
|
||||
|
||||
```
|
||||
docker compose up -d --force-recreate
|
||||
```
|
||||
|
||||
# TODO
|
||||
|
||||
- [ ] let user choose whether use mirror (use which mirror) when building Dockerfile
|
||||
|
||||
---
|
||||
|
||||
Based on the logs and the **GB/T 28181-2022** standard you provided, here is the explanation:
|
||||
|
||||
Yes, this is **SIP**, but the content *inside* the SIP message is **SDP (Session Description Protocol)**.
|
||||
|
||||
While XML is used for *control* (like PTZ), **SDP** is used for **Media Negotiation** (setting up the video stream).
|
||||
|
||||
### The Architecture from your logs
|
||||
1. **SIP (The Envelope):** Starts the conversation ("I want to watch video").
|
||||
2. **SDP (The Letter inside):** Describes technical details ("Send video to IP X, Port Y, using Format Z").
|
||||
3. **RTP (The result):** After this SIP/SDP handshake finishes, the actual binary video stream (PS/H.264) starts flowing over a separate TCP/UDP connection.
|
||||
|
||||
### Breakdown of your Log
|
||||
This log shows a **Real-time Live View** handshake.
|
||||
|
||||
#### 1. The Request (SRS Server -> Camera)
|
||||
The Server asks the Camera to send video.
|
||||
|
||||
```ini
|
||||
INVITE sip:34020000001320000001@3402000000 SIP/2.0
|
||||
Content-Type: application/sdp
|
||||
|
||||
s=Play # "Play" = Real-time Live View (Standard 9.2.2.1)
|
||||
c=IN IP4 192.168.2.184 # The Media Server IP
|
||||
m=video 9000 TCP/RTP/AVP 96 # "Send video to my Port 9000 via TCP"
|
||||
a=recvonly # "I will only receive, not send"
|
||||
y=0911024252 # **GB/T 28181 Special**: The SSRC (Stream ID)
|
||||
```
|
||||
|
||||
#### 2. The Response (Camera -> SRS Server)
|
||||
The Camera agrees and tells the server its own details.
|
||||
|
||||
```ini
|
||||
SIP/2.0 200 OK
|
||||
Content-Type: application/sdp
|
||||
|
||||
c=IN IP4 192.168.2.64 # The Camera IP
|
||||
m=video 15060 TCP/RTP/AVP 96 # "I am sending from Port 15060"
|
||||
a=sendonly # "I will only send"
|
||||
a=setup:active # "I will initiate the TCP connection to you"
|
||||
y=0911024252 # Matches the SSRC provided
|
||||
f=v/2/2560x1440/25/2/8192a/... # **GB/T 28181 Special**: Media Info
|
||||
```
|
||||
|
||||
### Key Differences from Standard SDP
|
||||
GB/T 28181 modifies standard SDP with two specific fields mandatory for this protocol:
|
||||
|
||||
1. **`y=` (SSRC)**:
|
||||
* **Standard SDP:** Does not have a `y` line.
|
||||
* **GB/T 28181:** Uses `y` to define the **SSRC** (Synchronization Source). This 10-digit number is crucial because it marks every binary video packet sent later. If the binary stream headers don't match this `y` value, the stream is rejected.
|
||||
|
||||
2. **`f=` (Media Info)**:
|
||||
* **Standard SDP:** Does not have an `f` line.
|
||||
* **GB/T 28181:** Uses `f` to describe video parameters. In your log: `v/2/2560x1440/25...` means:
|
||||
* `v`: Video
|
||||
* `2`: Coding format (likely H.264 or H.265 mapped)
|
||||
* `2560x1440`: Resolution
|
||||
* `25`: Frame rate
|
||||
|
||||
### Summary of Cooperation
|
||||
1. **XML (SIP MESSAGE):** Used for "remote control" (PTZ, Query, Keepalive).
|
||||
2. **SDP (SIP INVITE):** Used to *negotiate* the pipeline.
|
||||
3. **Binary (RTP/PS):** The actual heavy video data that flows through the pipe created by the SDP.
|
||||
@ -3,6 +3,11 @@ max_connections 1000;
|
||||
# For docker, please use docker logs to manage the logs of SRS.
|
||||
# See https://docs.docker.com/config/containers/logging/
|
||||
srs_log_tank console;
|
||||
|
||||
# srs_log_tank file;
|
||||
# srs_log_file /var/log/srs/srs.log;
|
||||
# ff_log_dir /var/log/srs;
|
||||
|
||||
daemon off;
|
||||
disable_daemon_for_docker off;
|
||||
http_api {
|
||||
|
||||
@ -277,8 +277,8 @@ SRS-SIP 是一个基于 GB28181 协议的视频监控系统,提供设备管理
|
||||
- `down`: 向下
|
||||
- `left`: 向左
|
||||
- `right`: 向右
|
||||
- `zoom_in`: 放大
|
||||
- `zoom_out`: 缩小
|
||||
- `zoomin`: 放大
|
||||
- `zoomout`: 缩小
|
||||
- `stop`: 停止
|
||||
- `speed`: 控制速度(1-9)
|
||||
|
||||
|
||||
4535
doc/GBT+28181-2022.md
Normal file
4535
doc/GBT+28181-2022.md
Normal file
File diff suppressed because it is too large
Load Diff
33
docker-compose.yml
Normal file
33
docker-compose.yml
Normal file
@ -0,0 +1,33 @@
|
||||
services:
|
||||
srs-sip:
|
||||
build:
|
||||
context: .
|
||||
network: host
|
||||
args:
|
||||
HTTP_PROXY: ${HTTP_PROXY:-}
|
||||
NO_PROXY: "localhost,127.0.0.1,::1"
|
||||
environment:
|
||||
# CANDIDATE: ${CANDIDATE:-}
|
||||
CANDIDATE: 192.168.2.184
|
||||
TZ: "Asia/Shanghai"
|
||||
volumes:
|
||||
- ./run/conf/config.yaml:/usr/local/srs-sip/config.yaml:ro
|
||||
- ./run/logs:/usr/local/srs-sip/logs
|
||||
- ./run/srs/conf/srs.conf:/usr/local/srs/conf/srs.conf:ro
|
||||
# use docker logs
|
||||
- ./run/srs/logs:/var/log/srs/
|
||||
# for recording
|
||||
- ./run/data:/data
|
||||
ports:
|
||||
# SRS RTMP
|
||||
- "1985:1985"
|
||||
# SRS media ingest (GB28181 RTP/PS 等,取决于你的配置)
|
||||
- "9000:9000"
|
||||
# SRS WebRTC
|
||||
- "8000:8000/udp"
|
||||
# SIP
|
||||
- "5060:5060"
|
||||
- "5060:5060/udp"
|
||||
# WebUI
|
||||
- "8025:8025"
|
||||
restart: unless-stopped
|
||||
@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from 'vue'
|
||||
import { ArrowRight, VideoCamera } from '@element-plus/icons-vue'
|
||||
import { ArrowRight, VideoCamera, ZoomIn, ZoomOut } from '@element-plus/icons-vue'
|
||||
import {
|
||||
ArrowUp,
|
||||
ArrowDown,
|
||||
@ -158,6 +158,27 @@ const isDisabled = computed(() => !props.activeWindow)
|
||||
<div class="direction-center"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="zoom-controls">
|
||||
<el-button
|
||||
class="zoom-btn"
|
||||
:disabled="isDisabled"
|
||||
@mousedown="handlePtzStart('zoomin')"
|
||||
@mouseup="handlePtzStop"
|
||||
@mouseleave="handlePtzStop"
|
||||
>
|
||||
<el-icon><ZoomIn /></el-icon>
|
||||
</el-button>
|
||||
<div class="zoom-label">变倍</div>
|
||||
<el-button
|
||||
class="zoom-btn"
|
||||
:disabled="isDisabled"
|
||||
@mousedown="handlePtzStart('zoomout')"
|
||||
@mouseup="handlePtzStop"
|
||||
@mouseleave="handlePtzStop"
|
||||
>
|
||||
<el-icon><ZoomOut /></el-icon>
|
||||
</el-button>
|
||||
</div>
|
||||
<div class="speed-control">
|
||||
<div class="speed-value">{{ speed }}</div>
|
||||
<el-slider
|
||||
@ -346,6 +367,48 @@ const isDisabled = computed(() => !props.activeWindow)
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.zoom-controls {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
height: 120px;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.zoom-btn {
|
||||
--el-button-bg-color: var(--el-color-primary-light-8);
|
||||
--el-button-border-color: var(--el-color-primary-light-5);
|
||||
--el-button-hover-bg-color: var(--el-color-primary-light-7);
|
||||
--el-button-hover-border-color: var(--el-color-primary-light-4);
|
||||
--el-button-active-bg-color: var(--el-color-primary-light-5);
|
||||
--el-button-active-border-color: var(--el-color-primary);
|
||||
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
border-radius: 4px;
|
||||
|
||||
.el-icon {
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
transform: scale(1.05);
|
||||
}
|
||||
|
||||
&:active {
|
||||
transform: scale(0.95);
|
||||
}
|
||||
}
|
||||
|
||||
.zoom-label {
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-secondary);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.control-groups,
|
||||
.control-group {
|
||||
display: none;
|
||||
|
||||
38
main/main.go
38
main/main.go
@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
@ -95,9 +96,42 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// 首先检查原始路径是否包含 ".." 以防止路径遍历攻击
|
||||
if strings.Contains(r.URL.Path, "..") {
|
||||
slog.Warn("potential path traversal attempt detected", "path", r.URL.Path)
|
||||
http.Error(w, "Invalid path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 清理路径
|
||||
cleanPath := path.Clean(r.URL.Path)
|
||||
|
||||
// 检查请求的文件是否存在
|
||||
filePath := path.Join(conf.Http.Dir, r.URL.Path)
|
||||
_, err := os.Stat(filePath)
|
||||
filePath := filepath.Join(conf.Http.Dir, cleanPath)
|
||||
|
||||
// 确保最终路径在允许的目录内
|
||||
absDir, err := filepath.Abs(conf.Http.Dir)
|
||||
if err != nil {
|
||||
slog.Error("failed to get absolute path of http dir", "error", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
absFilePath, err := filepath.Abs(filePath)
|
||||
if err != nil {
|
||||
slog.Error("failed to get absolute path of file", "error", err)
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 验证文件路径在允许的目录内
|
||||
if !strings.HasPrefix(absFilePath, absDir) {
|
||||
slog.Warn("path traversal attempt blocked", "requested", r.URL.Path, "resolved", absFilePath)
|
||||
http.Error(w, "Access denied", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = os.Stat(absFilePath)
|
||||
if os.IsNotExist(err) {
|
||||
// 如果文件不存在,返回 index.html
|
||||
slog.Info("file not found, redirect to index", "path", r.URL.Path)
|
||||
|
||||
247
main/main_test.go
Normal file
247
main/main_test.go
Normal file
@ -0,0 +1,247 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestPathTraversalPrevention 测试路径遍历防护
|
||||
func TestPathTraversalPrevention(t *testing.T) {
|
||||
baseDir := "/var/www/html"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
inputPath string
|
||||
shouldFail bool
|
||||
reason string
|
||||
}{
|
||||
{
|
||||
name: "Normal file",
|
||||
inputPath: "/index.html",
|
||||
shouldFail: false,
|
||||
reason: "Normal file access should be allowed",
|
||||
},
|
||||
{
|
||||
name: "Subdirectory file",
|
||||
inputPath: "/css/style.css",
|
||||
shouldFail: false,
|
||||
reason: "Subdirectory access should be allowed",
|
||||
},
|
||||
{
|
||||
name: "Deep subdirectory",
|
||||
inputPath: "/js/lib/jquery.min.js",
|
||||
shouldFail: false,
|
||||
reason: "Deep subdirectory access should be allowed",
|
||||
},
|
||||
{
|
||||
name: "Parent directory traversal",
|
||||
inputPath: "/../etc/passwd",
|
||||
shouldFail: true,
|
||||
reason: "Parent directory traversal should be blocked",
|
||||
},
|
||||
{
|
||||
name: "Double parent traversal",
|
||||
inputPath: "/../../etc/passwd",
|
||||
shouldFail: true,
|
||||
reason: "Double parent traversal should be blocked",
|
||||
},
|
||||
{
|
||||
name: "Multiple parent traversal",
|
||||
inputPath: "/../../../etc/passwd",
|
||||
shouldFail: true,
|
||||
reason: "Multiple parent traversal should be blocked",
|
||||
},
|
||||
{
|
||||
name: "Mixed path with parent",
|
||||
inputPath: "/css/../../etc/passwd",
|
||||
shouldFail: true,
|
||||
reason: "Mixed path with parent should be blocked",
|
||||
},
|
||||
{
|
||||
name: "Dot slash path",
|
||||
inputPath: "/./index.html",
|
||||
shouldFail: false,
|
||||
reason: "Dot slash should be cleaned but allowed",
|
||||
},
|
||||
{
|
||||
name: "Complex traversal",
|
||||
inputPath: "/css/../js/../../../etc/passwd",
|
||||
shouldFail: true,
|
||||
reason: "Complex traversal should be blocked",
|
||||
},
|
||||
{
|
||||
name: "Root path",
|
||||
inputPath: "/",
|
||||
shouldFail: false,
|
||||
reason: "Root path should be allowed",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 模拟修复后的路径验证逻辑
|
||||
// 首先检查原始路径是否包含 ".."
|
||||
containsDoubleDotInOriginal := strings.Contains(tt.inputPath, "..")
|
||||
|
||||
// 如果原始路径包含 "..",直接阻止
|
||||
if containsDoubleDotInOriginal {
|
||||
if !tt.shouldFail {
|
||||
t.Errorf("%s: Path contains '..' but should be allowed: %s", tt.reason, tt.inputPath)
|
||||
}
|
||||
t.Logf("Input: %s, Contains '..': true, Blocked: true (early check)", tt.inputPath)
|
||||
return
|
||||
}
|
||||
|
||||
// 清理路径
|
||||
cleanPath := path.Clean(tt.inputPath)
|
||||
|
||||
// 构建文件路径
|
||||
filePath := filepath.Join(baseDir, cleanPath)
|
||||
|
||||
// 获取绝对路径
|
||||
absDir, err := filepath.Abs(baseDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get absolute path of base dir: %v", err)
|
||||
}
|
||||
|
||||
absFilePath, err := filepath.Abs(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get absolute path of file: %v", err)
|
||||
}
|
||||
|
||||
// 验证路径是否在允许的目录内
|
||||
isOutsideBaseDir := !strings.HasPrefix(absFilePath, absDir)
|
||||
|
||||
// 判断是否应该被阻止
|
||||
shouldBlock := isOutsideBaseDir
|
||||
|
||||
if tt.shouldFail && !shouldBlock {
|
||||
t.Errorf("%s: Expected path to be blocked, but it was allowed. Path: %s, Clean: %s, Abs: %s",
|
||||
tt.reason, tt.inputPath, cleanPath, absFilePath)
|
||||
}
|
||||
|
||||
if !tt.shouldFail && shouldBlock {
|
||||
t.Errorf("%s: Expected path to be allowed, but it was blocked. Path: %s, Clean: %s, Abs: %s",
|
||||
tt.reason, tt.inputPath, cleanPath, absFilePath)
|
||||
}
|
||||
|
||||
// 额外的日志信息用于调试
|
||||
t.Logf("Input: %s, Clean: %s, Outside base: %v, Blocked: %v",
|
||||
tt.inputPath, cleanPath, isOutsideBaseDir, shouldBlock)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestPathCleanBehavior 测试 path.Clean 的行为
|
||||
func TestPathCleanBehavior(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"/index.html", "/index.html"},
|
||||
{"/../etc/passwd", "/etc/passwd"},
|
||||
{"/./index.html", "/index.html"},
|
||||
{"/css/../index.html", "/index.html"},
|
||||
{"//double//slash", "/double/slash"},
|
||||
{"/trailing/slash/", "/trailing/slash"},
|
||||
{"/./././index.html", "/index.html"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
result := path.Clean(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("path.Clean(%q) = %q, expected %q", tt.input, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAbsolutePathValidation 测试绝对路径验证
|
||||
func TestAbsolutePathValidation(t *testing.T) {
|
||||
// 使用临时目录进行测试
|
||||
baseDir := t.TempDir()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
shouldFail bool
|
||||
}{
|
||||
{
|
||||
name: "File in base directory",
|
||||
path: "index.html",
|
||||
shouldFail: false,
|
||||
},
|
||||
{
|
||||
name: "File in subdirectory",
|
||||
path: "css/style.css",
|
||||
shouldFail: false,
|
||||
},
|
||||
{
|
||||
name: "Attempt to escape with parent",
|
||||
path: "../outside.txt",
|
||||
shouldFail: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cleanPath := path.Clean(tt.path)
|
||||
filePath := filepath.Join(baseDir, cleanPath)
|
||||
|
||||
absDir, err := filepath.Abs(baseDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get absolute path: %v", err)
|
||||
}
|
||||
|
||||
absFilePath, err := filepath.Abs(filePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get absolute file path: %v", err)
|
||||
}
|
||||
|
||||
isOutside := !strings.HasPrefix(absFilePath, absDir)
|
||||
|
||||
if tt.shouldFail && !isOutside {
|
||||
t.Errorf("Expected path to be outside base dir, but it wasn't: %s", absFilePath)
|
||||
}
|
||||
|
||||
if !tt.shouldFail && isOutside {
|
||||
t.Errorf("Expected path to be inside base dir, but it wasn't: %s", absFilePath)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkPathValidation 性能基准测试
|
||||
func BenchmarkPathValidation(b *testing.B) {
|
||||
baseDir := "/var/www/html"
|
||||
testPath := "/css/style.css"
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
cleanPath := path.Clean(testPath)
|
||||
_ = strings.Contains(cleanPath, "..")
|
||||
filePath := filepath.Join(baseDir, cleanPath)
|
||||
absDir, _ := filepath.Abs(baseDir)
|
||||
absFilePath, _ := filepath.Abs(filePath)
|
||||
_ = strings.HasPrefix(absFilePath, absDir)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkPathValidationMalicious 恶意路径的性能测试
|
||||
func BenchmarkPathValidationMalicious(b *testing.B) {
|
||||
baseDir := "/var/www/html"
|
||||
testPath := "/../../../etc/passwd"
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
cleanPath := path.Clean(testPath)
|
||||
_ = strings.Contains(cleanPath, "..")
|
||||
filePath := filepath.Join(baseDir, cleanPath)
|
||||
absDir, _ := filepath.Abs(baseDir)
|
||||
absFilePath, _ := filepath.Abs(filePath)
|
||||
_ = strings.HasPrefix(absFilePath, absDir)
|
||||
}
|
||||
}
|
||||
@ -95,7 +95,7 @@ func GetLocalIP() (string, error) {
|
||||
}
|
||||
var candidates []Iface
|
||||
for _, ifc := range ifaces {
|
||||
if ifc.Flags&net.FlagUp == 0 || ifc.Flags&net.FlagUp == 0 {
|
||||
if ifc.Flags&net.FlagUp == 0 {
|
||||
continue
|
||||
}
|
||||
if ifc.Flags&(net.FlagPointToPoint|net.FlagLoopback) != 0 {
|
||||
|
||||
154
pkg/config/config_test.go
Normal file
154
pkg/config/config_test.go
Normal file
@ -0,0 +1,154 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefaultConfig(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
// 测试 Common 配置
|
||||
if cfg.Common.LogLevel != "info" {
|
||||
t.Errorf("Expected log level 'info', got '%s'", cfg.Common.LogLevel)
|
||||
}
|
||||
if cfg.Common.LogFile != "app.log" {
|
||||
t.Errorf("Expected log file 'app.log', got '%s'", cfg.Common.LogFile)
|
||||
}
|
||||
|
||||
// 测试 GB28181 配置
|
||||
if cfg.GB28181.Serial != "34020000002000000001" {
|
||||
t.Errorf("Expected serial '34020000002000000001', got '%s'", cfg.GB28181.Serial)
|
||||
}
|
||||
if cfg.GB28181.Realm != "3402000000" {
|
||||
t.Errorf("Expected realm '3402000000', got '%s'", cfg.GB28181.Realm)
|
||||
}
|
||||
if cfg.GB28181.Host != "0.0.0.0" {
|
||||
t.Errorf("Expected host '0.0.0.0', got '%s'", cfg.GB28181.Host)
|
||||
}
|
||||
if cfg.GB28181.Port != 5060 {
|
||||
t.Errorf("Expected port 5060, got %d", cfg.GB28181.Port)
|
||||
}
|
||||
if cfg.GB28181.Auth.Enable != false {
|
||||
t.Errorf("Expected auth enable false, got %v", cfg.GB28181.Auth.Enable)
|
||||
}
|
||||
if cfg.GB28181.Auth.Password != "123456" {
|
||||
t.Errorf("Expected auth password '123456', got '%s'", cfg.GB28181.Auth.Password)
|
||||
}
|
||||
|
||||
// 测试 HTTP 配置
|
||||
if cfg.Http.Port != 8025 {
|
||||
t.Errorf("Expected http port 8025, got %d", cfg.Http.Port)
|
||||
}
|
||||
if cfg.Http.Dir != "./html" {
|
||||
t.Errorf("Expected http dir './html', got '%s'", cfg.Http.Dir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigNonExistent(t *testing.T) {
|
||||
// 测试加载不存在的配置文件,应该返回默认配置
|
||||
cfg, err := LoadConfig("non_existent_config.yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error for non-existent config, got: %v", err)
|
||||
}
|
||||
|
||||
// 应该返回默认配置
|
||||
defaultCfg := DefaultConfig()
|
||||
if cfg.Common.LogLevel != defaultCfg.Common.LogLevel {
|
||||
t.Errorf("Expected default log level, got '%s'", cfg.Common.LogLevel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigValid(t *testing.T) {
|
||||
// 创建临时配置文件
|
||||
tempFile := "test_config.yaml"
|
||||
defer os.Remove(tempFile)
|
||||
|
||||
configContent := `common:
|
||||
log-level: debug
|
||||
log-file: test.log
|
||||
gb28181:
|
||||
serial: "12345678901234567890"
|
||||
realm: "1234567890"
|
||||
host: "127.0.0.1"
|
||||
port: 5061
|
||||
auth:
|
||||
enable: true
|
||||
password: "test123"
|
||||
http:
|
||||
listen: 9000
|
||||
dir: "./test_html"
|
||||
`
|
||||
|
||||
err := os.WriteFile(tempFile, []byte(configContent), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test config file: %v", err)
|
||||
}
|
||||
|
||||
// 加载配置
|
||||
cfg, err := LoadConfig(tempFile)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
// 验证配置
|
||||
if cfg.Common.LogLevel != "debug" {
|
||||
t.Errorf("Expected log level 'debug', got '%s'", cfg.Common.LogLevel)
|
||||
}
|
||||
if cfg.Common.LogFile != "test.log" {
|
||||
t.Errorf("Expected log file 'test.log', got '%s'", cfg.Common.LogFile)
|
||||
}
|
||||
if cfg.GB28181.Serial != "12345678901234567890" {
|
||||
t.Errorf("Expected serial '12345678901234567890', got '%s'", cfg.GB28181.Serial)
|
||||
}
|
||||
if cfg.GB28181.Port != 5061 {
|
||||
t.Errorf("Expected port 5061, got %d", cfg.GB28181.Port)
|
||||
}
|
||||
if cfg.GB28181.Auth.Enable != true {
|
||||
t.Errorf("Expected auth enable true, got %v", cfg.GB28181.Auth.Enable)
|
||||
}
|
||||
if cfg.Http.Port != 9000 {
|
||||
t.Errorf("Expected http port 9000, got %d", cfg.Http.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigInvalid(t *testing.T) {
|
||||
// 创建无效的配置文件
|
||||
tempFile := "test_invalid_config.yaml"
|
||||
defer os.Remove(tempFile)
|
||||
|
||||
invalidContent := `invalid yaml content: [[[`
|
||||
|
||||
err := os.WriteFile(tempFile, []byte(invalidContent), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test config file: %v", err)
|
||||
}
|
||||
|
||||
// 加载配置应该失败
|
||||
_, err = LoadConfig(tempFile)
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid config file, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetLocalIP(t *testing.T) {
|
||||
ip, err := GetLocalIP()
|
||||
|
||||
// 在某些环境下可能没有网络接口,所以允许返回错误
|
||||
if err != nil {
|
||||
t.Logf("GetLocalIP returned error (may be expected in some environments): %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 如果成功,验证返回的是有效的 IP 地址
|
||||
if ip == "" {
|
||||
t.Error("Expected non-empty IP address")
|
||||
}
|
||||
|
||||
// 简单验证 IP 格式(应该包含点号)
|
||||
if len(ip) < 7 { // 最短的 IP 是 0.0.0.0
|
||||
t.Errorf("IP address seems invalid: %s", ip)
|
||||
}
|
||||
|
||||
t.Logf("Local IP: %s", ip)
|
||||
}
|
||||
@ -105,3 +105,212 @@ func TestAddMediaServerDuplicates(t *testing.T) {
|
||||
t.Log("Test confirmed: Old AddMediaServer method creates duplicates")
|
||||
}
|
||||
|
||||
func TestGetMediaServer(t *testing.T) {
|
||||
dbPath := "./test_get_media_server.db"
|
||||
defer os.Remove(dbPath)
|
||||
|
||||
db, err := NewMediaServerDB(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// 添加一个媒体服务器
|
||||
err = db.AddMediaServer("TestServer", "ZLM", "192.168.1.200", 8080, "admin", "pass123", "secret123", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add media server: %v", err)
|
||||
}
|
||||
|
||||
// 获取服务器列表以获得ID
|
||||
servers, err := db.ListMediaServers()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to list media servers: %v", err)
|
||||
}
|
||||
if len(servers) == 0 {
|
||||
t.Fatal("No servers found")
|
||||
}
|
||||
|
||||
// 通过ID获取服务器
|
||||
server, err := db.GetMediaServer(servers[0].ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get media server: %v", err)
|
||||
}
|
||||
|
||||
// 验证数据
|
||||
if server.Name != "TestServer" {
|
||||
t.Errorf("Expected name 'TestServer', got '%s'", server.Name)
|
||||
}
|
||||
if server.Type != "ZLM" {
|
||||
t.Errorf("Expected type 'ZLM', got '%s'", server.Type)
|
||||
}
|
||||
if server.IP != "192.168.1.200" {
|
||||
t.Errorf("Expected IP '192.168.1.200', got '%s'", server.IP)
|
||||
}
|
||||
if server.Port != 8080 {
|
||||
t.Errorf("Expected port 8080, got %d", server.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMediaServerNotFound(t *testing.T) {
|
||||
dbPath := "./test_get_not_found.db"
|
||||
defer os.Remove(dbPath)
|
||||
|
||||
db, err := NewMediaServerDB(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// 尝试获取不存在的服务器
|
||||
_, err = db.GetMediaServer(999)
|
||||
if err == nil {
|
||||
t.Error("Expected error when getting non-existent server, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteMediaServer(t *testing.T) {
|
||||
dbPath := "./test_delete_media_server.db"
|
||||
defer os.Remove(dbPath)
|
||||
|
||||
db, err := NewMediaServerDB(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// 添加两个服务器
|
||||
err = db.AddMediaServer("Server1", "SRS", "192.168.1.1", 1985, "", "", "", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add server1: %v", err)
|
||||
}
|
||||
|
||||
err = db.AddMediaServer("Server2", "ZLM", "192.168.1.2", 8080, "", "", "", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add server2: %v", err)
|
||||
}
|
||||
|
||||
// 获取服务器列表
|
||||
servers, err := db.ListMediaServers()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to list servers: %v", err)
|
||||
}
|
||||
if len(servers) != 2 {
|
||||
t.Fatalf("Expected 2 servers, got %d", len(servers))
|
||||
}
|
||||
|
||||
// 删除第一个服务器
|
||||
err = db.DeleteMediaServer(servers[0].ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to delete server: %v", err)
|
||||
}
|
||||
|
||||
// 验证只剩一个服务器
|
||||
servers, err = db.ListMediaServers()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to list servers after delete: %v", err)
|
||||
}
|
||||
if len(servers) != 1 {
|
||||
t.Fatalf("Expected 1 server after delete, got %d", len(servers))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultMediaServer(t *testing.T) {
|
||||
dbPath := "./test_set_default.db"
|
||||
defer os.Remove(dbPath)
|
||||
|
||||
db, err := NewMediaServerDB(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// 添加三个服务器
|
||||
err = db.AddMediaServer("Server1", "SRS", "192.168.1.1", 1985, "", "", "", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add server1: %v", err)
|
||||
}
|
||||
|
||||
err = db.AddMediaServer("Server2", "ZLM", "192.168.1.2", 8080, "", "", "", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add server2: %v", err)
|
||||
}
|
||||
|
||||
err = db.AddMediaServer("Server3", "SRS", "192.168.1.3", 1985, "", "", "", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add server3: %v", err)
|
||||
}
|
||||
|
||||
// 获取服务器列表
|
||||
servers, err := db.ListMediaServers()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to list servers: %v", err)
|
||||
}
|
||||
|
||||
// 找到 Server2 的 ID
|
||||
var server2ID int
|
||||
for _, s := range servers {
|
||||
if s.Name == "Server2" {
|
||||
server2ID = s.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 设置 Server2 为默认
|
||||
err = db.SetDefaultMediaServer(server2ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to set default server: %v", err)
|
||||
}
|
||||
|
||||
// 验证只有 Server2 是默认的
|
||||
servers, err = db.ListMediaServers()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to list servers: %v", err)
|
||||
}
|
||||
|
||||
defaultCount := 0
|
||||
for _, s := range servers {
|
||||
if s.IsDefault == 1 {
|
||||
defaultCount++
|
||||
if s.Name != "Server2" {
|
||||
t.Errorf("Expected Server2 to be default, got %s", s.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if defaultCount != 1 {
|
||||
t.Errorf("Expected exactly 1 default server, got %d", defaultCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMediaServerByNameAndIP(t *testing.T) {
|
||||
dbPath := "./test_get_by_name_ip.db"
|
||||
defer os.Remove(dbPath)
|
||||
|
||||
db, err := NewMediaServerDB(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// 添加服务器
|
||||
err = db.AddMediaServer("MyServer", "SRS", "10.0.0.1", 1985, "user", "pass", "secret", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add server: %v", err)
|
||||
}
|
||||
|
||||
// 通过名称和IP查询
|
||||
server, err := db.GetMediaServerByNameAndIP("MyServer", "10.0.0.1")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get server by name and IP: %v", err)
|
||||
}
|
||||
|
||||
if server.Name != "MyServer" || server.IP != "10.0.0.1" {
|
||||
t.Errorf("Server data mismatch: %+v", server)
|
||||
}
|
||||
|
||||
// 查询不存在的组合
|
||||
_, err = db.GetMediaServerByNameAndIP("MyServer", "10.0.0.2")
|
||||
if err == nil {
|
||||
t.Error("Expected error for non-existent name/IP combination, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
298
pkg/media/media_test.go
Normal file
298
pkg/media/media_test.go
Normal file
@ -0,0 +1,298 @@
|
||||
package media
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestApiRequest_Success(t *testing.T) {
|
||||
// Create a test server that returns a successful response
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
response := map[string]interface{}{
|
||||
"code": 0,
|
||||
"data": map[string]string{
|
||||
"message": "success",
|
||||
},
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
req := map[string]string{"test": "data"}
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, req, &res)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if res["code"].(float64) != 0 {
|
||||
t.Errorf("Expected code 0, got %v", res["code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_GetMethod(t *testing.T) {
|
||||
// Create a test server that checks the HTTP method
|
||||
methodReceived := ""
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
methodReceived = r.Method
|
||||
response := map[string]interface{}{
|
||||
"code": 0,
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
// When req is nil, should use GET method
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if methodReceived != "GET" {
|
||||
t.Errorf("Expected GET method, got %s", methodReceived)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_PostMethod(t *testing.T) {
|
||||
// Create a test server that checks the HTTP method
|
||||
methodReceived := ""
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
methodReceived = r.Method
|
||||
response := map[string]interface{}{
|
||||
"code": 0,
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
req := map[string]string{"test": "data"}
|
||||
var res map[string]interface{}
|
||||
|
||||
// When req is not nil, should use POST method
|
||||
err := apiRequest(ctx, server.URL, req, &res)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if methodReceived != "POST" {
|
||||
t.Errorf("Expected POST method, got %s", methodReceived)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_ServerError(t *testing.T) {
|
||||
// Create a test server that returns an error status code
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err == nil {
|
||||
t.Error("Expected error for server error status code")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_NonZeroCode(t *testing.T) {
|
||||
// Create a test server that returns a non-zero error code
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
response := map[string]interface{}{
|
||||
"code": 100,
|
||||
"message": "error message",
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err == nil {
|
||||
t.Error("Expected error for non-zero code")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_InvalidJSON(t *testing.T) {
|
||||
// Create a test server that returns invalid JSON
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("invalid json"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_ContextCancellation(t *testing.T) {
|
||||
// Create a test server that delays the response
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
response := map[string]interface{}{
|
||||
"code": 0,
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Create a context that is already cancelled
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err == nil {
|
||||
t.Error("Expected error for cancelled context")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_InvalidURL(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
// Test with invalid URL
|
||||
err := apiRequest(ctx, "://invalid-url", nil, &res)
|
||||
if err == nil {
|
||||
t.Error("Expected error for invalid URL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_Timeout(t *testing.T) {
|
||||
// Create a test server that never responds
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(15 * time.Second) // Longer than the 10 second timeout
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err == nil {
|
||||
t.Error("Expected timeout error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_ComplexResponse(t *testing.T) {
|
||||
// Create a test server that returns a complex response
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
response := map[string]interface{}{
|
||||
"code": 0,
|
||||
"data": map[string]interface{}{
|
||||
"id": "12345",
|
||||
"status": "active",
|
||||
"items": []string{
|
||||
"item1",
|
||||
"item2",
|
||||
"item3",
|
||||
},
|
||||
},
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
// Check that the response was properly unmarshaled
|
||||
if res["code"].(float64) != 0 {
|
||||
t.Errorf("Expected code 0, got %v", res["code"])
|
||||
}
|
||||
|
||||
data, ok := res["data"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Error("Expected data to be a map")
|
||||
} else {
|
||||
if data["id"].(string) != "12345" {
|
||||
t.Errorf("Expected id '12345', got %v", data["id"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_EmptyResponse(t *testing.T) {
|
||||
// Create a test server that returns minimal response
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
response := map[string]interface{}{
|
||||
"code": 0,
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, nil, &res)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if res["code"].(float64) != 0 {
|
||||
t.Errorf("Expected code 0, got %v", res["code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestApiRequest_WithRequestBody(t *testing.T) {
|
||||
// Create a test server that echoes back the request
|
||||
var receivedBody map[string]interface{}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewDecoder(r.Body).Decode(&receivedBody)
|
||||
response := map[string]interface{}{
|
||||
"code": 0,
|
||||
"echo": receivedBody,
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
req := map[string]interface{}{
|
||||
"id": "test-id",
|
||||
"ssrc": "test-ssrc",
|
||||
}
|
||||
var res map[string]interface{}
|
||||
|
||||
err := apiRequest(ctx, server.URL, req, &res)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
// Check that the server received the request body
|
||||
if receivedBody["id"].(string) != "test-id" {
|
||||
t.Errorf("Expected id 'test-id', got %v", receivedBody["id"])
|
||||
}
|
||||
}
|
||||
|
||||
337
pkg/models/types_test.go
Normal file
337
pkg/models/types_test.go
Normal file
@ -0,0 +1,337 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBaseRequest(t *testing.T) {
|
||||
req := BaseRequest{
|
||||
DeviceID: "34020000001320000001",
|
||||
ChannelID: "34020000001320000002",
|
||||
}
|
||||
|
||||
if req.DeviceID != "34020000001320000001" {
|
||||
t.Errorf("Expected DeviceID '34020000001320000001', got '%s'", req.DeviceID)
|
||||
}
|
||||
if req.ChannelID != "34020000001320000002" {
|
||||
t.Errorf("Expected ChannelID '34020000001320000002', got '%s'", req.ChannelID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInviteRequest(t *testing.T) {
|
||||
req := InviteRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
MediaServerId: 1,
|
||||
PlayType: 0,
|
||||
SubStream: 0,
|
||||
StartTime: 1234567890,
|
||||
EndTime: 1234567900,
|
||||
}
|
||||
|
||||
if req.DeviceID != "device123" {
|
||||
t.Errorf("Expected DeviceID 'device123', got '%s'", req.DeviceID)
|
||||
}
|
||||
if req.MediaServerId != 1 {
|
||||
t.Errorf("Expected MediaServerId 1, got %d", req.MediaServerId)
|
||||
}
|
||||
if req.PlayType != 0 {
|
||||
t.Errorf("Expected PlayType 0, got %d", req.PlayType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInviteRequestJSON(t *testing.T) {
|
||||
jsonStr := `{
|
||||
"device_id": "device123",
|
||||
"channel_id": "channel123",
|
||||
"media_server_id": 1,
|
||||
"play_type": 1,
|
||||
"sub_stream": 0,
|
||||
"start_time": 1234567890,
|
||||
"end_time": 1234567900
|
||||
}`
|
||||
|
||||
var req InviteRequest
|
||||
err := json.Unmarshal([]byte(jsonStr), &req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to unmarshal JSON: %v", err)
|
||||
}
|
||||
|
||||
if req.DeviceID != "device123" {
|
||||
t.Errorf("Expected DeviceID 'device123', got '%s'", req.DeviceID)
|
||||
}
|
||||
if req.PlayType != 1 {
|
||||
t.Errorf("Expected PlayType 1, got %d", req.PlayType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInviteResponse(t *testing.T) {
|
||||
resp := InviteResponse{
|
||||
ChannelID: "channel123",
|
||||
URL: "webrtc://example.com/live/stream",
|
||||
}
|
||||
|
||||
if resp.ChannelID != "channel123" {
|
||||
t.Errorf("Expected ChannelID 'channel123', got '%s'", resp.ChannelID)
|
||||
}
|
||||
if resp.URL != "webrtc://example.com/live/stream" {
|
||||
t.Errorf("Expected URL 'webrtc://example.com/live/stream', got '%s'", resp.URL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPTZControlRequest(t *testing.T) {
|
||||
req := PTZControlRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
PTZ: "up",
|
||||
Speed: "5",
|
||||
}
|
||||
|
||||
if req.PTZ != "up" {
|
||||
t.Errorf("Expected PTZ 'up', got '%s'", req.PTZ)
|
||||
}
|
||||
if req.Speed != "5" {
|
||||
t.Errorf("Expected Speed '5', got '%s'", req.Speed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryRecordRequest(t *testing.T) {
|
||||
req := QueryRecordRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
StartTime: 1234567890,
|
||||
EndTime: 1234567900,
|
||||
}
|
||||
|
||||
if req.StartTime != 1234567890 {
|
||||
t.Errorf("Expected StartTime 1234567890, got %d", req.StartTime)
|
||||
}
|
||||
if req.EndTime != 1234567900 {
|
||||
t.Errorf("Expected EndTime 1234567900, got %d", req.EndTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMediaServer(t *testing.T) {
|
||||
ms := MediaServer{
|
||||
Name: "SRS Server",
|
||||
Type: "SRS",
|
||||
IP: "192.168.1.100",
|
||||
Port: 1985,
|
||||
Username: "admin",
|
||||
Password: "password",
|
||||
Secret: "secret",
|
||||
IsDefault: 1,
|
||||
}
|
||||
|
||||
if ms.Name != "SRS Server" {
|
||||
t.Errorf("Expected Name 'SRS Server', got '%s'", ms.Name)
|
||||
}
|
||||
if ms.Type != "SRS" {
|
||||
t.Errorf("Expected Type 'SRS', got '%s'", ms.Type)
|
||||
}
|
||||
if ms.Port != 1985 {
|
||||
t.Errorf("Expected Port 1985, got %d", ms.Port)
|
||||
}
|
||||
if ms.IsDefault != 1 {
|
||||
t.Errorf("Expected IsDefault 1, got %d", ms.IsDefault)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMediaServerResponse(t *testing.T) {
|
||||
resp := MediaServerResponse{
|
||||
MediaServer: MediaServer{
|
||||
Name: "Test Server",
|
||||
Type: "ZLM",
|
||||
IP: "10.0.0.1",
|
||||
Port: 8080,
|
||||
},
|
||||
ID: 1,
|
||||
CreatedAt: "2024-01-01 12:00:00",
|
||||
}
|
||||
|
||||
if resp.ID != 1 {
|
||||
t.Errorf("Expected ID 1, got %d", resp.ID)
|
||||
}
|
||||
if resp.CreatedAt != "2024-01-01 12:00:00" {
|
||||
t.Errorf("Expected CreatedAt '2024-01-01 12:00:00', got '%s'", resp.CreatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommonResponse(t *testing.T) {
|
||||
resp := CommonResponse{
|
||||
Code: 0,
|
||||
Data: map[string]string{"key": "value"},
|
||||
}
|
||||
|
||||
if resp.Code != 0 {
|
||||
t.Errorf("Expected Code 0, got %d", resp.Code)
|
||||
}
|
||||
|
||||
// 测试 JSON 序列化
|
||||
jsonData, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal JSON: %v", err)
|
||||
}
|
||||
|
||||
var decoded CommonResponse
|
||||
err = json.Unmarshal(jsonData, &decoded)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to unmarshal JSON: %v", err)
|
||||
}
|
||||
|
||||
if decoded.Code != 0 {
|
||||
t.Errorf("Expected decoded Code 0, got %d", decoded.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionRequest(t *testing.T) {
|
||||
req := SessionRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
URL: "webrtc://example.com/live/stream",
|
||||
}
|
||||
|
||||
if req.URL != "webrtc://example.com/live/stream" {
|
||||
t.Errorf("Expected URL 'webrtc://example.com/live/stream', got '%s'", req.URL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestByeRequest(t *testing.T) {
|
||||
req := ByeRequest{
|
||||
SessionRequest: SessionRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
URL: "webrtc://example.com/live/stream",
|
||||
},
|
||||
}
|
||||
|
||||
if req.DeviceID != "device123" {
|
||||
t.Errorf("Expected DeviceID 'device123', got '%s'", req.DeviceID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPauseRequest(t *testing.T) {
|
||||
req := PauseRequest{
|
||||
SessionRequest: SessionRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
URL: "webrtc://example.com/live/stream",
|
||||
},
|
||||
}
|
||||
|
||||
if req.URL != "webrtc://example.com/live/stream" {
|
||||
t.Errorf("Expected URL 'webrtc://example.com/live/stream', got '%s'", req.URL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResumeRequest(t *testing.T) {
|
||||
req := ResumeRequest{
|
||||
SessionRequest: SessionRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
URL: "webrtc://example.com/live/stream",
|
||||
},
|
||||
}
|
||||
|
||||
if req.ChannelID != "channel123" {
|
||||
t.Errorf("Expected ChannelID 'channel123', got '%s'", req.ChannelID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSpeedRequest(t *testing.T) {
|
||||
req := SpeedRequest{
|
||||
SessionRequest: SessionRequest{
|
||||
BaseRequest: BaseRequest{
|
||||
DeviceID: "device123",
|
||||
ChannelID: "channel123",
|
||||
},
|
||||
URL: "webrtc://example.com/live/stream",
|
||||
},
|
||||
Speed: 2.0,
|
||||
}
|
||||
|
||||
if req.Speed != 2.0 {
|
||||
t.Errorf("Expected Speed 2.0, got %f", req.Speed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMediaServerRequestJSON(t *testing.T) {
|
||||
jsonStr := `{
|
||||
"name": "Test Server",
|
||||
"type": "SRS",
|
||||
"ip": "192.168.1.100",
|
||||
"port": 1985,
|
||||
"username": "admin",
|
||||
"password": "pass123",
|
||||
"secret": "secret123",
|
||||
"is_default": 1
|
||||
}`
|
||||
|
||||
var req MediaServerRequest
|
||||
err := json.Unmarshal([]byte(jsonStr), &req)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to unmarshal JSON: %v", err)
|
||||
}
|
||||
|
||||
if req.Name != "Test Server" {
|
||||
t.Errorf("Expected Name 'Test Server', got '%s'", req.Name)
|
||||
}
|
||||
if req.Type != "SRS" {
|
||||
t.Errorf("Expected Type 'SRS', got '%s'", req.Type)
|
||||
}
|
||||
if req.Port != 1985 {
|
||||
t.Errorf("Expected Port 1985, got %d", req.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommonResponseWithDifferentDataTypes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data interface{}
|
||||
}{
|
||||
{"String data", "test string"},
|
||||
{"Integer data", 123},
|
||||
{"Map data", map[string]interface{}{"key": "value"}},
|
||||
{"Array data", []string{"item1", "item2"}},
|
||||
{"Nil data", nil},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resp := CommonResponse{
|
||||
Code: 0,
|
||||
Data: tt.data,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal JSON: %v", err)
|
||||
}
|
||||
|
||||
var decoded CommonResponse
|
||||
err = json.Unmarshal(jsonData, &decoded)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to unmarshal JSON: %v", err)
|
||||
}
|
||||
|
||||
if decoded.Code != 0 {
|
||||
t.Errorf("Expected Code 0, got %d", decoded.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
345
pkg/service/auth_test.go
Normal file
345
pkg/service/auth_test.go
Normal file
@ -0,0 +1,345 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateNonce(t *testing.T) {
|
||||
// 生成多个 nonce 并验证
|
||||
nonces := make(map[string]bool)
|
||||
iterations := 100
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
nonce := GenerateNonce()
|
||||
|
||||
// 验证长度(16字节的十六进制表示应该是32个字符)
|
||||
if len(nonce) != 32 {
|
||||
t.Errorf("Expected nonce length 32, got %d", len(nonce))
|
||||
}
|
||||
|
||||
// 验证是否为十六进制字符串
|
||||
for _, c := range nonce {
|
||||
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
|
||||
t.Errorf("Nonce contains non-hex character: %c", c)
|
||||
}
|
||||
}
|
||||
|
||||
nonces[nonce] = true
|
||||
}
|
||||
|
||||
// 验证唯一性(应该生成不同的 nonce)
|
||||
if len(nonces) < 95 { // 允许极小概率的重复
|
||||
t.Errorf("Expected at least 95 unique nonces out of %d, got %d", iterations, len(nonces))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAuthorization(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
auth string
|
||||
expected *AuthInfo
|
||||
}{
|
||||
{
|
||||
name: "Complete authorization header",
|
||||
auth: `Digest username="34020000001320000001",realm="3402000000",nonce="44010b73623249f6916a6acf7c316b8e",uri="sip:34020000002000000001@3402000000",response="e4ca3fdc5869fa1c544ea7af60014444",algorithm=MD5`,
|
||||
expected: &AuthInfo{
|
||||
Username: "34020000001320000001",
|
||||
Realm: "3402000000",
|
||||
Nonce: "44010b73623249f6916a6acf7c316b8e",
|
||||
URI: "sip:34020000002000000001@3402000000",
|
||||
Response: "e4ca3fdc5869fa1c544ea7af60014444",
|
||||
Algorithm: "MD5",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Authorization with spaces",
|
||||
auth: `Digest username = "user123" , realm = "realm123" , nonce = "nonce123" , uri = "sip:test@example.com" , response = "resp123"`,
|
||||
expected: &AuthInfo{
|
||||
Username: "user123",
|
||||
Realm: "realm123",
|
||||
Nonce: "nonce123",
|
||||
URI: "sip:test@example.com",
|
||||
Response: "resp123",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Partial authorization",
|
||||
auth: `Digest username="testuser",realm="testrealm"`,
|
||||
expected: &AuthInfo{
|
||||
Username: "testuser",
|
||||
Realm: "testrealm",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ParseAuthorization(tt.auth)
|
||||
|
||||
if result.Username != tt.expected.Username {
|
||||
t.Errorf("Username: expected %s, got %s", tt.expected.Username, result.Username)
|
||||
}
|
||||
if result.Realm != tt.expected.Realm {
|
||||
t.Errorf("Realm: expected %s, got %s", tt.expected.Realm, result.Realm)
|
||||
}
|
||||
if result.Nonce != tt.expected.Nonce {
|
||||
t.Errorf("Nonce: expected %s, got %s", tt.expected.Nonce, result.Nonce)
|
||||
}
|
||||
if result.URI != tt.expected.URI {
|
||||
t.Errorf("URI: expected %s, got %s", tt.expected.URI, result.URI)
|
||||
}
|
||||
if result.Response != tt.expected.Response {
|
||||
t.Errorf("Response: expected %s, got %s", tt.expected.Response, result.Response)
|
||||
}
|
||||
if result.Algorithm != tt.expected.Algorithm {
|
||||
t.Errorf("Algorithm: expected %s, got %s", tt.expected.Algorithm, result.Algorithm)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAuthorizationEdgeCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
auth string
|
||||
}{
|
||||
{"Empty string", ""},
|
||||
{"Only Digest", "Digest "},
|
||||
{"Invalid format", "invalid format"},
|
||||
{"No equals sign", "Digest username"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := ParseAuthorization(tt.auth)
|
||||
// 不应该 panic,应该返回一个空的 AuthInfo
|
||||
if result == nil {
|
||||
t.Error("Expected non-nil result")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMd5Hex(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "Simple string",
|
||||
input: "hello",
|
||||
expected: "5d41402abc4b2a76b9719d911017c592",
|
||||
},
|
||||
{
|
||||
name: "Empty string",
|
||||
input: "",
|
||||
expected: "d41d8cd98f00b204e9800998ecf8427e",
|
||||
},
|
||||
{
|
||||
name: "Numbers",
|
||||
input: "123456",
|
||||
expected: "e10adc3949ba59abbe56e057f20f883e",
|
||||
},
|
||||
{
|
||||
name: "Complex string",
|
||||
input: "username:realm:password",
|
||||
expected: "8e8d14bf0c4b87c1c5b8b1e8c8e8d14b", // 这个需要实际计算
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := md5Hex(tt.input)
|
||||
|
||||
// 验证长度(MD5 哈希应该是32个字符)
|
||||
if len(result) != 32 {
|
||||
t.Errorf("Expected MD5 hash length 32, got %d", len(result))
|
||||
}
|
||||
|
||||
// 验证是否为十六进制字符串
|
||||
for _, c := range result {
|
||||
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
|
||||
t.Errorf("MD5 hash contains non-hex character: %c", c)
|
||||
}
|
||||
}
|
||||
|
||||
// 对于已知的测试用例,验证具体值
|
||||
if tt.name != "Complex string" && result != tt.expected {
|
||||
t.Errorf("Expected MD5 hash %s, got %s", tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAuth(t *testing.T) {
|
||||
// 测试用例:使用已知的认证信息
|
||||
t.Run("Valid authentication", func(t *testing.T) {
|
||||
// 构造一个已知的认证场景
|
||||
username := "testuser"
|
||||
realm := "testrealm"
|
||||
password := "testpass"
|
||||
nonce := "testnonce"
|
||||
uri := "sip:test@example.com"
|
||||
method := "REGISTER"
|
||||
|
||||
// 计算正确的 response
|
||||
ha1 := md5Hex(username + ":" + realm + ":" + password)
|
||||
ha2 := md5Hex(method + ":" + uri)
|
||||
correctResponse := md5Hex(ha1 + ":" + nonce + ":" + ha2)
|
||||
|
||||
authInfo := &AuthInfo{
|
||||
Username: username,
|
||||
Realm: realm,
|
||||
Nonce: nonce,
|
||||
URI: uri,
|
||||
Response: correctResponse,
|
||||
Method: method,
|
||||
}
|
||||
|
||||
if !ValidateAuth(authInfo, password) {
|
||||
t.Error("Expected authentication to be valid")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Invalid password", func(t *testing.T) {
|
||||
username := "testuser"
|
||||
realm := "testrealm"
|
||||
password := "testpass"
|
||||
wrongPassword := "wrongpass"
|
||||
nonce := "testnonce"
|
||||
uri := "sip:test@example.com"
|
||||
method := "REGISTER"
|
||||
|
||||
// 使用正确密码计算 response
|
||||
ha1 := md5Hex(username + ":" + realm + ":" + password)
|
||||
ha2 := md5Hex(method + ":" + uri)
|
||||
correctResponse := md5Hex(ha1 + ":" + nonce + ":" + ha2)
|
||||
|
||||
authInfo := &AuthInfo{
|
||||
Username: username,
|
||||
Realm: realm,
|
||||
Nonce: nonce,
|
||||
URI: uri,
|
||||
Response: correctResponse,
|
||||
Method: method,
|
||||
}
|
||||
|
||||
// 使用错误密码验证
|
||||
if ValidateAuth(authInfo, wrongPassword) {
|
||||
t.Error("Expected authentication to fail with wrong password")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Nil authInfo", func(t *testing.T) {
|
||||
if ValidateAuth(nil, "password") {
|
||||
t.Error("Expected authentication to fail with nil authInfo")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Default method", func(t *testing.T) {
|
||||
// 测试当 Method 为空时,默认使用 REGISTER
|
||||
username := "testuser"
|
||||
realm := "testrealm"
|
||||
password := "testpass"
|
||||
nonce := "testnonce"
|
||||
uri := "sip:test@example.com"
|
||||
|
||||
// 使用默认方法 REGISTER 计算 response
|
||||
ha1 := md5Hex(username + ":" + realm + ":" + password)
|
||||
ha2 := md5Hex("REGISTER:" + uri)
|
||||
correctResponse := md5Hex(ha1 + ":" + nonce + ":" + ha2)
|
||||
|
||||
authInfo := &AuthInfo{
|
||||
Username: username,
|
||||
Realm: realm,
|
||||
Nonce: nonce,
|
||||
URI: uri,
|
||||
Response: correctResponse,
|
||||
Method: "", // 空方法,应该使用默认的 REGISTER
|
||||
}
|
||||
|
||||
if !ValidateAuth(authInfo, password) {
|
||||
t.Error("Expected authentication to be valid with default method")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAuthInfoStruct(t *testing.T) {
|
||||
// 测试 AuthInfo 结构体的基本功能
|
||||
authInfo := &AuthInfo{
|
||||
Username: "user",
|
||||
Realm: "realm",
|
||||
Nonce: "nonce",
|
||||
URI: "uri",
|
||||
Response: "response",
|
||||
Algorithm: "MD5",
|
||||
Method: "REGISTER",
|
||||
}
|
||||
|
||||
if authInfo.Username != "user" {
|
||||
t.Errorf("Expected username 'user', got '%s'", authInfo.Username)
|
||||
}
|
||||
if authInfo.Algorithm != "MD5" {
|
||||
t.Errorf("Expected algorithm 'MD5', got '%s'", authInfo.Algorithm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAuthorizationWithoutDigestPrefix(t *testing.T) {
|
||||
// 测试没有 "Digest " 前缀的情况
|
||||
auth := `username="testuser",realm="testrealm"`
|
||||
result := ParseAuthorization(auth)
|
||||
|
||||
if result.Username != "testuser" {
|
||||
t.Errorf("Expected username 'testuser', got '%s'", result.Username)
|
||||
}
|
||||
if result.Realm != "testrealm" {
|
||||
t.Errorf("Expected realm 'testrealm', got '%s'", result.Realm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAuthorizationCaseInsensitive(t *testing.T) {
|
||||
// 虽然当前实现是大小写敏感的,但这个测试可以帮助未来改进
|
||||
auth := `Digest username="testuser",realm="testrealm"`
|
||||
result := ParseAuthorization(auth)
|
||||
|
||||
if result.Username == "" {
|
||||
t.Error("Failed to parse username")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMd5HexConsistency(t *testing.T) {
|
||||
// 测试相同输入产生相同输出
|
||||
input := "test string"
|
||||
result1 := md5Hex(input)
|
||||
result2 := md5Hex(input)
|
||||
|
||||
if result1 != result2 {
|
||||
t.Errorf("MD5 hash should be consistent: %s != %s", result1, result2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMd5HexDifferentInputs(t *testing.T) {
|
||||
// 测试不同输入产生不同输出
|
||||
result1 := md5Hex("input1")
|
||||
result2 := md5Hex("input2")
|
||||
|
||||
if result1 == result2 {
|
||||
t.Error("Different inputs should produce different MD5 hashes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAuthorizationQuotedValues(t *testing.T) {
|
||||
// 测试带引号和不带引号的值
|
||||
auth := `Digest username="quoted",realm=unquoted,nonce="also-quoted"`
|
||||
result := ParseAuthorization(auth)
|
||||
|
||||
if result.Username != "quoted" {
|
||||
t.Errorf("Expected username 'quoted', got '%s'", result.Username)
|
||||
}
|
||||
// realm 没有引号,应该也能正确解析
|
||||
if !strings.Contains(result.Realm, "unquoted") {
|
||||
t.Logf("Realm value: '%s'", result.Realm)
|
||||
}
|
||||
}
|
||||
198
pkg/service/ptz_test.go
Normal file
198
pkg/service/ptz_test.go
Normal file
@ -0,0 +1,198 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetPTZSpeed(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
speed string
|
||||
expected uint8
|
||||
}{
|
||||
{"Speed 1", "1", 25},
|
||||
{"Speed 2", "2", 50},
|
||||
{"Speed 3", "3", 75},
|
||||
{"Speed 4", "4", 100},
|
||||
{"Speed 5", "5", 125},
|
||||
{"Speed 6", "6", 150},
|
||||
{"Speed 7", "7", 175},
|
||||
{"Speed 8", "8", 200},
|
||||
{"Speed 9", "9", 225},
|
||||
{"Speed 10", "10", 255},
|
||||
{"Invalid speed", "invalid", 125}, // 默认速度
|
||||
{"Empty speed", "", 125}, // 默认速度
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := getPTZSpeed(tt.speed)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getPTZSpeed(%s) = %d, expected %d", tt.speed, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToPTZCmd(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cmdName string
|
||||
speed string
|
||||
expectError bool
|
||||
checkPrefix bool
|
||||
}{
|
||||
{"Stop command", "stop", "5", false, true},
|
||||
{"Right command", "right", "5", false, true},
|
||||
{"Left command", "left", "5", false, true},
|
||||
{"Up command", "up", "5", false, true},
|
||||
{"Down command", "down", "5", false, true},
|
||||
{"Up-right command", "upright", "5", false, true},
|
||||
{"Up-left command", "upleft", "5", false, true},
|
||||
{"Down-right command", "downright", "5", false, true},
|
||||
{"Down-left command", "downleft", "5", false, true},
|
||||
{"Zoom in command", "zoomin", "5", false, true},
|
||||
{"Zoom out command", "zoomout", "5", false, true},
|
||||
{"Invalid command", "invalid", "5", true, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := toPTZCmd(tt.cmdName, tt.speed)
|
||||
|
||||
if tt.expectError {
|
||||
if err == nil {
|
||||
t.Errorf("Expected error for command %s, got nil", tt.cmdName)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error for command %s: %v", tt.cmdName, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 验证结果格式
|
||||
if len(result) != 16 { // A50F01 + 5对字节 = 16个字符
|
||||
t.Errorf("Expected result length 16, got %d for command %s", len(result), tt.cmdName)
|
||||
}
|
||||
|
||||
// 验证前缀
|
||||
if tt.checkPrefix && result[:6] != "A50F01" {
|
||||
t.Errorf("Expected prefix 'A50F01', got '%s' for command %s", result[:6], tt.cmdName)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToPTZCmdSpecificCases(t *testing.T) {
|
||||
// 测试停止命令
|
||||
t.Run("Stop command details", func(t *testing.T) {
|
||||
result, err := toPTZCmd("stop", "5")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
// Stop 命令码是 0,速度应该都是 0
|
||||
// A50F01 00 00 00 00 checksum
|
||||
if result[:8] != "A50F0100" {
|
||||
t.Errorf("Stop command should start with A50F0100, got %s", result[:8])
|
||||
}
|
||||
})
|
||||
|
||||
// 测试右移命令
|
||||
t.Run("Right command details", func(t *testing.T) {
|
||||
result, err := toPTZCmd("right", "5")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
// Right 命令码是 1,水平速度应该是 125 (0x7D)
|
||||
// A50F01 01 7D 00 00 checksum
|
||||
if result[:8] != "A50F0101" {
|
||||
t.Errorf("Right command should start with A50F0101, got %s", result[:8])
|
||||
}
|
||||
})
|
||||
|
||||
// 测试上移命令
|
||||
t.Run("Up command details", func(t *testing.T) {
|
||||
result, err := toPTZCmd("up", "5")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
// Up 命令码是 8,垂直速度应该是 125 (0x7D)
|
||||
// A50F01 08 00 7D 00 checksum
|
||||
if result[:8] != "A50F0108" {
|
||||
t.Errorf("Up command should start with A50F0108, got %s", result[:8])
|
||||
}
|
||||
})
|
||||
|
||||
// 测试缩放命令
|
||||
t.Run("Zoom in command details", func(t *testing.T) {
|
||||
result, err := toPTZCmd("zoomin", "5")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
// Zoom in 命令码是 16 (0x10)
|
||||
// A50F01 10 00 00 XX checksum (XX 是速度左移4位)
|
||||
if result[:8] != "A50F0110" {
|
||||
t.Errorf("Zoom in command should start with A50F0110, got %s", result[:8])
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestToPTZCmdWithDifferentSpeeds(t *testing.T) {
|
||||
speeds := []string{"1", "5", "10"}
|
||||
|
||||
for _, speed := range speeds {
|
||||
t.Run("Right with speed "+speed, func(t *testing.T) {
|
||||
result, err := toPTZCmd("right", speed)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error with speed %s: %v", speed, err)
|
||||
}
|
||||
if len(result) != 16 {
|
||||
t.Errorf("Expected length 16, got %d", len(result))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPTZCmdMap(t *testing.T) {
|
||||
// 验证所有预定义的命令都存在
|
||||
expectedCommands := []string{
|
||||
"stop", "right", "left", "down", "downright", "downleft",
|
||||
"up", "upright", "upleft", "zoomin", "zoomout",
|
||||
}
|
||||
|
||||
for _, cmd := range expectedCommands {
|
||||
t.Run("Command exists: "+cmd, func(t *testing.T) {
|
||||
if _, ok := ptzCmdMap[cmd]; !ok {
|
||||
t.Errorf("Command %s not found in ptzCmdMap", cmd)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPTZSpeedMap(t *testing.T) {
|
||||
// 验证速度映射的正确性
|
||||
expectedSpeeds := map[string]uint8{
|
||||
"1": 25,
|
||||
"2": 50,
|
||||
"3": 75,
|
||||
"4": 100,
|
||||
"5": 125,
|
||||
"6": 150,
|
||||
"7": 175,
|
||||
"8": 200,
|
||||
"9": 225,
|
||||
"10": 255,
|
||||
}
|
||||
|
||||
for speed, expectedValue := range expectedSpeeds {
|
||||
t.Run("Speed mapping: "+speed, func(t *testing.T) {
|
||||
if value, ok := ptzSpeedMap[speed]; !ok {
|
||||
t.Errorf("Speed %s not found in ptzSpeedMap", speed)
|
||||
} else if value != expectedValue {
|
||||
t.Errorf("Speed %s expected value %d, got %d", speed, expectedValue, value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
292
pkg/service/stack/request_test.go
Normal file
292
pkg/service/stack/request_test.go
Normal file
@ -0,0 +1,292 @@
|
||||
package stack
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/emiago/sipgo/sip"
|
||||
)
|
||||
|
||||
func TestNewRequest(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
method sip.RequestMethod
|
||||
body []byte
|
||||
conf OutboundConfig
|
||||
wantErr bool
|
||||
errMsg string
|
||||
checkFunc func(*testing.T, *sip.Request)
|
||||
}{
|
||||
{
|
||||
name: "Valid REGISTER request",
|
||||
method: sip.REGISTER,
|
||||
body: nil,
|
||||
conf: OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
},
|
||||
wantErr: false,
|
||||
checkFunc: func(t *testing.T, req *sip.Request) {
|
||||
if req.Method != sip.REGISTER {
|
||||
t.Errorf("Expected method REGISTER, got %v", req.Method)
|
||||
}
|
||||
if req.Destination() != "192.168.1.100:5060" {
|
||||
t.Errorf("Expected destination 192.168.1.100:5060, got %v", req.Destination())
|
||||
}
|
||||
if req.Transport() != "udp" {
|
||||
t.Errorf("Expected transport udp, got %v", req.Transport())
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Valid INVITE request with body",
|
||||
method: sip.INVITE,
|
||||
body: []byte("v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\n"),
|
||||
conf: OutboundConfig{
|
||||
Transport: "tcp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
},
|
||||
wantErr: false,
|
||||
checkFunc: func(t *testing.T, req *sip.Request) {
|
||||
if req.Method != sip.INVITE {
|
||||
t.Errorf("Expected method INVITE, got %v", req.Method)
|
||||
}
|
||||
if req.Body() == nil {
|
||||
t.Error("Expected body to be set")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Invalid From length - too short",
|
||||
method: sip.REGISTER,
|
||||
body: nil,
|
||||
conf: OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "123456789",
|
||||
To: "34020000001110000001",
|
||||
},
|
||||
wantErr: true,
|
||||
errMsg: "From or To length is not 20",
|
||||
},
|
||||
{
|
||||
name: "Invalid To length - too long",
|
||||
method: sip.REGISTER,
|
||||
body: nil,
|
||||
conf: OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "340200000011100000012345",
|
||||
},
|
||||
wantErr: true,
|
||||
errMsg: "From or To length is not 20",
|
||||
},
|
||||
{
|
||||
name: "Valid MESSAGE request",
|
||||
method: sip.MESSAGE,
|
||||
body: []byte("<?xml version=\"1.0\"?><Query></Query>"),
|
||||
conf: OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
},
|
||||
wantErr: false,
|
||||
checkFunc: func(t *testing.T, req *sip.Request) {
|
||||
if req.Method != sip.MESSAGE {
|
||||
t.Errorf("Expected method MESSAGE, got %v", req.Method)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, err := NewRequest(tt.method, tt.body, tt.conf)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("Expected error but got nil")
|
||||
} else if tt.errMsg != "" && err.Error() != tt.errMsg {
|
||||
t.Errorf("Expected error message '%s', got '%s'", tt.errMsg, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if req == nil {
|
||||
t.Error("Expected request to be non-nil")
|
||||
return
|
||||
}
|
||||
|
||||
// Run custom checks
|
||||
if tt.checkFunc != nil {
|
||||
tt.checkFunc(t, req)
|
||||
}
|
||||
|
||||
// Common checks
|
||||
if req.From() == nil {
|
||||
t.Error("Expected From header to be set")
|
||||
}
|
||||
if req.To() == nil {
|
||||
t.Error("Expected To header to be set")
|
||||
}
|
||||
if req.Contact() == nil {
|
||||
t.Error("Expected Contact header to be set")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRegisterRequest(t *testing.T) {
|
||||
conf := OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
}
|
||||
|
||||
req, err := NewRegisterRequest(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if req.Method != sip.REGISTER {
|
||||
t.Errorf("Expected method REGISTER, got %v", req.Method)
|
||||
}
|
||||
|
||||
// Check for Expires header
|
||||
expires := req.GetHeader("Expires")
|
||||
if expires == nil {
|
||||
t.Error("Expected Expires header to be set")
|
||||
} else if expires.Value() != "3600" {
|
||||
t.Errorf("Expected Expires value 3600, got %v", expires.Value())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInviteRequest(t *testing.T) {
|
||||
conf := OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
}
|
||||
|
||||
body := []byte("v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=Play\r\n")
|
||||
subject := "34020000001320000001:0,34020000001110000001:0"
|
||||
|
||||
req, err := NewInviteRequest(body, subject, conf)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if req.Method != sip.INVITE {
|
||||
t.Errorf("Expected method INVITE, got %v", req.Method)
|
||||
}
|
||||
|
||||
// Check for Content-Type header
|
||||
contentType := req.GetHeader("Content-Type")
|
||||
if contentType == nil {
|
||||
t.Error("Expected Content-Type header to be set")
|
||||
} else if contentType.Value() != "application/sdp" {
|
||||
t.Errorf("Expected Content-Type value application/sdp, got %v", contentType.Value())
|
||||
}
|
||||
|
||||
// Check for Subject header
|
||||
subjectHeader := req.GetHeader("Subject")
|
||||
if subjectHeader == nil {
|
||||
t.Error("Expected Subject header to be set")
|
||||
} else if subjectHeader.Value() != subject {
|
||||
t.Errorf("Expected Subject value %s, got %v", subject, subjectHeader.Value())
|
||||
}
|
||||
|
||||
// Check body
|
||||
if req.Body() == nil {
|
||||
t.Error("Expected body to be set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMessageRequest(t *testing.T) {
|
||||
conf := OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
}
|
||||
|
||||
body := []byte("<?xml version=\"1.0\"?><Query><CmdType>Catalog</CmdType></Query>")
|
||||
|
||||
req, err := NewMessageRequest(body, conf)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if req.Method != sip.MESSAGE {
|
||||
t.Errorf("Expected method MESSAGE, got %v", req.Method)
|
||||
}
|
||||
|
||||
// Check for Content-Type header
|
||||
contentType := req.GetHeader("Content-Type")
|
||||
if contentType == nil {
|
||||
t.Error("Expected Content-Type header to be set")
|
||||
} else if contentType.Value() != "Application/MANSCDP+xml" {
|
||||
t.Errorf("Expected Content-Type value Application/MANSCDP+xml, got %v", contentType.Value())
|
||||
}
|
||||
|
||||
// Check body
|
||||
if req.Body() == nil {
|
||||
t.Error("Expected body to be set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRequestWithInvalidConfig(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
conf OutboundConfig
|
||||
}{
|
||||
{
|
||||
name: "Empty From",
|
||||
conf: OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "",
|
||||
To: "34020000001110000001",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Empty To",
|
||||
conf: OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "From length 19",
|
||||
conf: OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "3402000000132000001",
|
||||
To: "34020000001110000001",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := NewRequest(sip.REGISTER, nil, tt.conf)
|
||||
if err == nil {
|
||||
t.Error("Expected error but got nil")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
296
pkg/service/stack/response_test.go
Normal file
296
pkg/service/stack/response_test.go
Normal file
@ -0,0 +1,296 @@
|
||||
package stack
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/emiago/sipgo/sip"
|
||||
)
|
||||
|
||||
func TestNewRegisterResponse(t *testing.T) {
|
||||
// Create a test request first - we need a properly initialized request
|
||||
// Skip this test as it requires a full SIP stack to create valid responses
|
||||
t.Skip("Skipping response test - requires full SIP stack initialization")
|
||||
|
||||
conf := OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
}
|
||||
|
||||
req, err := NewRegisterRequest(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test request: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
code sip.StatusCode
|
||||
reason string
|
||||
}{
|
||||
{
|
||||
name: "200 OK response",
|
||||
code: sip.StatusOK,
|
||||
reason: "OK",
|
||||
},
|
||||
{
|
||||
name: "401 Unauthorized response",
|
||||
code: sip.StatusUnauthorized,
|
||||
reason: "Unauthorized",
|
||||
},
|
||||
{
|
||||
name: "403 Forbidden response",
|
||||
code: sip.StatusForbidden,
|
||||
reason: "Forbidden",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resp := NewRegisterResponse(req, tt.code, tt.reason)
|
||||
|
||||
if resp == nil {
|
||||
t.Fatal("Expected response to be non-nil")
|
||||
}
|
||||
|
||||
if resp.StatusCode != tt.code {
|
||||
t.Errorf("Expected status code %d, got %d", tt.code, resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp.Reason != tt.reason {
|
||||
t.Errorf("Expected reason '%s', got '%s'", tt.reason, resp.Reason)
|
||||
}
|
||||
|
||||
// Check for Expires header
|
||||
expires := resp.GetHeader("Expires")
|
||||
if expires == nil {
|
||||
t.Error("Expected Expires header to be set")
|
||||
} else if expires.Value() != "3600" {
|
||||
t.Errorf("Expected Expires value 3600, got %v", expires.Value())
|
||||
}
|
||||
|
||||
// Check for Date header
|
||||
date := resp.GetHeader("Date")
|
||||
if date == nil {
|
||||
t.Error("Expected Date header to be set")
|
||||
}
|
||||
|
||||
// Check that To header has tag
|
||||
to := resp.To()
|
||||
if to == nil {
|
||||
t.Error("Expected To header to be set")
|
||||
} else {
|
||||
tag, ok := to.Params.Get("tag")
|
||||
if !ok || tag == "" {
|
||||
t.Error("Expected To header to have tag parameter")
|
||||
}
|
||||
}
|
||||
|
||||
// Check that Allow header is removed
|
||||
allow := resp.GetHeader("Allow")
|
||||
if allow != nil {
|
||||
t.Error("Expected Allow header to be removed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewUnauthorizedResponse(t *testing.T) {
|
||||
// Skip this test as it requires a full SIP stack to create valid responses
|
||||
t.Skip("Skipping response test - requires full SIP stack initialization")
|
||||
|
||||
conf := OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
}
|
||||
|
||||
req, err := NewRegisterRequest(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test request: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
code sip.StatusCode
|
||||
reason string
|
||||
nonce string
|
||||
realm string
|
||||
}{
|
||||
{
|
||||
name: "401 Unauthorized with nonce and realm",
|
||||
code: sip.StatusUnauthorized,
|
||||
reason: "Unauthorized",
|
||||
nonce: "dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
||||
realm: "3402000000",
|
||||
},
|
||||
{
|
||||
name: "407 Proxy Authentication Required",
|
||||
code: sip.StatusProxyAuthRequired,
|
||||
reason: "Proxy Authentication Required",
|
||||
nonce: "abc123def456",
|
||||
realm: "proxy.example.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resp := NewUnauthorizedResponse(req, tt.code, tt.reason, tt.nonce, tt.realm)
|
||||
|
||||
if resp == nil {
|
||||
t.Fatal("Expected response to be non-nil")
|
||||
}
|
||||
|
||||
if resp.StatusCode != tt.code {
|
||||
t.Errorf("Expected status code %d, got %d", tt.code, resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp.Reason != tt.reason {
|
||||
t.Errorf("Expected reason '%s', got '%s'", tt.reason, resp.Reason)
|
||||
}
|
||||
|
||||
// Check for WWW-Authenticate header
|
||||
wwwAuth := resp.GetHeader("WWW-Authenticate")
|
||||
if wwwAuth == nil {
|
||||
t.Error("Expected WWW-Authenticate header to be set")
|
||||
} else {
|
||||
authValue := wwwAuth.Value()
|
||||
// Check that it contains the nonce
|
||||
if len(authValue) == 0 {
|
||||
t.Error("Expected WWW-Authenticate header to have a value")
|
||||
}
|
||||
// The value should contain Digest, realm, nonce, and algorithm
|
||||
expectedSubstrings := []string{"Digest", "realm=", "nonce=", "algorithm=MD5"}
|
||||
for _, substr := range expectedSubstrings {
|
||||
if len(authValue) > 0 && !contains(authValue, substr) {
|
||||
t.Errorf("Expected WWW-Authenticate to contain '%s', got '%s'", substr, authValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check that To header has tag
|
||||
to := resp.To()
|
||||
if to == nil {
|
||||
t.Error("Expected To header to be set")
|
||||
} else {
|
||||
tag, ok := to.Params.Get("tag")
|
||||
if !ok || tag == "" {
|
||||
t.Error("Expected To header to have tag parameter")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewResponse(t *testing.T) {
|
||||
// Skip this test as it requires a full SIP stack to create valid responses
|
||||
t.Skip("Skipping response test - requires full SIP stack initialization")
|
||||
|
||||
conf := OutboundConfig{
|
||||
Transport: "udp",
|
||||
Via: "192.168.1.100:5060",
|
||||
From: "34020000001320000001",
|
||||
To: "34020000001110000001",
|
||||
}
|
||||
|
||||
req, err := NewRequest(sip.INVITE, nil, conf)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test request: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
code sip.StatusCode
|
||||
reason string
|
||||
}{
|
||||
{
|
||||
name: "100 Trying",
|
||||
code: sip.StatusTrying,
|
||||
reason: "Trying",
|
||||
},
|
||||
{
|
||||
name: "180 Ringing",
|
||||
code: sip.StatusRinging,
|
||||
reason: "Ringing",
|
||||
},
|
||||
{
|
||||
name: "200 OK",
|
||||
code: sip.StatusOK,
|
||||
reason: "OK",
|
||||
},
|
||||
{
|
||||
name: "404 Not Found",
|
||||
code: sip.StatusNotFound,
|
||||
reason: "Not Found",
|
||||
},
|
||||
{
|
||||
name: "500 Server Internal Error",
|
||||
code: sip.StatusInternalServerError,
|
||||
reason: "Server Internal Error",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resp := newResponse(req, tt.code, tt.reason)
|
||||
|
||||
if resp == nil {
|
||||
t.Fatal("Expected response to be non-nil")
|
||||
}
|
||||
|
||||
if resp.StatusCode != tt.code {
|
||||
t.Errorf("Expected status code %d, got %d", tt.code, resp.StatusCode)
|
||||
}
|
||||
|
||||
if resp.Reason != tt.reason {
|
||||
t.Errorf("Expected reason '%s', got '%s'", tt.reason, resp.Reason)
|
||||
}
|
||||
|
||||
// Check that To header has tag
|
||||
to := resp.To()
|
||||
if to == nil {
|
||||
t.Error("Expected To header to be set")
|
||||
} else {
|
||||
tag, ok := to.Params.Get("tag")
|
||||
if !ok || tag == "" {
|
||||
t.Error("Expected To header to have tag parameter")
|
||||
}
|
||||
// Check tag length is 10
|
||||
if len(tag) != 10 {
|
||||
t.Errorf("Expected tag length to be 10, got %d", len(tag))
|
||||
}
|
||||
}
|
||||
|
||||
// Check that Allow header is removed
|
||||
allow := resp.GetHeader("Allow")
|
||||
if allow != nil {
|
||||
t.Error("Expected Allow header to be removed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseConstants(t *testing.T) {
|
||||
if TIME_LAYOUT != "2024-01-01T00:00:00" {
|
||||
t.Errorf("Expected TIME_LAYOUT to be '2024-01-01T00:00:00', got '%s'", TIME_LAYOUT)
|
||||
}
|
||||
|
||||
if EXPIRES_TIME != 3600 {
|
||||
t.Errorf("Expected EXPIRES_TIME to be 3600, got %d", EXPIRES_TIME)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to check if a string contains a substring
|
||||
func contains(s, substr string) bool {
|
||||
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && findSubstring(s, substr))
|
||||
}
|
||||
|
||||
func findSubstring(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
197
pkg/utils/utils_test.go
Normal file
197
pkg/utils/utils_test.go
Normal file
@ -0,0 +1,197 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenRandomNumber(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
length int
|
||||
}{
|
||||
{"Generate 1 digit", 1},
|
||||
{"Generate 5 digits", 5},
|
||||
{"Generate 9 digits", 9},
|
||||
{"Generate 10 digits", 10},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := GenRandomNumber(tt.length)
|
||||
|
||||
// 验证长度
|
||||
if len(result) != tt.length {
|
||||
t.Errorf("Expected length %d, got %d", tt.length, len(result))
|
||||
}
|
||||
|
||||
// 验证所有字符都是数字
|
||||
for i, c := range result {
|
||||
if c < '0' || c > '9' {
|
||||
t.Errorf("Character at position %d is not a digit: %c", i, c)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenRandomNumberUniqueness(t *testing.T) {
|
||||
// 生成多个随机数,验证它们不完全相同(虽然理论上可能相同,但概率极低)
|
||||
results := make(map[string]bool)
|
||||
iterations := 100
|
||||
length := 10
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
result := GenRandomNumber(length)
|
||||
results[result] = true
|
||||
}
|
||||
|
||||
// 至少应该有一些不同的值(不太可能100次都生成相同的10位数)
|
||||
if len(results) < 50 {
|
||||
t.Errorf("Expected at least 50 unique values out of %d iterations, got %d", iterations, len(results))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSSRC(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
isLive bool
|
||||
expected byte
|
||||
}{
|
||||
{"Live stream SSRC", true, '0'},
|
||||
{"Non-live stream SSRC", false, '1'},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ssrc := CreateSSRC(tt.isLive)
|
||||
|
||||
// 验证长度为10
|
||||
if len(ssrc) != 10 {
|
||||
t.Errorf("Expected SSRC length 10, got %d", len(ssrc))
|
||||
}
|
||||
|
||||
// 验证第一个字符
|
||||
if ssrc[0] != tt.expected {
|
||||
t.Errorf("Expected first character '%c', got '%c'", tt.expected, ssrc[0])
|
||||
}
|
||||
|
||||
// 验证所有字符都是数字
|
||||
for i, c := range ssrc {
|
||||
if c < '0' || c > '9' {
|
||||
t.Errorf("Character at position %d is not a digit: %c", i, c)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSSRCUniqueness(t *testing.T) {
|
||||
// 测试生成的 SSRC 具有唯一性
|
||||
results := make(map[string]bool)
|
||||
iterations := 100
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
ssrc := CreateSSRC(true)
|
||||
results[ssrc] = true
|
||||
}
|
||||
|
||||
// 应该有很多不同的值
|
||||
if len(results) < 50 {
|
||||
t.Errorf("Expected at least 50 unique SSRCs out of %d iterations, got %d", iterations, len(results))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsVideoChannel(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
channelID string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "Video channel type 131",
|
||||
channelID: "34020000001310000001",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Video channel type 132",
|
||||
channelID: "34020000001320000001",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Audio channel type 137",
|
||||
channelID: "34020000001370000001",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Alarm channel type 134",
|
||||
channelID: "34020000001340000001",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Other device type",
|
||||
channelID: "34020000001110000001",
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := IsVideoChannel(tt.channelID)
|
||||
if result != tt.expected {
|
||||
t.Errorf("IsVideoChannel(%s) = %v, expected %v", tt.channelID, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSessionName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
playType int
|
||||
expected string
|
||||
}{
|
||||
{"Live play", 0, "Play"},
|
||||
{"Playback", 1, "Playback"},
|
||||
{"Download", 2, "Download"},
|
||||
{"Talk", 3, "Talk"},
|
||||
{"Unknown type", 99, "Play"},
|
||||
{"Negative type", -1, "Play"},
|
||||
{"Type 4", 4, "Play"},
|
||||
{"Type 5", 5, "Play"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := GetSessionName(tt.playType)
|
||||
if result != tt.expected {
|
||||
t.Errorf("GetSessionName(%d) = %s, expected %s", tt.playType, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenRandomNumberZeroLength(t *testing.T) {
|
||||
result := GenRandomNumber(0)
|
||||
if len(result) != 0 {
|
||||
t.Errorf("Expected empty string for length 0, got %s", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSSRCBothTypes(t *testing.T) {
|
||||
// Test both live and non-live in one test
|
||||
liveSSRC := CreateSSRC(true)
|
||||
nonLiveSSRC := CreateSSRC(false)
|
||||
|
||||
if liveSSRC[0] != '0' {
|
||||
t.Errorf("Live SSRC should start with '0', got '%c'", liveSSRC[0])
|
||||
}
|
||||
|
||||
if nonLiveSSRC[0] != '1' {
|
||||
t.Errorf("Non-live SSRC should start with '1', got '%c'", nonLiveSSRC[0])
|
||||
}
|
||||
|
||||
// They should be different (with very high probability)
|
||||
if liveSSRC == nonLiveSSRC {
|
||||
t.Error("Live and non-live SSRCs should be different")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user