Debian Codegraph @

go/pkg/electron/auth_test.go

raw
1 /*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18 */
19
20 package electron
21
22 import (
23 "fmt"
24 "io/ioutil"
25 "os"
26 "os/exec"
27 "path/filepath"
28 "strings"
29 "testing"
30
31 "github.com/apache/qpid-proton/go/pkg/internal/test"
32 )
33
34 func TestAuthAnonymous(t *testing.T) {
35 p := newPipe(t,
36 []ConnectionOption{User("fred"), VirtualHost("vhost"), SASLAllowInsecure(true)},
37 []ConnectionOption{SASLAllowedMechs("ANONYMOUS"), SASLAllowInsecure(true)})
38 test.FatalIf(t, p.server.Sync())
39 test.ErrorIf(t, test.Differ("anonymous", p.server.User()))
40 test.ErrorIf(t, test.Differ("vhost", p.server.VirtualHost()))
41 }
42
43 func TestAuthPlain(t *testing.T) {
44 extendedSASL.startTest(t)
45 p := newPipe(t,
46 []ConnectionOption{SASLAllowInsecure(true), SASLAllowedMechs("PLAIN"), User("fred@proton"), Password([]byte("xxx"))},
47 []ConnectionOption{SASLAllowInsecure(true), SASLAllowedMechs("PLAIN")})
48 test.FatalIf(t, p.server.Sync())
49 test.ErrorIf(t, test.Differ("fred@proton", p.server.User()))
50 }
51
52 func TestAuthBadPass(t *testing.T) {
53 extendedSASL.startTest(t)
54 p := newPipe(t,
55 []ConnectionOption{SASLAllowInsecure(true), SASLAllowedMechs("PLAIN"), User("fred@proton"), Password([]byte("yyy"))},
56 []ConnectionOption{SASLAllowInsecure(true), SASLAllowedMechs("PLAIN")})
57 if p.server.Sync() == nil {
58 t.Error("Expected auth failure for bad pass")
59 }
60 }
61
62 func TestAuthBadUser(t *testing.T) {
63 extendedSASL.startTest(t)
64 p := newPipe(t,
65 []ConnectionOption{SASLAllowInsecure(true), SASLAllowedMechs("PLAIN"), User("foo@bar"), Password([]byte("yyy"))},
66 []ConnectionOption{SASLAllowInsecure(true), SASLAllowedMechs("PLAIN")})
67 if p.server.Sync() == nil {
68 t.Error("Expected auth failure for bad user")
69 }
70 }
71
72 type extendedSASLState struct {
73 err error
74 dir string
75 }
76
77 func (s *extendedSASLState) setup() {
78 if SASLExtended() {
79 if s.dir, s.err = ioutil.TempDir("", ""); s.err == nil {
80 GlobalSASLConfigDir(s.dir)
81 GlobalSASLConfigName("test")
82 conf := filepath.Join(s.dir, "test.conf")
83 db := filepath.Join(s.dir, "proton.sasldb")
84 saslpasswd := os.Getenv("SASLPASSWD")
85 if saslpasswd == "" {
86 saslpasswd = "saslpasswd2"
87 }
88 cmd := exec.Command(saslpasswd, "-c", "-p", "-f", db, "-u", "proton", "fred")
89 cmd.Stdin = strings.NewReader("xxx") // Password
90 if _, s.err = cmd.CombinedOutput(); s.err == nil {
91 confStr := fmt.Sprintf(`
92 sasldb_path: %s
93 mech_list: EXTERNAL DIGEST-MD5 SCRAM-SHA-1 CRAM-MD5 PLAIN ANONYMOUS
94 `, db)
95 s.err = ioutil.WriteFile(conf, []byte(confStr), os.ModePerm)
96 }
97 }
98 }
99 // Note we don't do anything with s.err now, tests that need the
100 // extended SASL config will fail if s.err != nil. If no such tests
101 // are run then it is not an error that we couldn't set it up.
102 }
103
104 func (s extendedSASLState) teardown() {
105 if s.dir != "" {
106 _ = os.RemoveAll(s.dir)
107 }
108 }
109
110 func (s extendedSASLState) startTest(t *testing.T) {
111 if !SASLExtended() {
112 t.Skipf("Extended SASL not enabled")
113 } else if extendedSASL.err != nil {
114 t.Skipf("Extended SASL setup error: %v", extendedSASL.err)
115 }
116 }
117
118 var extendedSASL extendedSASLState
119
120 func TestMain(m *testing.M) {
121 // Do global SASL setup/teardown in main.
122 // Doing it on-demand makes the tests fragile to parallel test runs and
123 // changes in test ordering.
124 extendedSASL.setup()
125 status := m.Run()
126 extendedSASL.teardown()
127 os.Exit(status)
128 }