2021-01-29 08:55:55 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-22 16:41:10 +00:00
|
|
|
"github.com/aler9/gortsplib"
|
2021-01-29 08:55:55 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/crypto/nacl/secretbox"
|
2021-10-05 18:36:15 +00:00
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2021-01-29 08:55:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func writeTempFile(byts []byte) (string, error) {
|
|
|
|
tmpf, err := ioutil.TempFile(os.TempDir(), "rtsp-")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer tmpf.Close()
|
|
|
|
|
|
|
|
_, err = tmpf.Write(byts)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpf.Name(), nil
|
|
|
|
}
|
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
func TestConfFromFile(t *testing.T) {
|
2021-09-27 12:59:27 +00:00
|
|
|
func() {
|
2021-10-05 18:36:15 +00:00
|
|
|
tmpf, err := writeTempFile([]byte("logLevel: debug\n" +
|
2021-09-27 12:59:27 +00:00
|
|
|
"paths:\n" +
|
2021-10-05 18:36:15 +00:00
|
|
|
" cam1:\n" +
|
|
|
|
" runOnDemandStartTimeout: 5s\n"))
|
2021-09-27 12:59:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
|
|
|
|
|
|
|
conf, hasFile, err := Load(tmpf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, hasFile)
|
|
|
|
|
2021-10-05 18:36:15 +00:00
|
|
|
require.Equal(t, LogLevel(logger.Debug), conf.LogLevel)
|
|
|
|
|
2021-09-27 12:59:27 +00:00
|
|
|
pa, ok := conf.Paths["cam1"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, &PathConf{
|
|
|
|
Source: "publisher",
|
|
|
|
SourceOnDemandStartTimeout: 10 * StringDuration(time.Second),
|
|
|
|
SourceOnDemandCloseAfter: 10 * StringDuration(time.Second),
|
|
|
|
RunOnDemandStartTimeout: 5 * StringDuration(time.Second),
|
|
|
|
RunOnDemandCloseAfter: 10 * StringDuration(time.Second),
|
|
|
|
}, pa)
|
|
|
|
}()
|
2021-09-26 21:06:40 +00:00
|
|
|
|
2021-09-27 12:59:27 +00:00
|
|
|
func() {
|
|
|
|
tmpf, err := writeTempFile([]byte(``))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
2021-09-26 21:06:40 +00:00
|
|
|
|
2021-09-27 12:59:27 +00:00
|
|
|
_, _, err = Load(tmpf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
tmpf, err := writeTempFile([]byte(`paths:`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
|
|
|
|
|
|
|
_, _, err = Load(tmpf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
tmpf, err := writeTempFile([]byte(
|
|
|
|
"paths:\n" +
|
|
|
|
" mypath:\n"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
|
|
|
|
|
|
|
_, _, err = Load(tmpf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
2021-09-26 21:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfFromFileAndEnv(t *testing.T) {
|
2021-01-29 08:55:55 +00:00
|
|
|
os.Setenv("RTSP_PATHS_CAM1_SOURCE", "rtsp://testing")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCE")
|
|
|
|
|
2021-10-04 06:58:08 +00:00
|
|
|
os.Setenv("RTSP_PROTOCOLS", "tcp")
|
|
|
|
defer os.Unsetenv("RTSP_PROTOCOLS")
|
|
|
|
|
2021-01-29 08:55:55 +00:00
|
|
|
tmpf, err := writeTempFile([]byte("{}"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
|
|
|
|
|
|
|
conf, hasFile, err := Load(tmpf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, hasFile)
|
|
|
|
|
2021-10-22 16:41:10 +00:00
|
|
|
require.Equal(t, Protocols{Protocol(gortsplib.TransportTCP): {}}, conf.Protocols)
|
2021-10-04 06:58:08 +00:00
|
|
|
|
2021-05-09 15:51:38 +00:00
|
|
|
pa, ok := conf.Paths["cam1"]
|
2021-01-29 08:55:55 +00:00
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, &PathConf{
|
2021-05-09 15:51:38 +00:00
|
|
|
Source: "rtsp://testing",
|
2021-09-26 21:06:40 +00:00
|
|
|
SourceOnDemandStartTimeout: 10 * StringDuration(time.Second),
|
|
|
|
SourceOnDemandCloseAfter: 10 * StringDuration(time.Second),
|
|
|
|
RunOnDemandStartTimeout: 10 * StringDuration(time.Second),
|
|
|
|
RunOnDemandCloseAfter: 10 * StringDuration(time.Second),
|
2021-01-29 08:55:55 +00:00
|
|
|
}, pa)
|
|
|
|
}
|
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
func TestConfFromEnvOnly(t *testing.T) {
|
2021-01-29 08:55:55 +00:00
|
|
|
os.Setenv("RTSP_PATHS_CAM1_SOURCE", "rtsp://testing")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCE")
|
|
|
|
|
|
|
|
conf, hasFile, err := Load("rtsp-simple-server.yml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, false, hasFile)
|
|
|
|
|
|
|
|
pa, ok := conf.Paths["cam1"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, &PathConf{
|
|
|
|
Source: "rtsp://testing",
|
2021-09-26 21:06:40 +00:00
|
|
|
SourceOnDemandStartTimeout: 10 * StringDuration(time.Second),
|
|
|
|
SourceOnDemandCloseAfter: 10 * StringDuration(time.Second),
|
|
|
|
RunOnDemandStartTimeout: 10 * StringDuration(time.Second),
|
|
|
|
RunOnDemandCloseAfter: 10 * StringDuration(time.Second),
|
2021-01-29 08:55:55 +00:00
|
|
|
}, pa)
|
|
|
|
}
|
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
func TestConfEncryption(t *testing.T) {
|
2021-01-29 08:55:55 +00:00
|
|
|
key := "testing123testin"
|
2021-09-27 12:59:27 +00:00
|
|
|
plaintext := "paths:\n" +
|
|
|
|
" path1:\n" +
|
|
|
|
" path2:\n"
|
2021-01-29 08:55:55 +00:00
|
|
|
|
|
|
|
encryptedConf := func() string {
|
|
|
|
var secretKey [32]byte
|
2021-03-20 13:14:41 +00:00
|
|
|
copy(secretKey[:], key)
|
2021-01-29 08:55:55 +00:00
|
|
|
|
|
|
|
var nonce [24]byte
|
|
|
|
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypted := secretbox.Seal(nonce[:], []byte(plaintext), &nonce, &secretKey)
|
|
|
|
return base64.StdEncoding.EncodeToString(encrypted)
|
|
|
|
}()
|
|
|
|
|
|
|
|
os.Setenv("RTSP_CONFKEY", key)
|
|
|
|
defer os.Unsetenv("RTSP_CONFKEY")
|
|
|
|
|
|
|
|
tmpf, err := writeTempFile([]byte(encryptedConf))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
|
|
|
|
|
|
|
conf, hasFile, err := Load(tmpf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, hasFile)
|
|
|
|
|
|
|
|
_, ok := conf.Paths["path1"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
|
|
|
|
_, ok = conf.Paths["path2"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
}
|
2021-09-27 12:59:27 +00:00
|
|
|
|
|
|
|
func TestConfErrorNonExistentParameter(t *testing.T) {
|
|
|
|
func() {
|
|
|
|
tmpf, err := writeTempFile([]byte(`invalid: param`))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
|
|
|
|
|
|
|
_, _, err = Load(tmpf)
|
2021-11-03 16:52:47 +00:00
|
|
|
require.EqualError(t, err, "non-existent parameter: 'invalid'")
|
2021-09-27 12:59:27 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
tmpf, err := writeTempFile([]byte("paths:\n" +
|
|
|
|
" mypath:\n" +
|
|
|
|
" invalid: parameter\n"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpf)
|
|
|
|
|
|
|
|
_, _, err = Load(tmpf)
|
2021-11-03 16:52:47 +00:00
|
|
|
require.EqualError(t, err, "parameter paths, key mypath: non-existent parameter: 'invalid'")
|
2021-09-27 12:59:27 +00:00
|
|
|
}()
|
|
|
|
}
|