2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
package blobref
|
|
|
|
|
|
|
|
import (
|
2011-09-17 00:59:15 +00:00
|
|
|
"bytes"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"encoding/json"
|
2011-09-28 18:05:02 +00:00
|
|
|
"fmt"
|
2011-10-05 14:41:54 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2010-12-17 17:59:03 +00:00
|
|
|
"testing"
|
2011-09-28 18:05:02 +00:00
|
|
|
|
2011-03-19 04:17:26 +00:00
|
|
|
. "camli/test/asserts"
|
2010-12-17 17:59:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAll(t *testing.T) {
|
2011-03-02 18:18:45 +00:00
|
|
|
refStr := "sha1-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
|
|
|
|
br := Parse(refStr)
|
2010-12-17 17:59:03 +00:00
|
|
|
if br == nil {
|
|
|
|
t.Fatalf("Failed to parse blobref")
|
|
|
|
}
|
|
|
|
if br.hashName != "sha1" {
|
|
|
|
t.Errorf("Expected sha1 hashName")
|
|
|
|
}
|
|
|
|
if br.digest != "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" {
|
|
|
|
t.Errorf("Invalid digest")
|
|
|
|
}
|
2011-03-02 18:18:45 +00:00
|
|
|
Expect(t, br.IsSupported(), "sha1 should be supported")
|
|
|
|
ExpectString(t, refStr, br.String(), "String() value")
|
2010-12-17 17:59:03 +00:00
|
|
|
|
|
|
|
hash := br.Hash()
|
|
|
|
hash.Write([]byte("foo"))
|
|
|
|
if !br.HashMatches(hash) {
|
|
|
|
t.Errorf("Expected hash of bytes 'foo' to match")
|
|
|
|
}
|
|
|
|
hash.Write([]byte("bogusextra"))
|
|
|
|
if br.HashMatches(hash) {
|
|
|
|
t.Errorf("Unexpected hash match with bogus extra bytes")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotSupported(t *testing.T) {
|
2011-03-02 18:18:45 +00:00
|
|
|
br := Parse("unknownfunc-0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
|
2010-12-17 17:59:03 +00:00
|
|
|
if br == nil {
|
|
|
|
t.Fatalf("Failed to parse blobref")
|
|
|
|
}
|
|
|
|
if br.IsSupported() {
|
|
|
|
t.Fatalf("Unexpected IsSupported() on unknownfunc")
|
|
|
|
}
|
|
|
|
}
|
2011-05-21 21:32:25 +00:00
|
|
|
|
|
|
|
func TestSum32(t *testing.T) {
|
|
|
|
refStr := "sha1-0000000000000000000000000000000000000012"
|
|
|
|
br := Parse(refStr)
|
|
|
|
if br == nil {
|
|
|
|
t.Fatalf("Failed to parse blobref")
|
|
|
|
}
|
|
|
|
h32 := br.Sum32()
|
|
|
|
if h32 != 18 {
|
|
|
|
t.Errorf("got %d, want 18", h32)
|
|
|
|
}
|
2011-06-08 00:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Foo struct {
|
2011-06-30 04:36:14 +00:00
|
|
|
B *BlobRef `json:"foo"`
|
2011-06-08 00:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonUnmarshal(t *testing.T) {
|
|
|
|
var f Foo
|
|
|
|
if err := json.Unmarshal([]byte(`{"foo": "abc-def123", "other": 123}`), &f); err != nil {
|
|
|
|
t.Fatalf("Unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
if f.B == nil {
|
|
|
|
t.Fatal("blobref is nil")
|
|
|
|
}
|
|
|
|
if g, e := f.B.String(), "abc-def123"; g != e {
|
|
|
|
t.Errorf("got %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonMarshal(t *testing.T) {
|
|
|
|
f := &Foo{B: MustParse("def-1234abc")}
|
|
|
|
bs, err := json.Marshal(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Marshal: %v", err)
|
|
|
|
}
|
|
|
|
if g, e := string(bs), `{"foo":"def-1234abc"}`; g != e {
|
|
|
|
t.Errorf("got %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
}
|
2011-09-17 00:59:15 +00:00
|
|
|
|
|
|
|
func TestGobbing(t *testing.T) {
|
2011-10-05 14:41:54 +00:00
|
|
|
if strings.Contains(runtime.Version(), "release.r60") {
|
|
|
|
t.Logf("Known to fail on r60")
|
|
|
|
return
|
|
|
|
}
|
2011-09-17 00:59:15 +00:00
|
|
|
br := MustParse("def-1234abc")
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
e := gob.NewEncoder(buf)
|
|
|
|
err := e.Encode(br)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Encode: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := gob.NewDecoder(buf)
|
2011-09-20 19:04:10 +00:00
|
|
|
var got *BlobRef
|
2011-09-17 00:59:15 +00:00
|
|
|
err = d.Decode(&got)
|
|
|
|
if err != nil {
|
2011-09-27 23:02:28 +00:00
|
|
|
t.Errorf("Decode: %v", err)
|
|
|
|
}
|
2011-09-17 00:59:15 +00:00
|
|
|
if got.String() != br.String() {
|
|
|
|
t.Errorf("got = %q, want %q", &got, br)
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 18:05:02 +00:00
|
|
|
|
|
|
|
func TestBlobRefString(t *testing.T) {
|
|
|
|
bp := MustParse("abc-123")
|
|
|
|
e := "abc-123"
|
|
|
|
if g := bp.String(); g != e {
|
|
|
|
t.Errorf("(&BlobRef).String() = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
if g := fmt.Sprintf("%s", bp); g != e {
|
|
|
|
t.Errorf("fmt %%s &BlobRef = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSizedBlobRefString(t *testing.T) {
|
|
|
|
sbv := SizedBlobRef{BlobRef: MustParse("abc-123"), Size: 456}
|
|
|
|
sbp := &sbv
|
|
|
|
e := "[abc-123; 456 bytes]"
|
|
|
|
if g := sbv.String(); g != e {
|
|
|
|
t.Errorf("SizedBlobRef.String() = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
if g := sbp.String(); g != e {
|
|
|
|
t.Errorf("(&SizedBlobRef).String() = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
if g := fmt.Sprintf("%s", sbv); g != e {
|
|
|
|
t.Errorf("fmt %%s SizedBlobRef = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
if g := fmt.Sprintf("%s", sbp); g != e {
|
|
|
|
t.Errorf("fmt %%s &SizedBlobRef = %q, want %q", g, e)
|
|
|
|
}
|
|
|
|
}
|