github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/blob/ref_test.go (about) 1 /* 2 Copyright 2013 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package blob 18 19 import ( 20 "encoding/json" 21 "strings" 22 "testing" 23 ) 24 25 var parseTests = []struct { 26 in string 27 bad bool 28 }{ 29 {in: "sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"}, 30 {in: "foo-0b"}, 31 {in: "foo-0b0c"}, 32 33 {in: "/camli/sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", bad: true}, 34 {in: "", bad: true}, 35 {in: "foo", bad: true}, 36 {in: "-0f", bad: true}, 37 {in: "sha1-xx", bad: true}, 38 {in: "-", bad: true}, 39 {in: "sha1-0b", bad: true}, 40 41 // TODO: renable this later, once we clean all tests: 42 //{in: "foo-0b0cd", bad: true}, // odd number 43 {in: "foo-abc"}, // accepted for now. will delete later. 44 } 45 46 func TestParse(t *testing.T) { 47 for _, tt := range parseTests { 48 r, ok := Parse(tt.in) 49 if r.Valid() != ok { 50 t.Errorf("Valid != ok for %q", tt.in) 51 } 52 if ok && tt.bad { 53 t.Errorf("Parse(%q) didn't fail. It should've.", tt.in) 54 continue 55 } 56 if !ok { 57 if !tt.bad { 58 t.Errorf("Parse(%q) failed to parse", tt.in) 59 continue 60 } 61 continue 62 } 63 { 64 r2, ok := ParseBytes([]byte(tt.in)) 65 if r != r2 { 66 t.Errorf("ParseBytes(%q) = %v, %v; want %v", tt.in, r2, ok, r) 67 } 68 } 69 str := r.String() 70 if str != tt.in { 71 t.Errorf("Parsed %q but String() value differs: %q", tt.in, str) 72 } 73 wantDig := str[strings.Index(str, "-")+1:] 74 if dig := r.Digest(); dig != wantDig { 75 t.Errorf("Digest(%q) = %q; want %q", tt.in, dig, wantDig) 76 } 77 _ = r == r // test that concrete type of r supports equality 78 } 79 } 80 81 func TestEquality(t *testing.T) { 82 in := "sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" 83 in3 := "sha1-ffffffffffffffffffffffffffffffffffffffff" 84 r := ParseOrZero(in) 85 r2 := ParseOrZero(in) 86 r3 := ParseOrZero(in3) 87 if !r.Valid() || !r2.Valid() || !r3.Valid() { 88 t.Fatal("not valid") 89 } 90 if r != r2 { 91 t.Errorf("r and r2 should be equal") 92 } 93 if r == r3 { 94 t.Errorf("r and r3 should not be equal") 95 } 96 } 97 98 func TestSum32(t *testing.T) { 99 got := MustParse("sha1-1234567800000000000000000000000000000000").Sum32() 100 want := uint32(0x12345678) 101 if got != want { 102 t.Errorf("Sum32 = %x, want %x", got, want) 103 } 104 } 105 106 func TestSum64(t *testing.T) { 107 got := MustParse("sha1-12345678876543210000000000000000000000ff").Sum64() 108 want := uint64(0x1234567887654321) 109 if got != want { 110 t.Errorf("Sum64 = %x, want %x", got, want) 111 } 112 } 113 114 type Foo struct { 115 B Ref `json:"foo"` 116 } 117 118 func TestJSONUnmarshal(t *testing.T) { 119 var f Foo 120 if err := json.Unmarshal([]byte(`{"foo": "abc-def123", "other": 123}`), &f); err != nil { 121 t.Fatalf("Unmarshal: %v", err) 122 } 123 if !f.B.Valid() { 124 t.Fatal("blobref is nil") 125 } 126 if g, e := f.B.String(), "abc-def123"; g != e { 127 t.Errorf("got %q, want %q", g, e) 128 } 129 130 f = Foo{} 131 if err := json.Unmarshal([]byte(`{}`), &f); err != nil { 132 t.Fatalf("Unmarshal: %v", err) 133 } 134 if f.B.Valid() { 135 t.Fatal("blobref is valid and shouldn't be") 136 } 137 138 f = Foo{} 139 if err := json.Unmarshal([]byte(`{"foo":null}`), &f); err != nil { 140 t.Fatalf("Unmarshal: %v", err) 141 } 142 if f.B.Valid() { 143 t.Fatal("blobref is valid and shouldn't be") 144 } 145 } 146 147 func TestJSONMarshal(t *testing.T) { 148 f := &Foo{} 149 bs, err := json.Marshal(f) 150 if err != nil { 151 t.Fatalf("Marshal: %v", err) 152 } 153 if g, e := string(bs), `{"foo":null}`; g != e { 154 t.Errorf("got %q, want %q", g, e) 155 } 156 157 f = &Foo{B: MustParse("def-1234abcd")} 158 bs, err = json.Marshal(f) 159 if err != nil { 160 t.Fatalf("Marshal: %v", err) 161 } 162 if g, e := string(bs), `{"foo":"def-1234abcd"}`; g != e { 163 t.Errorf("got %q, want %q", g, e) 164 } 165 } 166 167 func TestSizedBlobRefString(t *testing.T) { 168 sr := SizedRef{Ref: MustParse("abc-1234"), Size: 456} 169 want := "[abc-1234; 456 bytes]" 170 if got := sr.String(); got != want { 171 t.Errorf("SizedRef.String() = %q, want %q", got, want) 172 } 173 } 174 175 func TestMarshalBinary(t *testing.T) { 176 br := MustParse("abc-00ff4869") 177 data, _ := br.MarshalBinary() 178 if got, want := string(data), "abc-\x00\xffHi"; got != want { 179 t.Fatalf("MarshalBinary = %q; want %q", got, want) 180 } 181 br2 := new(Ref) 182 if err := br2.UnmarshalBinary(data); err != nil { 183 t.Fatal(err) 184 } 185 if *br2 != br { 186 t.Error("UnmarshalBinary result != original") 187 } 188 189 if err := br2.UnmarshalBinary(data); err == nil { 190 t.Error("expect error on second UnmarshalBinary") 191 } 192 } 193 194 func BenchmarkParseBlob(b *testing.B) { 195 b.ReportAllocs() 196 ref := "sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" 197 refb := []byte(ref) 198 for i := 0; i < b.N; i++ { 199 if _, ok := Parse(ref); !ok { 200 b.Fatal() 201 } 202 if _, ok := ParseBytes(refb); !ok { 203 b.Fatal() 204 } 205 } 206 }