66 "encoding/json"
77 "errors"
88 "fmt"
9+ "io"
910 "os"
1011 "path/filepath"
1112 "reflect"
@@ -139,6 +140,100 @@ func TestClaimsListScansDoNotCreateLocksOrMutateState(t *testing.T) {
139140 }
140141}
141142
143+ func TestClaimsListEnforcesInclusiveClaimFileSizeLimit (t * testing.T ) {
144+ t .Setenv ("XDG_STATE_HOME" , t .TempDir ())
145+ writeClaimsListFixture (t , "cbx_partial.json" , leaseClaim {LeaseID : "cbx_partial" , Provider : "local-container" })
146+ writeClaimsListRawFixture (t , "cbx_exact.json" , sizedClaimsListJSON (t , "cbx_exact" , int (maxLocalClaimInventoryFileBytes )))
147+ writeClaimsListRawFixture (t , "cbx_large_valid.json" , sizedClaimsListJSON (t , "cbx_large_valid" , int (maxLocalClaimInventoryFileBytes + 1 )))
148+ writeClaimsListRawFixture (t , "cbx_large_malformed.json" , bytes .Repeat ([]byte ("{" ), int (maxLocalClaimInventoryFileBytes + 1 )))
149+
150+ stdout , stderr , err := runClaimsList (t , "--json" )
151+ assertClaimsExitCode (t , err , 2 )
152+ if stderr != "" {
153+ t .Fatalf ("stderr=%q" , stderr )
154+ }
155+ var output localClaimsListOutput
156+ if err := json .Unmarshal ([]byte (stdout ), & output ); err != nil {
157+ t .Fatalf ("decode output: %v\n %s" , err , stdout )
158+ }
159+ if len (output .Claims ) != 2 || output .Claims [0 ].LeaseID != "cbx_exact" || output .Claims [1 ].LeaseID != "cbx_partial" {
160+ t .Fatalf ("partial claims=%#v" , output .Claims )
161+ }
162+ wantProblems := []localClaimProblem {
163+ {File : "cbx_large_malformed.json" , Code : "claim_too_large" , Message : "claim file exceeds the 1 MiB inventory limit" },
164+ {File : "cbx_large_valid.json" , Code : "claim_too_large" , Message : "claim file exceeds the 1 MiB inventory limit" },
165+ }
166+ if ! reflect .DeepEqual (output .Problems , wantProblems ) {
167+ t .Fatalf ("problems=%#v want=%#v" , output .Problems , wantProblems )
168+ }
169+
170+ human , humanStderr , humanErr := runClaimsList (t )
171+ assertClaimsExitCode (t , humanErr , 2 )
172+ if humanStderr != "" {
173+ t .Fatalf ("human stderr=%q" , humanStderr )
174+ }
175+ for _ , want := range []string {
176+ `leaseId: "cbx_exact"` ,
177+ `leaseId: "cbx_partial"` ,
178+ `"cbx_large_malformed.json": claim file exceeds the 1 MiB inventory limit (claim_too_large)` ,
179+ `"cbx_large_valid.json": claim file exceeds the 1 MiB inventory limit (claim_too_large)` ,
180+ } {
181+ if ! strings .Contains (human , want ) {
182+ t .Fatalf ("human output missing %q:\n %s" , want , human )
183+ }
184+ }
185+ stateDir , err := crabboxStateDir ()
186+ if err != nil {
187+ t .Fatal (err )
188+ }
189+ if _ , err := os .Stat (filepath .Join (stateDir , "claim-locks" )); ! errors .Is (err , os .ErrNotExist ) {
190+ t .Fatalf ("claim-locks created or unreadable: %v" , err )
191+ }
192+ }
193+
194+ func TestClaimsListSizeLimitDoesNotChangeRuntimeClaimReader (t * testing.T ) {
195+ t .Setenv ("XDG_STATE_HOME" , t .TempDir ())
196+ const leaseID = "cbx_runtime_large"
197+ writeClaimsListRawFixture (t , leaseID + ".json" , sizedClaimsListJSON (t , leaseID , int (maxLocalClaimInventoryFileBytes + 1 )))
198+
199+ stdout , _ , inventoryErr := runClaimsList (t , "--json" )
200+ assertClaimsExitCode (t , inventoryErr , 2 )
201+ var output localClaimsListOutput
202+ if err := json .Unmarshal ([]byte (stdout ), & output ); err != nil {
203+ t .Fatal (err )
204+ }
205+ if len (output .Claims ) != 0 || len (output .Problems ) != 1 || output .Problems [0 ].Code != "claim_too_large" {
206+ t .Fatalf ("inventory output=%#v" , output )
207+ }
208+ stateDir , err := crabboxStateDir ()
209+ if err != nil {
210+ t .Fatal (err )
211+ }
212+ if _ , err := os .Stat (filepath .Join (stateDir , "claim-locks" )); ! errors .Is (err , os .ErrNotExist ) {
213+ t .Fatalf ("inventory created claim locks: %v" , err )
214+ }
215+
216+ claim , err := readLeaseClaim (leaseID )
217+ if err != nil {
218+ t .Fatalf ("runtime reader rejected compatible large claim: %v" , err )
219+ }
220+ if claim .LeaseID != leaseID {
221+ t .Fatalf ("runtime claim=%#v" , claim )
222+ }
223+ }
224+
225+ func TestLocalClaimInventoryBoundsUnknownOrGrowingRead (t * testing.T ) {
226+ reader := & claimsListRepeatingReader {remaining : 1 * 1024 * 1024 * 1024 }
227+ data , tooLarge , err := readLocalClaimInventoryData (reader )
228+ if err != nil {
229+ t .Fatal (err )
230+ }
231+ wantRead := maxLocalClaimInventoryFileBytes + 1
232+ if ! tooLarge || int64 (len (data )) != wantRead || reader .read != wantRead {
233+ t .Fatalf ("tooLarge=%t bytes=%d sourceRead=%d want=%d" , tooLarge , len (data ), reader .read , wantRead )
234+ }
235+ }
236+
142237func TestClaimsListClaimStoreFailureIsNotMalformedPartialOutput (t * testing.T ) {
143238 stateRoot := t .TempDir ()
144239 t .Setenv ("XDG_STATE_HOME" , stateRoot )
@@ -280,6 +375,7 @@ func TestClaimsListConcurrentDeleteAndChangeProduceDeterministicProblems(t *test
280375 t .Setenv ("XDG_STATE_HOME" , t .TempDir ())
281376 writeClaimsListFixture (t , "cbx_change.json" , leaseClaim {LeaseID : "cbx_change" , Slug : "before" })
282377 writeClaimsListFixture (t , "cbx_delete.json" , leaseClaim {LeaseID : "cbx_delete" })
378+ writeClaimsListFixture (t , "cbx_grow.json" , leaseClaim {LeaseID : "cbx_grow" })
283379 writeClaimsListFixture (t , "cbx_valid.json" , leaseClaim {LeaseID : "cbx_valid" , Provider : "local-container" })
284380
285381 reader := func (path , leaseID string , expected os.FileInfo ) (leaseClaim , bool , error ) {
@@ -291,6 +387,8 @@ func TestClaimsListConcurrentDeleteAndChangeProduceDeterministicProblems(t *test
291387 }
292388 case "cbx_delete" :
293389 mutate = func () error { return os .Remove (path ) }
390+ case "cbx_grow" :
391+ mutate = func () error { return os .Truncate (path , maxLocalClaimInventoryFileBytes + 1 ) }
294392 }
295393 if mutate != nil {
296394 result := make (chan error , 1 )
@@ -310,7 +408,7 @@ func TestClaimsListConcurrentDeleteAndChangeProduceDeterministicProblems(t *test
310408 if len (output .Claims ) != 1 || output .Claims [0 ].LeaseID != "cbx_valid" {
311409 t .Fatalf ("partial claims=%#v" , output .Claims )
312410 }
313- if len (output .Problems ) != 2 || len (output .Problems ) > maxLocalClaimProblems {
411+ if len (output .Problems ) != 3 || len (output .Problems ) > maxLocalClaimProblems {
314412 t .Fatalf ("problems=%#v" , output .Problems )
315413 }
316414 for i , problem := range output .Problems {
@@ -570,6 +668,41 @@ func writeClaimsListRawFixture(t *testing.T, name string, data []byte) {
570668 }
571669}
572670
671+ func sizedClaimsListJSON (t * testing.T , leaseID string , size int ) []byte {
672+ t .Helper ()
673+ prefix := []byte (fmt .Sprintf (`{"leaseID":%q,"padding":"` , leaseID ))
674+ suffix := []byte (`"}` )
675+ if size < len (prefix )+ len (suffix ) {
676+ t .Fatalf ("claim fixture size %d is too small" , size )
677+ }
678+ data := make ([]byte , 0 , size )
679+ data = append (data , prefix ... )
680+ data = append (data , bytes .Repeat ([]byte ("x" ), size - len (prefix )- len (suffix ))... )
681+ data = append (data , suffix ... )
682+ return data
683+ }
684+
685+ type claimsListRepeatingReader struct {
686+ remaining int64
687+ read int64
688+ }
689+
690+ func (r * claimsListRepeatingReader ) Read (p []byte ) (int , error ) {
691+ if r .remaining == 0 {
692+ return 0 , io .EOF
693+ }
694+ n := int64 (len (p ))
695+ if n > r .remaining {
696+ n = r .remaining
697+ }
698+ for i := range p [:n ] {
699+ p [i ] = 'x'
700+ }
701+ r .remaining -= n
702+ r .read += n
703+ return int (n ), nil
704+ }
705+
573706func makeClaimsListDirectoryFixture (t * testing.T , name string ) {
574707 t .Helper ()
575708 dir , err := crabboxStateDir ()
0 commit comments