1// Copyright 2018 syzkaller project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
3
4package main
5
6import (
7	"fmt"
8	"testing"
9)
10
11func TestAuth(t *testing.T) {
12	hub := &Hub{
13		keys: map[string]string{
14			"foo": "1234",
15			"bar": "abcd",
16		},
17	}
18	tests := []struct {
19		client  string
20		key     string
21		manager string
22		result  string
23		ok      bool
24	}{
25		{
26			client:  "",
27			key:     "",
28			manager: "",
29			result:  "",
30			ok:      false,
31		},
32		{
33			client:  "",
34			key:     "1234",
35			manager: "manager",
36			result:  "",
37			ok:      false,
38		},
39		{
40			client:  "foo",
41			key:     "",
42			manager: "foo",
43			result:  "",
44			ok:      false,
45		},
46		{
47			client:  "foo",
48			key:     "123",
49			manager: "foo",
50			result:  "",
51			ok:      false,
52		},
53		{
54			client:  "foo",
55			key:     "abcd",
56			manager: "foo",
57			result:  "",
58			ok:      false,
59		},
60		{
61			client:  "foo",
62			key:     "1234",
63			manager: "foo",
64			result:  "foo",
65			ok:      true,
66		},
67		{
68			client:  "foo",
69			key:     "1234",
70			manager: "foo-suffix",
71			result:  "foo-suffix",
72			ok:      true,
73		},
74		{
75			client:  "foo",
76			key:     "1234",
77			manager: "",
78			result:  "foo",
79			ok:      true,
80		},
81		{
82			client:  "foo",
83			key:     "1234",
84			manager: "bar",
85			result:  "",
86			ok:      false,
87		},
88	}
89	for _, test := range tests {
90		t.Run(fmt.Sprintf("%q/%q/%q", test.client, test.key, test.manager), func(t *testing.T) {
91			manager, err := hub.auth(test.client, test.key, test.manager)
92			if !test.ok && err == nil {
93				t.Fatalf("auth is expected to fail, but it did not")
94			}
95			if test.ok && err != nil {
96				t.Fatalf("auth is not expected to fail, but it did: %v", err)
97			}
98			if manager != test.result {
99				t.Fatalf("got bad manager %q, want %q", manager, test.result)
100			}
101		})
102	}
103}
104