Skip to content

Commit

Permalink
Add golangci-lint and fix lint issues; add CI for macOS and windows
Browse files Browse the repository at this point in the history
CI adds golangci-lint check.

Fix golangci-lint issues.
- Set max length of line to 120
- Test cases add t.Parallel()
- Test helpers add t.Helper()
- Warp the error with fmt.Errorf()
- Rename variables from C style to Golang style
- Add blank line.

Add CI for macOS and Windows.
Fix ldflags for windows.
Fix compile errors on windows.
  • Loading branch information
dongbeiouba committed Nov 4, 2024
1 parent f7742f8 commit 005d7fe
Show file tree
Hide file tree
Showing 63 changed files with 3,509 additions and 2,674 deletions.
230 changes: 214 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: CI
on: [push, pull_request]

jobs:
build:
golang-lint:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
Expand All @@ -22,34 +22,74 @@ jobs:
gofmt-path: './'
gofmt-flags: '-l -d'

- name: Go Mod
run: go mod tidy

- name: Clone Tongsuo
uses: actions/checkout@v3
with:
repository: Tongsuo-Project/Tongsuo
path: tongsuo
path: Tongsuo
ref: 8.3-stable

- name: Build Tongsuo
run: |
cd tongsuo
./config --prefix=/opt/tongsuo --libdir=/opt/tongsuo/lib enable-ntls
cd Tongsuo
./config --prefix=${RUNNER_TEMP}/tongsuo --libdir=${RUNNER_TEMP}/tongsuo/lib enable-ntls
make -j4
make install
- name: Go Mod
run: go mod tidy
- name: Golang lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.61.0
LD_LIBRARY_PATH=${RUNNER_TEMP}/tongsuo/lib CGO_CFLAGS="-Wall -I${RUNNER_TEMP}/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L${RUNNER_TEMP}/tongsuo/lib" golangci-lint run ./...
- name: Go vet Check
run: LD_LIBRARY_PATH=/opt/tongsuo/lib CGO_CFLAGS="-Wall -I/opt/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L/opt/tongsuo/lib" go vet ./...
build-and-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Clone Tongsuo
uses: actions/checkout@v3
with:
repository: Tongsuo-Project/Tongsuo
path: Tongsuo
ref: 8.3-stable

- name: Build Tongsuo
run: |
cd Tongsuo
./config --prefix=${RUNNER_TEMP}/tongsuo --libdir=${RUNNER_TEMP}/tongsuo/lib enable-ntls
make -j4
make install
- name: Build
run: CGO_CFLAGS="-Wall -I/opt/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L/opt/tongsuo/lib" go build
run: CGO_CFLAGS="-Wall -I${RUNNER_TEMP}/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L${RUNNER_TEMP}/tongsuo/lib" go build

- name: Test
run: LD_LIBRARY_PATH=/opt/tongsuo/lib CGO_CFLAGS="-Wall -I/opt/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L/opt/tongsuo/lib" go test ./...
- name: Test on Ubuntu
run: LD_LIBRARY_PATH=${RUNNER_TEMP}/tongsuo/lib CGO_CFLAGS="-Wall -I${RUNNER_TEMP}/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L${RUNNER_TEMP}/tongsuo/lib" go test ./...
if: matrix.os == 'ubuntu-latest'

build_static:
runs-on: ubuntu-22.04
- name: Test on macOS
run: DYLD_LIBRARY_PATH=${RUNNER_TEMP}/tongsuo/lib CGO_CFLAGS="-Wall -I${RUNNER_TEMP}/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L${RUNNER_TEMP}/tongsuo/lib" go test ./...
if: matrix.os == 'macos-latest'

build-static:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v3

Expand All @@ -68,9 +108,167 @@ jobs:
- name: Build Tongsuo Static
run: |
cd tongsuo
./config --prefix=/opt/tongsuo --libdir=/opt/tongsuo/lib enable-ntls no-shared
./config --prefix=${RUNNER_TEMP}/tongsuo --libdir=${RUNNER_TEMP}/tongsuo/lib enable-ntls no-shared
make -j4
make install
- name: Test
run: LD_LIBRARY_PATH=/opt/tongsuo/lib CGO_CFLAGS="-Wall -I/opt/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L/opt/tongsuo/lib" go test ./...
- name: Build
run: CGO_CFLAGS="-Wall -I${RUNNER_TEMP}/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L${RUNNER_TEMP}/tongsuo/lib" go build

