mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	
		
			Some checks failed
		
		
	
	Tests / test (./cmd/caddy/caddy, ~1.22.3, macos-14, 0, 1.22, mac) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy, ~1.23.0, macos-14, 0, 1.23, mac) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy.exe, ~1.22.3, windows-latest, True, 1.22, windows) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy.exe, ~1.23.0, windows-latest, True, 1.23, windows) (push) Waiting to run
				
			Lint / lint (macos-14, mac) (push) Waiting to run
				
			Lint / lint (windows-latest, windows) (push) Waiting to run
				
			Tests / test (./cmd/caddy/caddy, ~1.22.3, ubuntu-latest, 0, 1.22, linux) (push) Failing after 1m46s
				
			Tests / test (./cmd/caddy/caddy, ~1.23.0, ubuntu-latest, 0, 1.23, linux) (push) Failing after 1m29s
				
			Tests / test (s390x on IBM Z) (push) Has been skipped
				
			Tests / goreleaser-check (push) Successful in 22s
				
			Cross-Build / build (~1.22.3, 1.22, aix) (push) Successful in 1m43s
				
			Cross-Build / build (~1.22.3, 1.22, darwin) (push) Successful in 1m38s
				
			Cross-Build / build (~1.22.3, 1.22, dragonfly) (push) Successful in 1m36s
				
			Cross-Build / build (~1.22.3, 1.22, freebsd) (push) Successful in 1m40s
				
			Cross-Build / build (~1.22.3, 1.22, illumos) (push) Successful in 1m45s
				
			Cross-Build / build (~1.22.3, 1.22, linux) (push) Successful in 1m49s
				
			Cross-Build / build (~1.22.3, 1.22, netbsd) (push) Successful in 1m43s
				
			Cross-Build / build (~1.22.3, 1.22, openbsd) (push) Successful in 1m42s
				
			Cross-Build / build (~1.22.3, 1.22, solaris) (push) Successful in 1m41s
				
			Cross-Build / build (~1.22.3, 1.22, windows) (push) Successful in 1m40s
				
			Cross-Build / build (~1.23.0, 1.23, aix) (push) Successful in 1m26s
				
			Cross-Build / build (~1.23.0, 1.23, darwin) (push) Successful in 1m28s
				
			Cross-Build / build (~1.23.0, 1.23, dragonfly) (push) Successful in 1m25s
				
			Cross-Build / build (~1.23.0, 1.23, freebsd) (push) Successful in 1m30s
				
			Cross-Build / build (~1.23.0, 1.23, illumos) (push) Successful in 1m33s
				
			Cross-Build / build (~1.23.0, 1.23, linux) (push) Successful in 1m28s
				
			Cross-Build / build (~1.23.0, 1.23, netbsd) (push) Successful in 1m28s
				
			Cross-Build / build (~1.23.0, 1.23, openbsd) (push) Successful in 1m30s
				
			Cross-Build / build (~1.23.0, 1.23, solaris) (push) Successful in 1m31s
				
			Cross-Build / build (~1.23.0, 1.23, windows) (push) Successful in 1m31s
				
			Lint / lint (ubuntu-latest, linux) (push) Successful in 2m15s
				
			Lint / govulncheck (push) Successful in 1m24s
				
			* caddy adapt for listen_protocols * adapt listen_socket * allow multiple listen sockets for port ranges and readd socket fd listen logic * readd logic to start servers according to listener protocols * gofmt * adapt caddytest * gosec * fmt and rename listen to listenWithSocket * fmt and rename listen to listenWithSocket * more consistent error msg * non unix listenReusableWithSocketFile * remove unused func * doc comment typo * nonosec * commit * doc comments * more doc comments * comment was misleading, cardinality did not change * addressesWithProtocols * update test * fd/ and fdgram/ * rm addr * actually write... * i guess we doin' "skip": now * wrong var in placeholder * wrong var in placeholder II * update param name in comment * dont save nil file pointers * windows * key -> parsedKey * osx * multiple default_bind with protocols * check for h1 and h2 listener netw
		
			
				
	
	
		
			281 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			281 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package caddycmd
 | 
						|
 | 
						|
import (
 | 
						|
	"reflect"
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
)
 | 
						|
 | 
						|
