1// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2package parser2v3 3 4import ( 5 "testing" 6 7 "github.com/spdx/tools-golang/spdx/v2_3" 8) 9 10// ===== Parser review section state change tests ===== 11func TestParser2_3ReviewStartsNewReviewAfterParsingReviewerTag(t *testing.T) { 12 // create the first review 13 rev1 := "John Doe" 14 parser := tvParser2_3{ 15 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 16 st: psReview2_3, 17 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 18 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 19 otherLic: &v2_3.OtherLicense{ 20 LicenseIdentifier: "LicenseRef-Lic11", 21 LicenseName: "License 11", 22 }, 23 rev: &v2_3.Review{ 24 Reviewer: rev1, 25 ReviewerType: "Person", 26 }, 27 } 28 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 29 parser.pkg.Files = append(parser.pkg.Files, parser.file) 30 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 31 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 32 r1 := parser.rev 33 34 // the Document's Reviews should have this one only 35 if len(parser.doc.Reviews) != 1 { 36 t.Errorf("Expected only one review, got %d", len(parser.doc.Reviews)) 37 } 38 if parser.doc.Reviews[0] != r1 { 39 t.Errorf("Expected review %v in Reviews[0], got %v", r1, parser.doc.Reviews[0]) 40 } 41 if parser.doc.Reviews[0].Reviewer != rev1 { 42 t.Errorf("expected review name %s in Reviews[0], got %s", rev1, parser.doc.Reviews[0].Reviewer) 43 } 44 45 // now add a new review 46 rev2 := "Steve" 47 rp2 := "Person: Steve" 48 err := parser.parsePair2_3("Reviewer", rp2) 49 if err != nil { 50 t.Errorf("got error when calling parsePair2_3: %v", err) 51 } 52 // state should be correct 53 if parser.st != psReview2_3 { 54 t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) 55 } 56 // and a review should be created 57 if parser.rev == nil { 58 t.Fatalf("parser didn't create new review") 59 } 60 // and the reviewer's name should be as expected 61 if parser.rev.Reviewer != rev2 { 62 t.Errorf("expected reviewer name %s, got %s", rev2, parser.rev.Reviewer) 63 } 64 // and the Document's reviews should be of size 2 and have these two 65 if len(parser.doc.Reviews) != 2 { 66 t.Fatalf("Expected Reviews to have len 2, got %d", len(parser.doc.Reviews)) 67 } 68 if parser.doc.Reviews[0] != r1 { 69 t.Errorf("Expected review %v in Reviews[0], got %v", r1, parser.doc.Reviews[0]) 70 } 71 if parser.doc.Reviews[0].Reviewer != rev1 { 72 t.Errorf("expected reviewer name %s in Reviews[0], got %s", rev1, parser.doc.Reviews[0].Reviewer) 73 } 74 if parser.doc.Reviews[1] != parser.rev { 75 t.Errorf("Expected review %v in Reviews[1], got %v", parser.rev, parser.doc.Reviews[1]) 76 } 77 if parser.doc.Reviews[1].Reviewer != rev2 { 78 t.Errorf("expected reviewer name %s in Reviews[1], got %s", rev2, parser.doc.Reviews[1].Reviewer) 79 } 80 81} 82 83func TestParser2_3ReviewStaysAfterParsingRelationshipTags(t *testing.T) { 84 parser := tvParser2_3{ 85 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 86 st: psReview2_3, 87 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 88 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 89 otherLic: &v2_3.OtherLicense{ 90 LicenseIdentifier: "LicenseRef-Lic11", 91 LicenseName: "License 11", 92 }, 93 rev: &v2_3.Review{ 94 Reviewer: "Jane Doe", 95 ReviewerType: "Person", 96 }, 97 } 98 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 99 parser.pkg.Files = append(parser.pkg.Files, parser.file) 100 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 101 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 102 103 err := parser.parsePair2_3("Relationship", "SPDXRef-blah CONTAINS SPDXRef-blah-else") 104 if err != nil { 105 t.Errorf("got error when calling parsePair2_3: %v", err) 106 } 107 // state should remain unchanged 108 if parser.st != psReview2_3 { 109 t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) 110 } 111 // and the relationship should be in the Document's Relationships 112 if len(parser.doc.Relationships) != 1 { 113 t.Fatalf("expected doc.Relationships to have len 1, got %d", len(parser.doc.Relationships)) 114 } 115 deID := parser.doc.Relationships[0].RefA 116 if deID.DocumentRefID != "" || deID.ElementRefID != "blah" { 117 t.Errorf("expected RefA to be %s, got %s", "blah", parser.doc.Relationships[0].RefA) 118 } 119 120 err = parser.parsePair2_3("RelationshipComment", "blah") 121 if err != nil { 122 t.Errorf("got error when calling parsePair2_3: %v", err) 123 } 124 // state should still remain unchanged 125 if parser.st != psReview2_3 { 126 t.Errorf("expected state to be %v, got %v", psReview2_3, parser.st) 127 } 128} 129 130func TestParser2_3ReviewStaysAfterParsingAnnotationTags(t *testing.T) { 131 parser := tvParser2_3{ 132 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 133 st: psReview2_3, 134 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 135 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 136 otherLic: &v2_3.OtherLicense{ 137 LicenseIdentifier: "LicenseRef-Lic11", 138 LicenseName: "License 11", 139 }, 140 rev: &v2_3.Review{ 141 Reviewer: "Jane Doe", 142 ReviewerType: "Person", 143 }, 144 } 145 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 146 parser.pkg.Files = append(parser.pkg.Files, parser.file) 147 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 148 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 149 150 err := parser.parsePair2_3("Annotator", "Person: John Doe ()") 151 if err != nil { 152 t.Errorf("got error when calling parsePair2_3: %v", err) 153 } 154 if parser.st != psReview2_3 { 155 t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) 156 } 157 158 err = parser.parsePair2_3("AnnotationDate", "2018-09-15T00:36:00Z") 159 if err != nil { 160 t.Errorf("got error when calling parsePair2_3: %v", err) 161 } 162 if parser.st != psReview2_3 { 163 t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) 164 } 165 166 err = parser.parsePair2_3("AnnotationType", "REVIEW") 167 if err != nil { 168 t.Errorf("got error when calling parsePair2_3: %v", err) 169 } 170 if parser.st != psReview2_3 { 171 t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) 172 } 173 174 err = parser.parsePair2_3("SPDXREF", "SPDXRef-45") 175 if err != nil { 176 t.Errorf("got error when calling parsePair2_3: %v", err) 177 } 178 if parser.st != psReview2_3 { 179 t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) 180 } 181 182 err = parser.parsePair2_3("AnnotationComment", "i guess i had something to say about this particular file") 183 if err != nil { 184 t.Errorf("got error when calling parsePair2_3: %v", err) 185 } 186 if parser.st != psReview2_3 { 187 t.Errorf("parser is in state %v, expected %v", parser.st, psReview2_3) 188 } 189 190 // and the annotation should be in the Document's Annotations 191 if len(parser.doc.Annotations) != 1 { 192 t.Fatalf("expected doc.Annotations to have len 1, got %d", len(parser.doc.Annotations)) 193 } 194 if parser.doc.Annotations[0].Annotator.Annotator != "John Doe ()" { 195 t.Errorf("expected Annotator to be %s, got %s", "John Doe ()", parser.doc.Annotations[0].Annotator) 196 } 197} 198 199func TestParser2_3ReviewFailsAfterParsingOtherSectionTags(t *testing.T) { 200 parser := tvParser2_3{ 201 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 202 st: psReview2_3, 203 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 204 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 205 otherLic: &v2_3.OtherLicense{ 206 LicenseIdentifier: "LicenseRef-Lic11", 207 LicenseName: "License 11", 208 }, 209 rev: &v2_3.Review{}, 210 } 211 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 212 parser.pkg.Files = append(parser.pkg.Files, parser.file) 213 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 214 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 215 216 // can't go back to old sections 217 err := parser.parsePair2_3("SPDXVersion", "SPDX-2.3") 218 if err == nil { 219 t.Errorf("expected error when calling parsePair2_3, got nil") 220 } 221 err = parser.parsePair2_3("PackageName", "whatever") 222 if err == nil { 223 t.Errorf("expected error when calling parsePair2_3, got nil") 224 } 225 err = parser.parsePair2_3("FileName", "whatever") 226 if err == nil { 227 t.Errorf("expected error when calling parsePair2_3, got nil") 228 } 229 err = parser.parsePair2_3("LicenseID", "LicenseRef-Lic22") 230 if err == nil { 231 t.Errorf("expected error when calling parsePair2_3, got nil") 232 } 233} 234 235// ===== Review data section tests ===== 236func TestParser2_3CanParseReviewTags(t *testing.T) { 237 parser := tvParser2_3{ 238 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 239 st: psReview2_3, 240 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 241 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 242 otherLic: &v2_3.OtherLicense{ 243 LicenseIdentifier: "LicenseRef-Lic11", 244 LicenseName: "License 11", 245 }, 246 rev: &v2_3.Review{}, 247 } 248 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 249 parser.pkg.Files = append(parser.pkg.Files, parser.file) 250 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 251 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 252 253 // Reviewer (DEPRECATED) 254 // handled in subsequent subtests 255 256 // Review Date (DEPRECATED) 257 err := parser.parsePairFromReview2_3("ReviewDate", "2018-09-23T08:30:00Z") 258 if err != nil { 259 t.Errorf("expected nil error, got %v", err) 260 } 261 if parser.rev.ReviewDate != "2018-09-23T08:30:00Z" { 262 t.Errorf("got %v for ReviewDate", parser.rev.ReviewDate) 263 } 264 265 // Review Comment (DEPRECATED) 266 err = parser.parsePairFromReview2_3("ReviewComment", "this is a comment") 267 if err != nil { 268 t.Errorf("expected nil error, got %v", err) 269 } 270 if parser.rev.ReviewComment != "this is a comment" { 271 t.Errorf("got %v for ReviewComment", parser.rev.ReviewComment) 272 } 273} 274 275func TestParser2_3CanParseReviewerPersonTag(t *testing.T) { 276 parser := tvParser2_3{ 277 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 278 st: psReview2_3, 279 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 280 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 281 otherLic: &v2_3.OtherLicense{ 282 LicenseIdentifier: "LicenseRef-Lic11", 283 LicenseName: "License 11", 284 }, 285 rev: &v2_3.Review{}, 286 } 287 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 288 parser.pkg.Files = append(parser.pkg.Files, parser.file) 289 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 290 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 291 292 // Reviewer: Person 293 err := parser.parsePairFromReview2_3("Reviewer", "Person: John Doe") 294 if err != nil { 295 t.Errorf("expected nil error, got %v", err) 296 } 297 if parser.rev.Reviewer != "John Doe" { 298 t.Errorf("got %v for Reviewer", parser.rev.Reviewer) 299 } 300 if parser.rev.ReviewerType != "Person" { 301 t.Errorf("got %v for ReviewerType", parser.rev.ReviewerType) 302 } 303} 304 305func TestParser2_3CanParseReviewerOrganizationTag(t *testing.T) { 306 parser := tvParser2_3{ 307 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 308 st: psReview2_3, 309 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 310 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 311 otherLic: &v2_3.OtherLicense{ 312 LicenseIdentifier: "LicenseRef-Lic11", 313 LicenseName: "License 11", 314 }, 315 rev: &v2_3.Review{}, 316 } 317 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 318 parser.pkg.Files = append(parser.pkg.Files, parser.file) 319 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 320 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 321 322 // Reviewer: Organization 323 err := parser.parsePairFromReview2_3("Reviewer", "Organization: John Doe, Inc.") 324 if err != nil { 325 t.Errorf("expected nil error, got %v", err) 326 } 327 if parser.rev.Reviewer != "John Doe, Inc." { 328 t.Errorf("got %v for Reviewer", parser.rev.Reviewer) 329 } 330 if parser.rev.ReviewerType != "Organization" { 331 t.Errorf("got %v for ReviewerType", parser.rev.ReviewerType) 332 } 333} 334 335func TestParser2_3CanParseReviewerToolTag(t *testing.T) { 336 parser := tvParser2_3{ 337 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 338 st: psReview2_3, 339 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 340 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 341 otherLic: &v2_3.OtherLicense{ 342 LicenseIdentifier: "LicenseRef-Lic11", 343 LicenseName: "License 11", 344 }, 345 rev: &v2_3.Review{}, 346 } 347 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 348 parser.pkg.Files = append(parser.pkg.Files, parser.file) 349 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 350 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 351 352 // Reviewer: Tool 353 err := parser.parsePairFromReview2_3("Reviewer", "Tool: scannertool - 1.2.12") 354 if err != nil { 355 t.Errorf("expected nil error, got %v", err) 356 } 357 if parser.rev.Reviewer != "scannertool - 1.2.12" { 358 t.Errorf("got %v for Reviewer", parser.rev.Reviewer) 359 } 360 if parser.rev.ReviewerType != "Tool" { 361 t.Errorf("got %v for ReviewerType", parser.rev.ReviewerType) 362 } 363} 364 365func TestParser2_3FailsIfReviewerInvalidFormat(t *testing.T) { 366 parser := tvParser2_3{ 367 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 368 st: psReview2_3, 369 rev: &v2_3.Review{}, 370 } 371 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 372 373 err := parser.parsePairFromReview2_3("Reviewer", "oops") 374 if err == nil { 375 t.Errorf("expected non-nil error, got nil") 376 } 377} 378 379func TestParser2_3FailsIfReviewerUnknownType(t *testing.T) { 380 parser := tvParser2_3{ 381 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 382 st: psReview2_3, 383 rev: &v2_3.Review{}, 384 } 385 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 386 387 err := parser.parsePairFromReview2_3("Reviewer", "whoops: John Doe") 388 if err == nil { 389 t.Errorf("expected non-nil error, got nil") 390 } 391} 392 393func TestParser2_3ReviewUnknownTagFails(t *testing.T) { 394 parser := tvParser2_3{ 395 doc: &v2_3.Document{Packages: []*v2_3.Package{}}, 396 st: psReview2_3, 397 pkg: &v2_3.Package{PackageName: "test", PackageSPDXIdentifier: "test", Files: []*v2_3.File{}}, 398 file: &v2_3.File{FileName: "f1.txt", FileSPDXIdentifier: "f1"}, 399 otherLic: &v2_3.OtherLicense{ 400 LicenseIdentifier: "LicenseRef-Lic11", 401 LicenseName: "License 11", 402 }, 403 rev: &v2_3.Review{}, 404 } 405 parser.doc.Packages = append(parser.doc.Packages, parser.pkg) 406 parser.pkg.Files = append(parser.pkg.Files, parser.file) 407 parser.doc.OtherLicenses = append(parser.doc.OtherLicenses, parser.otherLic) 408 parser.doc.Reviews = append(parser.doc.Reviews, parser.rev) 409 410 err := parser.parsePairFromReview2_3("blah", "something") 411 if err == nil { 412 t.Errorf("expected error from parsing unknown tag") 413 } 414} 415