Skip to content

Commit

Permalink
Fix race condition with unused code
Browse files Browse the repository at this point in the history
encodeIndent was only there for debugging and not currently used for anything.
This was causing some projects downstream to fail testing with -race enabled.
+ Added `-race` to go test and made ber.go tests run in parallel
  • Loading branch information
hneiva committed Sep 4, 2024
1 parent 33d0574 commit 571191d
Show file tree
Hide file tree
Showing 5 changed files with 518 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
go-version: ${{ matrix.go }}
stable: false
- name: Test
run: go vet . && go build . && go test -count=1 -covermode=count -coverprofile=coverage.out .
run: go vet . && go build . && go test -race -covermode=atomic -count=1 -coverprofile=coverage.out .
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
all: vet staticcheck test

test:
go test -covermode=count -coverprofile=coverage.out .
go test -race -covermode=atomic -count=1 -coverprofile=coverage.out .

showcoverage: test
go tool cover -html=coverage.out
Expand Down
9 changes: 0 additions & 9 deletions ber.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
)

var encodeIndent = 0

type asn1Object interface {
EncodeTo(writer *bytes.Buffer) error
}
Expand All @@ -17,16 +15,13 @@ type asn1Structured struct {
}

func (s asn1Structured) EncodeTo(out *bytes.Buffer) error {
//fmt.Printf("%s--> tag: % X\n", strings.Repeat("| ", encodeIndent), s.tagBytes)
encodeIndent++
inner := new(bytes.Buffer)
for _, obj := range s.content {
err := obj.EncodeTo(inner)
if err != nil {
return err
}
}
encodeIndent--
out.Write(s.tagBytes)
encodeLength(out, inner.Len())
out.Write(inner.Bytes())
Expand All @@ -47,18 +42,14 @@ func (p asn1Primitive) EncodeTo(out *bytes.Buffer) error {
if err = encodeLength(out, p.length); err != nil {
return err
}
//fmt.Printf("%s--> tag: % X length: %d\n", strings.Repeat("| ", encodeIndent), p.tagBytes, p.length)
//fmt.Printf("%s--> content length: %d\n", strings.Repeat("| ", encodeIndent), len(p.content))
out.Write(p.content)

return nil
}

func ber2der(ber []byte) ([]byte, error) {
if len(ber) == 0 {
return nil, errors.New("ber2der: input ber is empty")
}
//fmt.Printf("--> ber2der: Transcoding %d bytes\n", len(ber))
out := new(bytes.Buffer)

obj, _, err := readObject(ber, 0)
Expand Down
4 changes: 4 additions & 0 deletions ber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestBer2Der(t *testing.T) {
t.Parallel()
// indefinite length fixture
ber := []byte{0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00}
expected := []byte{0x30, 0x03, 0x02, 0x01, 0x01}
Expand Down Expand Up @@ -40,6 +41,7 @@ func TestBer2Der(t *testing.T) {
}

func TestBer2Der_Negatives(t *testing.T) {
t.Parallel()
fixtures := []struct {
Input []byte
ErrorContains string
Expand All @@ -65,6 +67,7 @@ func TestBer2Der_Negatives(t *testing.T) {
}

func TestBer2Der_NestedMultipleIndefinite(t *testing.T) {
t.Parallel()
// indefinite length fixture
ber := []byte{0x30, 0x80, 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00, 0x30, 0x80, 0x02, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00}
expected := []byte{0x30, 0x0A, 0x30, 0x03, 0x02, 0x01, 0x01, 0x30, 0x03, 0x02, 0x01, 0x02}
Expand Down Expand Up @@ -101,6 +104,7 @@ func TestBer2Der_NestedMultipleIndefinite(t *testing.T) {
}

func TestVerifyIndefiniteLengthBer(t *testing.T) {
t.Parallel()
decoded := mustDecodePEM([]byte(testPKCS7))

_, err := ber2der(decoded)
Expand Down
Loading

0 comments on commit 571191d

Please sign in to comment.