github.com/QuangHoangHao/kafka-go@v0.4.36/sasl/sasl_test.go (about)

     1  package sasl_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/QuangHoangHao/kafka-go"
     9  	"github.com/QuangHoangHao/kafka-go/sasl"
    10  	"github.com/QuangHoangHao/kafka-go/sasl/plain"
    11  	"github.com/QuangHoangHao/kafka-go/sasl/scram"
    12  	ktesting "github.com/QuangHoangHao/kafka-go/testing"
    13  )
    14  
    15  const (
    16  	saslTestConnect = "localhost:9093" // connect to sasl listener
    17  	saslTestTopic   = "test-writer-0"  // this topic is guaranteed to exist.
    18  )
    19  
    20  func TestSASL(t *testing.T) {
    21  	tests := []struct {
    22  		valid    func() sasl.Mechanism
    23  		invalid  func() sasl.Mechanism
    24  		minKafka string
    25  	}{
    26  		{
    27  			valid: func() sasl.Mechanism {
    28  				return plain.Mechanism{
    29  					Username: "adminplain",
    30  					Password: "admin-secret",
    31  				}
    32  			},
    33  			invalid: func() sasl.Mechanism {
    34  				return plain.Mechanism{
    35  					Username: "adminplain",
    36  					Password: "badpassword",
    37  				}
    38  			},
    39  		},
    40  		{
    41  			valid: func() sasl.Mechanism {
    42  				mech, _ := scram.Mechanism(scram.SHA256, "adminscram", "admin-secret-256")
    43  				return mech
    44  			},
    45  			invalid: func() sasl.Mechanism {
    46  				mech, _ := scram.Mechanism(scram.SHA256, "adminscram", "badpassword")
    47  				return mech
    48  			},
    49  			minKafka: "0.10.2.0",
    50  		},
    51  		{
    52  			valid: func() sasl.Mechanism {
    53  				mech, _ := scram.Mechanism(scram.SHA512, "adminscram", "admin-secret-512")
    54  				return mech
    55  			},
    56  			invalid: func() sasl.Mechanism {
    57  				mech, _ := scram.Mechanism(scram.SHA512, "adminscram", "badpassword")
    58  				return mech
    59  			},
    60  			minKafka: "0.10.2.0",
    61  		},
    62  	}
    63  
    64  	for _, tt := range tests {
    65  		mech := tt.valid()
    66  		if !ktesting.KafkaIsAtLeast(tt.minKafka) {
    67  			t.Skip("requires min kafka version " + tt.minKafka)
    68  		}
    69  
    70  		t.Run(mech.Name()+" success", func(t *testing.T) {
    71  			testConnect(t, tt.valid(), true)
    72  		})
    73  		t.Run(mech.Name()+" failure", func(t *testing.T) {
    74  			testConnect(t, tt.invalid(), false)
    75  		})
    76  		t.Run(mech.Name()+" is reusable", func(t *testing.T) {
    77  			mech := tt.valid()
    78  			testConnect(t, mech, true)
    79  			testConnect(t, mech, true)
    80  			testConnect(t, mech, true)
    81  		})
    82  
    83  	}
    84  }
    85  
    86  func testConnect(t *testing.T, mechanism sasl.Mechanism, success bool) {
    87  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    88  	defer cancel()
    89  
    90  	d := kafka.Dialer{
    91  		SASLMechanism: mechanism,
    92  	}
    93  	_, err := d.DialLeader(ctx, "tcp", saslTestConnect, saslTestTopic, 0)
    94  	if success && err != nil {
    95  		t.Errorf("should have logged in correctly, got err: %v", err)
    96  	} else if !success && err == nil {
    97  		t.Errorf("should not have logged in correctly")
    98  	}
    99  }