forked from AdguardTeam/AdGuardHome
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be56df7
commit 2e57624
Showing
5 changed files
with
197 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,155 @@ | ||
package filtering | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/netip" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/AdguardTeam/AdGuardHome/internal/aghnet" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDNSFilter_handleFilteringSetURL(t *testing.T) { | ||
filtersDir := t.TempDir() | ||
|
||
var goodRulesEndpoint, anotherGoodRulesEndpoint, badRulesEndpoint string | ||
for _, rulesSource := range []struct { | ||
endpoint *string | ||
content []byte | ||
}{{ | ||
endpoint: &goodRulesEndpoint, | ||
content: []byte(`||example.org^`), | ||
}, { | ||
endpoint: &anotherGoodRulesEndpoint, | ||
content: []byte(`||example.com^`), | ||
}, { | ||
endpoint: &badRulesEndpoint, | ||
content: []byte(`<html></html>`), | ||
}} { | ||
rulesSource := rulesSource | ||
listener := testStartFilterListener(t, &rulesSource.content) | ||
|
||
addr := listener.Addr() | ||
require.IsType(t, new(net.TCPAddr), addr) | ||
port := addr.(*net.TCPAddr).AddrPort().Port() | ||
|
||
*rulesSource.endpoint = (&url.URL{ | ||
Scheme: "http", | ||
Host: netip.AddrPortFrom(aghnet.IPv4Localhost(), port).String(), | ||
}).String() | ||
} | ||
|
||
testCases := []struct { | ||
name string | ||
wantBody string | ||
oldURL string | ||
newName string | ||
newURL string | ||
initial []FilterYAML | ||
}{{ | ||
name: "success", | ||
wantBody: "", | ||
oldURL: goodRulesEndpoint, | ||
newName: "default_one", | ||
newURL: anotherGoodRulesEndpoint, | ||
initial: []FilterYAML{{ | ||
Enabled: true, | ||
URL: goodRulesEndpoint, | ||
Name: "default_one", | ||
white: false, | ||
}}, | ||
}, { | ||
name: "non-existing", | ||
wantBody: "url doesn't exist\n", | ||
oldURL: anotherGoodRulesEndpoint, | ||
newName: "default_one", | ||
newURL: goodRulesEndpoint, | ||
initial: []FilterYAML{{ | ||
Enabled: true, | ||
URL: goodRulesEndpoint, | ||
Name: "default_one", | ||
white: false, | ||
}}, | ||
}, { | ||
name: "existing", | ||
wantBody: "url already exists\n", | ||
oldURL: goodRulesEndpoint, | ||
newName: "default_one", | ||
newURL: anotherGoodRulesEndpoint, | ||
initial: []FilterYAML{{ | ||
Enabled: true, | ||
URL: goodRulesEndpoint, | ||
Name: "default_one", | ||
white: false, | ||
}, { | ||
Enabled: true, | ||
URL: anotherGoodRulesEndpoint, | ||
Name: "another_default_one", | ||
white: false, | ||
}}, | ||
}, { | ||
name: "bad_rules", | ||
wantBody: "data is HTML, not plain text\n", | ||
oldURL: goodRulesEndpoint, | ||
newName: "default_one", | ||
newURL: badRulesEndpoint, | ||
initial: []FilterYAML{{ | ||
Enabled: true, | ||
URL: goodRulesEndpoint, | ||
Name: "default_one", | ||
white: false, | ||
}}, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
configModified := false | ||
d, err := New(&Config{ | ||
FilteringEnabled: true, | ||
Filters: tc.initial, | ||
HTTPClient: &http.Client{ | ||
Timeout: 5 * time.Second, | ||
}, | ||
ConfigModified: func() { configModified = true }, | ||
DataDir: filtersDir, | ||
}, nil) | ||
require.NoError(t, err) | ||
t.Cleanup(d.Close) | ||
|
||
d.Start() | ||
|
||
reqData := &filterURLReq{ | ||
Data: &filterURLReqData{ | ||
// Leave the name of an existing list. | ||
Name: tc.newName, | ||
URL: tc.newURL, | ||
Enabled: true, | ||
}, | ||
URL: tc.oldURL, | ||
Whitelist: false, | ||
} | ||
data, err := json.Marshal(reqData) | ||
require.NoError(t, err) | ||
|
||
r := httptest.NewRequest(http.MethodPost, "http://example.org", bytes.NewReader(data)) | ||
w := httptest.NewRecorder() | ||
|
||
d.handleFilteringSetURL(w, r) | ||
assert.Equal(t, tc.wantBody, w.Body.String()) | ||
if tc.wantBody != "" { | ||
assert.False(t, configModified) | ||
} else { | ||
assert.True(t, configModified) | ||
} | ||
}) | ||
} | ||
} |