Skip to content

Commit

Permalink
Add local SMB2 FileSystem tests (frankframework#5169)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsm5 authored Aug 3, 2023
1 parent bf72d99 commit 936a044
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 12 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/codeql-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ jobs:
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# Use Maven Wrapper so we can build with an older version of Maven, see PR #4579.
- name: Build with Maven
env:
jdk11: true
CI_SERVICE: GITHUB
TZ: Europe/Amsterdam
JAVA_OPTS: "-Xms1G -XX:+UseParallelGC"
jdk11: true # allows the use of the JDK11 profile, when running JDK11
run: ./mvnw -B -V -T1 package -Pibissource

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand Down
24 changes: 24 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@
<version>1.3.15</version>
<scope>test</scope>
</dependency>
<!-- available through our nexus -->
<dependency>
<groupId>org.filesys</groupId>
<artifactId>jfileserver-enterprise</artifactId>
<version>1.2.15</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
Expand Down Expand Up @@ -929,6 +936,23 @@
</build>

<profiles>
<profile>
<id>proprietary</id>
<activation>
<property>
<name>proprietary</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.filesys</groupId>
<artifactId>jfileserver-enterprise</artifactId>
<version>1.2.15</version>
<classifier>license</classifier>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>ibm-jdk</id>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ public void close() throws FileSystemException {
if(session != null) {
session.close();
}
if(connection != null) {
connection.close();
}
if(client != null) {
client.close();
}
Expand Down Expand Up @@ -345,7 +342,14 @@ public Map<String, Object> getAdditionalFileProperties(SmbFileRef f) {

@Override
public boolean folderExists(String folder) throws FileSystemException {
return diskShare.folderExists(folder);
try {
return diskShare.folderExists(folder);
} catch (SMBApiException e) {
if(NtStatus.STATUS_OBJECT_NAME_COLLISION.equals(NtStatus.valueOf(e.getStatusCode()))) {
return false;
}
throw e;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
package nl.nn.adapterframework.filesystem.smb;

import org.junit.jupiter.api.Disabled;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.InetAddress;
import java.net.URL;
import java.nio.file.Path;
import java.util.EnumSet;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.filesys.debug.ConsoleDebug;
import org.filesys.debug.DebugConfigSection;
import org.filesys.server.auth.EnterpriseSMBAuthenticator;
import org.filesys.server.auth.ISMBAuthenticator.AuthMode;
import org.filesys.server.auth.UserAccount;
import org.filesys.server.auth.UserAccountList;
import org.filesys.server.config.CoreServerConfigSection;
import org.filesys.server.config.GlobalConfigSection;
import org.filesys.server.config.LicenceConfigSection;
import org.filesys.server.config.SecurityConfigSection;
import org.filesys.server.config.ServerConfiguration;
import org.filesys.server.filesys.DiskDeviceContext;
import org.filesys.server.filesys.DiskSharedDevice;
import org.filesys.server.filesys.FilesystemsConfigSection;
import org.filesys.smb.Dialect;
import org.filesys.smb.DialectSelector;
import org.filesys.smb.server.SMBConfigSection;
import org.filesys.smb.server.SMBServer;
import org.filesys.smb.server.SMBSrvSession.Dbg;
import org.filesys.smb.server.disk.original.JavaFileDiskDriver;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.extensions.config.ConfigElement;

import nl.nn.adapterframework.filesystem.FileSystemTest;
import nl.nn.adapterframework.filesystem.IFileSystemTestHelper;
import nl.nn.adapterframework.filesystem.LocalFileSystemTestHelper;
import nl.nn.adapterframework.filesystem.Samba2FileSystem;
import nl.nn.adapterframework.filesystem.Samba2FileSystem.Samba2AuthType;
import nl.nn.adapterframework.util.ClassUtils;
import nl.nn.adapterframework.util.StreamUtil;

@Disabled
public class Samba2FileSystemTest extends FileSystemTest<SmbFileRef, Samba2FileSystem> {

private String username = "frankframework";
Expand All @@ -20,23 +59,148 @@ public class Samba2FileSystemTest extends FileSystemTest<SmbFileRef, Samba2FileS
private String realm = "DUMMYDOMAIN.NL";
private String domain = "dummyDomain.nl";

// Default memory pool settings
private static final int[] DefaultMemoryPoolBufSizes = { 256, 4096, 16384, 65536 };
private static final int[] DefaultMemoryPoolInitAlloc = { 20, 20, 5, 5 };
private static final int[] DefaultMemoryPoolMaxAlloc = { 100, 50, 50, 50 };

// Default thread pool size
private static final int DefaultThreadPoolInit = 25;
private static final int DefaultThreadPoolMax = 50;

private static SMBServer smbServer;
private static final boolean DEBUG = false;

@TempDir
public static Path testDirectory;

@Override
protected IFileSystemTestHelper getFileSystemTestHelper() {
if("localhost".equals(host)) {
return new LocalFileSystemTestHelper(testDirectory);
}
return new Samba2FileSystemTestHelper(host, port, shareName, username, password, domain);
}

@Override
@BeforeEach
public void setUp() throws Exception {
if("localhost".equals(host) && smbServer == null) {
String license = getLicense();
assumeTrue(license != null); //Only run test when a license is present!
setWaitMillis(1000);

ServerConfiguration serverConfig = new ServerConfiguration("dummySmbServer");
FilesystemsConfigSection fsConfig = new FilesystemsConfigSection(serverConfig);

JavaFileDiskDriver diskDriver = new JavaFileDiskDriver();
DiskDeviceContext ctx = new DiskDeviceContext(testDirectory.toAbsolutePath().toString(), shareName);
DiskSharedDevice dsd = new DiskSharedDevice(shareName, diskDriver, ctx);
fsConfig.addShare(dsd);

SecurityConfigSection secConfig = new SecurityConfigSection(serverConfig);
UserAccountList ual = new UserAccountList();
ual.addUser(new UserAccount(username, password));
secConfig.setUsersInterface("org.filesys.server.auth.DefaultUsersInterface", null);
secConfig.setUserAccounts(ual);
secConfig.setJCEProvider(BouncyCastleProvider.class.getCanonicalName());

DebugConfigSection ds = new DebugConfigSection(serverConfig);
ds.setDebug(ConsoleDebug.class.getCanonicalName(), mock(ConfigElement.class));

GlobalConfigSection gcs = new GlobalConfigSection(serverConfig);
gcs.setTimeZoneOffset(0);
CoreServerConfigSection coreConfig = new CoreServerConfigSection(serverConfig);
coreConfig.setMemoryPool(DefaultMemoryPoolBufSizes, DefaultMemoryPoolInitAlloc, DefaultMemoryPoolMaxAlloc);
coreConfig.setThreadPool( DefaultThreadPoolInit, DefaultThreadPoolMax);

SMBConfigSection cifsConfig = new SMBConfigSection(serverConfig);
cifsConfig.setHostAnnouncer(false);
cifsConfig.setWin32HostAnnouncer(false);
cifsConfig.setNetBIOSSMB(false);
cifsConfig.setWin32NetBIOS(false);

DialectSelector dialects = new DialectSelector();
dialects.AddDialect(Dialect.SMB2_202);
dialects.AddDialect(Dialect.SMB2_210);
dialects.AddDialect(Dialect.SMB3_311);
dialects.AddDialect(Dialect.SMB3_302);
dialects.AddDialect(Dialect.SMB3_311);
cifsConfig.setEnabledDialects(dialects);

ConfigElement configParams = mock(ConfigElement.class);
when(configParams.getChild("useSPNEGO")).thenReturn(mock(ConfigElement.class)); //Assertions are done on the presence of the element, not the contents

EnterpriseSMBAuthenticator auth = new EnterpriseSMBAuthenticator();
auth.setAccessMode(AuthMode.SHARE);
if(DEBUG) auth.setDebug(true);
cifsConfig.setAuthenticator(auth);

cifsConfig.setTcpipSMB(true);
cifsConfig.setSMBBindAddress(InetAddress.getByName("localhost"));
cifsConfig.setTcpipSMBPort(port);
cifsConfig.setServerName("localhost");
cifsConfig.setDomainName(domain);
cifsConfig.setSocketTimeout(0);
cifsConfig.setSessionDebugFlags(DEBUG ? EnumSet.allOf(Dbg.class) : EnumSet.noneOf(Dbg.class));
auth.initialize(serverConfig, configParams);

LicenceConfigSection licenseSection = new LicenceConfigSection(serverConfig);
licenseSection.setLicenceKey(license);

Class<?> clazz = ClassUtils.loadClass("org.filesys.smb.server.EnterpriseSMBServer");
Constructor<?> con = clazz.getConstructor(serverConfig.getClass());
smbServer = (SMBServer) con.newInstance(serverConfig);
smbServer.startServer();
}

super.setUp();
}

private String getLicense() throws IOException {
URL license = Samba2FileSystemTest.class.getResource("/jfileserver.lic");
if(license != null && ClassUtils.isClassPresent("org.filesys.smb.server.EnterpriseSMBServer")) {
return StreamUtil.streamToString(license.openStream());
}
return null;
}

@AfterAll
public static void removeSmbServer() throws Exception {
if(smbServer != null) {
smbServer.shutdownServer(true);
smbServer = null;
}
}

@Override
public Samba2FileSystem createFileSystem() {
Samba2FileSystem result = new Samba2FileSystem();
result.setShare(shareName);
result.setUsername(username);
result.setPassword(password);
result.setAuthType(Samba2AuthType.NTLM);
if("localhost".equals(host)) { // test stub only works with NTLM
result.setAuthType(Samba2AuthType.NTLM);
}
result.setHostname(host);
result.setPort(port);
result.setKdc(kdc);
result.setRealm(realm);
result.setDomainName(domain);
return result;
}

@Test
@Override
public void basicFileSystemTestCopyFile() throws Exception {
assumeFalse("localhost".equals(host)); //Returns 'STATUS_NOT_SUPPORTED (0xc00000bb): IOCTL failed' in combination with JFileServer
super.basicFileSystemTestCopyFile();
}

@Test
@Override
public void writableFileSystemTestCopyFileToNonExistentDirectoryCreateFolderTrue() throws Exception {
assumeFalse("localhost".equals(host)); //Returns 'STATUS_NOT_SUPPORTED (0xc00000bb): IOCTL failed' in combination with JFileServer
super.writableFileSystemTestCopyFileToNonExistentDirectoryCreateFolderTrue();
}
}

0 comments on commit 936a044

Please sign in to comment.