18
18
package cyclonedx
19
19
20
20
import (
21
- "encoding/json"
22
- "encoding/xml"
23
21
"fmt"
24
22
"os/exec"
25
23
"strings"
26
24
"testing"
27
25
28
26
"github.com/bradleyjkemp/cupaloy/v2"
29
27
"github.com/stretchr/testify/assert"
30
- "github.com/stretchr/testify/require"
31
28
)
32
29
33
30
var snapShooter = cupaloy .NewDefaultConfig ().
@@ -38,193 +35,6 @@ func TestBool(t *testing.T) {
38
35
assert .Equal (t , false , * Bool (false ))
39
36
}
40
37
41
- func TestBOMReference_MarshalXML (t * testing.T ) {
42
- // Marshal empty bomRef
43
- bomRef := BOMReference ("" )
44
- xmlBytes , err := xml .Marshal (bomRef )
45
- assert .NoError (t , err )
46
- assert .Equal (t , "<BOMReference ref=\" \" ></BOMReference>" , string (xmlBytes ))
47
-
48
- // Marshal bomRef
49
- bomRef = "bomRef"
50
- xmlBytes , err = xml .Marshal (bomRef )
51
- assert .NoError (t , err )
52
- assert .Equal (t , "<BOMReference ref=\" bomRef\" ></BOMReference>" , string (xmlBytes ))
53
- }
54
-
55
- func TestBOMReference_UnmarshalXML (t * testing.T ) {
56
- // Unmarshal empty bomRef
57
- bomRef := new (BOMReference )
58
- err := xml .Unmarshal ([]byte ("<BOMReference ref=\" \" ></BOMReference>" ), bomRef )
59
- require .NoError (t , err )
60
- require .Equal (t , "" , string (* bomRef ))
61
-
62
- // Unmarshal bomRef
63
- err = xml .Unmarshal ([]byte ("<BOMReference ref=\" bomRef\" ></BOMReference>" ), bomRef )
64
- require .NoError (t , err )
65
- require .Equal (t , "bomRef" , string (* bomRef ))
66
- }
67
-
68
- func TestCopyright_MarshalXML (t * testing.T ) {
69
- // Marshal empty copyright
70
- copyright := Copyright {}
71
- xmlBytes , err := xml .Marshal (copyright )
72
- require .NoError (t , err )
73
- require .Equal (t , "<Copyright></Copyright>" , string (xmlBytes ))
74
-
75
- // Marshal copyright
76
- copyright .Text = "copyright"
77
- xmlBytes , err = xml .Marshal (copyright )
78
- require .NoError (t , err )
79
- require .Equal (t , "<Copyright>copyright</Copyright>" , string (xmlBytes ))
80
- }
81
-
82
- func TestCopyright_UnmarshalXML (t * testing.T ) {
83
- // Unmarshal empty copyright
84
- copyright := new (Copyright )
85
- err := xml .Unmarshal ([]byte ("<Copyright></Copyright>" ), copyright )
86
- require .NoError (t , err )
87
- require .Equal (t , "" , copyright .Text )
88
-
89
- // Unmarshal copyright
90
- err = xml .Unmarshal ([]byte ("<Copyright>copyright</Copyright>" ), copyright )
91
- require .NoError (t , err )
92
- require .Equal (t , "copyright" , copyright .Text )
93
- }
94
-
95
- func TestDependency_MarshalJSON (t * testing.T ) {
96
- // Marshal empty dependency
97
- dependency := Dependency {}
98
- jsonBytes , err := json .Marshal (dependency )
99
- assert .NoError (t , err )
100
- assert .Equal (t , "{\" ref\" :\" \" }" , string (jsonBytes ))
101
-
102
- // Marshal dependency with empty dependencies
103
- dependency = Dependency {
104
- Ref : "dependencyRef" ,
105
- Dependencies : & []Dependency {},
106
- }
107
- jsonBytes , err = json .Marshal (dependency )
108
- assert .NoError (t , err )
109
- assert .Equal (t , "{\" ref\" :\" dependencyRef\" }" , string (jsonBytes ))
110
-
111
- // Marshal dependency with dependencies
112
- dependency = Dependency {
113
- Ref : "dependencyRef" ,
114
- Dependencies : & []Dependency {
115
- {Ref : "transitiveDependencyRef" },
116
- },
117
- }
118
- jsonBytes , err = json .Marshal (dependency )
119
- assert .NoError (t , err )
120
- assert .Equal (t , "{\" ref\" :\" dependencyRef\" ,\" dependsOn\" :[\" transitiveDependencyRef\" ]}" , string (jsonBytes ))
121
- }
122
-
123
- func TestDependency_UnmarshalJSON (t * testing.T ) {
124
- // Unmarshal empty dependency
125
- dependency := new (Dependency )
126
- err := json .Unmarshal ([]byte ("{}" ), dependency )
127
- assert .NoError (t , err )
128
- assert .Equal (t , "" , dependency .Ref )
129
- assert .Nil (t , dependency .Dependencies )
130
-
131
- // Unmarshal dependency with empty dependencies
132
- dependency = new (Dependency )
133
- err = json .Unmarshal ([]byte ("{\" ref\" :\" dependencyRef\" ,\" dependsOn\" :[]}" ), dependency )
134
- assert .NoError (t , err )
135
- assert .Equal (t , "dependencyRef" , dependency .Ref )
136
- assert .Nil (t , dependency .Dependencies )
137
-
138
- // Unmarshal dependency with dependencies
139
- dependency = new (Dependency )
140
- err = json .Unmarshal ([]byte ("{\" ref\" :\" dependencyRef\" ,\" dependsOn\" :[\" transitiveDependencyRef\" ]}" ), dependency )
141
- assert .NoError (t , err )
142
- assert .Equal (t , "dependencyRef" , dependency .Ref )
143
- assert .Equal (t , 1 , len (* dependency .Dependencies ))
144
- assert .Equal (t , "transitiveDependencyRef" , (* dependency .Dependencies )[0 ].Ref )
145
- }
146
-
147
- func TestLicenses_MarshalXML (t * testing.T ) {
148
- // Marshal license and expressions
149
- licenses := Licenses {
150
- LicenseChoice {
151
- Expression : "expressionValue1" ,
152
- },
153
- LicenseChoice {
154
- License : & License {
155
- ID : "licenseID" ,
156
- URL : "licenseURL" ,
157
- },
158
- },
159
- LicenseChoice {
160
- Expression : "expressionValue2" ,
161
- },
162
- }
163
- xmlBytes , err := xml .MarshalIndent (licenses , "" , " " )
164
- assert .NoError (t , err )
165
- assert .Equal (t , `<Licenses>
166
- <expression>expressionValue1</expression>
167
- <license>
168
- <id>licenseID</id>
169
- <url>licenseURL</url>
170
- </license>
171
- <expression>expressionValue2</expression>
172
- </Licenses>` , string (xmlBytes ))
173
-
174
- // Should return error when both license and expression are set on an element
175
- licenses = Licenses {
176
- LicenseChoice {
177
- License : & License {
178
- ID : "licenseID" ,
179
- },
180
- Expression : "expressionValue" ,
181
- },
182
- }
183
- _ , err = xml .Marshal (licenses )
184
- assert .Error (t , err )
185
-
186
- // Should encode nothing when empty
187
- licenses = Licenses {}
188
- xmlBytes , err = xml .Marshal (licenses )
189
- assert .NoError (t , err )
190
- assert .Nil (t , xmlBytes )
191
- }
192
-
193
- func TestLicenses_UnmarshalXML (t * testing.T ) {
194
- // Unmarshal license and expressions
195
- licenses := new (Licenses )
196
- err := xml .Unmarshal ([]byte (`
197
- <Licenses>
198
- <expression>expressionValue1</expression>
199
- <license>
200
- <id>licenseID</id>
201
- <url>licenseURL</url>
202
- </license>
203
- <expression>expressionValue2</expression>
204
- </Licenses>` ), licenses )
205
- assert .NoError (t , err )
206
- assert .Len (t , * licenses , 3 )
207
- assert .Nil (t , (* licenses )[0 ].License )
208
- assert .Equal (t , "expressionValue1" , (* licenses )[0 ].Expression )
209
- assert .NotNil (t , (* licenses )[1 ].License )
210
- assert .Equal (t , "licenseID" , (* licenses )[1 ].License .ID )
211
- assert .Equal (t , "licenseURL" , (* licenses )[1 ].License .URL )
212
- assert .Empty (t , (* licenses )[1 ].Expression )
213
- assert .Nil (t , (* licenses )[2 ].License )
214
- assert .Equal (t , "expressionValue2" , (* licenses )[2 ].Expression )
215
-
216
- // Unmarshal empty licenses
217
- licenses = new (Licenses )
218
- err = xml .Unmarshal ([]byte ("<Licenses></Licenses>" ), licenses )
219
- assert .NoError (t , err )
220
- assert .Empty (t , * licenses )
221
-
222
- // Should return error when an element is neither license nor expression
223
- licenses = new (Licenses )
224
- err = xml .Unmarshal ([]byte ("<Licenses><somethingElse>expressionValue</somethingElse></Licenses>" ), licenses )
225
- assert .Error (t , err )
226
- }
227
-
228
38
func TestVulnerability_Properties (t * testing.T ) {
229
39
// GIVEN
230
40
properties := []Property {}
0 commit comments