Skip to content
Snippets Groups Projects
Select Git revision
  • 1c55b1840321ff53d2899aa1f14e619ca561ead2
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

authenticated_test.go

Blame
  • authenticated_test.go 3.55 KiB
    ///////////////////////////////////////////////////////////////////////////////
    // Copyright © 2020 xx network SEZC                                          //
    //                                                                           //
    // Use of this source code is governed by a license that can be found in the //
    // LICENSE file                                                              //
    ///////////////////////////////////////////////////////////////////////////////
    
    package connect
    
    import (
    	"gitlab.com/elixxir/crypto/contact"
    	"gitlab.com/elixxir/crypto/diffieHellman"
    	"gitlab.com/elixxir/crypto/fastRNG"
    	"gitlab.com/xx_network/crypto/csprng"
    	"gitlab.com/xx_network/crypto/signature/rsa"
    	"gitlab.com/xx_network/crypto/xx"
    	"gitlab.com/xx_network/primitives/id"
    	"math/rand"
    	"testing"
    	"time"
    )
    
    // TestConnectWithAuthentication will test the client/server relationship for
    // an AuthenticatedConnection. This will construct a client which will send an
    // IdentityAuthentication message to the server, who will hear it and verify
    // the contents. This will use a mock connection interface and private
    // production code helper functions for easier testing.
    func TestConnectWithAuthentication(t *testing.T) {
    	grp := getGroup()
    	numPrimeByte := len(grp.GetPBytes())
    
    	// Set up cmix handler
    	mockNet := newMockCmix()
    
    	// Set up connect arguments
    	prng := rand.New(rand.NewSource(42))
    	dhPrivKey := diffieHellman.GeneratePrivateKey(
    		numPrimeByte, grp, prng)
    	dhPubKey := diffieHellman.GeneratePublicKey(dhPrivKey, grp)
    	salt := make([]byte, 32)
    	copy(salt, "salt")
    
    	myRsaPrivKey, err := rsa.LoadPrivateKeyFromPem(getPrivKey())
    	if err != nil {
    		t.Fatalf("Faled to load private key: %v", err)
    	}
    
    	// Construct client ID the proper way as server will need to verify it
    	// using the xx.NewID function call
    	myId, err := xx.NewID(myRsaPrivKey.GetPublic(), salt, id.User)
    	if err != nil {
    		t.Fatalf("Failed to generate client's id: %+v", err)
    	}
    
    	// Generate server ID using testing interface
    	serverID := id.NewIdFromString("server", id.User, t)
    
    	// Construct recipient
    	recipient := contact.Contact{
    		ID:       serverID,
    		DhPubKey: dhPubKey,
    	}
    
    	rng := fastRNG.NewStreamGenerator(1, 1,
    		csprng.NewSystemRNG)
    
    	// Create the mock connection, which will be shared by the client and
    	// server. This will send the client's request to the server internally
    	mockConn := newMockConnection(myId, serverID, dhPrivKey, dhPubKey)
    
    	// Set up the server's callback, which will pass the authenticated
    	// connection through via a channel
    	authConnChan := make(chan AuthenticatedConnection, 1)
    	serverCb := AuthenticatedCallback(
    		func(connection AuthenticatedConnection) {
    			authConnChan <- connection
    		})
    
    	// Initialize params with a shorter timeout to hasten test results
    	customParams := GetDefaultParams()
    	customParams.Timeout = 3 * time.Second
    
    	// Initialize the server
    	serverHandler := buildAuthConfirmationHandler(serverCb, mockConn)
    
    	// Pass the server's listener to the mock connection so the connection
    	// can pass the client's message directly to the server
    	mockConn.listener = serverHandler
    
    	// Initialize the client
    	_, err = connectWithAuthentication(mockConn, time.Now(), recipient,
    		salt, myRsaPrivKey, rng, mockNet,
    		customParams)
    	if err != nil {
    		t.Fatalf("ConnectWithAuthentication error: %+v", err)
    	}
    
    	// Wait for the server to establish it's connection via the callback
    	timeout := time.NewTimer(customParams.Timeout)
    	select {
    	case <-authConnChan:
    		return
    	case <-timeout.C:
    		t.Fatalf("Timed out waiting for server's authenticated connection " +
    			"to be established")
    	}
    
    }