-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
cyclonedx_test.go
164 lines (148 loc) · 4.85 KB
/
cyclonedx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// This file is part of CycloneDX Go
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) Niklas Düster. All Rights Reserved.
package cyclonedx
import (
"encoding/json"
"encoding/xml"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBool(t *testing.T) {
assert.Equal(t, true, *Bool(true))
assert.Equal(t, false, *Bool(false))
}
func TestDependency_MarshalJSON(t *testing.T) {
// Marshal empty dependency
dependency := Dependency{}
jsonBytes, err := json.Marshal(dependency)
assert.NoError(t, err)
assert.Equal(t, "{\"ref\":\"\"}", string(jsonBytes))
// Marshal dependency with empty dependencies
dependency = Dependency{
Ref: "dependencyRef",
Dependencies: &[]Dependency{},
}
jsonBytes, err = json.Marshal(dependency)
assert.NoError(t, err)
assert.Equal(t, "{\"ref\":\"dependencyRef\"}", string(jsonBytes))
// Marshal dependency with dependencies
dependency = Dependency{
Ref: "dependencyRef",
Dependencies: &[]Dependency{
{Ref: "transitiveDependencyRef"},
},
}
jsonBytes, err = json.Marshal(dependency)
assert.NoError(t, err)
assert.Equal(t, "{\"ref\":\"dependencyRef\",\"dependsOn\":[\"transitiveDependencyRef\"]}", string(jsonBytes))
}
func TestDependency_UnmarshalJSON(t *testing.T) {
// Unmarshal empty dependency
dependency := new(Dependency)
err := json.Unmarshal([]byte("{}"), dependency)
assert.NoError(t, err)
assert.Equal(t, "", dependency.Ref)
assert.Nil(t, dependency.Dependencies)
// Unmarshal dependency with empty dependencies
dependency = new(Dependency)
err = json.Unmarshal([]byte("{\"ref\":\"dependencyRef\",\"dependsOn\":[]}"), dependency)
assert.NoError(t, err)
assert.Equal(t, "dependencyRef", dependency.Ref)
assert.Nil(t, dependency.Dependencies)
// Unmarshal dependency with dependencies
dependency = new(Dependency)
err = json.Unmarshal([]byte("{\"ref\":\"dependencyRef\",\"dependsOn\":[\"transitiveDependencyRef\"]}"), dependency)
assert.NoError(t, err)
assert.Equal(t, "dependencyRef", dependency.Ref)
assert.Equal(t, 1, len(*dependency.Dependencies))
assert.Equal(t, "transitiveDependencyRef", (*dependency.Dependencies)[0].Ref)
}
func TestLicenses_MarshalXML(t *testing.T) {
// Marshal license and expressions
licenses := Licenses{
LicenseChoice{
Expression: "expressionValue1",
},
LicenseChoice{
License: &License{
ID: "licenseID",
URL: "licenseURL",
},
},
LicenseChoice{
Expression: "expressionValue2",
},
}
xmlBytes, err := xml.MarshalIndent(licenses, "", " ")
assert.NoError(t, err)
assert.Equal(t, `<Licenses>
<expression>expressionValue1</expression>
<license>
<id>licenseID</id>
<url>licenseURL</url>
</license>
<expression>expressionValue2</expression>
</Licenses>`, string(xmlBytes))
// Should return error when both license and expression are set on an element
licenses = Licenses{
LicenseChoice{
License: &License{
ID: "licenseID",
},
Expression: "expressionValue",
},
}
_, err = xml.Marshal(licenses)
assert.Error(t, err)
// Should encode nothing when empty
licenses = Licenses{}
xmlBytes, err = xml.Marshal(licenses)
assert.NoError(t, err)
assert.Nil(t, xmlBytes)
}
func TestLicenses_UnmarshalXML(t *testing.T) {
// Unmarshal license and expressions
licenses := new(Licenses)
err := xml.Unmarshal([]byte(`
<Licenses>
<expression>expressionValue1</expression>
<license>
<id>licenseID</id>
<url>licenseURL</url>
</license>
<expression>expressionValue2</expression>
</Licenses>`), licenses)
assert.NoError(t, err)
assert.Len(t, *licenses, 3)
assert.Nil(t, (*licenses)[0].License)
assert.Equal(t, "expressionValue1", (*licenses)[0].Expression)
assert.NotNil(t, (*licenses)[1].License)
assert.Equal(t, "licenseID", (*licenses)[1].License.ID)
assert.Equal(t, "licenseURL", (*licenses)[1].License.URL)
assert.Empty(t, (*licenses)[1].Expression)
assert.Nil(t, (*licenses)[2].License)
assert.Equal(t, "expressionValue2", (*licenses)[2].Expression)
// Unmarshal empty licenses
licenses = new(Licenses)
err = xml.Unmarshal([]byte("<Licenses></Licenses>"), licenses)
assert.NoError(t, err)
assert.Empty(t, *licenses)
// Should return error when an element is neither license nor expression
licenses = new(Licenses)
err = xml.Unmarshal([]byte("<Licenses><somethingElse>expressionValue</somethingElse></Licenses>"), licenses)
assert.Error(t, err)
}