- name: Test on Ubuntu
run: LD_LIBRARY_PATH=${RUNNER_TEMP}/tongsuo/lib CGO_CFLAGS="-Wall -I${RUNNER_TEMP}/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L${RUNNER_TEMP}/tongsuo/lib" go test ./...
if: matrix.os == 'ubuntu-latest'

- name: Test on macOS
run: DYLD_LIBRARY_PATH=${RUNNER_TEMP}/tongsuo/lib CGO_CFLAGS="-Wall -I${RUNNER_TEMP}/tongsuo/include -Wno-deprecated-declarations" CGO_LDFLAGS="-L${RUNNER_TEMP}/tongsuo/lib" go test ./...
if: matrix.os == 'macos-latest'

build-on-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Clone Tongsuo
uses: actions/checkout@v3
with:
repository: Tongsuo-Project/Tongsuo
path: Tongsuo
ref: 8.3-stable
- uses: ilammy/msvc-dev-cmd@v1
- uses: ilammy/setup-nasm@v1
- uses: shogo82148/actions-setup-perl@v1
- name: Build Tongsuo
shell: cmd
run: |
mkdir _build
cd _build
perl ..\Configure VC-WIN64A no-makedepend --prefix=%RUNNER_TEMP%\tongsuo enable-ntls
nmake /S
nmake install
working-directory: Tongsuo

