package gallery import ( "slices" "testing" ) // TestFingerprintedNamesTheHashedTwinsOnly: an asset's hashed name is // listed under the mount when the tree holds it beside its plain twin // with the same bytes. The plain twin is not listed, since its bytes // change under the same name, and neither is a name that merely looks // hashed: one whose hex is not its own bytes' hash, or one with no // plain twin, could be replaced by other bytes under the same name. // Nor is a twin under the plain file's true hashed name that holds // other bytes, a corrupted copy: the name is right, so only comparing // the bytes keeps a browser from holding the wrong ones for a year. func TestFingerprintedNamesTheHashedTwinsOnly(t *testing.T) { css, js, pic := []byte("body{}\n"), []byte("0\n"), []byte("GIF89a") plain, corrupted := []byte("main{}\n"), []byte("main{color:red}\n") tree := map[string][]byte{ "assets/css/fiesta.css": css, "assets/css/" + hashedName("fiesta.css", css): css, "assets/gallery.js": js, "assets/" + hashedName("gallery.js", js): js, "assets/images/a.gif": pic, "assets/images/" + hashedName("a.gif", pic): pic, "assets/lone.0123456789abcdef.css": css, "assets/wrong.css": css, "assets/wrong.0123456789abcdef.css": css, "assets/torn.css": plain, "assets/" + hashedName("torn.css", plain): corrupted, "day/en/" + hashedName("page.html", js): js, "day/en/page.html": js, } want := []string{ "/x/assets/" + hashedName("gallery.js", js), "/x/assets/css/" + hashedName("fiesta.css", css), "/x/assets/images/" + hashedName("a.gif", pic), } slices.Sort(want) if got := Fingerprinted("/x/", tree); !slices.Equal(got, want) { t.Errorf("Fingerprinted is\n%q\nwant\n%q", got, want) } }