File: testSpdxSerializer.cxx

package info (click to toggle)
cmake 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 158,704 kB
  • sloc: ansic: 406,077; cpp: 309,512; sh: 4,233; python: 3,696; yacc: 3,109; lex: 1,279; f90: 538; asm: 471; lisp: 375; java: 310; cs: 270; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 110; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22; sed: 2
file content (84 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download | duplicates (2)
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

/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */

#include <string>
#include <utility>
#include <vector>

#include <cm/optional>

#include <cm3p/json/value.h>

#include "cmSbomObject.h"
#include "cmSpdx.h"
#include "cmSpdxSerializer.h"

#include "testCommon.h"

namespace {

bool testSerializeSpdxJson()
{

  auto constexpr contextUrl = "https://spdx.org/rdf/3.0.1/spdx-context.jsonld";

  cmSbomDocument doc;
  doc.Context = contextUrl;

  cmSpdxDocument spdxValue;
  spdxValue.SpdxId = "_:SPDXRef-Document";
  spdxValue.DataLicense = "CC0-1.0";
  auto spdx = insert_back(doc.Graph, std::move(spdxValue));

  {
    cmSpdxCreationInfo ci;
    ci.SpdxId = "_:SPDXRef-CreationInfo";
    ci.SpecVersion = "3.0.1";
    spdx->CreationInfo = std::move(ci);
  }

  {
    cmSpdxPackage pkg;
    pkg.Name = "sample-package";
    pkg.SpdxId = "_:SPDXRef-Package";

    spdx->RootElements.emplace_back(std::move(pkg));
  }

  {
    cmSpdxSerializer serializer;
    doc.Serialize(serializer);
    auto value = serializer.GetJson();

    ASSERT_TRUE(value.isObject());

    Json::Value const& context = value["@context"];
    ASSERT_EQUAL(context.asString(), contextUrl);

    Json::Value const& graph = value["@graph"];
    ASSERT_TRUE(graph.isArray());

    Json::Value const& docValue = graph[0];
    ASSERT_TRUE(docValue.isObject());
    ASSERT_EQUAL(docValue["type"].asString(), "SpdxDocument");
    ASSERT_EQUAL(docValue["spdxId"].asString(), "_:SPDXRef-Document");

    auto const& creationInfo = docValue["creationInfo"];
    ASSERT_EQUAL(creationInfo["@id"].asString(), "_:SPDXRef-CreationInfo");
    ASSERT_EQUAL(creationInfo["type"].asString(), "CreationInfo");

    auto const& package = docValue["rootElement"][0];
    ASSERT_EQUAL(package["type"].asString(), "software_Package");
    ASSERT_EQUAL(package["spdxId"].asString(), "_:SPDXRef-Package");
  }

  return true;
}

} // namespace

int testSpdxSerializer(int /*unused*/, char* /*unused*/[])
{
  return runTests({ testSerializeSpdxJson });
}