okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
2f6697a
main
1// Collection streams must validate whole batches before changing the client view. 2// A controlled peer supplies malformed rows and overflowing activity batches. 3package daemon 4 5import ( 6"fmt" 7"net/http" 8"testing" 9) 10 11func TestCollectionReadyOverflowAndAtomicValidation (t * testing.T ) { 12for _ ,bad := range []bool {false ,true } { 13t .Run (fmt .Sprint (bad ),func (t * testing.T ) { 14conn := controlledConnection (t ,func (w http.ResponseWriter ,r * http.Request ) { 15if r .URL .Path != "/sessions" || r .Header .Get ("Accept" )!= "text/event-stream" { 16t .Errorf ("wrong collection route %s" ,r .URL ) 17 } 18w .Header ().Set ("Content-Type" ,"text/event-stream" ) 19writeInitialStream (w ,"agents" ) 20if bad { 21writeBatch (w ,map [string ]any {"events" : []any {map [string ]any {"type" :"mail" ,"data" :map [string ]any {"mail_id" :"m" ,"sender_session_id" :nil ,"sender_label" :nil ,"receiver_session_id" :"s" ,"kind" :"message" ,"bytes" :1 }},map [string ]any {"type" :"invalidate" ,"data" :map [string ]any {"urls" : []any {},"session_ids" : []any {},"scope_dirty" :"false" }}}}) 22 }else { 23writeBatch (w ,map [string ]any {"events" : []any {map [string ]any {"type" :"overflow" ,"data" :map [string ]any {}}}}) 24 } 25 }) 26batches := 0 27err := StreamAgents (t .Context (),conn ,func ([]AgentEvent )error {batches ++ ;return nil }) 28expected := 2 29if bad { 30expected = 1 31 } 32if err == nil || batches != expected { 33t .Fatalf ("collection delivery changed: %v batches=%d" ,err ,batches ) 34 } 35 }) 36 } 37}