Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions keepcurrent_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package keepcurrent

import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/rand"
"encoding/json"
"io"
Expand Down Expand Up @@ -203,3 +205,69 @@ func TestBackoffOnFail(t *testing.T) {
assert.EqualValues(t, 1, atomic.LoadInt32(&updates))
assert.EqualValues(t, 1, atomic.LoadInt32(&finalFailures))
}

// bytesSource is a Source that returns a fixed byte slice every Fetch.
type bytesSource struct{ data []byte }

func (b *bytesSource) Fetch(time.Time) (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(b.data)), nil
}

// makeTarGz builds an in-memory .tar.gz containing a single file stored
// under the given path (e.g. "dir/file") so tests can exercise the basename-
// match path without shipping a fixture.
func makeTarGz(t *testing.T, storedPath string, payload []byte) []byte {
t.Helper()
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
tw := tar.NewWriter(gz)
if err := tw.WriteHeader(&tar.Header{
Name: storedPath,
Mode: 0644,
Size: int64(len(payload)),
Typeflag: tar.TypeReg,
ModTime: time.Now(),
}); err != nil {
t.Fatalf("tar header: %v", err)
}
if _, err := tw.Write(payload); err != nil {
t.Fatalf("tar write: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatalf("tar close: %v", err)
}
if err := gz.Close(); err != nil {
t.Fatalf("gz close: %v", err)
}
return buf.Bytes()
}

// Regression test for the two bugs fixed in PR #7: FromTarGz must extract
// a file whose stored path includes a directory prefix (as MaxMind ships
// its mmdb tarballs), matched by the basename the caller passes.
func TestFromTarGzBasenameMatch(t *testing.T) {
want := []byte("hello world")
tgz := makeTarGz(t, "GeoLite2-City_20260116/GeoLite2-City.mmdb", want)

src := FromTarGz(&bytesSource{data: tgz}, "GeoLite2-City.mmdb")
rc, err := src.Fetch(time.Time{})
assert.NoError(t, err, "Fetch should find the file by basename")
if err != nil {
return
}
defer rc.Close()
got, err := io.ReadAll(rc)
assert.NoError(t, err)
assert.Equal(t, want, got)
}

// When the requested file isn't in the archive we should return a clear
// error, rather than the silent "no extraction format" that hid the
// original regression.
func TestFromTarGzMissingFile(t *testing.T) {
tgz := makeTarGz(t, "some/other-file.txt", []byte("x"))
src := FromTarGz(&bytesSource{data: tgz}, "GeoLite2-City.mmdb")
_, err := src.Fetch(time.Time{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "not found")
}
11 changes: 9 additions & 2 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"os"
"path"
"sync"
"time"

Expand Down Expand Up @@ -92,14 +93,20 @@ func (s *tarGzSource) Fetch(ifNewerThan time.Time) (io.ReadCloser, error) {
}
defer rc.Close()

// archives.CompressedArchive.Extract() reads ca.Extraction (not ca.Archival),
// and returns "no extraction format" if it's nil. Setting Archival here was a
// bug that made every Fetch() fail silently.
format := archives.CompressedArchive{
Compression: archives.Gz{},
Archival: archives.Tar{},
Extraction: archives.Tar{},
}

var buf []byte
err = format.Extract(context.Background(), rc, func(ctx context.Context, info archives.FileInfo) error {
if info.NameInArchive == s.expectedName {
// NameInArchive is the full stored path (e.g. "GeoLite2-City_20260116/GeoLite2-City.mmdb").
// Callers pass the basename, so compare on basename — mirrors the behavior of the
// archiver/v3-based implementation this replaced.
if path.Base(info.NameInArchive) == s.expectedName {
Comment thread
myleshorton marked this conversation as resolved.
f, err := info.Open()
if err != nil {
return err
Expand Down
Loading