...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/auth/auth_test.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/auth

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package auth_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"go.mongodb.org/mongo-driver/internal/require"
    14  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    15  	. "go.mongodb.org/mongo-driver/x/mongo/driver/auth"
    16  	"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
    17  )
    18  
    19  func TestCreateAuthenticator(t *testing.T) {
    20  
    21  	tests := []struct {
    22  		name   string
    23  		source string
    24  		auth   Authenticator
    25  	}{
    26  		{name: "", auth: &DefaultAuthenticator{}},
    27  		{name: "SCRAM-SHA-1", auth: &ScramAuthenticator{}},
    28  		{name: "SCRAM-SHA-256", auth: &ScramAuthenticator{}},
    29  		{name: "MONGODB-CR", auth: &MongoDBCRAuthenticator{}},
    30  		{name: "PLAIN", auth: &PlainAuthenticator{}},
    31  		{name: "MONGODB-X509", auth: &MongoDBX509Authenticator{}},
    32  	}
    33  
    34  	for _, test := range tests {
    35  		t.Run(test.name, func(t *testing.T) {
    36  			cred := &Cred{
    37  				Username:    "user",
    38  				Password:    "pencil",
    39  				PasswordSet: true,
    40  			}
    41  
    42  			a, err := CreateAuthenticator(test.name, cred)
    43  			require.NoError(t, err)
    44  			require.IsType(t, test.auth, a)
    45  		})
    46  	}
    47  }
    48  
    49  func compareResponses(t *testing.T, wm []byte, expectedPayload bsoncore.Document, dbName string) {
    50  	_, _, _, opcode, wm, ok := wiremessage.ReadHeader(wm)
    51  	if !ok {
    52  		t.Fatalf("wiremessage is too short to unmarshal")
    53  	}
    54  	var actualPayload bsoncore.Document
    55  	switch opcode {
    56  	case wiremessage.OpMsg:
    57  		// Append the $db field.
    58  		elems, err := expectedPayload.Elements()
    59  		if err != nil {
    60  			t.Fatalf("expectedPayload is not valid: %v", err)
    61  		}
    62  		elems = append(elems, bsoncore.AppendStringElement(nil, "$db", dbName))
    63  		elems = append(elems, bsoncore.AppendDocumentElement(nil,
    64  			"$readPreference",
    65  			bsoncore.BuildDocumentFromElements(nil, bsoncore.AppendStringElement(nil, "mode", "primaryPreferred")),
    66  		))
    67  		bslc := make([][]byte, 0, len(elems)) // BuildDocumentFromElements takes a [][]byte, not a []bsoncore.Element.
    68  		for _, elem := range elems {
    69  			bslc = append(bslc, elem)
    70  		}
    71  		expectedPayload = bsoncore.BuildDocumentFromElements(nil, bslc...)
    72  
    73  		_, wm, ok := wiremessage.ReadMsgFlags(wm)
    74  		if !ok {
    75  			t.Fatalf("wiremessage is too short to unmarshal")
    76  		}
    77  	loop:
    78  		for {
    79  			var stype wiremessage.SectionType
    80  			stype, wm, ok = wiremessage.ReadMsgSectionType(wm)
    81  			if !ok {
    82  				t.Fatalf("wiremessage is too short to unmarshal")
    83  			}
    84  			switch stype {
    85  			case wiremessage.DocumentSequence:
    86  				_, _, wm, ok = wiremessage.ReadMsgSectionDocumentSequence(wm)
    87  				if !ok {
    88  					t.Fatalf("wiremessage is too short to unmarshal")
    89  				}
    90  			case wiremessage.SingleDocument:
    91  				actualPayload, wm, ok = wiremessage.ReadMsgSectionSingleDocument(wm)
    92  				if !ok {
    93  					t.Fatalf("wiremessage is too short to unmarshal")
    94  				}
    95  				break loop
    96  			}
    97  		}
    98  	}
    99  
   100  	if !cmp.Equal(actualPayload, expectedPayload) {
   101  		t.Errorf("Payloads don't match. got %v; want %v", actualPayload, expectedPayload)
   102  	}
   103  }
   104  

View as plain text