mirror of
https://github.com/livekit/livekit.git
synced 2026-12-05 10:58:09 +00:00
When a WHIP PATCH carries an If-Match that no longer matches the publisher's ICE session, the RTC node returns psrpc.FailedPrecondition, but iceTrickle had no case for that code and answered 500. RFC 9725 section 4.3.1 requires 412 Precondition Failed, which is also what psrpc's own ErrorCode.ToHTTP maps FailedPrecondition to. Also pass the error instead of nil to handleError in the non-psrpc branches of iceTrickle and iceRestart. handleError calls err.Error(), so a nil error would panic. The psrpc client wraps all of its own failures today, so this is hardening. Add a unit test for the PATCH status mapping and an integration test that drives a real WHIP session through trickle and ICE restart.
126 lines
4.3 KiB
Go
126 lines
4.3 KiB
Go
// Copyright 2026 LiveKit, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package test
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/pion/webrtc/v4"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
lksdp "github.com/livekit/protocol/sdp"
|
|
)
|
|
|
|
// RFC 9725 section 4.3.1: a PATCH without an entity-tag gets 428, one whose
|
|
// entity-tag does not match the current ICE session gets 412.
|
|
func TestWHIPPatchEntityTag(t *testing.T) {
|
|
_, finish := setupSingleNodeTest("TestWHIPPatchEntityTag")
|
|
defer finish()
|
|
|
|
pc, err := webrtc.NewPeerConnection(webrtc.Configuration{})
|
|
require.NoError(t, err)
|
|
defer pc.Close()
|
|
_, err = pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
|
require.NoError(t, err)
|
|
|
|
token := joinToken(testRoom, "whip-publisher", nil)
|
|
baseURL := fmt.Sprintf("http://localhost:%d", defaultServerPort)
|
|
|
|
do := func(method, path, contentType, ifMatch, body string) *http.Response {
|
|
req, err := http.NewRequest(method, baseURL+path, bytes.NewBufferString(body))
|
|
require.NoError(t, err)
|
|
req.Header.Set("Content-Type", contentType)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
if ifMatch != "" {
|
|
req.Header.Set("If-Match", ifMatch)
|
|
}
|
|
res, err := http.DefaultClient.Do(req)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { res.Body.Close() })
|
|
return res
|
|
}
|
|
|
|
// fragment with the publisher's current ICE credentials, mid and candidates
|
|
localFragment := func() string {
|
|
parsed, err := pc.LocalDescription().Unmarshal()
|
|
require.NoError(t, err)
|
|
frag, err := lksdp.ExtractSDPFragment(parsed)
|
|
require.NoError(t, err)
|
|
s, err := frag.Marshal()
|
|
require.NoError(t, err)
|
|
return s
|
|
}
|
|
|
|
offer, err := pc.CreateOffer(nil)
|
|
require.NoError(t, err)
|
|
gathered := webrtc.GatheringCompletePromise(pc)
|
|
require.NoError(t, pc.SetLocalDescription(offer))
|
|
<-gathered
|
|
|
|
res := do(http.MethodPost, "/whip/v1", "application/sdp", "", pc.LocalDescription().SDP)
|
|
answer, _ := io.ReadAll(res.Body)
|
|
require.Equal(t, http.StatusCreated, res.StatusCode, string(answer))
|
|
require.NoError(t, pc.SetRemoteDescription(webrtc.SessionDescription{Type: webrtc.SDPTypeAnswer, SDP: string(answer)}))
|
|
|
|
location := res.Header.Get("Location")
|
|
etag := res.Header.Get("ETag")
|
|
require.NotEmpty(t, location)
|
|
require.NotEmpty(t, etag)
|
|
|
|
patch := func(ifMatch, fragment string) *http.Response {
|
|
return do(http.MethodPatch, location, "application/trickle-ice-sdpfrag", ifMatch, fragment)
|
|
}
|
|
|
|
t.Run("missing entity-tag", func(t *testing.T) {
|
|
require.Equal(t, http.StatusPreconditionRequired, patch("", localFragment()).StatusCode)
|
|
})
|
|
|
|
t.Run("matching entity-tag", func(t *testing.T) {
|
|
require.Equal(t, http.StatusNoContent, patch(etag, localFragment()).StatusCode)
|
|
})
|
|
|
|
t.Run("non-matching entity-tag", func(t *testing.T) {
|
|
require.Equal(t, http.StatusPreconditionFailed, patch(etag+"-stale", localFragment()).StatusCode)
|
|
})
|
|
|
|
t.Run("entity-tag outdated by an ICE restart", func(t *testing.T) {
|
|
restartOffer, err := pc.CreateOffer(&webrtc.OfferOptions{ICERestart: true})
|
|
require.NoError(t, err)
|
|
gathered := webrtc.GatheringCompletePromise(pc)
|
|
require.NoError(t, pc.SetLocalDescription(restartOffer))
|
|
<-gathered
|
|
|
|
res := patch("*", localFragment())
|
|
body, _ := io.ReadAll(res.Body)
|
|
require.Equal(t, http.StatusOK, res.StatusCode, string(body))
|
|
newETag := res.Header.Get("ETag")
|
|
require.NotEmpty(t, newETag)
|
|
require.NotEqual(t, etag, newETag)
|
|
|
|
// a trickle that was sent before the restart arrives after it
|
|
require.Equal(t, http.StatusPreconditionFailed, patch(etag, localFragment()).StatusCode)
|
|
require.Equal(t, http.StatusNoContent, patch(newETag, localFragment()).StatusCode)
|
|
})
|
|
|
|
t.Run("unknown session", func(t *testing.T) {
|
|
res := do(http.MethodPatch, "/whip/v1/PA_unknown", "application/trickle-ice-sdpfrag", etag, localFragment())
|
|
require.Equal(t, http.StatusNotFound, res.StatusCode)
|
|
})
|
|
}
|