aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/meta_test.go
blob: 640926cec6d1528b7b71382d79b7122b9a59faaa (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
package feed

import (
  "bytes"
  "encoding/json"
  "fmt"
  "testing"
)

func TestParseMetaSize(t *testing.T) {
  passTests := []struct {
    val string
    exp uint64
  } {
    { "0", 0 },
    { "1024", 1024 },
    { "18446744073709551615", 18446744073709551615 },
  }

  for _, test := range(passTests) {
    t.Run(test.val, func(t *testing.T) {
      got, err := parseMetaSize("foo", test.val)
      if err != nil {
        t.Error(err)
        return
      }

      if got != test.exp {
        t.Errorf("got %d, exp %d", got, test.exp)
        return
      }
    })
  }

  failTests := []struct {
    val string
    exp string
  } {
    { "-1", "invalid foo: \"-1\"" },
    { "a", "invalid foo: \"a\"" },
    { "18446744073709551616", "invalid foo: \"18446744073709551616\"" },
  }

  for _, test := range(failTests) {
    t.Run(test.val, func(t *testing.T) {
      got, err := parseMetaSize("foo", test.val)
      if err == nil {
        t.Errorf("got %d, exp error", got)
      } else if err.Error() != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp)
      }
    })
  }
}

// test data
const testMeta = `lastModifiedDate:2022-01-29T03:01:16-05:00
size:73202582
zipSize:3753799
gzSize:3753663
sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440
`

// Test meta parser
func TestNewMeta(t *testing.T) {
  passTests := []string {
    `lastModifiedDate:2022-01-29T03:01:16-05:00
size:73202582
zipSize:3753799
gzSize:3753663
sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440
`,
  }

  for i, val := range(passTests) {
    // build test name
    name := fmt.Sprintf("passTests[%d]", i)

    t.Run(name, func(t *testing.T) {
      // create buffer
      buf := bytes.NewBufferString(val)

      // decode meta, check for error
      _, err := NewMeta(buf)
      if err != nil {
        t.Error(err)
      }
    })
  }

  failTests := []struct {
    val string
    exp string
  } {
    { "asdf", "bad meta line: \"asdf\"" },
    { "size:a", "invalid size: \"a\"" },
    { "zipSize:a", "invalid zipSize: \"a\"" },
    { "gzSize:a", "invalid gzSize: \"a\"" },
    { "sha256:a", "invalid sha256 hash length: 1" },
    {
      val: "sha256:0z00000000000000000000000000000000000000000000000000000000000000",
      exp: "invalid sha256 hash: encoding/hex: invalid byte: U+007A 'z'",
    },
    { "foo:bar", "unknown key: \"foo\"" },
  }

  for _, test := range(failTests) {
    t.Run(test.val, func(t *testing.T) {
      // create buffer
      buf := bytes.NewBufferString(test.val)

      // decode meta, check for error
      got, err := NewMeta(buf)
      if err == nil {
        t.Errorf("got %v, exp error", got)
      } else if err.Error() != test.exp {
        t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp)
      }
    })
  }

  t.Run("JsonEncode", func(t *testing.T) {
    // create buffer
    buf := bytes.NewBufferString(passTests[0])

    // decode meta, check for error
    meta, err := NewMeta(buf)
    if err != nil {
      t.Error(err)
    }

    // create destination buffer
    var dst bytes.Buffer

    // create json encoder
    e := json.NewEncoder(&dst)
    if err := e.Encode(meta); err != nil {
      t.Error(err)
    }
  })
}