File: section_writer_test.go

package info (click to toggle)
golang-github-azure-azure-storage-blob-go 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,084 kB
  • sloc: makefile: 3
file content (91 lines) | stat: -rw-r--r-- 3,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package azblob

import (
	"bytes"
	"io"

	chk "gopkg.in/check.v1"
)

func (s *aztestsSuite) TestSectionWriter(c *chk.C) {
	b := [10]byte{}
	buffer := newBytesWriter(b[:])

	section := newSectionWriter(buffer, 0, 5)
	c.Assert(section.count, chk.Equals, int64(5))
	c.Assert(section.offset, chk.Equals, int64(0))
	c.Assert(section.position, chk.Equals, int64(0))

	count, err := section.Write([]byte{1, 2, 3})
	c.Assert(err, chk.IsNil)
	c.Assert(count, chk.Equals, 3)
	c.Assert(section.position, chk.Equals, int64(3))
	c.Assert(b, chk.Equals, [10]byte{1, 2, 3, 0, 0, 0, 0, 0, 0, 0})

	count, err = section.Write([]byte{4, 5, 6})
	c.Assert(err, chk.ErrorMatches, "Not enough space for all bytes")
	c.Assert(count, chk.Equals, 2)
	c.Assert(section.position, chk.Equals, int64(5))
	c.Assert(b, chk.Equals, [10]byte{1, 2, 3, 4, 5, 0, 0, 0, 0, 0})

	count, err = section.Write([]byte{6, 7, 8})
	c.Assert(err, chk.ErrorMatches, "End of section reached")
	c.Assert(count, chk.Equals, 0)
	c.Assert(section.position, chk.Equals, int64(5))
	c.Assert(b, chk.Equals, [10]byte{1, 2, 3, 4, 5, 0, 0, 0, 0, 0})

	// Intentionally create a section writer which will attempt to write
	// outside the bounds of the buffer.
	section = newSectionWriter(buffer, 5, 6)
	c.Assert(section.count, chk.Equals, int64(6))
	c.Assert(section.offset, chk.Equals, int64(5))
	c.Assert(section.position, chk.Equals, int64(0))

	count, err = section.Write([]byte{6, 7, 8})
	c.Assert(err, chk.IsNil)
	c.Assert(count, chk.Equals, 3)
	c.Assert(section.position, chk.Equals, int64(3))
	c.Assert(b, chk.Equals, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 0, 0})

	// Attempt to write past the end of the section. Since the underlying
	// buffer rejects the write it gives the same error as in the normal case.
	count, err = section.Write([]byte{9, 10, 11})
	c.Assert(err, chk.ErrorMatches, "Not enough space for all bytes")
	c.Assert(count, chk.Equals, 2)
	c.Assert(section.position, chk.Equals, int64(5))
	c.Assert(b, chk.Equals, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})

	// Attempt to write past the end of the buffer. In this case the buffer
	// rejects the write completely since it falls completely out of bounds.
	count, err = section.Write([]byte{11, 12, 13})
	c.Assert(err, chk.ErrorMatches, "Offset value is out of range")
	c.Assert(count, chk.Equals, 0)
	c.Assert(section.position, chk.Equals, int64(5))
	c.Assert(b, chk.Equals, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}

func (s *aztestsSuite) TestSectionWriterCopySrcDestEmpty(c *chk.C) {
	input := make([]byte, 0)
	reader := bytes.NewReader(input)

	output := make([]byte, 0)
	buffer := newBytesWriter(output)
	section := newSectionWriter(buffer, 0, 0)

	count, err := io.Copy(section, reader)
	c.Assert(err, chk.IsNil)
	c.Assert(count, chk.Equals, int64(0))
}

func (s *aztestsSuite) TestSectionWriterCopyDestEmpty(c *chk.C) {
	input := make([]byte, 10)
	reader := bytes.NewReader(input)

	output := make([]byte, 0)
	buffer := newBytesWriter(output)
	section := newSectionWriter(buffer, 0, 0)

	count, err := io.Copy(section, reader)
	c.Assert(err, chk.ErrorMatches, "End of section reached")
	c.Assert(count, chk.Equals, int64(0))
}