aboutsummaryrefslogtreecommitdiff
path: root/nvdmirror/syncconfig_test.go
blob: cc1cdfe6707bf87606137d9a4cdae797c71ec2dd (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package nvdmirror

import (
  "fmt"
  "reflect"
  "testing"
  "time"
)

// base url for tests
const testBaseUrl = "https://example.com"

func TestSyncConfigUserAgent(t *testing.T) {
  tests := []struct {
    name string
    val string
    exp string
  } {
    { "random", "foo-bar", "foo-bar" },
    { "default", "", DefaultConfig.UserAgent },
  }

  for _, test := range(tests) {
    t.Run(test.name, func(t *testing.T) {
      config := SyncConfig { UserAgent: test.val }

      got := config.GetUserAgent()
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }
}

func TestSyncConfigGetCveUrl(t *testing.T) {
  tests := []struct {
    id  string
    ext string
    exp string
  } {
    { "foo", "meta", "https://example.com/nvdcve-1.1-foo.meta" },
    { "bar", "json.gz", "https://example.com/nvdcve-1.1-bar.json.gz" },
  }

  for _, test := range(tests) {
    t.Run(test.id, func(t *testing.T) {
      config := SyncConfig { Cve11BaseUrl: testBaseUrl }

      got := config.GetCveUrl(test.id, test.ext)
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }
}

func TestSyncConfigGetCveYearUrl(t *testing.T) {
  tests := []struct {
    year int
    exp string
  } {
    { 2022, "https://example.com/nvdcve-1.1-2022.meta" },
    { 4,    "https://example.com/nvdcve-1.1-0004.meta" },
  }

  for _, test := range(tests) {
    t.Run(fmt.Sprintf("%04d", test.year), func(t *testing.T) {
      config := SyncConfig { Cve11BaseUrl: testBaseUrl }

      got := config.GetCveYearUrl(test.year, "meta")
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }
}

func TestSyncConfigGetCpeMatchUrl(t *testing.T) {
  tests := []struct {
    ext string
    exp string
  } {
    { "meta", "https://example.com/nvdcpematch-1.0.meta" },
    { "json.gz", "https://example.com/nvdcpematch-1.0.json.gz" },
    { "whatever", "https://example.com/nvdcpematch-1.0.whatever" },
  }

  for _, test := range(tests) {
    t.Run(test.ext, func(t *testing.T) {
      config := SyncConfig { CpeMatch10BaseUrl: testBaseUrl }

      got := config.GetCpeMatchUrl(test.ext)
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }
}

func TestSyncConfigGetCpeDictUrl(t *testing.T) {
  tests := []struct {
    name string
    val string
    exp string
  } {
    { "custom", "https://example.com/", "https://example.com/" },
    { "default", "", DefaultConfig.Cpe23DictUrl },
  }

  for _, test := range(tests) {
    t.Run(test.name, func(t *testing.T) {
      config := SyncConfig { Cpe23DictUrl: test.val }

      got := config.GetCpeDictUrl()
      if got != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
      }
    })
  }
}

func TestSyncConfigGetMetaUrls(t *testing.T) {
  // declare expected result
  exp := make(map[string]string)

  // add years
  for year := 2002; year <= time.Now().Year(); year++ {
    k := fmt.Sprintf("%s/nvdcve-1.1-%04d.meta", testBaseUrl, year)
    v := fmt.Sprintf("%s/nvdcve-1.1-%04d.json.gz", testBaseUrl, year)
    exp[k] = v
  }

  // add cve extra feeds
  for _, id := range([]string { "modified", "recent" }) {
    k := fmt.Sprintf("%s/nvdcve-1.1-%s.meta", testBaseUrl, id)
    v := fmt.Sprintf("%s/nvdcve-1.1-%s.json.gz", testBaseUrl, id)
    exp[k] = v
  }

  {
    // add cpe match
    k := fmt.Sprintf("%s/nvdcpematch-1.0.meta", testBaseUrl)
    v := fmt.Sprintf("%s/nvdcpematch-1.0.json.gz", testBaseUrl)
    exp[k] = v
  }

  // declare custom config
  config := SyncConfig {
    Cve11BaseUrl: testBaseUrl,
    CpeMatch10BaseUrl: testBaseUrl,
  }

  // get/check meta urls
  got := config.getMetaUrls()
  if !reflect.DeepEqual(got, exp) {
    t.Errorf("got \"%v\", exp \"%v\"", got, exp)
  }
}