| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package integrity |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/config/testconfig" |
| 21 |
"github.com/gohugoio/hugo/resources/resource" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
"github.com/gohugoio/hugo/resources/resource_transformers/htesting" |
| 25 |
) |
| 26 |
|
| 27 |
func TestHashFromAlgo(t *testing.T) { |
| 28 |
for _, algo := range []struct { |
| 29 |
name string |
| 30 |
bits int |
| 31 |
}{ |
| 32 |
{"md5", 128}, |
| 33 |
{"sha256", 256}, |
| 34 |
{"sha384", 384}, |
| 35 |
{"sha512", 512}, |
| 36 |
{"shaman", -1}, |
| 37 |
} { |
| 38 |
t.Run(algo.name, func(t *testing.T) { |
| 39 |
c := qt.New(t) |
| 40 |
h, err := newHash(algo.name) |
| 41 |
if algo.bits > 0 { |
| 42 |
c.Assert(err, qt.IsNil) |
| 43 |
c.Assert(h.Size(), qt.Equals, algo.bits/8) |
| 44 |
} else { |
| 45 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 46 |
c.Assert(err.Error(), qt.Contains, "use either md5, sha256, sha384 or sha512") |
| 47 |
} |
| 48 |
}) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestTransform(t *testing.T) { |
| 53 |
c := qt.New(t) |
| 54 |
|
| 55 |
d := testconfig.GetTestDeps(nil, nil) |
| 56 |
t.Cleanup(func() { c.Assert(d.Close(), qt.IsNil) }) |
| 57 |
|
| 58 |
client := New(d.ResourceSpec) |
| 59 |
|
| 60 |
r, err := htesting.NewResourceTransformerForSpec(d.ResourceSpec, "hugo.txt", "Hugo Rocks!") |
| 61 |
c.Assert(err, qt.IsNil) |
| 62 |
|
| 63 |
transformed, err := client.Fingerprint(r, "") |
| 64 |
|
| 65 |
c.Assert(err, qt.IsNil) |
| 66 |
c.Assert(transformed.RelPermalink(), qt.Equals, "/hugo.a5ad1c6961214a55de53c1ce6e60d27b6b761f54851fa65e33066460dfa6a0db.txt") |
| 67 |
c.Assert(transformed.Data(), qt.DeepEquals, map[string]any{"Integrity": "sha256-pa0caWEhSlXeU8HObmDSe2t2H1SFH6ZeMwZkYN+moNs="}) |
| 68 |
content, err := transformed.(resource.ContentProvider).Content(context.Background()) |
| 69 |
c.Assert(err, qt.IsNil) |
| 70 |
c.Assert(content, qt.Equals, "Hugo Rocks!") |
| 71 |
} |