Exemplo n.º 1
0
func TestKeyNewPrivateNum(t *testing.T) {
	k := cas.NewKeyPrivateNum(31337)
	buf := k.Bytes()
	k2 := cas.NewKey(buf)
	if g, e := k2, cas.Invalid; g != e {
		t.Errorf("expected NewKey to give Invalid: %v", g)
	}
	k3 := cas.NewKeyPrivate(buf)
	if g, e := k3, k; g != e {
		t.Errorf("expected NewKeyPrivate to give original key: %v", g)
	}
	priv, ok := k3.Private()
	if !ok {
		t.Fatalf("expected Private to work: %v %v", priv, ok)
	}
	if g, e := priv, uint64(31337); g != e {
		t.Errorf("expected Private to match original: %v", g)
	}
	if g, e := k.IsSpecial(), true; g != e {
		t.Errorf("not Special: %v != %v", g, e)
	}
	if g, e := k.IsPrivate(), true; g != e {
		t.Errorf("not Private: %v != %v", g, e)
	}
	if g, e := k.IsReserved(), false; g != e {
		t.Errorf("bad Reserved: %v != %v", g, e)
	}
}
Exemplo n.º 2
0
// Clone is like Get but clones the chunk if it's not already private.
// Chunks that are already private are returned as-is.
//
// A cloned chunk will have a buffer of size bytes. This is intended
// to use for re-inflating zero-trimmed chunks.
//
// Modifying the returned chunk *will* cause the locally stored data
// to change. This is the intended usage of a stash.
func (s *Stash) Clone(key cas.Key, typ string, level uint8, size uint32) (cas.Key, *chunks.Chunk, error) {
	priv, ok := key.Private()
	if ok {
		chunk, ok := s.local[priv]
		if !ok {
			return key, nil, cas.NotFound{
				Type:  typ,
				Level: level,
				Key:   key,
			}
		}
		return key, chunk, nil
	}

	chunk, err := s.chunks.Get(key, typ, level)
	if err != nil {
		return key, nil, err
	}

	// clone the byte slice
	tmp := make([]byte, size)
	copy(tmp, chunk.Buf)
	chunk.Buf = tmp

	priv = s.ids.Get()
	privkey := cas.NewKeyPrivateNum(priv)
	s.local[priv] = chunk
	return privkey, chunk, nil
}