...

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

View as plain text