2023-02-05 05:12:31 -05:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package cargo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParsePackage(t *testing.T) {
|
2024-11-11 21:57:30 -05:00
|
|
|
const (
|
|
|
|
description = "Package Description"
|
|
|
|
author = "KN4CK3R"
|
|
|
|
homepage = "https://gitea.io/"
|
|
|
|
license = "MIT"
|
|
|
|
payload = "gitea test dummy payload" // a fake payload for test only
|
|
|
|
)
|
|
|
|
makeDefaultPackageMeta := func(name, version string) string {
|
|
|
|
return `{
|
2023-02-05 05:12:31 -05:00
|
|
|
"name":"` + name + `",
|
|
|
|
"vers":"` + version + `",
|
|
|
|
"description":"` + description + `",
|
|
|
|
"authors": ["` + author + `"],
|
|
|
|
"deps":[
|
|
|
|
{
|
|
|
|
"name":"dep",
|
|
|
|
"version_req":"1.0"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"homepage":"` + homepage + `",
|
|
|
|
"license":"` + license + `"
|
|
|
|
}`
|
2024-11-11 21:57:30 -05:00
|
|
|
}
|
|
|
|
createPackage := func(metadata string) io.Reader {
|
2023-02-05 05:12:31 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
binary.Write(&buf, binary.LittleEndian, uint32(len(metadata)))
|
|
|
|
buf.WriteString(metadata)
|
2024-11-11 21:57:30 -05:00
|
|
|
binary.Write(&buf, binary.LittleEndian, uint32(len(payload)))
|
|
|
|
buf.WriteString(payload)
|
2023-02-05 05:12:31 -05:00
|
|
|
return &buf
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("InvalidName", func(t *testing.T) {
|
|
|
|
for _, name := range []string{"", "0test", "-test", "_test", strings.Repeat("a", 65)} {
|
2024-11-11 21:57:30 -05:00
|
|
|
data := createPackage(makeDefaultPackageMeta(name, "1.0.0"))
|
2023-02-05 05:12:31 -05:00
|
|
|
|
|
|
|
cp, err := ParsePackage(data)
|
|
|
|
assert.Nil(t, cp)
|
|
|
|
assert.ErrorIs(t, err, ErrInvalidName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("InvalidVersion", func(t *testing.T) {
|
|
|
|
for _, version := range []string{"", "1.", "-1.0", "1.0.0/1"} {
|
2024-11-11 21:57:30 -05:00
|
|
|
data := createPackage(makeDefaultPackageMeta("test", version))
|
2023-02-05 05:12:31 -05:00
|
|
|
|
|
|
|
cp, err := ParsePackage(data)
|
|
|
|
assert.Nil(t, cp)
|
|
|
|
assert.ErrorIs(t, err, ErrInvalidVersion)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
2024-11-11 21:57:30 -05:00
|
|
|
data := createPackage(makeDefaultPackageMeta("test", "1.0.0"))
|
2023-02-05 05:12:31 -05:00
|
|
|
|
|
|
|
cp, err := ParsePackage(data)
|
|
|
|
assert.NotNil(t, cp)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "test", cp.Name)
|
|
|
|
assert.Equal(t, "1.0.0", cp.Version)
|
|
|
|
assert.Equal(t, description, cp.Metadata.Description)
|
|
|
|
assert.Equal(t, []string{author}, cp.Metadata.Authors)
|
|
|
|
assert.Len(t, cp.Metadata.Dependencies, 1)
|
|
|
|
assert.Equal(t, "dep", cp.Metadata.Dependencies[0].Name)
|
2024-11-11 21:57:30 -05:00
|
|
|
assert.Nil(t, cp.Metadata.Dependencies[0].Package)
|
2023-02-05 05:12:31 -05:00
|
|
|
assert.Equal(t, homepage, cp.Metadata.ProjectURL)
|
|
|
|
assert.Equal(t, license, cp.Metadata.License)
|
|
|
|
content, _ := io.ReadAll(cp.Content)
|
2024-11-11 21:57:30 -05:00
|
|
|
assert.Equal(t, payload, string(content))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Renamed", func(t *testing.T) {
|
|
|
|
data := createPackage(`{
|
|
|
|
"name":"test-pkg",
|
|
|
|
"vers":"1.0",
|
|
|
|
"description":"test-desc",
|
|
|
|
"authors": ["test-author"],
|
|
|
|
"deps":[
|
|
|
|
{
|
|
|
|
"name":"dep-renamed",
|
|
|
|
"explicit_name_in_toml":"dep-explicit",
|
|
|
|
"version_req":"1.0"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"homepage":"https://gitea.io/",
|
|
|
|
"license":"MIT"
|
|
|
|
}`)
|
|
|
|
cp, err := ParsePackage(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test-pkg", cp.Name)
|
|
|
|
assert.Equal(t, "https://gitea.io/", cp.Metadata.ProjectURL)
|
|
|
|
assert.Equal(t, "dep-explicit", cp.Metadata.Dependencies[0].Name)
|
|
|
|
assert.Equal(t, "dep-renamed", *cp.Metadata.Dependencies[0].Package)
|
2023-02-05 05:12:31 -05:00
|
|
|
})
|
|
|
|
}
|