Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ type Crypto struct {
Walksize *uint64
}

var cryptoFile = "crypto"

// Crypto parses an crypto-file (/proc/crypto) and returns a slice of
// structs containing the relevant info. More information available here:
// https://kernel.readthedocs.io/en/sphinx-samples/crypto-API.html
func (fs FS) Crypto() ([]Crypto, error) {
path := fs.proc.Path("crypto")
path := fs.proc.Path(cryptoFile)
b, err := util.ReadFileNoStat(path)
if err != nil {
return nil, fmt.Errorf("%w: Cannot read file %v: %w", ErrFileRead, b, err)
Expand Down Expand Up @@ -82,6 +84,10 @@ func parseCrypto(r io.Reader) ([]Crypto, error) {
continue
}

if len(out) == 0 {
return nil, fmt.Errorf("%w: parsed invalid line before name parsed: %q", ErrFileParse, text)
}

kv := strings.Split(text, ":")
if len(kv) != 2 {
return nil, fmt.Errorf("%w: Cannot parse line: %q", ErrFileParse, text)
Expand Down
15 changes: 15 additions & 0 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package procfs

import (
"errors"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -121,6 +122,20 @@ func TestFS_Crypto(t *testing.T) {
}
}

func TestFS_CryptoCorrupted(t *testing.T) {
cryptoFile = "crypto_corrupted"
fs := getProcFixtures(t)
crypto, err := fs.Crypto()

if !errors.Is(err, ErrFileParse) {
t.Fatalf("expected ErrFileParse error, got: %s", err)
}

if crypto != nil {
t.Fatalf("expected empty crypto result")
}
}

func newint64(i int64) *int64 {
return &i
}
Expand Down
17 changes: 17 additions & 0 deletions testdata/fixtures.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,23 @@ max keysize : 32
Mode: 444
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/proc/crypto_corrupted
Lines: 13
driver : ccm_base(ctr(aes-aesni),cbcmac(aes-aesni))
name : ccm(aes)
module : ccm
priority : 300
refcnt : 4
selftest : passed
internal : no
type : aead
async : no
blocksize : 1
ivsize : 16
maxauthsize : 16
geniv : <none>
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: fixtures/proc/diskstats
Lines: 52
1 0 ram0 0 0 0 0 0 0 0 0 0 0 0
Expand Down