- name: Build
shell: cmd
run: |
set CGO_CFLAGS=-Wall -I%RUNNER_TEMP%\tongsuo\include -Wno-deprecated-declarations
set CGO_LDFLAGS=-L%RUNNER_TEMP%\tongsuo\lib"
go build
- name: debug
shell: cmd
run: |
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.41.34120\bin\HostX64\x64"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\VC\VCPackages"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\bin\Roslyn"
dir "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\"
dir "C:\Program Files (x86)\HTML Help Workshop"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Team Tools\DiagnosticsHub\Collector"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\Microsoft\CodeCoverage.Console"
dir "C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\\x64"
dir "C:\Program Files (x86)\Windows Kits\10\bin\\x64"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\\MSBuild\Current\Bin\amd64"
dir "C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\"
dir "C:\Users\runneradmin\go\bin;C:\hostedtoolcache\windows\go\1.19.13\x64\bin"
dir "C:\Program Files\MongoDB\Server\5.0\bin"
dir "C:\aliyun-cli"
dir "C:\vcpkg"
dir "C:\Program Files (x86)\NSIS\"
dir "C:\tools\zstd"
dir "C:\Program Files\Mercurial\"
dir "C:\hostedtoolcache\windows\stack\3.1.1\x64"
dir "C:\cabal\bin"
dir "C:\\ghcup\bin"
dir "C:\mingw64\bin"
dir "C:\Program Files\dotnet"
dir "C:\Program Files\MySQL\MySQL Server 8.0\bin"
dir "C:\Program Files\R\R-4.4.1\bin\x64"
dir "C:\SeleniumWebDrivers\GeckoDriver"
dir "C:\SeleniumWebDrivers\EdgeDriver\"
dir "C:\SeleniumWebDrivers\ChromeDriver"
dir "C:\Program Files (x86)\sbt\bin"
dir "C:\Program Files (x86)\GitHub CLI"
dir "C:\Program Files\Git\bin"
dir "C:\Program Files (x86)\pipx_bin"
dir "C:\npm\prefix"
dir "C:\hostedtoolcache\windows\go\1.21.13\x64\bin"
dir "C:\hostedtoolcache\windows\Python\3.9.13\x64\Scripts"
dir "C:\hostedtoolcache\windows\Python\3.9.13\x64"
dir "C:\hostedtoolcache\windows\Ruby\3.0.7\x64\bin"
dir "C:\Program Files\OpenSSL\bin"
dir "C:\tools\kotlinc\bin"
dir "C:\hostedtoolcache\windows\Java_Temurin-Hotspot_jdk\8.0.432-6\x64\bin"
dir "C:\Program Files\ImageMagick-7.1.1-Q16-HDRI"
dir "C:\Program Files\Microsoft SDKs\Azure\CLI2\wbin"
dir "C:\ProgramData\kind"
dir "C:\ProgramData\Chocolatey\bin"
dir "C:\Windows\system32"
dir "C:\Windows"
dir "C:\Windows\System32\Wbem"
dir "C:\Windows\System32\WindowsPowerShell\v1.0\"
dir "C:\Windows\System32\OpenSSH\"
dir "C:\Program Files\dotnet\"
dir "C:\Program Files\PowerShell\7\"
dir "C:\Program Files\Microsoft\Web Platform Installer\"
dir "C:\Program Files\TortoiseSVN\bin"
dir "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\"
dir "C:\Program Files\Microsoft SQL Server\150\Tools\Binn\"
dir "C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\"
dir "C:\Program Files (x86)\WiX Toolset v3.14\bin"
dir "C:\Program Files\Microsoft SQL Server\130\DTS\Binn\"
dir "C:\Program Files\Microsoft SQL Server\140\DTS\Binn\"
dir "C:\Program Files\Microsoft SQL Server\150\DTS\Binn\"
dir "C:\Program Files\Microsoft SQL Server\160\DTS\Binn\"
dir "C:\Strawberry\c\bin"
dir "C:\Strawberry\perl\site\bin"
dir "C:\Strawberry\perl\bin"
dir "C:\ProgramData\chocolatey\lib\pulumi\tools\Pulumi\bin"
dir "C:\Program Files\CMake\bin"
dir "C:\ProgramData\chocolatey\lib\maven\apache-maven-3.8.7\bin"
dir "C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code"
dir "C:\Program Files\Microsoft SDKs\Service Fabric\Tools\ServiceFabricLocalClusterManager"
dir "C:\Program Files\nodejs\"
dir "C:\Program Files\Git\cmd"
dir "C:\Program Files\Git\mingw64\bin"
dir "C:\Program Files\Git\usr\bin"
dir "C:\Program Files\GitHub CLI\"
dir "c:\tools\php"
dir "C:\Program Files\Amazon\AWSCLIV2\"
dir "C:\Program Files\Amazon\SessionManagerPlugin\bin\"
dir "C:\Program Files\Amazon\AWSSAMCLI\bin\"
dir "C:\Program Files\Microsoft SQL Server\130\Tools\Binn\"
dir "C:\Program Files\LLVM\bin"
dir "C:\Users\runneradmin\.dotnet\tools"
dir "C:\Users\runneradmin\.cargo\bin"
dir "C:\Users\runneradmin\AppData\Local\Microsoft\WindowsApps"
dir "C:\Program Files (x86)\Microsoft Visual Studio\Installer"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\Llvm\x64\bin"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\VC\Linux\bin\ConnectionManagerExe"
dir "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\vcpkg"
- name: Set PATH for go test runtime library search
shell: perl {0}
run: |
use Actions::Core;
add_path("$ENV{RUNNER_TEMP}\\tongsuo\\bin");
add_path("$ENV{RUNNER_TEMP}\\tongsuo\\lib");
- name: Test on Windows
shell: cmd
run: |
set CGO_CFLAGS=-Wall -I%RUNNER_TEMP%\tongsuo\include -Wno-deprecated-declarations
set CGO_LDFLAGS=-L%RUNNER_TEMP%\tongsuo\lib
go env
echo %PATH%
go test ./...
89 changes: 89 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
linters:
enable-all: true
disable:
- ireturn
- gochecknoinits
- exhaustruct
- nlreturn

