Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #82 from go-kivik/escFix3
Browse files Browse the repository at this point in the history
Fix database escaping
  • Loading branch information
flimzy authored Nov 10, 2020
2 parents 12809e5 + 60839c2 commit 18f5411
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
7 changes: 5 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ vendor:

lint:
stage: test
image: golang:1.14
image: golangci/golangci-lint:v1.30
services: []
before_script:
- ''
script:
- go mod download
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.7
- golangci-lint run ./...

go-1.9:
Expand Down Expand Up @@ -71,6 +70,10 @@ go-1.14:
<<: *test_template
image: golang:1.14

go-1.15:
<<: *test_template
image: golang:1.15

go-rc:
<<: *test_template
image: golang:rc
Expand Down
2 changes: 1 addition & 1 deletion .golangci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ format = "colored-line-number"

[linters]
enable = [
"interfacer", "gocyclo", "unconvert", "goimports", "unused", "varcheck",
"gocyclo", "unconvert", "goimports", "unused", "varcheck",
"vetshadow", "misspell", "nakedret", "errcheck", "golint", "ineffassign",
"deadcode", "goconst", "vet", "unparam", "gofmt"
]
56 changes: 39 additions & 17 deletions client/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"net/http"
"net/url"
"path/filepath"
"strings"
"testing"
"time"

kivik "github.com/go-kivik/kivik/v3"
Expand Down Expand Up @@ -40,12 +42,15 @@ func testReplication(ctx *kt.Context, client *kivik.Client) {
case "none":
prefix = ""
}
dbtarget := prefix + ctx.TestDB()
dbsource := prefix + ctx.TestDB()
defer ctx.DestroyDB(dbtarget)
defer ctx.DestroyDB(dbsource)
targetDB, sourceDB := ctx.TestDB(), ctx.TestDB()
defer func() {
ctx.DestroyDB(targetDB)
ctx.DestroyDB(sourceDB)
}()
dbtarget := prefix + targetDB
dbsource := prefix + sourceDB

db := ctx.Admin.DB(context.Background(), dbsource)
db := ctx.Admin.DB(context.Background(), sourceDB)
if err := db.Err(); err != nil {
ctx.Fatalf("Failed to open db: %s", err)
}
Expand Down Expand Up @@ -178,14 +183,8 @@ func doReplicationTest(ctx *kt.Context, client *kivik.Client, dbtarget, dbsource
ctx.Errorf("Expected a replication ID")
}
}
source := stripCreds(dbsource)
target := stripCreds(dbtarget)
if rep.Source != source && rep.Source != source+"/" {
ctx.Errorf("Unexpected source. Expected: %s, Actual: %s\n", source, rep.Source)
}
if rep.Target != target && rep.Target != target+"/" {
ctx.Errorf("Unexpected target. Expected: %s, Actual: %s\n", target, rep.Target)
}
checkReplicationURL(ctx.T, "source", dbsource, rep.Source)
checkReplicationURL(ctx.T, "target", dbtarget, rep.Target)
if rep.State() != kivik.ReplicationComplete {
ctx.Errorf("Replication failed to complete. Final state: %s\n", rep.State())
}
Expand All @@ -196,8 +195,31 @@ func doReplicationTest(ctx *kt.Context, client *kivik.Client, dbtarget, dbsource
return success
}

func stripCreds(addr string) string {
url, _ := url.Parse(addr)
url.User = nil
return url.String()
func checkReplicationURL(t *testing.T, name, want, got string) {
wantURL, err := url.Parse(want)
if err != nil {
t.Fatal(err)
}
gotURL, err := url.Parse(got)
if err != nil {
t.Fatal(err)
}
if !replicationURLsEqual(wantURL, gotURL) {
t.Errorf("Unexpected %s URL. Want: %s, got %s", name, want, got)
}
}

func replicationURLsEqual(want, got *url.URL) bool {
if want.User != nil && got.User != nil {
wantUser := want.User.Username()
gotUser := got.User.Username()
if wantUser != "" && gotUser != "" && wantUser != gotUser {
return false
}
}
want.User = nil
got.User = nil
want.Path = filepath.Join(want.Path, "")
got.Path = filepath.Join(got.Path, "")
return want.String() == got.String()
}

0 comments on commit 18f5411

Please sign in to comment.