aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--README.md8
-rw-r--r--util/errors.go50
-rw-r--r--util/util.go33
-rw-r--r--util/util_test.go60
5 files changed, 148 insertions, 5 deletions
diff --git a/Makefile b/Makefile
index a174471..b891387 100644
--- a/Makefile
+++ b/Makefile
@@ -40,7 +40,7 @@ $(NAME):
# Makes sure go files build and tests pass
go:
- go build $(GOFLAGS) -o /dev/null ./...
+ go build $(GOFLAGS) ./...
go test $(GOFLAGS) ./...
lint:
diff --git a/README.md b/README.md
index 37497a4..c1ed4e2 100644
--- a/README.md
+++ b/README.md
@@ -76,10 +76,10 @@ default.
## Running and Installing
-`fscrypt` is a standalone binary, and it currently has no runtime dependencies.
-Installing it just requires placing it in your path or running `make install`.
-Change `$GOBIN` to change the install location of `fscrypt`; by default it is
-installed to `$GOPATH/bin`.
+`fscrypt` currently has no runtime dependencies. Installing it just requires
+placing it in your path or running `make install`. Change `$GOBIN` to change the
+install location of `fscrypt`. By default, `fscrypt` is installed to
+`$GOPATH/bin`.
## Example Usage
diff --git a/util/errors.go b/util/errors.go
new file mode 100644
index 0000000..aafeadd
--- /dev/null
+++ b/util/errors.go
@@ -0,0 +1,50 @@
+/*
+ * errors.go - Custom errors and error functions used by fscrypt
+ *
+ * Copyright 2017 Google Inc.
+ * Author: Joe Richey (joerichey@google.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package util
+
+import (
+ "fmt"
+ "log"
+)
+
+// InvalidInputF creates an error that should indicate either bad input from a
+// caller of a public library function or bad user input.
+func InvalidInputF(format string, a ...interface{}) error {
+ return fmt.Errorf("invalid input: "+format, a...)
+}
+
+// InvalidLengthError indicates name should have had length expected.
+func InvalidLengthError(name string, expected int, actual int) error {
+ return InvalidInputF("expected %s of length %d, actual length was %d", name, expected, actual)
+}
+
+// SystemErrorF creates an error that should indicate something has gone wrong
+// in the underlying system (syscall failure, bad ioctl, etc...).
+func SystemErrorF(format string, a ...interface{}) error {
+ return fmt.Errorf("system error: "+format, a...)
+}
+
+// NeverError panics if a non-nil error is passed in. It should be used to check
+// for logic errors, not to handle recoverable errors.
+func NeverError(err error) {
+ if err != nil {
+ log.Panicf("NeverError() check failed: %v", err)
+ }
+}
diff --git a/util/util.go b/util/util.go
new file mode 100644
index 0000000..9439d0e
--- /dev/null
+++ b/util/util.go
@@ -0,0 +1,33 @@
+/*
+ * util.go - Various helpers used throughout fscrypt
+ *
+ * Copyright 2017 Google Inc.
+ * Author: Joe Richey (joerichey@google.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+// Package util contains useful components for simplifying Go code.
+//
+// The package contains common error types (errors.go) and functions for
+// converting arrays to pointers.
+package util
+
+import (
+ "unsafe"
+)
+
+// Ptr converts an Go byte array to a pointer to the start of the array.
+func Ptr(slice []byte) unsafe.Pointer {
+ return unsafe.Pointer(&slice[0])
+}
diff --git a/util/util_test.go b/util/util_test.go
new file mode 100644
index 0000000..65541b3
--- /dev/null
+++ b/util/util_test.go
@@ -0,0 +1,60 @@
+/*
+ * util_test.go - Tests the util package
+ *
+ * Copyright 2017 Google Inc.
+ * Author: Joe Richey (joerichey@google.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package util
+
+import (
+ "testing"
+)
+
+const offset = 3
+
+// Make sure the address behaves well under slicing
+func TestPtrOffset(t *testing.T) {
+ arr := []byte{'a', 'b', 'c', 'd'}
+ i1 := uintptr(Ptr(arr[offset:]))
+ i2 := uintptr(Ptr(arr))
+
+ if i1 != i2+offset {
+ t.Fatalf("pointers %v and %v do not have an offset of %v", i1, i2, offset)
+ }
+}
+
+// Make sure NeverError actually panics
+func TestNeverErrorPanic(t *testing.T) {
+ defer func() {
+ if r := recover(); r == nil {
+ t.Errorf("NeverError did not panic")
+ }
+ }()
+
+ err := SystemErrorF("Hello")
+ NeverError(err)
+}
+
+// Make sure NeverError doesn't panic on nil
+func TestNeverErrorNoPanic(t *testing.T) {
+ defer func() {
+ if r := recover(); r != nil {
+ t.Errorf("NeverError panicked")
+ }
+ }()
+
+ NeverError(nil)
+}