aboutsummaryrefslogtreecommitdiff
path: root/cpedict/cpedict_test.go
blob: 6c7c28070b3e6d97eac382efdb0e59016fc00ac9 (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
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
package cpedict

import (
  "compress/gzip"
  "encoding/xml"
  "os"
  "testing"
  "time"
  "reflect"
)

func TestDictionaryXMLUnmarshal(t *testing.T) {
  // open test data
  f, err := os.Open("testdata/test-0.xml.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 xml decoder, decode xml
  d := xml.NewDecoder(gz)

  // decode xml, check for error
  var dict Dictionary
  if err := d.Decode(&dict); err != nil {
    t.Error(err)
    return
  }

  // build expected time
  var expTime time.Time
  if err := expTime.UnmarshalText([]byte("2022-02-02T04:51:00.437Z")); err != nil {
    t.Error(err)
    return
  }

  // expected generator
  expGenerator := Generator {
    ProductName: "National Vulnerability Database (NVD)",
    ProductVersion: "4.9",
    SchemaVersion: "2.3",
    Timestamp: expTime,
  }

  // compare generator
  if !reflect.DeepEqual(dict.Generator, expGenerator) {
    t.Errorf("got \"%v\", exp \"%v\"", dict.Generator, expGenerator)
    return
  }

  // check item count
  gotNumItems := len(dict.Items)
  expNumItems := 19
  if gotNumItems != expNumItems {
    t.Errorf("item count: got %d, exp %d", gotNumItems, expNumItems)
    return
  }

  // build expected item
  expItems := []Item {
    // first item in the list
    Item {
      CpeUri: "cpe:/a:%240.99_kindle_books_project:%240.99_kindle_books:6::~~~android~~",

      Cpe23Item: Cpe23Item {
        Name: "cpe:2.3:a:\\$0.99_kindle_books_project:\\$0.99_kindle_books:6:*:*:*:*:android:*:*",
      },

      Titles: []Title {
        Title {
          Lang: "en-US",
          Text: "$0.99 Kindle Books project $0.99 Kindle Books (aka com.kindle.books.for99) for android 6.0",
        },
      },

      References: []Reference {
        Reference {
          Href: "https://play.google.com/store/apps/details?id=com.kindle.books.for99",
          Text: "Product information",
        },

        Reference {
          Href: "https://docs.google.com/spreadsheets/d/1t5GXwjw82SyunALVJb2w0zi3FoLRIkfGPc7AMjRF0r4/edit?pli=1#gid=1053404143",
          Text: "Government Advisory",
        },
      },
    },

    // last item in the list
    Item {
      CpeUri: "cpe:/a:3com:3c16116-us:2.0",

      Cpe23Item: Cpe23Item {
        Name: "cpe:2.3:a:3com:3c16116-us:2.0:*:*:*:*:*:*:*",
      },

      Titles: []Title {
        Title {
          Lang: "ja-JP",
          Text: "スリーコム WebCache 3000 2.0",
        },

        Title {
          Lang: "en-US",
          Text: "3Com WebCache 3000 2.0",
        },
      },
    },
  }

  // build item comparisons
  compares := []struct {
    name string
    exp  Item
    got  Item
  } {
    { "head", expItems[0], dict.Items[0] },
    { "tail", expItems[1], dict.Items[len(dict.Items) - 1] },
  }

  for _, row := range(compares) {
    t.Run(row.name, func(t *testing.T) {
      if !reflect.DeepEqual(row.got, row.exp) {
        t.Errorf("got \"%v\", exp \"%v\"", row.got, row.exp)
      }
    })
  }
}