Repository navigation
fix(filterlists): clean up obsolete entries and rebuild bloom filters on base list updates #2300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package filterlists | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/go-version" | ||
|
|
||
| "github.com/safing/portmaster/service/updates" | ||
| ) | ||
|
|
||
| func TestResolveUpdateOrder(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const ( | ||
| vZero = "0.0.0" | ||
| v20260925 = "20260925.0.0" | ||
| v20260927 = "20260927.0.0" | ||
| v20260930 = "20260930.21.5" | ||
| v20261001 = "20261001.0.0" | ||
| v20261004 = "20261004.0.0" | ||
| v20261005 = "20261005.0.0" | ||
| ) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| files map[string]string // filename -> version | ||
| cacheDBVersion string | ||
| expected []string | ||
| }{ | ||
| { | ||
| name: "fresh install, base newer than diffs", | ||
| files: map[string]string{ | ||
| baseListFilePath: v20261001, | ||
| intermediateListFilePath: v20260927, | ||
| urgentListFilePath: v20260930, | ||
| }, | ||
| cacheDBVersion: vZero, | ||
| expected: []string{baseListFilePath}, | ||
| }, | ||
| { | ||
| name: "fresh install, diffs newer than base", | ||
| files: map[string]string{ | ||
| baseListFilePath: v20261001, | ||
| intermediateListFilePath: v20261004, | ||
| urgentListFilePath: v20261005, | ||
| }, | ||
| cacheDBVersion: vZero, | ||
| expected: []string{baseListFilePath, intermediateListFilePath, urgentListFilePath}, | ||
| }, | ||
| { | ||
| name: "only urgent is newer", | ||
| files: map[string]string{ | ||
| baseListFilePath: v20261001, | ||
| intermediateListFilePath: v20261004, | ||
| urgentListFilePath: v20261005, | ||
| }, | ||
| cacheDBVersion: v20261004, | ||
| expected: []string{urgentListFilePath}, | ||
| }, | ||
| { | ||
| name: "diffs newer than base already applied", | ||
| files: map[string]string{ | ||
| baseListFilePath: v20261001, | ||
| intermediateListFilePath: v20261004, | ||
| urgentListFilePath: v20261005, | ||
| }, | ||
| cacheDBVersion: v20261001, | ||
| expected: []string{intermediateListFilePath, urgentListFilePath}, | ||
| }, | ||
| { | ||
| name: "new base starts the update", | ||
| files: map[string]string{ | ||
| baseListFilePath: v20261001, | ||
| intermediateListFilePath: v20261004, | ||
| }, | ||
| cacheDBVersion: v20260925, | ||
| expected: []string{baseListFilePath, intermediateListFilePath}, | ||
| }, | ||
| { | ||
| name: "new base skips older diffs", | ||
| files: map[string]string{ | ||
| baseListFilePath: v20261001, | ||
| intermediateListFilePath: v20260927, | ||
| urgentListFilePath: v20260930, | ||
| }, | ||
| cacheDBVersion: v20260925, | ||
| expected: []string{baseListFilePath}, | ||
| }, | ||
| { | ||
| name: "everything already applied", | ||
| files: map[string]string{ | ||
| baseListFilePath: v20261001, | ||
| intermediateListFilePath: v20261004, | ||
| urgentListFilePath: v20261005, | ||
| }, | ||
| cacheDBVersion: v20261005, | ||
| expected: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cacheDBVersion, err := version.NewSemver(tc.cacheDBVersion) | ||
| if err != nil { | ||
| t.Fatalf("invalid cache database version %q: %s", tc.cacheDBVersion, err) | ||
| } | ||
|
|
||
| result, err := resolveUpdateOrder(makeTestArtifacts(t, tc.files), cacheDBVersion) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %s", err) | ||
| } | ||
|
|
||
| resultNames := make([]string, 0, len(result)) | ||
| for _, file := range result { | ||
| resultNames = append(resultNames, file.Filename) | ||
| } | ||
| if !slices.Equal(resultNames, tc.expected) { | ||
| t.Errorf("expected %v, got %v", tc.expected, resultNames) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // makeTestArtifacts creates artifacts with parsed versions by going through | ||
| // updates.ParseIndex, as the parsed version of an artifact is not exported. | ||
| func makeTestArtifacts(t *testing.T, files map[string]string) []*updates.Artifact { | ||
| t.Helper() | ||
|
|
||
| index := updates.Index{Name: "test", Version: "1.0.0"} | ||
| for filename, fileVersion := range files { | ||
| index.Artifacts = append(index.Artifacts, &updates.Artifact{ | ||
| Filename: filename, | ||
| Version: fileVersion, | ||
| }) | ||
| } | ||
|
|
||
| indexJSON, err := json.Marshal(&index) | ||
| if err != nil { | ||
| t.Fatalf("failed to marshal test index: %s", err) | ||
| } | ||
|
|
||
| parsed, err := updates.ParseIndex(indexJSON, "", nil) | ||
| if err != nil { | ||
| t.Fatalf("failed to parse test index: %s", err) | ||
| } | ||
|
|
||
| return parsed.Artifacts | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: safing/portmaster
Length of output: 9315
🏁 Script executed:
Repository: safing/portmaster
Length of output: 30289
🏁 Script executed:
Repository: safing/portmaster
Length of output: 30123
🏁 Script executed:
Repository: safing/portmaster
Length of output: 8259
Do not fall back to
v0.0.0after a cache-version read failure.When the cache is loaded but
getCacheDatabaseVersion()returns a non-ErrNotFounderror, the updater can select every fetched artifact as newer. A base artifact then rebuilds the Bloom filters and cleanup can remove entries from an already-applied diff that was not fetched. The updater can also persist the lower highest fetched version. Return the read error instead of applying a partial update.Suggested fix
🤖 Prompt for AI Agents