func TestParseEnvFile(t *testing.T) {
 | 
						|
	for i, tc := range []struct {
 | 
						|
		input     string
 | 
						|
		expect    map[string]string
 | 
						|
		shouldErr bool
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			input: `KEY=value`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY": "value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY":       "value",
 | 
						|
				"OTHER_KEY": "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				INVALID KEY=asdf
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			shouldErr: true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				SIMPLE_QUOTED="quoted value"
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY":           "value",
 | 
						|
				"SIMPLE_QUOTED": "quoted value",
 | 
						|
				"OTHER_KEY":     "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				NEWLINES="foo
 | 
						|
	bar"
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY":       "value",
 | 
						|
				"NEWLINES":  "foo\n\tbar",
 | 
						|
				"OTHER_KEY": "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				ESCAPED="\"escaped quotes\"
 | 
						|
here"
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY":       "value",
 | 
						|
				"ESCAPED":   "\"escaped quotes\"\nhere",
 | 
						|
				"OTHER_KEY": "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				export KEY=value
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY":       "value",
 | 
						|
				"OTHER_KEY": "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				=value
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			shouldErr: true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				EMPTY=
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"EMPTY":     "",
 | 
						|
				"OTHER_KEY": "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				EMPTY=""
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"EMPTY":     "",
 | 
						|
				"OTHER_KEY": "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				#OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY": "value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				COMMENT=foo bar  # some comment here
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY":       "value",
 | 
						|
				"COMMENT":   "foo bar",
 | 
						|
				"OTHER_KEY": "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				WHITESPACE=   foo 
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			shouldErr: true,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			input: `
 | 
						|
				KEY=value
 | 
						|
				WHITESPACE="   foo bar "
 | 
						|
				OTHER_KEY=Some Value
 | 
						|
			`,
 | 
						|
			expect: map[string]string{
 | 
						|
				"KEY":        "value",
 | 
						|
				"WHITESPACE": "   foo bar ",
 | 
						|
				"OTHER_KEY":  "Some Value",
 | 
						|
			},
 | 
						|
		},
 | 
						|
	} {
 | 
						|
		actual, err := parseEnvFile(strings.NewReader(tc.input))
 | 
						|
		if err != nil && !tc.shouldErr {
 | 
						|
			t.Errorf("Test %d: Got error but shouldn't have: %v", i, err)
 | 
						|
		}
 | 
						|
		if err == nil && tc.shouldErr {
 | 
						|
			t.Errorf("Test %d: Did not get error but should have", i)
 | 
						|
		}
 | 
						|
		if tc.shouldErr {
 | 
						|
			continue
 | 
						|
		}
 | 
						|
		if !reflect.DeepEqual(tc.expect, actual) {
 | 
						|
			t.Errorf("Test %d: Expected %v but got %v", i, tc.expect, actual)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func Test_isCaddyfile(t *testing.T) {
 | 
						|
	type args struct {
 | 
						|
		configFile  string
 | 
						|
		adapterName string
 | 
						|
	}
 | 
						|
	tests := []struct {
 | 
						|
		name    string
 | 
						|
		args    args
 | 
						|
		want    bool
 | 
						|
		wantErr bool
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			name: "bare Caddyfile without adapter",
 | 
						|
			args: args{
 | 
						|
				configFile:  "Caddyfile",
 | 
						|
				adapterName: "",
 | 
						|
			},
 | 
						|
			want:    true,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "local Caddyfile without adapter",
 | 
						|
			args: args{
 | 
						|
				configFile:  "./Caddyfile",
 | 
						|
				adapterName: "",
 | 
						|
			},
 | 
						|
			want:    true,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "local caddyfile with adapter",
 | 
						|
			args: args{
 | 
						|
				configFile:  "./Caddyfile",
 | 
						|
				adapterName: "caddyfile",
 | 
						|
			},
 | 
						|
			want:    true,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "ends with .caddyfile with adapter",
 | 
						|
			args: args{
 | 
						|
				configFile:  "./conf.caddyfile",
 | 
						|
				adapterName: "caddyfile",
 | 
						|
			},
 | 
						|
			want:    true,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "ends with .caddyfile without adapter",
 | 
						|
			args: args{
 | 
						|
				configFile:  "./conf.caddyfile",
 | 
						|
				adapterName: "",
 | 
						|
			},
 | 
						|
			want:    true,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name: "config is Caddyfile.yaml with adapter",
 | 
						|
			args: args{
 | 
						|
				configFile:  "./Caddyfile.yaml",
 | 
						|
				adapterName: "yaml",
 | 
						|
			},
 | 
						|
			want:    false,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
 | 
						|
			name: "json is not caddyfile but not error",
 | 
						|
			args: args{
 | 
						|
				configFile:  "./Caddyfile.json",
 | 
						|
				adapterName: "",
 | 
						|
			},
 | 
						|
			want:    false,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
 | 
						|
			name: "prefix of Caddyfile and ./ with any extension is Caddyfile",
 | 
						|
			args: args{
 | 
						|
				configFile:  "./Caddyfile.prd",
 | 
						|
				adapterName: "",
 | 
						|
			},
 | 
						|
			want:    true,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
		{
 | 
						|
 | 
						|
			name: "prefix of Caddyfile without ./ with any extension is Caddyfile",
 | 
						|
			args: args{
 | 
						|
				configFile:  "Caddyfile.prd",
 | 
						|
				adapterName: "",
 | 
						|
			},
 | 
						|
			want:    true,
 | 
						|
			wantErr: false,
 | 
						|
		},
 | 
						|
	}
 | 
						|
	for _, tt := range tests {
 | 
						|
		t.Run(tt.name, func(t *testing.T) {
 | 
						|
			got, err := isCaddyfile(tt.args.configFile, tt.args.adapterName)
 | 
						|
			if (err != nil) != tt.wantErr {
 | 
						|
				t.Errorf("isCaddyfile() error = %v, wantErr %v", err, tt.wantErr)
 | 
						|
				return
 | 
						|
			}
 | 
						|
			if got != tt.want {
 | 
						|
				t.Errorf("isCaddyfile() = %v, want %v", got, tt.want)
 | 
						|
			}
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |