@@ -3,6 +3,8 @@ package core
33import (
44 "encoding/base64"
55 "encoding/json"
6+ "io"
7+ "strings"
68 "testing"
79
810 "github.com/go-jose/go-jose/v4"
@@ -80,3 +82,38 @@ func TestFingerprint(t *testing.T) {
8082 t .Errorf ("Incorrect SHA-256 fingerprint: %v" , digest )
8183 }
8284}
85+
86+ func TestErrOnLimitReader (t * testing.T ) {
87+ // test a read where the limit is larger than the source
88+ strReader := strings .NewReader ("foo bar baz bot" )
89+ lmr := ErrOnLimitReader (strReader , 21 )
90+
91+ strOut01 , err := io .ReadAll (lmr )
92+
93+ // should return no err, and the full source
94+ test .AssertNotError (t , err , "unexpectedly errored" )
95+ test .AssertEquals (t , string (strOut01 ), "foo bar baz bot" )
96+
97+ // test a read where the limit exactly matches source size
98+ strReader = strings .NewReader ("foo bar baz bot" )
99+ lmr = ErrOnLimitReader (strReader , 15 )
100+
101+ strOut02 , err := io .ReadAll (lmr )
102+
103+ // should return an error, and the full source
104+ test .AssertError (t , err , "unexpectedly succeeded" )
105+ test .AssertEquals (t , err , ErrReaderLimitReached )
106+ test .AssertEquals (t , string (strOut02 ), "foo bar baz bot" )
107+
108+ // test a read where the limit is lower than the source
109+ strReader = strings .NewReader ("foo bar baz bot" )
110+ lmr = ErrOnLimitReader (strReader , 9 )
111+
112+ strOut03 , err := io .ReadAll (lmr )
113+
114+ // should return an error, and a partial read result
115+ test .AssertError (t , err , "unexpectedly succeeded" )
116+ test .AssertEquals (t , err , ErrReaderLimitReached )
117+ test .AssertEquals (t , string (strOut03 ), "foo bar b" )
118+
119+ }
0 commit comments