move json blob TestFetcher to camli/test package

This commit is contained in:
Brad Fitzpatrick 2011-03-24 16:47:51 -07:00
parent a785d945f8
commit db5e3fe358
2 changed files with 89 additions and 69 deletions

View File

@ -17,14 +17,11 @@ limitations under the License.
package jsonsign
import (
"camli/blobref"
"camli/test"
. "camli/test/asserts"
"fmt"
"os"
"strings"
"sync"
"testing"
)
@ -98,7 +95,7 @@ jyVNPb8AaaWVW1uZLg6Em61aKnbOG10B30m3CQ8dwBjF9hgmtcY0IZ/Y
var pubKeyBlob1 = &test.Blob{pubKey1} // user 1
var pubKeyBlob2 = &test.Blob{pubKey2} // user 2
var testFetcher = &TestFetcher{}
var testFetcher = &test.Fetcher{}
func init() {
testFetcher.AddBlob(pubKeyBlob1)
@ -168,68 +165,3 @@ func TestSigning(t *testing.T) {
t.Logf("TODO: verify GPG-vs-Go sign & verify interop both ways, once implemented.")
}
type TestFetcher struct {
l sync.Mutex
m map[string]*test.Blob
}
func (tf *TestFetcher) AddBlob(b *test.Blob) {
tf.l.Lock()
defer tf.l.Unlock()
if tf.m == nil {
tf.m = make(map[string]*test.Blob)
}
tf.m[b.BlobRef().String()] = b
}
func (tf *TestFetcher) Fetch(ref *blobref.BlobRef) (file blobref.ReadSeekCloser, size int64, err os.Error) {
tf.l.Lock()
defer tf.l.Unlock()
if tf.m == nil {
err = os.ENOENT
return
}
tb, ok := tf.m[ref.String()]
if !ok {
err = os.ENOENT
return
}
file = &strReader{tb.Contents, 0}
size = int64(len(tb.Contents))
return
}
type strReader struct {
s string
pos int
}
func (sr *strReader) Close() os.Error { return nil }
func (sr *strReader) Seek(offset int64, whence int) (ret int64, err os.Error) {
// Note: ignoring 64-bit offsets. test data should be tiny.
switch whence {
case 0:
sr.pos = int(offset)
case 1:
sr.pos += int(offset)
case 2:
sr.pos = len(sr.s) + int(offset)
}
ret = int64(sr.pos)
return
}
func (sr *strReader) Read(p []byte) (n int, err os.Error) {
if sr.pos >= len(sr.s) {
err = os.EOF
return
}
n = copy(p, sr.s[sr.pos:])
if n == 0 {
err = os.EOF
}
sr.pos += n
return
}

View File

@ -0,0 +1,88 @@
/*
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.
*/
package test
import (
"camli/blobref"
"os"
"sync"
)
type Fetcher struct {
l sync.Mutex
m map[string]*Blob
}
func (tf *Fetcher) AddBlob(b *Blob) {
tf.l.Lock()
defer tf.l.Unlock()
if tf.m == nil {
tf.m = make(map[string]*Blob)
}
tf.m[b.BlobRef().String()] = b
}
func (tf *Fetcher) Fetch(ref *blobref.BlobRef) (file blobref.ReadSeekCloser, size int64, err os.Error) {
tf.l.Lock()
defer tf.l.Unlock()
if tf.m == nil {
err = os.ENOENT
return
}
tb, ok := tf.m[ref.String()]
if !ok {
err = os.ENOENT
return
}
file = &strReader{tb.Contents, 0}
size = int64(len(tb.Contents))
return
}
type strReader struct {
s string
pos int
}
func (sr *strReader) Close() os.Error { return nil }
func (sr *strReader) Seek(offset int64, whence int) (ret int64, err os.Error) {
// Note: ignoring 64-bit offsets. test data should be tiny.
switch whence {
case 0:
sr.pos = int(offset)
case 1:
sr.pos += int(offset)
case 2:
sr.pos = len(sr.s) + int(offset)
}
ret = int64(sr.pos)
return
}
func (sr *strReader) Read(p []byte) (n int, err os.Error) {
if sr.pos >= len(sr.s) {
err = os.EOF
return
}
n = copy(p, sr.s[sr.pos:])
if n == 0 {
err = os.EOF
}
sr.pos += n
return
}