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

import (
  "os"
  "path/filepath"
  "reflect"
  "testing"
)

func TestNewJsonCache(t *testing.T) {
  passTests := []struct {
    name string
    vals map[string]map[string]string
  } {{
    name: "empty",
  }, {
    name: "foobarbaz",
    vals: map[string]map[string]string {
      "foo": map[string]string {
        "bar": "baz",
      },
    },
  }}

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

  for _, test := range(passTests) {
    t.Run(test.name, func(t *testing.T) {
      // build cache path
	    path := filepath.Join(dir, test.name)

      {
        // load cache
        cache, err := NewJsonCache(path)
        if err != nil {
          t.Error(err)
          return
        }

        // set values
        for k, v := range(test.vals) {
          if err = cache.Set(k, v); err != nil {
            t.Error(err)
            return
          }
        }

        // close cache
        if err = cache.Close(); err != nil {
          t.Error(err)
          return
        }
      }

      {
        // open previously saved cache
        cache, err := NewJsonCache(path)
        if err != nil {
          t.Error(err)
          return
        }
        defer cache.Close()

        // check values
        for k, exp := range(test.vals) {
          if got, ok := cache.Get(k); !ok {
            t.Errorf("missing key: %s", k)
            return
          } else if !reflect.DeepEqual(got, exp) {
            t.Errorf("got %v, exp %v", got, exp)
            return
          }

          // delete entry
          if err = cache.Delete(k); err != nil {
            t.Error(err)
            return
          }

          // get entry again
          if got, ok := cache.Get(k); ok {
            t.Errorf("key: %s, got %v, exp missing", k, got)
            return
          }
        }

        // try accessing missing value
        if got, ok := cache.Get("some key that doesn't exist"); ok {
          t.Errorf("got %v, exp !ok", got)
        }

        // close cache, check for error
        if err = cache.Close(); err != nil {
          t.Error(err)
          return
        }

        if cache.dirty {
          t.Errorf("got dirty, exp clean")
          return
        }
      }
    })
  }

  failNewTests := []string {
    "testdata/does/not/exist",
    "testdata/bad-data.json.gz",
    "testdata/bad-stat.json.gz",
  }

  for _, test := range(failNewTests) {
    t.Run(test, func(t *testing.T) {
      if got, err := NewJsonCache(test); err == nil {
        t.Errorf("got %v, exp err", got)
      }
    })
  }

  failCloseTests := []string {
    "/dev/null",
  }

  testVals := map[string]string { "bar": "baz" }

  for _, test := range(failCloseTests) {
    t.Run(test, func(t *testing.T) {
      cache, err := NewJsonCache(test)
      if err != nil {
        t.Error(err)
        return
      }

      if err = cache.Set("foo", testVals); err != nil {
        t.Error(err)
        return
      }

      if err = cache.Close(); err == nil {
        t.Errorf("got success, exp error")
      }
    })
  }
}