osbase/blockdev: add tests, fix minor issues

Add a lot of bounds checks which should make BlockDev safer to use. Fix
a bug in the ReadWriteSeeker.Seek function with io.SeekEnd; the offset
should be added to, not subtracted from the size. Add the Sync()
function to the BlockDev interface.

Change-Id: I247095b3dbc6410064844b4ac7c6208d88a7abcd
Reviewed-on: https://review.monogon.dev/c/monogon/+/3338
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/osbase/blockdev/blockdev_darwin.go b/osbase/blockdev/blockdev_darwin.go
index 5422e55..725c3a5 100644
--- a/osbase/blockdev/blockdev_darwin.go
+++ b/osbase/blockdev/blockdev_darwin.go
@@ -45,22 +45,26 @@
 	return d.blockSize
 }
 
+func (d *Device) OptimalBlockSize() int64 {
+	return d.blockSize
+}
+
 func (d *Device) Discard(startByte int64, endByte int64) error {
 	// Can be implemented using DKIOCUNMAP, but needs x/sys/unix extension.
 	// Not mandatory, so this is fine for now.
 	return errors.ErrUnsupported
 }
 
-func (d *Device) OptimalBlockSize() int64 {
-	return d.blockSize
-}
-
 func (d *Device) Zero(startByte int64, endByte int64) error {
 	// It doesn't look like MacOS even has any zeroing acceleration, so just
 	// use the generic one.
 	return GenericZero(d, startByte, endByte)
 }
 
+func (d *Device) Sync() error {
+	return d.backend.Sync()
+}
+
 // Open opens a block device given a path to its inode.
 func Open(path string) (*Device, error) {
 	outFile, err := os.OpenFile(path, os.O_RDWR, 0640)
@@ -156,16 +160,20 @@
 	return d.blockSize
 }
 
+func (d *File) OptimalBlockSize() int64 {
+	return d.blockSize
+}
+
 func (d *File) Discard(startByte int64, endByte int64) error {
 	// Can be supported in the future via fnctl.
 	return errors.ErrUnsupported
 }
 
-func (d *File) OptimalBlockSize() int64 {
-	return d.blockSize
-}
-
 func (d *File) Zero(startByte int64, endByte int64) error {
 	// Can possibly be accelerated in the future via fnctl.
 	return GenericZero(d, startByte, endByte)
 }
+
+func (d *File) Sync() error {
+	return d.backend.Sync()
+}