Skip to content

Commit

Permalink
Renamed native to sys
Browse files Browse the repository at this point in the history
The term _native_ does not actually describe what the functions/symbols are doing. Renamed them to `sys` as all are related to system calls.
  • Loading branch information
danieljoos committed Aug 5, 2018
1 parent ba0c728 commit ab1dbd1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 92 deletions.
10 changes: 5 additions & 5 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func goBytes(src uintptr, len uint32) []byte {
}

// Convert the given CREDENTIAL struct to a more usable structure
func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
func sysToCredential(cred *sysCREDENTIAL) (result *Credential) {
if cred == nil {
return nil
}
Expand All @@ -68,7 +68,7 @@ func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {
result.Persist = CredentialPersistence(cred.Persist)
result.CredentialBlob = goBytes(cred.CredentialBlob, cred.CredentialBlobSize)
result.Attributes = make([]CredentialAttribute, cred.AttributeCount)
attrSlice := *(*[]nativeCREDENTIAL_ATTRIBUTE)(unsafe.Pointer(&reflect.SliceHeader{
attrSlice := *(*[]sysCREDENTIAL_ATTRIBUTE)(unsafe.Pointer(&reflect.SliceHeader{
Data: cred.Attributes,
Len: int(cred.AttributeCount),
Cap: int(cred.AttributeCount),
Expand All @@ -83,11 +83,11 @@ func nativeToCredential(cred *nativeCREDENTIAL) (result *Credential) {

// Convert the given Credential object back to a CREDENTIAL struct, which can be used for calling the
// Windows APIs
func nativeFromCredential(cred *Credential) (result *nativeCREDENTIAL) {
func sysFromCredential(cred *Credential) (result *sysCREDENTIAL) {
if cred == nil {
return nil
}
result = new(nativeCREDENTIAL)
result = new(sysCREDENTIAL)
result.Flags = 0
result.Type = 0
result.TargetName, _ = syscall.UTF16PtrFromString(cred.TargetName)
Expand All @@ -101,7 +101,7 @@ func nativeFromCredential(cred *Credential) (result *nativeCREDENTIAL) {
}
result.Persist = uint32(cred.Persist)
result.AttributeCount = uint32(len(cred.Attributes))
attributes := make([]nativeCREDENTIAL_ATTRIBUTE, len(cred.Attributes))
attributes := make([]sysCREDENTIAL_ATTRIBUTE, len(cred.Attributes))
if len(attributes) > 0 {
result.Attributes = uintptr(unsafe.Pointer(&attributes[0]))
} else {
Expand Down
64 changes: 32 additions & 32 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func BenchmarkGoBytes(b *testing.B) {

func TestConversion(t *testing.T) {
cred := fixtureCredential()
native := nativeFromCredential(cred)
res := nativeToCredential(native)
assert.NotEqual(t, uintptr(0), native.TargetName)
sys := sysFromCredential(cred)
res := sysToCredential(sys)
assert.NotEqual(t, uintptr(0), sys.TargetName)
assert.Equal(t, cred.TargetName, res.TargetName)
assert.Equal(t, cred.Comment, res.Comment)
assert.Equal(t, cred.LastWritten, res.LastWritten)
Expand All @@ -116,42 +116,42 @@ func TestConversion(t *testing.T) {

func TestConversion_Nil(t *testing.T) {
assert.NotPanics(t, func() {
res := nativeToCredential(nil)
res := sysToCredential(nil)
assert.Nil(t, res)
})
assert.NotPanics(t, func() {
res := nativeFromCredential(nil)
res := sysFromCredential(nil)
assert.Nil(t, res)
})
}

func TestConversion_CredentialBlob(t *testing.T) {
cred := new(Credential)
cred.CredentialBlob = []byte{1, 2, 3}
native := nativeFromCredential(cred)
res := nativeToCredential(native)
assert.Equal(t, uint32(3), native.CredentialBlobSize)
assert.NotEqual(t, uintptr(0), native.CredentialBlob)
sys := sysFromCredential(cred)
res := sysToCredential(sys)
assert.Equal(t, uint32(3), sys.CredentialBlobSize)
assert.NotEqual(t, uintptr(0), sys.CredentialBlob)
assert.Equal(t, cred.CredentialBlob, res.CredentialBlob)
}

func TestConversion_CredentialBlob_Empty(t *testing.T) {
cred := new(Credential)
cred.CredentialBlob = []byte{} // empty blob
native := nativeFromCredential(cred)
res := nativeToCredential(native)
assert.Equal(t, uintptr(0), native.CredentialBlob)
assert.Equal(t, uint32(0), native.CredentialBlobSize)
sys := sysFromCredential(cred)
res := sysToCredential(sys)
assert.Equal(t, uintptr(0), sys.CredentialBlob)
assert.Equal(t, uint32(0), sys.CredentialBlobSize)
assert.Equal(t, []byte{}, res.CredentialBlob)
}

func TestConversion_CredentialBlob_Nil(t *testing.T) {
cred := new(Credential)
cred.CredentialBlob = nil // nil blob
native := nativeFromCredential(cred)
res := nativeToCredential(native)
assert.Equal(t, uintptr(0), native.CredentialBlob)
assert.Equal(t, uint32(0), native.CredentialBlobSize)
sys := sysFromCredential(cred)
res := sysToCredential(sys)
assert.Equal(t, uintptr(0), sys.CredentialBlob)
assert.Equal(t, uint32(0), sys.CredentialBlobSize)
assert.Equal(t, []byte{}, res.CredentialBlob)
}

Expand All @@ -161,44 +161,44 @@ func TestConversion_Attributes(t *testing.T) {
{Keyword: "Foo", Value: []byte{1, 2, 3}},
{Keyword: "Bar", Value: []byte{}},
}
native := nativeFromCredential(cred)
res := nativeToCredential(native)
assert.NotEqual(t, uintptr(0), native.Attributes)
assert.Equal(t, uint32(2), native.AttributeCount)
sys := sysFromCredential(cred)
res := sysToCredential(sys)
assert.NotEqual(t, uintptr(0), sys.Attributes)
assert.Equal(t, uint32(2), sys.AttributeCount)
assert.Equal(t, cred.Attributes, res.Attributes)
}

func TestConversion_Attributes_Empty(t *testing.T) {
cred := new(Credential)
cred.Attributes = []CredentialAttribute{}
native := nativeFromCredential(cred)
res := nativeToCredential(native)
assert.Equal(t, uintptr(0), native.Attributes)
assert.Equal(t, uint32(0), native.AttributeCount)
sys := sysFromCredential(cred)
res := sysToCredential(sys)
assert.Equal(t, uintptr(0), sys.Attributes)
assert.Equal(t, uint32(0), sys.AttributeCount)
assert.Equal(t, []CredentialAttribute{}, res.Attributes)
}

func TestConversion_Attributes_Nil(t *testing.T) {
cred := new(Credential)
cred.Attributes = nil
native := nativeFromCredential(cred)
res := nativeToCredential(native)
assert.Equal(t, uintptr(0), native.Attributes)
assert.Equal(t, uint32(0), native.AttributeCount)
sys := sysFromCredential(cred)
res := sysToCredential(sys)
assert.Equal(t, uintptr(0), sys.Attributes)
assert.Equal(t, uint32(0), sys.AttributeCount)
assert.Equal(t, []CredentialAttribute{}, res.Attributes)
}

func BenchmarkConversionFrom(b *testing.B) {
cred := fixtureCredential()
for i := 0; i < b.N; i++ {
nativeFromCredential(cred)
sysFromCredential(cred)
}
}

func BenchmarkConversionTo(b *testing.B) {
cred := fixtureCredential()
n := nativeFromCredential(cred)
n := sysFromCredential(cred)
for i := 0; i < b.N; i++ {
nativeToCredential(n)
sysToCredential(n)
}
}
40 changes: 20 additions & 20 deletions native.go → sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type proc interface {
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788(v=vs.85).aspx
type nativeCREDENTIAL struct {
type sysCREDENTIAL struct {
Flags uint32
Type uint32
TargetName *uint16
Expand All @@ -40,30 +40,30 @@ type nativeCREDENTIAL struct {
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374790(v=vs.85).aspx
type nativeCREDENTIAL_ATTRIBUTE struct {
type sysCREDENTIAL_ATTRIBUTE struct {
Keyword *uint16
Flags uint32
ValueSize uint32
Value uintptr
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788(v=vs.85).aspx
type nativeCRED_TYPE uint32
type sysCRED_TYPE uint32

const (
naCRED_TYPE_GENERIC nativeCRED_TYPE = 0x1
naCRED_TYPE_DOMAIN_PASSWORD nativeCRED_TYPE = 0x2
naCRED_TYPE_DOMAIN_CERTIFICATE nativeCRED_TYPE = 0x3
naCRED_TYPE_DOMAIN_VISIBLE_PASSWORD nativeCRED_TYPE = 0x4
naCRED_TYPE_GENERIC_CERTIFICATE nativeCRED_TYPE = 0x5
naCRED_TYPE_DOMAIN_EXTENDED nativeCRED_TYPE = 0x6

naERROR_NOT_FOUND = "Element not found."
sysCRED_TYPE_GENERIC sysCRED_TYPE = 0x1
sysCRED_TYPE_DOMAIN_PASSWORD sysCRED_TYPE = 0x2
sysCRED_TYPE_DOMAIN_CERTIFICATE sysCRED_TYPE = 0x3
sysCRED_TYPE_DOMAIN_VISIBLE_PASSWORD sysCRED_TYPE = 0x4
sysCRED_TYPE_GENERIC_CERTIFICATE sysCRED_TYPE = 0x5
sysCRED_TYPE_DOMAIN_EXTENDED sysCRED_TYPE = 0x6

sysERROR_NOT_FOUND = "Element not found."
)

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374804(v=vs.85).aspx
func nativeCredRead(targetName string, typ nativeCRED_TYPE) (*Credential, error) {
var pcred *nativeCREDENTIAL
func sysCredRead(targetName string, typ sysCRED_TYPE) (*Credential, error) {
var pcred *sysCREDENTIAL
targetNamePtr, _ := syscall.UTF16PtrFromString(targetName)
ret, _, err := procCredRead.Call(
uintptr(unsafe.Pointer(targetNamePtr)),
Expand All @@ -76,12 +76,12 @@ func nativeCredRead(targetName string, typ nativeCRED_TYPE) (*Credential, error)
}
defer procCredFree.Call(uintptr(unsafe.Pointer(pcred)))

return nativeToCredential(pcred), nil
return sysToCredential(pcred), nil
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa375187(v=vs.85).aspx
func nativeCredWrite(cred *Credential, typ nativeCRED_TYPE) error {
ncred := nativeFromCredential(cred)
func sysCredWrite(cred *Credential, typ sysCRED_TYPE) error {
ncred := sysFromCredential(cred)
ncred.Type = uint32(typ)
ret, _, err := procCredWrite.Call(
uintptr(unsafe.Pointer(ncred)),
Expand All @@ -95,7 +95,7 @@ func nativeCredWrite(cred *Credential, typ nativeCRED_TYPE) error {
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/aa374787(v=vs.85).aspx
func nativeCredDelete(cred *Credential, typ nativeCRED_TYPE) error {
func sysCredDelete(cred *Credential, typ sysCRED_TYPE) error {
targetNamePtr, _ := syscall.UTF16PtrFromString(cred.TargetName)
ret, _, err := procCredDelete.Call(
uintptr(unsafe.Pointer(targetNamePtr)),
Expand All @@ -110,7 +110,7 @@ func nativeCredDelete(cred *Credential, typ nativeCRED_TYPE) error {
}

// https://msdn.microsoft.com/en-us/library/windows/desktop/aa374794(v=vs.85).aspx
func nativeCredEnumerate(filter string, all bool) ([]*Credential, error) {
func sysCredEnumerate(filter string, all bool) ([]*Credential, error) {
var count int
var pcreds uintptr
var filterPtr uintptr
Expand All @@ -130,14 +130,14 @@ func nativeCredEnumerate(filter string, all bool) ([]*Credential, error) {
return nil, err
}
defer procCredFree.Call(pcreds)
credsSlice := *(*[]*nativeCREDENTIAL)(unsafe.Pointer(&reflect.SliceHeader{
credsSlice := *(*[]*sysCREDENTIAL)(unsafe.Pointer(&reflect.SliceHeader{
Data: pcreds,
Len: count,
Cap: count,
}))
creds := make([]*Credential, count, count)
for i, cred := range credsSlice {
creds[i] = nativeToCredential(cred)
creds[i] = sysToCredential(cred)
}

return creds, nil
Expand Down
Loading

0 comments on commit ab1dbd1

Please sign in to comment.