aboutsummaryrefslogtreecommitdiff
path: root/nvdmirror/sync_test.go
blob: fb9b56bea0c7a91fb4a3d67f4c30c92b6fa115d0 (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
package nvdmirror

import (
  "fmt"
  "os"
  "net/http"
  "path/filepath"
  "testing"
)

// get test config
func getTestConfig(port int) SyncConfig {
  // build url
  url := fmt.Sprintf("http://localhost:%d", port)

  return SyncConfig {
    Cve11BaseUrl: url,
    CpeMatch10BaseUrl: url,
    Cpe23DictUrl: fmt.Sprintf("%s/official-cpe-dictionary_v2.3.xml.gz", url),
  }
}

// serve on given port
func serve(port int, ch chan bool) {
  s := http.Server {
    Addr: fmt.Sprintf(":%d", port),
    Handler: http.FileServer(http.Dir("testdata/files")),
  }

  go (func() {
    // block on channel
    _ = <-ch

    // shut down server
    s.Close()
  })()

  // start server
  s.ListenAndServe()
}

// close server immediately
func stopServer(ch chan bool) {
  ch <- false
}

func TestSync(t *testing.T) {
  // 
  port := 8888
  ch := make(chan bool)
  defer stopServer(ch)

  // spin up local server
  go serve(port, ch)

  // create temp dir
  dir, err := os.MkdirTemp("", "")
  if err != nil {
    t.Error(err)
    return
  }
  defer os.RemoveAll(dir)
  // dir = "testdata/out"

  // create cache
  cache, err := NewJsonCache(filepath.Join(dir, "cache.json.gz"))
  if err != nil {
    t.Error(err)
    return
  }
  defer cache.Close()

  // custom sync config
  config := getTestConfig(port)

  // sync data
  t.Run("initial", func(t *testing.T) {
    Sync(config, &cache, dir)
  })

  // sync data again (to test caching)
  t.Run("caching", func(t *testing.T) {
    Sync(config, &cache, dir)
  })

  // sync w/ missing dir
  t.Run("missingDir", func(t *testing.T) {
    missingDir := filepath.Join(dir, "does/not/exist")
    Sync(config, &cache, missingDir)
  })

  // sync w/ bad cache
  t.Run("failSetCache", func(t *testing.T) {
    var cache FailSetCache
    Sync(config, &cache, dir)
  })

  t.Run("customUserAgent", func(t *testing.T) {
    // custom sync config
    config := getTestConfig(port)
    config.UserAgent = "custom-user-agent/0.0.0"

    Sync(config, &cache, dir)
  })

  t.Run("clientFail", func(t *testing.T) {
    // custom sync config
    config := getTestConfig(port)
    config.Cve11BaseUrl = "http://localhost:0"

    Sync(config, &cache, dir)
  })
}

func TestBadUrls(t *testing.T) {
  // create temp dir
  dir, err := os.MkdirTemp("", "")
  if err != nil {
    t.Error(err)
    return
  }
  defer os.RemoveAll(dir)

  // create cache
  cache, err := NewJsonCache(filepath.Join(dir, "cache.json.gz"))
  if err != nil {
    t.Error(err)
    return
  }
  defer cache.Close()

  // invalid base URLs
  failTests := []string {
    "httsldkfjasldkfjadp:// \\ localhost:0",
  }

  for _, test := range(failTests) {
    t.Run(test, func(t *testing.T) {
      // custom sync config
      config := getTestConfig(0)
      config.Cve11BaseUrl = test

      // sync data; note: even with an invalid base URL we still expect
      // this call to succeed; it's just that all of the URLs will be
      // nonsensical
      Sync(config, &cache, dir)
    })
  }
}