Skip to content

Commit 0a64de3

Browse files
jonasfjsigurdm
andauthored
Fix logic for finding libwebcrypto.so in tests (#35)
* Fix logic for finding libwebcrypto.so in tests * Update lib/src/boringssl/lookup/utils.dart Co-authored-by: Sigurd Meldgaard <[email protected]> * Better explanation Co-authored-by: Sigurd Meldgaard <[email protected]>
1 parent 20c7c4d commit 0a64de3

File tree

4 files changed

+56
-100
lines changed

4 files changed

+56
-100
lines changed

lib/src/boringssl/lookup/lookup.dart

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,53 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// ignore_for_file: non_constant_identifier_names
16+
1517
import 'dart:ffi';
1618

1719
import '../../third_party/boringssl/generated_bindings.dart';
1820
import '../bindings/generated_bindings.dart';
1921

20-
import 'lookup_symbol_dart.dart'
21-
if (dart.library.ui) 'lookup_symbol_flutter.dart';
22+
import 'dart:io' show Platform;
23+
24+
import 'symbols.generated.dart';
25+
import 'utils.dart';
2226

2327
export 'symbols.generated.dart' show Sym;
2428

29+
/// Dynamically load `webcrypto_lookup_symbol` function.
30+
final Pointer<T> Function<T extends NativeType>(String symbolName) lookup = () {
31+
try {
32+
final library = Platform.isAndroid || Platform.isLinux
33+
? DynamicLibrary.open('libwebcrypto.so')
34+
: DynamicLibrary.executable();
35+
36+
// Try to lookup the 'webcrypto_lookup_symbol' symbol.
37+
final webcryptoDartDL = WebCryptoDartDL(library);
38+
final webcrypto_lookup_symbol = webcryptoDartDL.webcrypto_lookup_symbol;
39+
40+
// Return a function from Sym to lookup using `webcrypto_lookup_symbol`
41+
Pointer<T> lookup<T extends NativeType>(String s) =>
42+
webcrypto_lookup_symbol(symFromString(s).index).cast<T>();
43+
44+
// Initialize the dynamic linking with Dart.
45+
initialize_dart_dl(lookup);
46+
47+
return lookup;
48+
} on ArgumentError {
49+
final lookup = lookupLibraryInDotDartTool();
50+
if (lookup != null) {
51+
return lookup;
52+
}
53+
54+
throw UnsupportedError(
55+
'package:webcrypto cannot be used from scripts or `flutter test` '
56+
'unless `flutter pub run webcrypto:setup` has been run for the current '
57+
'root project.',
58+
);
59+
}
60+
}();
61+
2562
final Pointer<T> Function<T extends NativeType>(String symbolName)
2663
_cachedLookup = lookup;
2764

lib/src/boringssl/lookup/lookup_symbol_dart.dart

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/src/boringssl/lookup/lookup_symbol_flutter.dart

Lines changed: 0 additions & 53 deletions
This file was deleted.

lib/src/boringssl/lookup/utils.dart

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Pointer<T> Function<T extends NativeType>(String symbolName)?
6060
final webcrypto_lookup_symbol = webcryptoDartDL.webcrypto_lookup_symbol;
6161

6262
// Return a function from Sym to lookup using `webcrypto_lookup_symbol`
63-
final lookup = <T extends NativeType>(String s) =>
63+
Pointer<T> lookup<T extends NativeType>(String s) =>
6464
webcrypto_lookup_symbol(symFromString(s).index).cast<T>();
6565

6666
// Initialize library
@@ -92,23 +92,12 @@ void initialize_dart_dl(
9292

9393
/// Find the `.dart_tool/` folder, returns `null` if unable to find it.
9494
Uri? _findDotDartTool() {
95-
// HACK: Because 'dart:isolate' is unavailable in Flutter we have no means
96-
// by which we can find the location of the package_config.json file.
97-
// Which we need, because the binary library created by:
95+
// HACK: We have no good mechanism for finding the library created by:
9896
// flutter pub run webcrypto:setup
99-
// is located relative to this path. As a workaround we use
100-
// `Platform.script` and traverse level-up until we find a
101-
// `.dart_tool/package_config.json` file.
97+
// So we search relative to the script path and CWD.
10298

10399
// Find script directory
104-
Uri root;
105-
if (Platform.script.isScheme('data')) {
106-
// If `Platform.script` is a data: [Uri] then we are being called from
107-
// `package:test`, luckily this means that CWD is project root.
108-
root = Directory.current.uri;
109-
} else {
110-
root = Platform.script.resolve('./');
111-
}
100+
Uri root = Platform.script.resolve('./');
112101

113102
// Traverse up until we see a `.dart_tool/package_config.json` file.
114103
do {
@@ -117,5 +106,18 @@ Uri? _findDotDartTool() {
117106
return root.resolve('.dart_tool/');
118107
}
119108
} while (root != (root = root.resolve('..')));
109+
110+
// If traversing from script directory didn't work, we can look starting from
111+
// CWD, this typically happens if running as test.
112+
root = Directory.current.uri;
113+
114+
// Traverse up until we see a `.dart_tool/package_config.json` file.
115+
do {
116+
if (File.fromUri(root.resolve('.dart_tool/package_config.json'))
117+
.existsSync()) {
118+
return root.resolve('.dart_tool/');
119+
}
120+
} while (root != (root = root.resolve('..')));
121+
120122
return null;
121123
}

0 commit comments

Comments
 (0)