mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-01 05:27:24 +08:00
Many of our tests (db package, I'm looking at you) generate many RSA keys. This has two main side effects; makes our tests slow and flaky as CPU usage spikes in random moments when the tests are run in parallel. This change pre-generates RSA keys at the beginning of each test module and reuse them in randomized order to reduce the situation that one key has been used multiple times in one test. I had to move a few files to avoid circular dependencies.
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
/*
|
|
Copyright 2017-2018 Gravitational, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package native
|
|
|
|
import (
|
|
"crypto/x509"
|
|
"encoding/pem"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/gravitational/teleport/lib/utils"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
utils.InitLoggerForTests()
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
// TestPrecomputeMode verifies that package enters precompute mode when
|
|
// PrecomputeKeys is called.
|
|
func TestPrecomputeMode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
PrecomputeKeys()
|
|
|
|
select {
|
|
case <-precomputedKeys:
|
|
case <-time.After(time.Second * 10):
|
|
t.Fatal("Key precompute routine failed to start.")
|
|
}
|
|
}
|
|
|
|
// TestGenerateRSAPKSC1Keypair tests that GeneratePrivateKey generates
|
|
// a valid PKCS1 rsa key.
|
|
func TestGeneratePKSC1RSAKey(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
priv, err := GeneratePrivateKey()
|
|
require.NoError(t, err)
|
|
|
|
block, rest := pem.Decode(priv.PrivateKeyPEM())
|
|
require.NoError(t, err)
|
|
require.Empty(t, rest)
|
|
|
|
_, err = x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
require.NoError(t, err)
|
|
}
|