linters-settings:
cyclop:
max-complexity: 20
interfacebloat:
max: 11
lll:
line-length: 120
funlen:
lines: 120
statements: 80
ignore-comments: true
depguard:
rules:
main:
allow:
- $gostd
- github.com/tongsuo-project/tongsuo-go-sdk
- github.com/tongsuo-project/tongsuo-go-sdk/crypto
- github.com/tongsuo-project/tongsuo-go-sdk/utils

issues:
exclude:
- "variable name '(i|e|n|wg|md|ok|ca|bn|iv|ip|rv|rc|fn)' is too short for the scope of its usage"
- "parameter name '(e|r|s|ok|in|ip|iv|fn|rv)' is too short for the scope of its usage"
exclude-rules:
- path: crypto/sha1/sha1_test.go
linters:
- gosec
- path: crypto/md5/md5_test.go
linters:
- gosec
- path: conn.go
text: "Error return value of `c.flushOutputBuffer` is not checked"
- path: utils/errors.go
text: "do not define dynamic errors, use wrapped static errors instead:"
- path: ntls_test.go
text: "Error return value of `server.(Run|RunForALPN)` is not checked"
- path: ssl_test.go
text: "G402: TLS InsecureSkipVerify set true."
- path: crypto/key_test.go
text: "G101: Potential hardcoded credentials: (RSA|SSH \\(EC\\)) private key"
- path: ssl_test.go
text: "G101: Potential hardcoded credentials: (RSA|SSH \\(EC\\)) private key"
- path: ssl_test.go
text: "G402: TLS MinVersion too low."
- path: ctx.go
text: "Consider pre-allocating `protoList`"
- path: crypto/ciphers_gcm.go
text: "Magic number: (128|192|256), in <case> detected"
- path: .*\.go
text: "dupSubExpr: suspicious identical LHS and RHS for `==` operator"
- path: crypto/sm2/sm2.go
text: "return with no blank line before"
- path: crypto/bio.go
text: "return with no blank line before"
- path: crypto/bio.go
text: "(readBioMapping|writeBioMapping) is a global variable"
- path: crypto/key_test.go
text: "Function '(TestMarshal|TestMarshalEC)' has too many statements"
- path: ctx.go
text: "sslCtxIdx is a global variable"
- path: ssl.go
text: "sslIdx is a global variable"
- path: .*_test\.go
text: "cognitive complexity (.*) of func `(TestMarshalEC|TestMarshal|TestSessionReuse|TestNTLS)` is high"
- path: .*_test\.go
text: "cyclomatic complexity (.*) of func `(TestMarshalEC|TestMarshal)` is high"
- path: .*_test\.go
text: "calculated cyclomatic complexity for function (TestMarshal|TestMarshalEC) is (.*), max is (.*)"
- path: .*_test\.go
text: "error returned from external package is unwrapped"
- path: crypto/key.go
text: "`if curve == SM2Curve` has complex nested blocks \\(complexity: 6\\)"
- path: crypto/init.go
text: "do not define dynamic errors, use wrapped static errors instead:"
- path: http.go
text: "http.go:(.*): Line contains TODO/BUG/FIXME: \"TODO: http client integration\""
- path: ctx.go
text: "errorf: should replace errors.New"
- path: ctx.go
text: "do not define dynamic errors, use wrapped static errors instead:"
2 changes: 1 addition & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ package tongsuogo
// #cgo linux LDFLAGS: -lssl -lcrypto
// #cgo darwin LDFLAGS: -lssl -lcrypto
// #cgo windows CFLAGS: -DWIN32_LEAN_AND_MEAN
// #cgo windows pkg-config: libssl libcrypto
// #cgo windows LDFLAGS: -lssl -lcrypto
import "C"
2 changes: 1 addition & 1 deletion build_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ package tongsuogo
// #cgo linux LDFLAGS: -extldflags -static -lssl -lcrypto
// #cgo darwin LDFLAGS: -extldflags -static -lssl -lcrypto
// #cgo windows CFLAGS: -DWIN32_LEAN_AND_MEAN
// #cgo windows pkg-config: libssl libcrypto
// #cgo windows LDFLAGS: -extldflags -static -lssl -lcrypto
import "C"
Loading

0 comments on commit 005d7fe

Please sign in to comment.