Speed up goodkey test. (#3733)

This is one of our slowest unittests, clocking in at 23 seconds in a recent run.
This was largely due to generating keys. Note that performance is significantly
worse under the race detector.
This commit is contained in:
Jacob Hoffman-Andrews 2018-05-23 16:11:46 -07:00 committed by GitHub
parent 2540d59296
commit b3f5c0f6e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 8 deletions

View File

@ -23,17 +23,31 @@ func TestUnknownKeyType(t *testing.T) {
}
func TestSmallModulus(t *testing.T) {
private, err := rsa.GenerateKey(rand.Reader, 2040)
test.AssertNotError(t, err, "Error generating key")
test.AssertError(t, testingPolicy.GoodKey(&private.PublicKey), "Should have rejected too-short key.")
test.AssertError(t, testingPolicy.GoodKey(private.PublicKey), "Should have rejected too-short key.")
pubKey := rsa.PublicKey{
N: big.NewInt(0),
E: 65537,
}
// 2040 bits
_, ok := pubKey.N.SetString("104192126510885102608953552259747211060428328569316484779167706297543848858189721071301121307701498317286069484848193969810800653457088975832436062805901725915630417996487259956349018066196416400386483594314258078114607080545265502078791826837453107382149801328758721235866366842649389274931060463277516954884108984101391466769505088222180613883737986792254164577832157921425082478871935498631777878563742033332460445633026471887331001305450139473524438241478798689974351175769895824322173301257621327448162705637127373457350813027123239805772024171112299987923305882261194120410409098448380641378552305583392176287", 10)
if !ok {
t.Errorf("error parsing pubkey modulus")
}
test.AssertError(t, testingPolicy.GoodKey(&pubKey), "Should have rejected too-short key.")
test.AssertError(t, testingPolicy.GoodKey(pubKey), "Should have rejected too-short key.")
}
func TestLargeModulus(t *testing.T) {
private, err := rsa.GenerateKey(rand.Reader, 4097)
test.AssertNotError(t, err, "Error generating key")
test.AssertError(t, testingPolicy.GoodKey(&private.PublicKey), "Should have rejected too-long key.")
test.AssertError(t, testingPolicy.GoodKey(private.PublicKey), "Should have rejected too-long key.")
pubKey := rsa.PublicKey{
N: big.NewInt(0),
E: 65537,
}
// 4097 bits
_, ok := pubKey.N.SetString("1528586537844618544364689295678280797814937047039447018548513699782432768815684971832418418955305671838918285565080181315448131784543332408348488544125812746629522583979538961638790013578302979210481729874191053412386396889481430969071543569003141391030053024684850548909056275565684242965892176703473950844930842702506635531145654194239072799616096020023445127233557468234181352398708456163013484600764686209741158795461806441111028922165846800488957692595308009319392149669715238691709012014980470238746838534949750493558807218940354555205690667168930634644030378921382266510932028134500172599110460167962515262077587741235811653717121760943005253103187409557573174347385738572144714188928416780963680160418832333908040737262282830643745963536624555340279793555475547508851494656512855403492456740439533790565640263514349940712999516725281940465613417922773583725174223806589481568984323871222072582132221706797917380250216291620957692131931099423995355390698925093903005385497308399692769135287821632877871068909305276870015125960884987746154344006895331078411141197233179446805991116541744285238281451294472577537413640009811940462311100056023815261650331552185459228689469446389165886801876700815724561451940764544990177661873073", 10)
if !ok {
t.Errorf("error parsing pubkey modulus")
}
test.AssertError(t, testingPolicy.GoodKey(&pubKey), "Should have rejected too-long key.")
test.AssertError(t, testingPolicy.GoodKey(pubKey), "Should have rejected too-long key.")
}
func TestModulusModulo8(t *testing.T) {