File: main_test.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (114 lines) | stat: -rw-r--r-- 2,448 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2013-2015 Apcera Inc. All rights reserved.

package test

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"os"
	"sync"
	"testing"

	"github.com/apcera/gssapi"
)

const (
	micHeader = "X-go-gssapi-test-mic"
)

type Context struct {
	DebugLog       bool
	RunAsService   bool
	ServiceName    string
	ServiceAddress string

	gssapi.Options

	*gssapi.Lib `json:"-"`
	loadonce    sync.Once

	// Service credentials loaded from keytab
	credential *gssapi.CredId
}

var c = &Context{}

func init() {
	flag.BoolVar(&c.DebugLog, "debug", false, "Output debug log")
	flag.BoolVar(&c.RunAsService, "service", false, "Stay running as sample service after executing the tests")
	flag.StringVar(&c.ServiceName, "service-name", "SampleService", "service name")
	flag.StringVar(&c.ServiceAddress, "service-address", ":8080", "service address hostname:port")
	flag.StringVar(&c.Options.LibPath, "gssapi-path", "", "use the specified path to libgssapi shared object")
	flag.StringVar(&c.Options.Krb5Ktname, "krb5-ktname", "", "path to the keytab file")
	flag.StringVar(&c.Options.Krb5Config, "krb5-config", "", "path to krb5.config file")
}

func TestMain(m *testing.M) {
	flag.Parse()
	prefix := "go-gssapi-test-client"
	if c.RunAsService {
		prefix = "go-gssapi-test-service"
	}
	lib, err := loadlib(c.DebugLog, prefix)
	if err != nil {
		log.Fatal(err)
	}
	c.Lib = lib

	j, _ := json.MarshalIndent(c, "", "  ")
	c.Debug(fmt.Sprintf("Config: %s", string(j)))

	code := m.Run()
	if code != 0 {
		os.Exit(code)
	}

	if c.RunAsService {
		log.Fatal(Service(c))
	}
}

func loadlib(debug bool, prefix string) (*gssapi.Lib, error) {
	max := gssapi.Err + 1
	if debug {
		max = gssapi.MaxSeverity
	}
	pp := make([]gssapi.Printer, 0, max)
	for i := gssapi.Severity(0); i < max; i++ {
		p := log.New(os.Stderr,
			fmt.Sprintf("%s: %s\t", prefix, i),
			log.LstdFlags)
		pp = append(pp, p)
	}
	c.Options.Printers = pp

	lib, err := gssapi.Load(&c.Options)
	if err != nil {
		return nil, err
	}
	return lib, nil
}

func prepareServiceName(t *testing.T) *gssapi.Name {
	if c.ServiceName == "" {
		t.Fatal("Need a --service-name")
	}

	nameBuf, err := c.MakeBufferString(c.ServiceName)
	if err != nil {
		t.Fatal(err)
	}
	defer nameBuf.Release()

	name, err := nameBuf.Name(c.GSS_KRB5_NT_PRINCIPAL_NAME)
	if err != nil {
		t.Fatal(err)
	}
	if name.String() != c.ServiceName {
		t.Fatalf("name: got %q, expected %q", name.String(), c.ServiceName)
	}

	return name
}