2022-03-30 11:42:47 +03:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-30 11:42:47 +03:00
|
|
|
|
|
|
|
package nuget
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-30 11:42:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const pdbContent = `QlNKQgEAAQAAAAAADAAAAFBEQiB2MS4wAAAAAAAABgB8AAAAWAAAACNQZGIAAAAA1AAAAAgBAAAj
|
|
|
|
fgAA3AEAAAQAAAAjU3RyaW5ncwAAAADgAQAABAAAACNVUwDkAQAAMAAAACNHVUlEAAAAFAIAACgB
|
|
|
|
AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`
|
|
|
|
|
|
|
|
func TestExtractPortablePdb(t *testing.T) {
|
|
|
|
createArchive := func(name string, content []byte) []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
archive := zip.NewWriter(&buf)
|
|
|
|
w, _ := archive.Create(name)
|
|
|
|
w.Write(content)
|
|
|
|
archive.Close()
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("MissingPdbFiles", func(t *testing.T) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
zip.NewWriter(&buf).Close()
|
|
|
|
|
|
|
|
pdbs, err := ExtractPortablePdb(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
|
2024-07-30 22:41:10 +03:00
|
|
|
require.ErrorIs(t, err, ErrMissingPdbFiles)
|
2022-03-30 11:42:47 +03:00
|
|
|
assert.Empty(t, pdbs)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("InvalidFiles", func(t *testing.T) {
|
|
|
|
data := createArchive("sub/test.bin", []byte{})
|
|
|
|
|
|
|
|
pdbs, err := ExtractPortablePdb(bytes.NewReader(data), int64(len(data)))
|
2024-07-30 22:41:10 +03:00
|
|
|
require.ErrorIs(t, err, ErrInvalidFiles)
|
2022-03-30 11:42:47 +03:00
|
|
|
assert.Empty(t, pdbs)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
|
|
|
b, _ := base64.StdEncoding.DecodeString(pdbContent)
|
|
|
|
data := createArchive("test.pdb", b)
|
|
|
|
|
|
|
|
pdbs, err := ExtractPortablePdb(bytes.NewReader(data), int64(len(data)))
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-30 11:42:47 +03:00
|
|
|
assert.Len(t, pdbs, 1)
|
|
|
|
assert.Equal(t, "test.pdb", pdbs[0].Name)
|
|
|
|
assert.Equal(t, "d910bb6948bd4c6cb40155bcf52c3c94", pdbs[0].ID)
|
|
|
|
pdbs.Close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseDebugHeaderID(t *testing.T) {
|
|
|
|
t.Run("InvalidPdbMagicNumber", func(t *testing.T) {
|
|
|
|
id, err := ParseDebugHeaderID(bytes.NewReader([]byte{0, 0, 0, 0}))
|
2024-07-30 22:41:10 +03:00
|
|
|
require.ErrorIs(t, err, ErrInvalidPdbMagicNumber)
|
2022-03-30 11:42:47 +03:00
|
|
|
assert.Empty(t, id)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("MissingPdbStream", func(t *testing.T) {
|
|
|
|
b, _ := base64.StdEncoding.DecodeString(`QlNKQgEAAQAAAAAADAAAAFBEQiB2MS4wAAAAAAAAAQB8AAAAWAAAACNVUwA=`)
|
|
|
|
|
|
|
|
id, err := ParseDebugHeaderID(bytes.NewReader(b))
|
2024-07-30 22:41:10 +03:00
|
|
|
require.ErrorIs(t, err, ErrMissingPdbStream)
|
2022-03-30 11:42:47 +03:00
|
|
|
assert.Empty(t, id)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
|
|
|
b, _ := base64.StdEncoding.DecodeString(pdbContent)
|
|
|
|
|
|
|
|
id, err := ParseDebugHeaderID(bytes.NewReader(b))
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-30 11:42:47 +03:00
|
|
|
assert.Equal(t, "d910bb6948bd4c6cb40155bcf52c3c94", id)
|
|
|
|
})
|
|
|
|
}
|