aboutsummaryrefslogtreecommitdiff
path: root/cpematch/cpematch_test.go
blob: 7d694107d37f8a63eec8c4ec1e72dbaf3ae14e1e (plain)
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
package cpematch

import (
  "compress/gzip"
  "encoding/json"
  "os"
  "reflect"
  "testing"
)

func TestMatchesUnmarshal(t *testing.T) {
  // expected data
  exp := Matches {
    Matches: []Match {
      Match {
        Cpe23Uri: "cpe:2.3:a:101_project:101:*:*:*:*:*:node.js:*:*",
        VersionStartIncluding: "1.0.0",
        VersionEndIncluding: "1.6.3",
        Names: []Name {
          Name {
            Cpe23Uri: "cpe:2.3:a:101_project:101:1.0.0:*:*:*:*:node.js:*:*",
          },

          Name {
            Cpe23Uri: "cpe:2.3:a:101_project:101:1.1.0:*:*:*:*:node.js:*:*",
          },

          Name {
            Cpe23Uri: "cpe:2.3:a:101_project:101:1.1.1:*:*:*:*:node.js:*:*",
          },
        },
      },

      Match {
        Cpe23Uri: "cpe:2.3:a:1password:1password:*:*:*:*:*:macos:*:*",
        VersionStartIncluding: "7.7.0",
        VersionEndExcluding: "7.8.7",
        Names: []Name {
          Name {
            Cpe23Uri: "cpe:2.3:a:1password:1password:7.7.0:*:*:*:*:macos:*:*",
          },
        },
      },

      Match {
        Cpe23Uri: "cpe:2.3:a:zimbra:collaboration:*:*:*:*:*:*:*:*",
        VersionStartExcluding: "8.8.0",
        VersionEndExcluding: "8.8.15",
        Names: []Name {
          Name {
            Cpe23Uri: "cpe:2.3:a:zimbra:collaboration:8.8.6:*:*:*:*:*:*:*",
          },

          Name {
            Cpe23Uri: "cpe:2.3:a:zimbra:collaboration:8.8.7:*:*:*:*:*:*:*",
          },

          Name {
            Cpe23Uri: "cpe:2.3:a:zimbra:collaboration:8.8.8:-:*:*:*:*:*:*",
          },

          Name {
            Cpe23Uri: "cpe:2.3:a:zimbra:collaboration:8.8.8:p1:*:*:*:*:*:*",
          },
        },
      },
    },
  }

  // open test data
  f, err := os.Open("testdata/test-0.json.gz")
  if err != nil {
    t.Error(err)
    return
  }
  defer f.Close()

  // create gzip reader
  gz, err := gzip.NewReader(f)
  if err != nil {
    t.Error(err)
    return
  }
  defer gz.Close()

  // create json decoder
  d := json.NewDecoder(gz)
  var got Matches

  // decode match data, check for error
  if err := d.Decode(&got); err != nil {
    t.Error(err)
    return
  }

  // check for match
  if !reflect.DeepEqual(got, exp) {
    t.Errorf("got \"%v\", exp \"%v\"", got, exp)
  }
}