@@ -2,6 +2,7 @@ package server
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"crypto/sha256"
6
7
"encoding/hex"
7
8
"encoding/json"
@@ -506,3 +507,59 @@ func TestRemoteReturnsNotFound(t *testing.T) {
506
507
t .Errorf ("Wrong status code, expected %d, got %d" , http .StatusNotFound , statusCode )
507
508
}
508
509
}
510
+
511
+ func TestManglingACKeys (t * testing.T ) {
512
+ cacheDir , err := os .MkdirTemp ("" , "bazel-remote" )
513
+ if err != nil {
514
+ t .Fatal (err )
515
+ }
516
+ defer os .RemoveAll (cacheDir )
517
+
518
+ blobSize := int64 (1024 )
519
+ cacheSize := blobSize * 2 + disk .BlockSize
520
+ diskCache , err := disk .New (cacheDir , cacheSize , disk .WithAccessLogger (testutils .NewSilentLogger ()))
521
+ if err != nil {
522
+ t .Fatal (err )
523
+ }
524
+
525
+ h := NewHTTPCache (diskCache , testutils .NewSilentLogger (), testutils .NewSilentLogger (), false , true , false , false , "" )
526
+ // create a fake http.Request
527
+ data , hash := testutils .RandomDataAndHash (blobSize )
528
+ err = diskCache .Put (context .Background (), cache .RAW , hash , int64 (len (data )), bytes .NewReader (data ))
529
+ if err != nil {
530
+ t .Fatal (err )
531
+ }
532
+
533
+ url , _ := url .Parse (fmt .Sprintf ("http://localhost:8080/ac/%s" , hash ))
534
+ reader := bytes .NewReader ([]byte {})
535
+ body := io .NopCloser (reader )
536
+ req := & http.Request {
537
+ Method : "GET" ,
538
+ URL : url ,
539
+ Proto : "HTTP/1.1" ,
540
+ ProtoMajor : 1 ,
541
+ ProtoMinor : 1 ,
542
+ Body : body ,
543
+ }
544
+ statusCode := 0
545
+ respWriter := & fakeResponseWriter {
546
+ statusCode : & statusCode ,
547
+ }
548
+ h .CacheHandler (respWriter , req )
549
+ if statusCode != 0 {
550
+ t .Errorf ("Wrong status code, expected %d, got %d" , 0 , statusCode )
551
+ }
552
+
553
+ url , _ = url .Parse (fmt .Sprintf ("http://localhost:8080/test-instance/ac/%s" , hash ))
554
+ reader .Reset ([]byte {})
555
+ body .Close ()
556
+ body = io .NopCloser (reader )
557
+ req .URL = url
558
+ req .Body = body
559
+ statusCode = 0
560
+
561
+ h .CacheHandler (respWriter , req )
562
+ if statusCode != http .StatusNotFound {
563
+ t .Errorf ("Wrong status code, expected %d, got %d" , http .StatusNotFound , statusCode )
564
+ }
565
+ }
0 commit comments