Пример #1
0
func TestHash(t *testing.T) {
	password := []byte("test")
	sizes := []int{0, 24, 1337, 66560}
	for _, size := range sizes {
		h := sha256.New()
		t.Logf("Testing file of size: %db, with password: %s", size, password)
		b, err := randBytes(size)
		if err != nil {
			t.Errorf("randBytes(%d) => %q; want nil", size, err)
			continue
		}
		encReader, err := dcrypto.NewEncrypter(bytes.NewReader(b), password)
		if err != nil {
			t.Errorf("NewEncryper() => %q; want nil", err)
			continue
		}
		cipher, err := ioutil.ReadAll(io.TeeReader(encReader, h))
		if err != nil {
			t.Errorf("ioutil.ReadAll(*EncryptReader) => %q; want nil", err)
			continue
		}
		want := h.Sum(nil)
		got, err := dcrypto.Hash(bytes.NewReader(b), bytes.NewReader(cipher[0:dcrypto.MaxHeaderSize]), password, sha256.New)
		if err != nil {
			t.Errorf("Hash() => err = %q; want nil", err)
			continue
		}
		if !bytes.Equal(got, want) {
			t.Errorf("Hash() => %v; want %v", got, want)
		}
	}

}
Пример #2
0
// TestRoundTrip tests several size sets of data going through the encrypt/decrypt
// to make sure they come out the same.
func TestRoundTrip(t *testing.T) {
	sizes := []int{0, 24, 1337, 66560}
	spasswords := []string{
		"",
		"guest",
	}
	for _, x := range []int{13, 400} {
		rp, err := randBytes(x)
		if err != nil {
			t.Fatalf("randBytes(%d) => err", x)
		}
		spasswords = append(spasswords, string(rp))
	}
	for _, spass := range spasswords {
		password := []byte(spass)
		for _, size := range sizes {
			t.Logf("Testing file of size: %db", size)
			b, err := randBytes(size)
			if err != nil {
				t.Errorf("randBytes(%d) => %q; want nil", size, err)
				continue
			}
			encReader, err := dcrypto.NewEncrypter(bytes.NewReader(b), password)
			if err != nil {
				t.Errorf("NewEncrypter() => %q; want nil", err)
				continue
			}
			cipher, err := ioutil.ReadAll(encReader)
			if err != nil {
				t.Errorf("ioutil.ReadAll(*Encrypter) => %q; want nil", err)
				continue
			}
			decReader, err := dcrypto.NewDecrypter(bytes.NewReader(cipher), password)
			if err != nil {
				t.Errorf("NewDecrypter() => %q; want nil", err)
				continue
			}
			plain, err := ioutil.ReadAll(decReader)
			decReader.Close()
			if err != nil {
				t.Errorf("ioutil.ReadAll(*Decrypter) => %q; want nil", err)
				continue
			}
			if !bytes.Equal(b, plain) {
				t.Errorf("Encrypt/Decrypt of file size %d, resulted in different values", size)
			}
		}
	}
}