|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/binary" |
5 | 6 | "os" |
6 | 7 | "path/filepath" |
| 8 | + "strings" |
7 | 9 | "testing" |
8 | 10 |
|
9 | 11 | "github.com/chainguard-dev/clog/slogtest" |
@@ -852,56 +854,67 @@ func TestSetCapability(t *testing.T) { |
852 | 854 | t.Fatalf("Failed to collect capabilities: %v", err) |
853 | 855 | } |
854 | 856 |
|
855 | | - for path, caps := range caps { |
856 | | - enc := EncodeCapability(caps.Effective, caps.Permitted, caps.Inheritable) |
857 | | - if err := b.WorkspaceDirFS.SetXattr(path, "security.capability", enc); err != nil { |
858 | | - t.Fatalf("Failed to set capability: %v", err) |
| 857 | + expectedAttrs := make(map[string][]byte) |
| 858 | + for path, c := range caps { |
| 859 | + encoded := EncodeCapability(c.Effective, c.Permitted, c.Inheritable) |
| 860 | + expectedAttrs[path] = encoded |
| 861 | + |
| 862 | + if err := b.WorkspaceDirFS.SetXattr(path, "security.capability", encoded); err != nil { |
| 863 | + t.Fatalf("failed to set xattr for %s: %v", path, err) |
859 | 864 | } |
860 | 865 | } |
861 | 866 |
|
862 | | - for path, attrs := range tc.expectedAttrs { |
863 | | - for attr := range attrs { |
864 | | - data, err := b.WorkspaceDirFS.GetXattr(path, attr) |
865 | | - if err != nil { |
866 | | - t.Errorf("Failed to get xattr %s for path %s: %v", attr, path, err) |
867 | | - continue |
868 | | - } |
| 867 | + for path, expected := range expectedAttrs { |
| 868 | + data, err := b.WorkspaceDirFS.GetXattr(path, "security.capability") |
| 869 | + if err != nil { |
| 870 | + t.Errorf("Failed to get xattr %s: %v", path, err) |
| 871 | + continue |
| 872 | + } |
869 | 873 |
|
870 | | - if len(data) < 24 { |
871 | | - t.Errorf("Capability data for %s is too short: %d bytes", path, len(data)) |
872 | | - continue |
873 | | - } |
| 874 | + if !bytes.Equal(data, expected) { |
| 875 | + t.Errorf("Mismatched xattr for %s:\ngot: %x\nwant: %x", path, data, expected) |
| 876 | + } |
874 | 877 |
|
875 | | - magic := binary.LittleEndian.Uint32(data[0:4]) |
876 | | - if magic != 0x20080522 { |
877 | | - t.Errorf("Invalid magic number: %x", magic) |
878 | | - } |
| 878 | + if len(data) < 24 { |
| 879 | + t.Errorf("Capability data too short for %s: got %d bytes", path, len(data)) |
| 880 | + continue |
| 881 | + } |
| 882 | + |
| 883 | + magic := binary.LittleEndian.Uint32(data[0:4]) |
| 884 | + revision := magic & 0xFF000000 |
| 885 | + flags := magic & 0x000000FF |
879 | 886 |
|
880 | | - version := binary.LittleEndian.Uint32(data[4:8]) |
881 | | - if version != 0x3 { |
882 | | - t.Errorf("Invalid version: %d, expected 3", version) |
| 887 | + if revision != 0x03000000 { |
| 888 | + t.Errorf("Invalid revision: %x", revision) |
| 889 | + } |
| 890 | + |
| 891 | + permitted := binary.LittleEndian.Uint32(data[4:8]) |
| 892 | + inheritable := binary.LittleEndian.Uint32(data[8:12]) |
| 893 | + rootid := binary.LittleEndian.Uint32(data[20:24]) |
| 894 | + |
| 895 | + if rootid != 0 { |
| 896 | + t.Errorf("Unexpected rootid: %d", rootid) |
| 897 | + } |
| 898 | + |
| 899 | + effective := flags & 0x01 |
| 900 | + |
| 901 | + for _, capEntry := range tc.caps { |
| 902 | + if capEntry.Path != path { |
| 903 | + continue |
883 | 904 | } |
| 905 | + for attr, flag := range capEntry.Add { |
| 906 | + for _, a := range strings.Split(attr, ",") { |
| 907 | + val := getCapabilityValue(a) |
| 908 | + e, p, i := parseCapability(flag) |
884 | 909 |
|
885 | | - effective := binary.LittleEndian.Uint32(data[8:12]) |
886 | | - permitted := binary.LittleEndian.Uint32(data[12:16]) |
887 | | - inheritable := binary.LittleEndian.Uint32(data[16:20]) |
888 | | - |
889 | | - caps := b.Configuration.Package.SetCap |
890 | | - for _, c := range caps { |
891 | | - if c.Path == path { |
892 | | - for attr, flag := range c.Add { |
893 | | - capValues := getCapabilityValue(attr) |
894 | | - e, p, i := parseCapability(flag) |
895 | | - |
896 | | - if e && (effective&capValues != capValues) { |
897 | | - t.Errorf("Expected capabilities %s to be in effective set for %s", attr, path) |
898 | | - } |
899 | | - if p && (permitted&capValues != capValues) { |
900 | | - t.Errorf("Expected capabilities %s to be in permitted set for %s", attr, path) |
901 | | - } |
902 | | - if i && (inheritable&capValues != capValues) { |
903 | | - t.Errorf("Expected capabilities %s to be in inheritable set for %s", attr, path) |
904 | | - } |
| 910 | + if e && effective != 1 { |
| 911 | + t.Errorf("Expected effective bit set for %s", path) |
| 912 | + } |
| 913 | + if p && (permitted&val != val) { |
| 914 | + t.Errorf("Expected permitted cap %s in %s", a, path) |
| 915 | + } |
| 916 | + if i && (inheritable&val != val) { |
| 917 | + t.Errorf("Expected inheritable cap %s in %s", a, path) |
905 | 918 | } |
906 | 919 | } |
907 | 920 | } |
|
0 commit comments