@@ -72,26 +72,91 @@ func TestATarGzLaidOutForACutComesToEverySizeAndReads(t *testing.T) {
7272 }
7373}
7474
75+ // A tar.gz a damage cuts comes to every size from its smallest, with a label
76+ // and without.
77+ //
78+ // The frame for a cut costs a few bytes more than the smallest archive, so
79+ // near the smallest size an archive keeps the layout of one nothing cuts until
80+ // the frame fits. One byte over the frame's floor goes to the comment, as a
81+ // space behind the label. Without a label no field takes it, and that size
82+ // stopped the run with an internal defect (docs/REVIEW-240-2026-11-13.md). So
83+ // this writes the first sizes from the smallest, cut half way and not, holds
84+ // each to its size and to compress/gzip and archive/tar, and asserts that it
85+ // met the frame - and, only without a label, the size after its floor that
86+ // keeps the layout of an archive nothing cuts.
87+ func TestATarGzCutNearItsSmallestComesToEverySize (t * testing.T ) {
88+ desc , err := format .Get ("targz" )
89+ if err != nil {
90+ t .Fatal (err )
91+ }
92+ for _ , label := range []bool {false , true } {
93+ smallest := desc .SmallestAccepted (format.Request {Label : label })
94+ framed , plainAfter := false , false
95+ for size := smallest ; size < smallest + 16 ; size ++ {
96+ req := format.Request {Bytes : size , Label : label , Seed : 7 }
97+ if _ , err := desc .Generator .Plan (req ); err != nil {
98+ // The one size in the gap just above the smallest archive.
99+ continue
100+ }
101+ plain := writeRequest (t , desc , req )
102+ req .CutAt = size / 2
103+ cut := writeRequest (t , desc , req )
104+ if int64 (len (cut )) != size {
105+ t .Errorf ("label %v, %d B: a cut half way came to %d B" , label , size , len (cut ))
106+ continue
107+ }
108+ if _ , err := entriesRead (cut ); err != nil {
109+ t .Errorf ("label %v, %d B: %v" , label , size , err )
110+ }
111+ laid := ! bytes .Equal (cut , plain )
112+ plainAfter = plainAfter || framed && ! laid
113+ framed = framed || laid
114+ }
115+ if ! framed {
116+ t .Errorf ("label %v: none of the 16 sizes from %d B was laid out for a cut" , label , smallest )
117+ }
118+ if plainAfter != ! label {
119+ t .Errorf ("label %v: a size after the frame's floor kept the layout of an archive nothing cuts: %v - due only without a label" , label , plainAfter )
120+ }
121+ }
122+ }
123+
75124// writeWhole writes a tar.gz of size bytes laid out for a cut half way, whole.
76125func writeWhole (t * testing.T , desc format.Descriptor , size int64 ) []byte {
77126 t .Helper ()
78- p , err := desc .Generator .Plan (format.Request {Bytes : size , Label : true , Seed : 7 , CutAt : size / 2 })
127+ return writeRequest (t , desc , format.Request {Bytes : size , Label : true , Seed : 7 , CutAt : size / 2 })
128+ }
129+
130+ // writeRequest plans and writes req, whole.
131+ func writeRequest (t * testing.T , desc format.Descriptor , req format.Request ) []byte {
132+ t .Helper ()
133+ p , err := desc .Generator .Plan (req )
79134 if err != nil {
80- t .Fatalf ("%d B: planning: %v" , size , err )
135+ t .Fatalf ("%d B: planning: %v" , req . Bytes , err )
81136 }
82137 var out bytes.Buffer
83138 if err := desc .Generator .Write (t .Context (), & out , p ); err != nil {
84- t .Fatalf ("%d B: writing: %v" , size , err )
139+ t .Fatalf ("%d B: writing: %v" , req . Bytes , err )
85140 }
86141 return out .Bytes ()
87142}
88143
89- // readsEveryEntry reads every entry of a tar.gz to its end, through
90- // compress/gzip, which checks the trailer, and archive/tar .
144+ // readsEveryEntry reads every entry of a tar.gz laid out for a cut to its end,
145+ // and holds it to a file and padding beside it .
91146func readsEveryEntry (data []byte ) error {
147+ files , err := entriesRead (data )
148+ if err == nil && files < 2 {
149+ err = fmt .Errorf ("%d entries, where a file and padding beside it were due" , files )
150+ }
151+ return err
152+ }
153+
154+ // entriesRead reads every entry of a tar.gz to its end, through
155+ // compress/gzip, which checks the trailer, and archive/tar, and counts them.
156+ func entriesRead (data []byte ) (int , error ) {
92157 zr , err := gzip .NewReader (bytes .NewReader (data ))
93158 if err != nil {
94- return err
159+ return 0 , err
95160 }
96161 tr := tar .NewReader (zr )
97162 files := 0
@@ -101,20 +166,17 @@ func readsEveryEntry(data []byte) error {
101166 break
102167 }
103168 if err != nil {
104- return err
169+ return files , err
105170 }
106171 if _ , err := io .Copy (io .Discard , tr ); err != nil {
107- return fmt .Errorf ("%s: %w" , h .Name , err )
172+ return files , fmt .Errorf ("%s: %w" , h .Name , err )
108173 }
109174 files ++
110175 }
111176 if _ , err := io .Copy (io .Discard , zr ); err != nil {
112- return fmt .Errorf ("after the tar: %w" , err )
113- }
114- if files < 2 {
115- return fmt .Errorf ("%d entries, where a file and padding beside it were due" , files )
177+ return files , fmt .Errorf ("after the tar: %w" , err )
116178 }
117- return nil
179+ return files , nil
118180}
119181
120182// gzipStreamStart is where the deflate stream of a gzip member starts: after
0 commit comments