Revert "Update for cgo changes in go 1.10"

This commit is contained in:
Maxwell Krohn 2018-02-26 11:16:11 -05:00 committed by GitHub
parent ad81a34fcc
commit b881829908
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 51 deletions

View file

@ -1,4 +1,4 @@
// +build darwin ios
// +build darwin
package keychain
@ -26,15 +26,15 @@ func Release(ref C.CFTypeRef) {
// Release(ref).
func BytesToCFData(b []byte) (C.CFDataRef, error) {
if uint64(len(b)) > math.MaxUint32 {
return 0, errors.New("Data is too large")
return nil, errors.New("Data is too large")
}
var p *C.UInt8
if len(b) > 0 {
p = (*C.UInt8)(&b[0])
}
cfData := C.CFDataCreate(nil, p, C.CFIndex(len(b)))
if cfData == 0 {
return 0, fmt.Errorf("CFDataCreate failed")
if cfData == nil {
return nil, fmt.Errorf("CFDataCreate failed")
}
return cfData, nil
}
@ -59,24 +59,22 @@ func MapToCFDictionary(m map[C.CFTypeRef]C.CFTypeRef) (C.CFDictionaryRef, error)
valuesPointer = &values[0]
}
cfDict := C.CFDictionaryCreate(nil, keysPointer, valuesPointer, C.CFIndex(numValues), &C.kCFTypeDictionaryKeyCallBacks, &C.kCFTypeDictionaryValueCallBacks)
if cfDict == 0 {
return 0, fmt.Errorf("CFDictionaryCreate failed")
if cfDict == nil {
return nil, fmt.Errorf("CFDictionaryCreate failed")
}
return cfDict, nil
}
// CFDictionaryToMap converts CFDictionaryRef to a map.
func CFDictionaryToMap(cfDict C.CFDictionaryRef) (m map[C.CFTypeRef]uintptr) {
func CFDictionaryToMap(cfDict C.CFDictionaryRef) (m map[C.CFTypeRef]C.CFTypeRef) {
count := C.CFDictionaryGetCount(cfDict)
if count > 0 {
keys := make([]C.CFTypeRef, count)
values := make([]C.CFTypeRef, count)
C.CFDictionaryGetKeysAndValues(cfDict, (*unsafe.Pointer)(unsafe.Pointer(&keys[0])), (*unsafe.Pointer)(unsafe.Pointer(&values[0])))
m = make(map[C.CFTypeRef]uintptr, count)
C.CFDictionaryGetKeysAndValues(cfDict, (*unsafe.Pointer)(&keys[0]), (*unsafe.Pointer)(&values[0]))
m = make(map[C.CFTypeRef]C.CFTypeRef, count)
for i := C.CFIndex(0); i < count; i++ {
k := keys[i]
v := values[i]
m[k] = uintptr(v)
m[keys[i]] = values[i]
}
}
return
@ -86,10 +84,10 @@ func CFDictionaryToMap(cfDict C.CFDictionaryRef) (m map[C.CFTypeRef]uintptr) {
// Release(ref).
func StringToCFString(s string) (C.CFStringRef, error) {
if !utf8.ValidString(s) {
return 0, errors.New("Invalid UTF-8 string")
return nil, errors.New("Invalid UTF-8 string")
}
if uint64(len(s)) > math.MaxUint32 {
return 0, errors.New("String is too large")
return nil, errors.New("String is too large")
}
bytes := []byte(s)
@ -140,7 +138,7 @@ func CFArrayToArray(cfArray C.CFArrayRef) (a []C.CFTypeRef) {
count := C.CFArrayGetCount(cfArray)
if count > 0 {
a = make([]C.CFTypeRef, count)
C.CFArrayGetValues(cfArray, C.CFRange{0, count}, (*unsafe.Pointer)(unsafe.Pointer(&a[0])))
C.CFArrayGetValues(cfArray, C.CFRange{0, count}, (*unsafe.Pointer)(&a[0]))
}
return
}
@ -158,7 +156,7 @@ func ConvertMapToCFDictionary(attr map[string]interface{}) (C.CFDictionaryRef, e
var valueRef C.CFTypeRef
switch val := i.(type) {
default:
return 0, fmt.Errorf("Unsupported value type: %v", reflect.TypeOf(i))
return nil, fmt.Errorf("Unsupported value type: %v", reflect.TypeOf(i))
case C.CFTypeRef:
valueRef = val
case bool:
@ -170,28 +168,28 @@ func ConvertMapToCFDictionary(attr map[string]interface{}) (C.CFDictionaryRef, e
case []byte:
bytesRef, err := BytesToCFData(val)
if err != nil {
return 0, err
return nil, err
}
valueRef = C.CFTypeRef(bytesRef)
defer Release(valueRef)
case string:
stringRef, err := StringToCFString(val)
if err != nil {
return 0, err
return nil, err
}
valueRef = C.CFTypeRef(stringRef)
defer Release(valueRef)
case Convertable:
convertedRef, err := val.Convert()
if err != nil {
return 0, err
return nil, err
}
valueRef = C.CFTypeRef(convertedRef)
defer Release(valueRef)
}
keyRef, err := StringToCFString(key)
if err != nil {
return 0, err
return nil, err
}
m[C.CFTypeRef(keyRef)] = valueRef
defer Release(C.CFTypeRef(keyRef))
@ -199,7 +197,7 @@ func ConvertMapToCFDictionary(attr map[string]interface{}) (C.CFDictionaryRef, e
cfDict, err := MapToCFDictionary(m)
if err != nil {
return 0, err
return nil, err
}
return cfDict, nil
}
@ -258,7 +256,7 @@ func ConvertCFDictionary(d C.CFDictionaryRef) (map[interface{}]interface{}, erro
if err != nil {
return nil, err
}
gv, err := Convert(C.CFTypeRef(v))
gv, err := Convert(v)
if err != nil {
return nil, err
}

Binary file not shown.

View file

@ -17,28 +17,14 @@
- (void)fail:(NSString*)s;
@end
/**
* AddGenericPassword adds generic password
*/
FOUNDATION_EXPORT BOOL BindAddGenericPassword(NSString* service, NSString* account, NSString* label, NSString* password, NSString* accessGroup, NSError** error);
/**
* DeleteGenericPassword deletes generic password
*/
FOUNDATION_EXPORT BOOL BindDeleteGenericPassword(NSString* service, NSString* account, NSString* accessGroup, NSError** error);
/**
* GenericPasswordTest runs test code for generic password keychain item.
This is here so we can export using gomobile bind and run this method on iOS simulator and device.
Access groups aren't supported in iOS simulator.
*/
FOUNDATION_EXPORT void BindGenericPasswordTest(id<BindTest> t, NSString* service, NSString* accessGroup);
@class BindTest;
/**
* Test is a bind interface for the test
*/
@interface BindTest : NSObject <goSeqRefInterface, BindTest> {
}
@property(strong, readonly) id _ref;

View file

@ -335,18 +335,18 @@ type QueryResult struct {
func QueryItemRef(item Item) (C.CFTypeRef, error) {
cfDict, err := ConvertMapToCFDictionary(item.attr)
if err != nil {
return 0, err
return nil, err
}
defer Release(C.CFTypeRef(cfDict))
var resultsRef C.CFTypeRef
errCode := C.SecItemCopyMatching(cfDict, &resultsRef)
if Error(errCode) == ErrorItemNotFound {
return 0, nil
return nil, nil
}
err = checkError(errCode)
if err != nil {
return 0, err
return nil, err
}
return resultsRef, nil
}
@ -357,7 +357,7 @@ func QueryItem(item Item) ([]QueryResult, error) {
if err != nil {
return nil, err
}
if resultsRef == 0 {
if resultsRef == nil {
return nil, nil
}
defer Release(resultsRef)

View file

@ -39,7 +39,7 @@ func createAccess(label string, trustedApplications []string) (C.CFTypeRef, erro
var err error
var labelRef C.CFStringRef
if labelRef, err = StringToCFString(label); err != nil {
return 0, err
return nil, err
}
defer C.CFRelease(C.CFTypeRef(labelRef))
@ -55,7 +55,7 @@ func createAccess(label string, trustedApplications []string) (C.CFTypeRef, erro
for _, trustedApplication := range trustedApplications {
trustedApplicationRef, createErr := createTrustedApplication(trustedApplication)
if createErr != nil {
return 0, createErr
return nil, createErr
}
defer C.CFRelease(C.CFTypeRef(trustedApplicationRef))
trustedApplicationsRefs = append(trustedApplicationsRefs, trustedApplicationRef)
@ -69,7 +69,7 @@ func createAccess(label string, trustedApplications []string) (C.CFTypeRef, erro
errCode := C.SecAccessCreate(labelRef, trustedApplicationsArray, &access)
err = checkError(errCode)
if err != nil {
return 0, err
return nil, err
}
return C.CFTypeRef(access), nil
@ -88,7 +88,7 @@ func createTrustedApplication(trustedApplication string) (C.CFTypeRef, error) {
errCode := C.SecTrustedApplicationCreateFromPath(trustedApplicationCStr, &trustedApplicationRef)
err := checkError(errCode)
if err != nil {
return 0, err
return nil, err
}
return C.CFTypeRef(trustedApplicationRef), nil
@ -151,11 +151,11 @@ func newKeychain(path, password string, promptUser bool) (Keychain, error) {
var kref C.SecKeychainRef
if promptUser {
errCode = C.SecKeychainCreate(pathRef, C.UInt32(0), nil, C.Boolean(1), 0, &kref)
errCode = C.SecKeychainCreate(pathRef, C.UInt32(0), nil, C.Boolean(1), nil, &kref)
} else {
passwordRef := C.CString(password)
defer C.free(unsafe.Pointer(passwordRef))
errCode = C.SecKeychainCreate(pathRef, C.UInt32(len(password)), unsafe.Pointer(passwordRef), C.Boolean(0), 0, &kref)
errCode = C.SecKeychainCreate(pathRef, C.UInt32(len(password)), unsafe.Pointer(passwordRef), C.Boolean(0), nil, &kref)
}
if err := checkError(errCode); err != nil {
@ -197,7 +197,7 @@ func openKeychainRef(path string) (C.SecKeychainRef, error) {
var kref C.SecKeychainRef
if err := checkError(C.SecKeychainOpen(pathName, &kref)); err != nil {
return 0, err
return nil, err
}
return kref, nil
@ -252,11 +252,11 @@ func (ka keychainArray) Convert() (C.CFTypeRef, error) {
if refs[idx], err = kc.Convert(); err != nil {
// If we error trying to convert lets release any we converted before
for _, ref := range refs {
if ref != 0 {
if ref != nil {
Release(ref)
}
}
return 0, err
return nil, err
}
}

View file

@ -121,7 +121,7 @@ func TestGenericPasswordRef(t *testing.T) {
ref, err := QueryItemRef(query)
if err != nil {
t.Fatal(err)
} else if ref == 0 {
} else if ref == nil {
t.Fatal("Missing result")
} else {
err = DeleteItemRef(ref)
@ -256,7 +256,7 @@ func TestStatus(t *testing.T) {
t.Fatal(err)
}
nonexistent := NewWithPath(fmt.Sprintf("this_shouldnt_exist_%s", time.Now().String()))
nonexistent := NewWithPath(fmt.Sprintf("this_shouldnt_exist_%d", time.Now()))
if err := nonexistent.Status(); err != ErrorNoSuchKeychain {
t.Fatalf("Expected %v, get %v", ErrorNoSuchKeychain, err)
}