Path: blob/master/test/jdk/sun/security/krb5/config/native/libTestDynamicStore.m
41161 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import <Cocoa/Cocoa.h>26#import <SystemConfiguration/SystemConfiguration.h>27#import <jni.h>2829#define KERBEROS_DEFAULT_REALMS @"Kerberos-Default-Realms"30#define KERBEROS_DEFAULT_REALM_MAPPINGS @"Kerberos-Domain-Realm-Mappings"31#define KERBEROS_REALM_INFO @"Kerberos:%@"3233int removeAll(SCDynamicStoreRef store) {34fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) KERBEROS_DEFAULT_REALMS));35fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"A.COM"]));36fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"B.COM"]));37fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) KERBEROS_DEFAULT_REALM_MAPPINGS));38return 1;39}4041int removeRealm(SCDynamicStoreRef store) {42fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"A.COM"]));43return 1;44}4546int removeMapping(SCDynamicStoreRef store) {47fprintf(stderr, "%d\n", SCDynamicStoreRemoveValue(store, (CFStringRef) KERBEROS_DEFAULT_REALM_MAPPINGS));48return 1;49}5051int addMapping(SCDynamicStoreRef store) {52NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:53@"a", @"A",54@"b", @"B",55@"c", @"C",56@"d", @"D",57nil];58fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) KERBEROS_DEFAULT_REALM_MAPPINGS, [NSArray arrayWithObjects: dict, nil]));59return 1;60}6162int addAll(SCDynamicStoreRef store) {63NSArray *keys = [NSArray arrayWithObjects:@"A.COM", @"B.COM", nil];64fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) KERBEROS_DEFAULT_REALMS, keys));6566NSDictionary *k1 = [NSDictionary dictionaryWithObjectsAndKeys:67@"kdc1.a.com", @"host", nil];68NSDictionary *k2 = [NSDictionary dictionaryWithObjectsAndKeys:69@"kdc2.a.com", @"host", nil];70NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:71[NSArray arrayWithObjects: k1, k2, nil], @"kdc",72nil];73fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"A.COM"], dict));7475k1 = [NSDictionary dictionaryWithObjectsAndKeys:76@"kdc1.b.com", @"host", nil];77k2 = [NSDictionary dictionaryWithObjectsAndKeys:78@"kdc2.b.com", @"host", nil];79dict = [NSDictionary dictionaryWithObjectsAndKeys:80[NSArray arrayWithObjects: k1, k2, nil], @"kdc",81nil];82fprintf(stderr, "%d\n", SCDynamicStoreSetValue(store, (CFStringRef) [NSString stringWithFormat:KERBEROS_REALM_INFO, @"B.COM"], dict));83addMapping(store);84return 1;85}8687JNIEXPORT jint JNICALL Java_TestDynamicStore_actionInternal(JNIEnv *env, jclass clazz, jchar what, jchar whom) {88SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR("java-kerberos"), NULL, NULL);89fprintf(stderr, ">>> action: %c %c\n", what, whom);90@try {91switch (what) {92case 'a':93switch (whom) {94case 'a': return addAll(store);95case 'm': return addMapping(store);96}97break;98case 'r':99switch (whom) {100case 'a': return removeAll(store);101case 'r': return removeRealm(store);102case 'm': return removeMapping(store);103}104break;105}106return 0;107} @finally {108CFRelease(store);109}110}111112113