Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/tls test #485

Merged
merged 34 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a314cd3
internal/tls test
kevindiu Jun 18, 2020
fb22b4f
fix comments
kevindiu Jun 18, 2020
4e9a292
fix
kevindiu Jun 18, 2020
8d2d9e5
fix deepcode
kevindiu Jun 18, 2020
2d5326e
remove unnecessary checking
kevindiu Jun 18, 2020
d874042
remove unnecessary checking
kevindiu Jun 18, 2020
f98d5b0
remove unsed part
kevindiu Jun 18, 2020
2cf693e
remove deprecated function all
kevindiu Jun 18, 2020
206faab
remove deprecated function all
kevindiu Jun 18, 2020
fb00e58
remove deprecated function cll
kevindiu Jun 18, 2020
93bb0db
Update internal/tls/tls_test.go
kevindiu Jun 22, 2020
f7f8d87
create asset package and put all testdata into it
kevindiu Jun 25, 2020
06cb7db
fix
kevindiu Jun 25, 2020
4ce264a
rename asset to testdata
kevindiu Jun 29, 2020
6981070
rename testdata to test/data
kevindiu Jun 29, 2020
e814f21
fix indent
kevindiu Jun 29, 2020
e94a488
fix
kevindiu Jun 29, 2020
d8debd5
:robot: Update license headers / Format go codes and yaml files
vdaas-ci Jun 30, 2020
dbd4a3f
add package comments
kevindiu Jun 30, 2020
a3c5d28
:robot: Update license headers / Format go codes and yaml files
vdaas-ci Jun 30, 2020
eac37ae
fix
kevindiu Jun 30, 2020
91f32fc
move file path
kevindiu Jun 30, 2020
f3ca8e7
add test case and comment
kevindiu Jun 30, 2020
c3437c6
rename function name and remove unused function call
kevindiu Jun 30, 2020
da77a2d
fix
kevindiu Jul 2, 2020
9bebdd8
fix
kevindiu Jul 2, 2020
53733fd
fix
kevindiu Jul 6, 2020
f52b03c
fix
kevindiu Jul 7, 2020
0e59520
Apply suggestions from code review
kevindiu Jul 7, 2020
c618c63
Apply suggestions from code review
kevindiu Jul 7, 2020
585dfd2
Apply suggestions from code review
kevindiu Jul 7, 2020
ea04cde
fix
kevindiu Jul 8, 2020
72e687b
:robot: Update license headers / Format go codes and yaml files
vdaas-ci Jul 9, 2020
5e2c0c5
add back the package doc
kevindiu Jul 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/test/data/tls/dummyCa.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDoTCCAomgAwIBAgILBAAAAAABD4WqLUgwDQYJKoZIhvcNAQEFBQAwOzEYMBYG
A1UEChMPQ3liZXJ0cnVzdCwgSW5jMR8wHQYDVQQDExZDeWJlcnRydXN0IEdsb2Jh
bCBSb290MB4XDTA2MTIxNTA4MDAwMFoXDTIxMTIxNTA4MDAwMFowOzEYMBYGA1UE
ChMPQ3liZXJ0cnVzdCwgSW5jMR8wHQYDVQQDExZDeWJlcnRydXN0IEdsb2JhbCBS
b290MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA+Mi8vRRQZhP/8NN5
7CPytxrHjoXxEnOmGaoQ25yiZXRadz5RfVb23CO21O1fWLE3TdVJDm71aofW0ozS
J8bi/zafmGWgE07GKmSb1ZASzxQG9Dvj1Ci+6A74q05IlG2OlTEQXO2iLb3VOm2y
HLtgwEZLAfVJrn5GitB0jaEMAs7u/OePuGtm839EAL9mJRQr3RAwHQeWP032a7iP
t3sMpTjr3kfb1V05/Iin89cqdPHoWqI7n1C6poxFNcJQZZXcY4Lv3b93TZxiyWNz
FtApD0mpSPCzqrdsxacwOUBdrsTiXSZT8M4cIwhhqJQZugRiQOwfOHB3EgZxpzAY
XSUnpQIDAQABo4GlMIGiMA4GA1UdDwEB/wQEAwIBBjAPBgNVHRMBAf8EBTADAQH/
MB0GA1UdDgQWBBS2CHsNesysIEyGVjJez6tuhS1wVzA/BgNVHR8EODA2MDSgMqAw
hi5odHRwOi8vd3d3Mi5wdWJsaWMtdHJ1c3QuY29tL2NybC9jdC9jdHJvb3QuY3Js
MB8GA1UdIwQYMBaAFLYIew16zKwgTIZWMl7Pq26FLXBXMA0GCSqGSIb3DQEBBQUA
A4IBAQBW7wojoFROlZfJ+InaRcHUowAl9B8Tq7ejhVhpwjCt2BWKLePJzYFa+HMj
Wqd8BfP9IjsO0QbE2zZMcwSO5bAi5MXzLqXZI+O4Tkogp24CJJ8iYGd7ix1yCcUx
XOl5n4BHPa2hCwcUPUf/A2kaDAtE52Mlp3+yybh2hO0j9n0Hq0V+09+zv+mKts2o
omcrUtW3ZfA5TGOgkXmTUg9U3YO7n9GPp1Nzw8v/MOx8BLjYRB+TX3EJIrduPuoc
A06dGiBh+4E37F78CkWr1+cXVdCg6mCbpvbjjFspwgZgFJ0tl0ypkxWdYcQBX0jW
WL1WMRJOEcgh4LMRkWXbtKaIOM5V
-----END CERTIFICATE-----
19 changes: 19 additions & 0 deletions internal/test/data/tls/dummyServer.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIC/jCCAeYCCQCb1aA3TeyoljANBgkqhkiG9w0BAQUFADBBMQswCQYDVQQGEwJK
UDEOMAwGA1UECAwFVG9reW8xEjAQBgNVBAcMCWNoaXlvZGFrdTEOMAwGA1UECgwF
eWFob28wHhcNMTgwOTE4MDMxMjQyWhcNMjgwOTE1MDMxMjQyWjBBMQswCQYDVQQG
EwJKUDEOMAwGA1UECAwFVG9reW8xEjAQBgNVBAcMCWNoaXlvZGFrdTEOMAwGA1UE
CgwFeWFob28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDUBKwhDHGl
yj5GFOhDlyH67BsW9l/A2rna21WVnq69ANN9WmaiFp32C+2BrqF8DYwtPyoaTgXd
+WQ8wmtjlYwp378P743vBgf6tazZYKbCa7PZsNMLCdI0KurXf6iwOkCY7zQQcOji
STUVtfr009LFgc8KBduxBiyEe1tXoN1qrjUZgty/05EtW09QFE2XqVFQS4jN6CC4
whmN3J7yWHyZv3K1lO0aeC31kIOqw0/DxD/Dj/RMJBpz0q3jeYJwvM3rE4DNajTW
6BLiPWVkLW4H3paRE6jYfyCRazE8tLLWHzGGqPWUElV2rKWzdV6OnyWLFxWC2RK/
71IuZS1UDqmHAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAFaGiKYXqlKhqHDND/RJ
oJnZfgEYca6urTKHgmYYu3SJRe6Gf3j/1NZZJ9EKr9vGIiFhpKOHcdnBkeXEweWK
7+8yyCxG7Uw3Ixw7jQPQ/bAzb7CJ8Q2Ogsj1EqSHbPoOyu2a0fYCu3rLomDVNdbn
9BX2ywyiUonV36H773DrAbe0a/+LhThW9SftUyeyK+0RIU4PML0C5rQWUxNsz6yR
OIkfFdRA10nOqIfV2jVHbNT7bnMI9HLBpltbfoY7SBEFncRdEbkGvemjUIB7YAe4
sjosi65MZ/BcCUcC0s8tczl1/e/UjoJ/uC91APKFSmKu2RltBNVTLxSpAxZv+2bV
SGI=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions internal/test/data/tls/dummyServer.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA1ASsIQxxpco+RhToQ5ch+uwbFvZfwNq52ttVlZ6uvQDTfVpm
ohad9gvtga6hfA2MLT8qGk4F3flkPMJrY5WMKd+/D++N7wYH+rWs2WCmwmuz2bDT
CwnSNCrq13+osDpAmO80EHDo4kk1FbX69NPSxYHPCgXbsQYshHtbV6Ddaq41GYLc
v9ORLVtPUBRNl6lRUEuIzegguMIZjdye8lh8mb9ytZTtGngt9ZCDqsNPw8Q/w4/0
TCQac9Kt43mCcLzN6xOAzWo01ugS4j1lZC1uB96WkROo2H8gkWsxPLSy1h8xhqj1
lBJVdqyls3Vej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GBwRhx9SbiVLx9Fgmm
OGjGBY0CgYEAr2Go4s2QBQXpvJPdYSUGi/osF1jfntIL0JydqBOy9xx9R6d/CvWX
8ekhlYdXLZPmgUoZ8EIBlULRs+8XJRn1zXGBUu1hEdU8rO7WdkHV6tJBIw6mSppV
btGQ8m0qKBLMJco77ZZCjp5lsG/Af308ojuK/DJcomH1slxyTkSYEjg=
-----END RSA PRIVATE KEY-----
1 change: 1 addition & 0 deletions internal/test/data/tls/invalid.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid
1 change: 1 addition & 0 deletions internal/test/data/tls/invalid.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid
18 changes: 18 additions & 0 deletions internal/test/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
package comment should be of the form "Package test ..." (golint)

// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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
//
// https://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.
//

kevindiu marked this conversation as resolved.
Show resolved Hide resolved
// Package test provides functions for general testing use
package test
42 changes: 42 additions & 0 deletions internal/test/testdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
package comment should be of the form "Package test ..." (golint)

// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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
//
// https://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 test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
ST1000: at least one file in a package should have a package comment (stylecheck)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevindiu please submit package description

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it on internal/test/doc.go :(


import (
"os"
"path/filepath"
"strings"
)

// GetTestdataPath returns the test data file path under `internal/test/data`
func GetTestdataPath(filename string) string {
fp, _ := filepath.Abs(baseDir() + "/internal/test/data/" + filename)
return fp
}

func baseDir() string {
wd, err := os.Getwd()
if err != nil {
return ""
}

for cur := filepath.Dir(wd); cur != "/"; cur = filepath.Dir(cur) {
if strings.HasSuffix(cur, "vald") {
return cur
}
}
return ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
return statements should not be cuddled if block has more than two lines (wsl)

}
129 changes: 129 additions & 0 deletions internal/test/testdata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// 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
//
// https://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 test

import (
"os"
"reflect"
"testing"

"github.com/pkg/errors"
"go.uber.org/goleak"
)

func TestGetTestdataPath(t *testing.T) {
type args struct {
filename string
}
type want struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
declarations should never be cuddled (wsl)

want string
}
type test struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
declarations should never be cuddled (wsl)

name string
args args
want want
checkFunc func(want, string) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
assignments should only be cuddled with other assignments (wsl)

if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got = %v, want %v", got, w.want)
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
return statements should not be cuddled if block has more than two lines (wsl)

}
tests := []test{
{
name: "returns the result of combining the given path and test directory",
args: args{
filename: "tls/dummyCa.pem",
},
want: want{
want: func() string {
return baseDir() + "/internal/test/data/tls/dummyCa.pem"
}(),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := GetTestdataPath(test.args.filename)
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func Test_baseDir(t *testing.T) {
type want struct {
want string
}
type test struct {
name string
want want
checkFunc func(want, string) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
assignments should only be cuddled with other assignments (wsl)

if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got = %v, want %v", got, w.want)
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[golangci] reported by reviewdog 🐶
return statements should not be cuddled if block has more than two lines (wsl)

}
tests := []test{
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add an abnormal test ?

Copy link
Contributor Author

@kevindiu kevindiu Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your comment. Since it only return the file path, I couldn't think any abnormal test....
do you have any idea?>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevindiu please add error pattern test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanna to add it but I couldn't think of any error pattern for testing (as it called os.Getwd(). Do you have any idea?

name: "check testdata.go exists",
checkFunc: func(w want, got string) error {
_, err := os.Stat(got + "/internal/test/testdata.go")
return err
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := baseDir()
if err := test.checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}
52 changes: 48 additions & 4 deletions internal/tls/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,51 @@ import "crypto/tls"

type Option func(*credentials) error

var (
defaultOpts = []Option{}
)
func defaultOptions() []Option {
return []Option{
WithTLSConfig(&tls.Config{
MinVersion: tls.VersionTLS12,
NextProtos: []string{
"http/1.1",
"h2",
},
CurvePreferences: []tls.CurveID{
tls.CurveP521,
tls.CurveP384,
tls.CurveP256,
tls.X25519,
},
SessionTicketsDisabled: true,
// PreferServerCipherSuites: true,
// CipherSuites: []uint16{
// tls.TLS_RSA_WITH_RC4_128_SHA,
// tls.TLS_RSA_WITH_AES_128_CBC_SHA,
// tls.TLS_RSA_WITH_AES_256_CBC_SHA,
// tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
// tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
// tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
// tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
// tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
// tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
// tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
// tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
// tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
// tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, // Maybe this is work on TLS 1.2
// tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, // TLS1.3 Feature
// tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, // TLS1.3 Feature
// tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, // Go 1.8 only
// tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, // Go 1.8 only
// },
ClientAuth: tls.NoClientCert,
}),
}
}

func WithCert(cert string) Option {
return func(c *credentials) error {
Expand All @@ -48,7 +90,9 @@ func WithCa(ca string) Option {

func WithTLSConfig(cfg *tls.Config) Option {
return func(c *credentials) error {
c.cfg = cfg
if cfg != nil {
c.cfg = cfg
}
return nil
}
}
Loading