Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | package acmeserver |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | bolt "go.etcd.io/bbolt" |
| 12 | bolterrors "go.etcd.io/bbolt/errors" |
| 13 | "go.uber.org/zap" |
| 14 | "go.uber.org/zap/zaptest/observer" |
| 15 | ) |
| 16 | |
| 17 | func TestHandler_warnIfPolicyAllowsAll(t *testing.T) { |
| 18 | tests := []struct { |
| 19 | name string |
| 20 | policy *Policy |
| 21 | wantWarns int |
| 22 | wantAllowWildcard bool |
| 23 | }{ |
| 24 | { |
| 25 | name: "warns when policy is nil", |
| 26 | policy: nil, |
| 27 | wantWarns: 1, |
| 28 | wantAllowWildcard: false, |
| 29 | }, |
| 30 | { |
| 31 | name: "warns when allow/deny rules are empty", |
| 32 | policy: &Policy{}, |
| 33 | wantWarns: 1, |
| 34 | wantAllowWildcard: false, |
| 35 | }, |
| 36 | { |
| 37 | name: "warns when only allow_wildcard_names is true", |
| 38 | policy: &Policy{ |
| 39 | AllowWildcardNames: true, |
| 40 | }, |
| 41 | wantWarns: 1, |
| 42 | wantAllowWildcard: true, |
| 43 | }, |
| 44 | { |
| 45 | name: "does not warn when allow rules are configured", |
| 46 | policy: &Policy{ |
| 47 | Allow: &RuleSet{ |
| 48 | Domains: []string{"example.com"}, |
| 49 | }, |
| 50 | }, |
| 51 | wantWarns: 0, |
| 52 | wantAllowWildcard: false, |
| 53 | }, |
| 54 | { |
| 55 | name: "does not warn when deny rules are configured", |
| 56 | policy: &Policy{ |
| 57 | Deny: &RuleSet{ |
| 58 | Domains: []string{"bad.example.com"}, |
| 59 | }, |
| 60 | }, |
| 61 | wantWarns: 0, |
| 62 | wantAllowWildcard: false, |
| 63 | }, |
| 64 | } |
| 65 | |
| 66 | for _, tt := range tests { |
| 67 | t.Run(tt.name, func(t *testing.T) { |
| 68 | core, logs := observer.New(zap.WarnLevel) |
| 69 | ash := &Handler{ |
| 70 | CA: "local", |
| 71 | Policy: tt.policy, |
| 72 | logger: zap.New(core), |
| 73 | } |
| 74 | |
| 75 | ash.warnIfPolicyAllowsAll() |
| 76 | if logs.Len() != tt.wantWarns { |
| 77 | t.Fatalf("expected %d warning logs, got %d", tt.wantWarns, logs.Len()) |
| 78 | } |
| 79 | |
| 80 | if tt.wantWarns == 0 { |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | entry := logs.All()[0] |
| 85 | if entry.Level != zap.WarnLevel { |
| 86 | t.Fatalf("expected warn level, got %v", entry.Level) |
| 87 | } |
| 88 | if !strings.Contains(entry.Message, "policy has no allow/deny rules") { |
| 89 | t.Fatalf("unexpected log message: %q", entry.Message) |
| 90 | } |
| 91 | ctx := entry.ContextMap() |
| 92 | if ctx["ca"] != "local" { |
| 93 | t.Fatalf("expected ca=local, got %v", ctx["ca"]) |
| 94 | } |
| 95 | if ctx["allow_wildcard_names"] != tt.wantAllowWildcard { |
| 96 | t.Fatalf("expected allow_wildcard_names=%v, got %v", tt.wantAllowWildcard, ctx["allow_wildcard_names"]) |
| 97 | } |
| 98 | }) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestHandler_openDatabaseLocked(t *testing.T) { |
| 103 | if testing.Short() { |
| 104 | t.Skip("skipping: bbolt waits 5s for the lock") |
| 105 | } |
| 106 | |
| 107 | dataDir := t.TempDir() |
| 108 | t.Setenv("XDG_DATA_HOME", dataDir) |
| 109 | |
| 110 | ash := Handler{CA: "locked-db-test", logger: zap.NewNop()} |
| 111 | key := ash.getDatabaseKey() |
| 112 | |
| 113 | dbFolder := filepath.Join(dataDir, "caddy", "acme_server", key) |
| 114 | if err := os.MkdirAll(dbFolder, 0o755); err != nil { |
| 115 | t.Fatalf("making database folder: %v", err) |
| 116 | } |
| 117 | dbPath := filepath.Join(dbFolder, "db") |
| 118 | |
| 119 | // hold the lock like a running Caddy instance would |
| 120 | locked, err := bolt.Open(dbPath, 0o600, &bolt.Options{Timeout: 5 * time.Second}) |
| 121 | if err != nil { |
| 122 | t.Fatalf("opening database: %v", err) |
| 123 | } |
| 124 | defer locked.Close() |
| 125 | |
| 126 | _, err = ash.openDatabase() |
| 127 | if err == nil { |
| 128 | t.Fatal("expected an error opening a locked database, got none") |
| 129 | } |
| 130 | if !errors.Is(err, bolterrors.ErrTimeout) { |
| 131 | t.Errorf("expected error to wrap bolterrors.ErrTimeout, got: %v", err) |
| 132 | } |
| 133 | if !strings.Contains(err.Error(), "already locked") { |
| 134 | t.Errorf("expected error to explain the lock, got: %v", err) |
| 135 | } |
| 136 | if _, err := databasePool.Delete(key); err != nil { |
| 137 | t.Errorf("cleaning up database pool: %v", err) |
| 138 | } |
| 139 | } |