Michael Fenwick
Michael Fenwick

Reputation: 2494

Create a file relative path for secure server certificate

I'm working on a Dart HttpServer using SSL, which looks something like this:

class Server {
  //The path for the database is relative to the code's entry point (main.dart)
  static const String CERTIFICATE_DB_PATH = '../lib/server/';
  static const String CERTIFICATE_DB_PASS = '*******';
  static const String CERTIFICATE_NAME = 'CN=mycert';

  Future start() async {
    SecureSocket.initialize(database: CERTIFICATE_DB_PATH, password: CERTIFICATE_DB_PASS);
    httpServer = await HttpServer.bindSecure(ADDRESS, PORT, certificateName: CERTIFICATE_NAME);
    listenSubscription = httpServer.listen(onRequest, onError: onError);
  }

  //more server code here
}

This all works exactly as expected, so no problems with the actual certificate or server code. The part that I'm having problems with is mentioned in that first comment. The CERTIFICATE_DB_PATH seems to be relative not to the file the Server class is defined in, but rather to the file that contains the main() method. This means that when I try to write a unit test for this class, the path is no longer pointing to the correct directory. If this were an import, I'd use the package:packageName/path/to/cert syntax, but it doesn't seem that applies here. How can I specify the path of the certificate in a way that will work with multiple entry points (actually running the server vs unit tests)?

Upvotes: 1

Views: 453

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657058

I don't think there is a way to define the path so it is relative to the source file.
What you can do is to change the current working directory either before you run main() or pass a working directory path as argument to main() and let main() make this directory the current working directory.

Directory.current = someDirectory;

Upvotes: 1

Related Questions