...

Text file src/cmd/go/testdata/script/goauth_userauth.txt

Documentation: cmd/go/testdata/script

     1# This test covers the HTTP authentication mechanism over GOAUTH by using a custom authenticator.
     2# See golang.org/issue/26232
     3
     4env GOPROXY=direct
     5env GOSUMDB=off
     6mkdir $WORK/bin
     7env PATH=$WORK/bin${:}$PATH
     8
     9# Without credentials, downloading a module from a path that requires HTTPS
    10# basic auth should fail.
    11env GOAUTH=off
    12cp go.mod.orig go.mod
    13! go get vcs-test.golang.org/auth/or401
    14stderr '^\tserver response: ACCESS DENIED, buddy$'
    15# go imports should fail as well.
    16! go mod tidy
    17stderr '^\tserver response: ACCESS DENIED, buddy$'
    18
    19# Initial invocation of authenticator is successful.
    20go build -o $WORK/bin/basic$GOEXE scripts/basic.go
    21# With credentials from the binary, it should succeed.
    22env GOAUTH='basic'$GOEXE
    23cp go.mod.orig go.mod
    24go get vcs-test.golang.org/auth/or401
    25# go imports should resolve correctly as well.
    26go mod tidy
    27go list all
    28stdout vcs-test.golang.org/auth/or401
    29
    30# Second invocation of authenticator is successful.
    31go build -o $WORK/bin/reinvocation$GOEXE scripts/reinvocation.go
    32# With credentials from the binary, it should succeed.
    33env GOAUTH='reinvocation'$GOEXE
    34cp go.mod.orig go.mod
    35go get vcs-test.golang.org/auth/or401
    36# go imports should resolve correctly as well.
    37go mod tidy
    38go list all
    39stdout vcs-test.golang.org/auth/or401
    40
    41# Authenticator can parse arguments correctly.
    42go build -o $WORK/bin/arguments$GOEXE scripts/arguments.go
    43# With credentials from the binary, it should succeed.
    44env GOAUTH='arguments'$GOEXE' --arg1 "value with spaces"'
    45cp go.mod.orig go.mod
    46go get vcs-test.golang.org/auth/or401
    47# go imports should resolve correctly as well.
    48go mod tidy
    49go list all
    50stdout vcs-test.golang.org/auth/or401
    51
    52# Authenticator provides bad credentials.
    53go build -o $WORK/bin/invalid$GOEXE scripts/invalid.go
    54# With credentials from the binary, it should fail.
    55env GOAUTH='invalid'$GOEXE
    56cp go.mod.orig go.mod
    57! go get vcs-test.golang.org/auth/or401
    58stderr '^\tserver response: ACCESS DENIED, buddy$'
    59# go imports should fail as well.
    60! go mod tidy
    61stderr '^\tserver response: ACCESS DENIED, buddy$'
    62
    63-- go.mod.orig --
    64module private.example.com
    65-- main.go --
    66package useprivate
    67
    68import "vcs-test.golang.org/auth/or401"
    69-- scripts/basic.go --
    70package main
    71
    72import "fmt"
    73
    74func main() {
    75	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n\n")
    76}
    77-- scripts/reinvocation.go --
    78package main
    79
    80import(
    81	"bufio"
    82	"flag"
    83	"fmt"
    84	"io"
    85	"log"
    86	"net/http"
    87	"os"
    88	"strings"
    89)
    90
    91func main() {
    92	flag.Parse()
    93	// wait for re-invocation
    94	if !strings.HasPrefix(flag.Arg(0), "https://vcs-test.golang.org") {
    95		return
    96	}
    97	input, err := io.ReadAll(os.Stdin)
    98	if err != nil {
    99		log.Fatal("unexpected error while reading from stdin")
   100	}
   101	reader := bufio.NewReader(strings.NewReader(string(input)))
   102	resp, err := http.ReadResponse(reader, nil)
   103	if err != nil {
   104		log.Fatal("could not parse HTTP response")
   105	}
   106	if resp.StatusCode != 401 {
   107		log.Fatal("expected 401 error code")
   108	}
   109	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n\n")
   110}
   111-- scripts/arguments.go --
   112package main
   113
   114import(
   115	"flag"
   116	"fmt"
   117	"log"
   118)
   119
   120func main() {
   121	arg1 := flag.String("arg1", "", "")
   122	flag.Parse()
   123	if *arg1 != "value with spaces" {
   124		log.Fatal("argument with spaces does not work")
   125	}
   126	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l\n\n")
   127}
   128-- scripts/invalid.go --
   129package main
   130
   131import "fmt"
   132
   133func main() {
   134	fmt.Printf("https://vcs-test.golang.org\n\nAuthorization: Basic invalid\n\n")
   135}

View as plain text