github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/nuget/symbol_extractor_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package nuget
     7  
     8  import (
     9  	"archive/zip"
    10  	"bytes"
    11  	"encoding/base64"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  const pdbContent = `QlNKQgEAAQAAAAAADAAAAFBEQiB2MS4wAAAAAAAABgB8AAAAWAAAACNQZGIAAAAA1AAAAAgBAAAj
    18  fgAA3AEAAAQAAAAjU3RyaW5ncwAAAADgAQAABAAAACNVUwDkAQAAMAAAACNHVUlEAAAAFAIAACgB
    19  AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`
    20  
    21  func TestExtractPortablePdb(t *testing.T) {
    22  	createArchive := func(name string, content []byte) []byte {
    23  		var buf bytes.Buffer
    24  		archive := zip.NewWriter(&buf)
    25  		w, _ := archive.Create(name)
    26  		w.Write(content)
    27  		archive.Close()
    28  		return buf.Bytes()
    29  	}
    30  
    31  	t.Run("MissingPdbFiles", func(t *testing.T) {
    32  		var buf bytes.Buffer
    33  		zip.NewWriter(&buf).Close()
    34  
    35  		pdbs, err := ExtractPortablePdb(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
    36  		assert.ErrorIs(t, err, ErrMissingPdbFiles)
    37  		assert.Empty(t, pdbs)
    38  	})
    39  
    40  	t.Run("InvalidFiles", func(t *testing.T) {
    41  		data := createArchive("sub/test.bin", []byte{})
    42  
    43  		pdbs, err := ExtractPortablePdb(bytes.NewReader(data), int64(len(data)))
    44  		assert.ErrorIs(t, err, ErrInvalidFiles)
    45  		assert.Empty(t, pdbs)
    46  	})
    47  
    48  	t.Run("Valid", func(t *testing.T) {
    49  		b, _ := base64.StdEncoding.DecodeString(pdbContent)
    50  		data := createArchive("test.pdb", b)
    51  
    52  		pdbs, err := ExtractPortablePdb(bytes.NewReader(data), int64(len(data)))
    53  		assert.NoError(t, err)
    54  		assert.Len(t, pdbs, 1)
    55  		assert.Equal(t, "test.pdb", pdbs[0].Name)
    56  		assert.Equal(t, "d910bb6948bd4c6cb40155bcf52c3c94", pdbs[0].ID)
    57  		pdbs.Close()
    58  	})
    59  }
    60  
    61  func TestParseDebugHeaderID(t *testing.T) {
    62  	t.Run("InvalidPdbMagicNumber", func(t *testing.T) {
    63  		id, err := ParseDebugHeaderID(bytes.NewReader([]byte{0, 0, 0, 0}))
    64  		assert.ErrorIs(t, err, ErrInvalidPdbMagicNumber)
    65  		assert.Empty(t, id)
    66  	})
    67  
    68  	t.Run("MissingPdbStream", func(t *testing.T) {
    69  		b, _ := base64.StdEncoding.DecodeString(`QlNKQgEAAQAAAAAADAAAAFBEQiB2MS4wAAAAAAAAAQB8AAAAWAAAACNVUwA=`)
    70  
    71  		id, err := ParseDebugHeaderID(bytes.NewReader(b))
    72  		assert.ErrorIs(t, err, ErrMissingPdbStream)
    73  		assert.Empty(t, id)
    74  	})
    75  
    76  	t.Run("Valid", func(t *testing.T) {
    77  		b, _ := base64.StdEncoding.DecodeString(pdbContent)
    78  
    79  		id, err := ParseDebugHeaderID(bytes.NewReader(b))
    80  		assert.NoError(t, err)
    81  		assert.Equal(t, "d910bb6948bd4c6cb40155bcf52c3c94", id)
    82  	})
    83  }