Skip to content
Snippets Groups Projects
Select Git revision
  • 2b8b09336095a0ec391132c0ad3ca53c40e75db0
  • 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

receive.go

Blame
  • key_test.go 2.36 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2022 xx foundation                                             //
    //                                                                            //
    // Use of this source code is governed by a license that can be found in the  //
    // LICENSE file.                                                              //
    ////////////////////////////////////////////////////////////////////////////////
    
    package backup
    
    import (
    	"bytes"
    	"encoding/base64"
    	"gitlab.com/xx_network/crypto/csprng"
    	"testing"
    )
    
    // Tests that DeriveKey returns a key of the correct length and that it is the
    // same for the same set of password and salt. Also checks that keys with the
    // same salt or passwords do not collide.
    func TestDeriveKey(t *testing.T) {
    	p := testParams()
    	salts := make([][]byte, 6)
    	passwords := make([]string, len(salts))
    	keys := make(map[string]bool, len(salts)*len(passwords))
    
    	for i := range salts {
    		prng := csprng.NewSystemRNG()
    		salt, _ := MakeSalt(prng)
    		salts[i] = salt
    
    		password := make([]byte, 16)
    		_, _ = prng.Read(password)
    		passwords[i] = base64.StdEncoding.EncodeToString(password)[:16]
    	}
    
    	for _, salt := range salts {
    		for _, password := range passwords {
    			key := DeriveKey(password, salt, p)
    
    			// Check that the length of the key is correct
    			if len(key) != KeyLen {
    				t.Errorf("Incorrect key length.\nexpected: %d\nreceived: %d",
    					KeyLen, len(key))
    			}
    
    			// Check that the same key is generated when the same password and salt
    			// are used
    			key2 := DeriveKey(password, salt, p)
    
    			if !bytes.Equal(key, key2) {
    				t.Errorf("Keys with same password and salt do not match."+
    					"\nexpected: %v\nreceived: %v", key, key2)
    			}
    
    			if keys[string(key)] {
    				t.Errorf("Key already exists.")
    			}
    			keys[string(key)] = true
    		}
    	}
    }
    
    // Tests that multiple calls to MakeSalt results in unique salts of the
    // specified length.
    func TestMakeSalt(t *testing.T) {
    	salts := make(map[string]bool, 50)
    	for i := 0; i < 50; i++ {
    		salt, err := MakeSalt(csprng.NewSystemRNG())
    		if err != nil {
    			t.Errorf("MakeSalt returned an error: %+v", err)
    		}
    
    		if len(salt) != SaltLen {
    			t.Errorf("Incorrect salt length.\nexpected: %d\nreceived: %d",
    				SaltLen, len(salt))
    		}
    
    		if salts[string(salt)] {
    			t.Errorf("Salt already exists (%d).", i)
    		}
    		salts[string(salt)] = true
    	}
    }