1#! /usr/bin/env perl
2# Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (the "License").  You may not use
5# this file except in compliance with the License.  You can obtain a copy
6# in the file LICENSE in the source distribution or at
7# https://www.openssl.org/source/license.html
8
9
10use OpenSSL::Test;
11use OpenSSL::Test::Utils;
12use OpenSSL::Test qw/:DEFAULT bldtop_file data_file srctop_file cmdstr/;
13
14setup("test_windows_registry");
15
16plan skip_all => "Windows registry tests are only available on windows"
17    if $^O !~ /(MSWin)/;
18
19my $actual;
20my $expect;
21
22my @tempout = run(app(["openssl", "version", "-w"]), capture => 1);
23my $context = "@tempout";
24$context =~ s/^.*: //;
25
26
27@tempout = run(app(["openssl", "version", "-v"]), capture => 1);
28my $version = "@tempout";
29$version =~ s/^OpenSSL //;
30$version =~ s/(^[0-9]+\.[0-9]+)(.*$)/\1/;
31
32my $regkey = "HKLM\\SOFTWARE\\OpenSSL-".$version."-".$context;
33$regkey =~ s/\n//g;
34print "REGKEY IS $regkey\n";
35
36my $exit = run(cmd(["reg.exe", "query", $regkey, "/v", "OPENSSLDIR", "/reg:32"]));
37
38plan skip_all => "Skipping test as registry keys aren't set"
39    if $exit == 0;
40
41plan tests => 3;
42
43my @expectossldir = run(cmd(["reg.exe", "query", $regkey, "/reg:32", "/t", "REG_EXPAND_SZ", "/v", "OPENSSLDIR"]), capture => 1);
44
45my @expectengdir = run(cmd(["reg.exe", "query", $regkey, "/reg:32", "/t", "REG_EXPAND_SZ", "/v", "ENGINESDIR"]), capture => 1);
46
47my @expectmoddir = run(cmd(["reg.exe", "query", $regkey, "/reg:32", "/t", "REG_EXPAND_SZ", "/v", "MODULESDIR"]), capture => 1);
48
49my @ossldir = run(app(["openssl", "version", "-d"]), capture => 1);
50
51print "@ossldir";
52$expect = "@expectossldir";
53$actual = "@ossldir";
54$expect =~ s/HKEY_LOCAL_MACHINE.*\n*//;
55$expect =~ s/\n//g;
56$expect =~ s/.*REG_EXPAND_SZ *//;
57$expect =~ s/ .*$//;
58$actual =~ s/OPENSSLDIR: *//;
59
60ok(grep(/$expect/,$actual), "Confirming version output for openssldir from registry");
61
62my @osslengineout = run(app(["openssl", "version", "-e"]), capture => 1);
63
64$expect = "@expectengdir";
65$actual = "@osslengineout";
66$expect =~ s/HKEY_LOCAL_MACHINE.*\n*//;
67$expect =~ s/\n//g;
68$expect =~ s/.*REG_EXPAND_SZ *//;
69$expect =~ s/ .*$//;
70$actual =~ s/ENGINESDIR: *//;
71
72ok(grep(/$expect/, $actual) == 1, "Confirming version output for enginesdir from registry");
73
74my @osslmoduleout = run(app(["openssl", "version", "-m"]), capture => 1);
75
76$expect = "@expectmoddir";
77$actual = "@osslmoduleout";
78$expect =~ s/HKEY_LOCAL_MACHINE.*\n*//;
79$expect =~ s/\n//g;
80$expect =~ s/.*REG_EXPAND_SZ *//;
81$expect =~ s/ .*$//;
82$actual =~ s/MODULESSDIR: *//;
83ok(grep(/$expect/, $actual) == 1, "Confirming version output for modulesdir from registry");
84