|
| 1 | +package sid_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/TheManticoreProject/winacl/sid" |
| 7 | +) |
| 8 | + |
| 9 | +// TestSID_ToString_NoSpuriousRID is a regression test for ToString fabricating a |
| 10 | +// trailing "-0" for a SID with SubAuthorityCount == 0. A bare NT Authority SID |
| 11 | +// (binary: revision 1, 0 sub-authorities, identifier authority 5) must render |
| 12 | +// as "S-1-5", not "S-1-5-0" (which is the canonical string of a different SID). |
| 13 | +func TestSID_ToString_NoSpuriousRID(t *testing.T) { |
| 14 | + // 01 00 000000000005 : Revision 1, SubAuthorityCount 0, IdentifierAuthority 5. |
| 15 | + bare := []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05} |
| 16 | + |
| 17 | + s := &sid.SID{} |
| 18 | + if _, err := s.Unmarshal(bare); err != nil { |
| 19 | + t.Fatalf("Unmarshal() error = %v", err) |
| 20 | + } |
| 21 | + if s.SubAuthorityCount != 0 { |
| 22 | + t.Fatalf("SubAuthorityCount = %d, want 0", s.SubAuthorityCount) |
| 23 | + } |
| 24 | + if got := s.ToString(); got != "S-1-5" { |
| 25 | + t.Fatalf("ToString() = %q, want %q", got, "S-1-5") |
| 26 | + } |
| 27 | + |
| 28 | + // A normal SID must still include its RID. |
| 29 | + normal := &sid.SID{} |
| 30 | + if err := normal.FromString("S-1-5-32-544"); err != nil { |
| 31 | + t.Fatalf("FromString() error = %v", err) |
| 32 | + } |
| 33 | + if got := normal.ToString(); got != "S-1-5-32-544" { |
| 34 | + t.Fatalf("ToString() = %q, want %q", got, "S-1-5-32-544") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// TestSID_HexIdentifierAuthority is a regression test for the identifier |
| 39 | +// authority >= 2^32 not being formatted/parsed in the hexadecimal form that |
| 40 | +// MS-DTYP 2.4.2.1 mandates ("0x" followed by 12 hex digits). |
| 41 | +func TestSID_HexIdentifierAuthority(t *testing.T) { |
| 42 | + // 2^40 = 0x10000000000, which is >= 2^32 and within the 48-bit field. |
| 43 | + const authority = uint64(1) << 40 |
| 44 | + const wantStr = "S-1-0x010000000000-1-2" |
| 45 | + |
| 46 | + // ToString must emit the hex form. |
| 47 | + s := &sid.SID{} |
| 48 | + if err := s.FromString("S-1-1099511627776-1-2"); err != nil { |
| 49 | + t.Fatalf("FromString(decimal) error = %v", err) |
| 50 | + } |
| 51 | + if s.IdentifierAuthority.Value != authority { |
| 52 | + t.Fatalf("parsed authority = %d, want %d", s.IdentifierAuthority.Value, authority) |
| 53 | + } |
| 54 | + if got := s.ToString(); got != wantStr { |
| 55 | + t.Fatalf("ToString() = %q, want %q", got, wantStr) |
| 56 | + } |
| 57 | + |
| 58 | + // FromString must accept the hex form and decode the same value. |
| 59 | + h := &sid.SID{} |
| 60 | + if err := h.FromString(wantStr); err != nil { |
| 61 | + t.Fatalf("FromString(hex) error = %v", err) |
| 62 | + } |
| 63 | + if h.IdentifierAuthority.Value != authority { |
| 64 | + t.Fatalf("hex-parsed authority = %d, want %d", h.IdentifierAuthority.Value, authority) |
| 65 | + } |
| 66 | + |
| 67 | + // Round-trip: the hex string re-serializes and re-parses to the same value. |
| 68 | + if got := h.ToString(); got != wantStr { |
| 69 | + t.Fatalf("hex round-trip ToString() = %q, want %q", got, wantStr) |
| 70 | + } |
| 71 | + |
| 72 | + // A value < 2^32 must still be decimal. |
| 73 | + dec := &sid.SID{} |
| 74 | + if err := dec.FromString("S-1-5-18"); err != nil { |
| 75 | + t.Fatalf("FromString() error = %v", err) |
| 76 | + } |
| 77 | + if got := dec.ToString(); got != "S-1-5-18" { |
| 78 | + t.Fatalf("ToString() = %q, want %q (small authority must stay decimal)", got, "S-1-5-18") |
| 79 | + } |
| 80 | +} |
0 commit comments