package erasureencode import ( // "fmt" "bytes" "io" "testing" ) var simpleEncodeMeta = &EEMeta{ Params: Params{ Size: 0, Stride: 2, Shards: 3, Parity: 2, }, } func TestEncode_EvenStride_Full(t *testing.T) { file := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11} simpleEncodeMeta.Params.Size = uint64(len(file)) reader := bytes.NewReader(file) outputs := []io.Writer{&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}} if err := Encode(reader, outputs, simpleEncodeMeta); err != nil { t.Errorf(`Error: %s`, err) } actual := outputs[0].(*bytes.Buffer).Bytes() expected := []byte{0x00, 0x01, 0x06, 0x07, 0x0c, 0x0d} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[0] %x to equal %x`, actual, expected) } actual = outputs[1].(*bytes.Buffer).Bytes() expected = []byte{0x02, 0x03, 0x08, 0x09, 0x0e, 0x0f} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[1] %x to equal %x`, actual, expected) } actual = outputs[2].(*bytes.Buffer).Bytes() expected = []byte{0x04, 0x05, 0x0a, 0x0b, 0x10, 0x11} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[2] %x to equal %x`, actual, expected) } unexpected := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} actual = outputs[3].(*bytes.Buffer).Bytes() if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[3] %x to not equal %x`, actual, unexpected) } actual = outputs[4].(*bytes.Buffer).Bytes() if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[4] %x to not equal %x`, actual, unexpected) } } func TestEncode_EvenStride_Short1(t *testing.T) { file := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} simpleEncodeMeta.Params.Size = uint64(len(file)) reader := bytes.NewReader(file) outputs := []io.Writer{&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}} if err := Encode(reader, outputs, simpleEncodeMeta); err != nil { t.Errorf(`Error: %s`, err) } actual := outputs[0].(*bytes.Buffer).Bytes() expected := []byte{0x00, 0x01, 0x06, 0x07, 0x0c, 0x0d} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[0] %x to equal %x`, actual, expected) } actual = outputs[1].(*bytes.Buffer).Bytes() expected = []byte{0x02, 0x03, 0x08, 0x09, 0x0e, 0x0f} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[1] %x to equal %x`, actual, expected) } actual = outputs[2].(*bytes.Buffer).Bytes() expected = []byte{0x04, 0x05, 0x0a, 0x0b, 0x10, 0x00} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[2] %x to equal %x`, actual, expected) } unexpected := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} actual = outputs[3].(*bytes.Buffer).Bytes() if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[3] %x to not equal %x`, actual, unexpected) } actual = outputs[4].(*bytes.Buffer).Bytes() if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[4] %x to not equal %x`, actual, unexpected) } } func TestEncode_EvenStride_Short2(t *testing.T) { file := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f} simpleEncodeMeta.Params.Size = uint64(len(file)) reader := bytes.NewReader(file) outputs := []io.Writer{&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}} if err := Encode(reader, outputs, simpleEncodeMeta); err != nil { t.Errorf(`Error: %s`, err) } actual := outputs[0].(*bytes.Buffer).Bytes() expected := []byte{0x00, 0x01, 0x06, 0x07, 0x0c, 0x0d} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[0] %x to equal %x`, actual, expected) } actual = outputs[1].(*bytes.Buffer).Bytes() expected = []byte{0x02, 0x03, 0x08, 0x09, 0x0e, 0x0f} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[1] %x to equal %x`, actual, expected) } actual = outputs[2].(*bytes.Buffer).Bytes() expected = []byte{0x04, 0x05, 0x0a, 0x0b, 0x00, 0x00} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[2] %x to equal %x`, actual, expected) } unexpected := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} actual = outputs[3].(*bytes.Buffer).Bytes() if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[3] %x to not equal %x`, actual, unexpected) } actual = outputs[4].(*bytes.Buffer).Bytes() if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[4] %x to not equal %x`, actual, unexpected) } } func TestEncode_OddStride_Full(t *testing.T) { file := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e} simpleEncodeMeta.Params.Size = uint64(len(file)) reader := bytes.NewReader(file) outputs := []io.Writer{&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}} if err := Encode(reader, outputs, simpleEncodeMeta); err != nil { t.Errorf(`Error: %s`, err) } actual := outputs[0].(*bytes.Buffer).Bytes() expected := []byte{0x00, 0x01, 0x06, 0x07, 0x0c} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[0] %x to equal %x`, actual, expected) } actual = outputs[1].(*bytes.Buffer).Bytes() expected = []byte{0x02, 0x03, 0x08, 0x09, 0x0d} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[1] %x to equal %x`, actual, expected) } actual = outputs[2].(*bytes.Buffer).Bytes() expected = []byte{0x04, 0x05, 0x0a, 0x0b, 0x0e} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[2] %x to equal %x`, actual, expected) } unexpected := []byte{0x00, 0x00, 0x00, 0x00, 0x00} actual = outputs[3].(*bytes.Buffer).Bytes() if len(actual) != len(unexpected) { t.Errorf("Expected[3] actual len to be %d, not %d", len(unexpected), len(actual)) } if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[3] %x to not equal %x`, actual, unexpected) } actual = outputs[4].(*bytes.Buffer).Bytes() if len(actual) != len(unexpected) { t.Errorf("Expected[4] actual len to be %d, not %d", len(unexpected), len(actual)) } if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[4] %x to not equal %x`, actual, unexpected) } } func TestEncode_OddStride_Short1(t *testing.T) { file := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d} simpleEncodeMeta.Params.Size = uint64(len(file)) reader := bytes.NewReader(file) outputs := []io.Writer{&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}} if err := Encode(reader, outputs, simpleEncodeMeta); err != nil { t.Errorf(`Error: %s`, err) } actual := outputs[0].(*bytes.Buffer).Bytes() expected := []byte{0x00, 0x01, 0x06, 0x07, 0x0c} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[0] %x to equal %x`, actual, expected) } actual = outputs[1].(*bytes.Buffer).Bytes() expected = []byte{0x02, 0x03, 0x08, 0x09, 0x0d} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[1] %x to equal %x`, actual, expected) } actual = outputs[2].(*bytes.Buffer).Bytes() expected = []byte{0x04, 0x05, 0x0a, 0x0b, 0x00} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[2] %x to equal %x`, actual, expected) } unexpected := []byte{0x00, 0x00, 0x00, 0x00, 0x00} actual = outputs[3].(*bytes.Buffer).Bytes() if len(actual) != len(unexpected) { t.Errorf("Expected[3] actual len to be %d, not %d", len(unexpected), len(actual)) } if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[3] %x to not equal %x`, actual, unexpected) } actual = outputs[4].(*bytes.Buffer).Bytes() if len(actual) != len(unexpected) { t.Errorf("Expected[4] actual len to be %d, not %d", len(unexpected), len(actual)) } if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[4] %x to not equal %x`, actual, unexpected) } } func TestEncode_OddStride_Short2(t *testing.T) { file := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c} simpleEncodeMeta.Params.Size = uint64(len(file)) reader := bytes.NewReader(file) outputs := []io.Writer{&bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}, &bytes.Buffer{}} if err := Encode(reader, outputs, simpleEncodeMeta); err != nil { t.Errorf(`Error: %s`, err) } actual := outputs[0].(*bytes.Buffer).Bytes() expected := []byte{0x00, 0x01, 0x06, 0x07, 0x0c} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[0] %x to equal %x`, actual, expected) } actual = outputs[1].(*bytes.Buffer).Bytes() expected = []byte{0x02, 0x03, 0x08, 0x09, 0x00} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[1] %x to equal %x`, actual, expected) } actual = outputs[2].(*bytes.Buffer).Bytes() expected = []byte{0x04, 0x05, 0x0a, 0x0b, 0x00} if !bytes.Equal(actual, expected) { t.Errorf(`Expected[2] %x to equal %x`, actual, expected) } unexpected := []byte{0x00, 0x00, 0x00, 0x00, 0x00} actual = outputs[3].(*bytes.Buffer).Bytes() if len(actual) != len(unexpected) { t.Errorf("Expected[3] actual len to be %d, not %d", len(unexpected), len(actual)) } if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[3] %x to not equal %x`, actual, unexpected) } actual = outputs[4].(*bytes.Buffer).Bytes() if len(actual) != len(unexpected) { t.Errorf("Expected[4] actual len to be %d, not %d", len(unexpected), len(actual)) } if bytes.Equal(actual, unexpected) { t.Errorf(`Expected[4] %x to not equal %x`, actual, unexpected) } }