summaryrefslogtreecommitdiff
path: root/tests/version.c
blob: 15c4919c88f47a23f976e3b7a26ec843040d1338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* version.c --- Version handling self tests.
 * Copyright (C) 2019-2022 Free Software Foundation, Inc.
 *
 * This file is part of LIBTASN1.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>		/* printf */
#include <stdlib.h>		/* EXIT_SUCCESS */
#include <string.h>		/* strcmp */

/* Get ASN1 prototypes. */
#include <libtasn1.h>

int
main (int argc, char *argv[])
{
  int exit_code = EXIT_SUCCESS;
  char out[100];
  int j;
  unsigned gvn = ASN1_VERSION_NUMBER;
  unsigned gvmmp = (ASN1_VERSION_MAJOR << 16)
    + (ASN1_VERSION_MINOR << 8) + ASN1_VERSION_PATCH;

  printf ("ASN1_VERSION: %s\n", ASN1_VERSION);
  printf ("ASN1_VERSION_MAJOR: %d\n", ASN1_VERSION_MAJOR);
  printf ("ASN1_VERSION_MINOR: %d\n", ASN1_VERSION_MINOR);
  printf ("ASN1_VERSION_PATCH: %d\n", ASN1_VERSION_PATCH);
  printf ("ASN1_VERSION_NUMBER: %x\n", gvn);
  printf ("(ASN1_VERSION_MAJOR << 16) + (ASN1_VERSION_MINOR << 8)"
	  " + ASN1_VERSION_PATCH: %x\n", gvmmp);

  j = snprintf (out, sizeof (out) - 1, "%d.%d.%d", ASN1_VERSION_MAJOR,
		ASN1_VERSION_MINOR, ASN1_VERSION_PATCH);
  if (j < 0)
    {
      printf ("snprintf failure: %d", j);
      exit_code = EXIT_FAILURE;
      out[0] = '\0';
    }

  if (out[0])
    printf ("ASN1_VERSION_MAJOR.ASN1_VERSION_MINOR.ASN1_VERSION_PATCH: %s\n",
	    out);

  printf ("asn1_check_version (NULL): %s\n", asn1_check_version (NULL));

  if (!asn1_check_version (ASN1_VERSION))
    {
      printf ("FAIL: asn1_check_version (ASN1_VERSION)\n");
      exit_code = EXIT_FAILURE;
    }

  if (!asn1_check_version ("4.17.0"))
    {
      printf ("asn1_check_version(4.17.0) failure\n");
      exit_code = EXIT_FAILURE;
    }

  if (!asn1_check_version ("4.17"))
    {
      printf ("asn1_check_version(4.17) failure\n");
      exit_code = EXIT_FAILURE;
    }

  if (strcmp (ASN1_VERSION, asn1_check_version (NULL)) != 0)
    {
      printf ("FAIL: strcmp (ASN1_VERSION, asn1_check_version (NULL))\n");
      exit_code = EXIT_FAILURE;
    }

  if (ASN1_VERSION_NUMBER != gvn)
    {
      printf ("FAIL: ASN1_VERSION_NUMBER != gvn\n");
      exit_code = EXIT_FAILURE;
    }

  if (out[0])
    {
      if (!asn1_check_version (out))
	{
	  printf ("FAIL: asn1_check_version(%s)\n", out);
	  exit_code = EXIT_FAILURE;
	}

      /* ASN1_VERSION may look like "1.0.4.10-b872" but the derived string
         should be "1.0.4" anyway.  */
      if (strncmp (ASN1_VERSION, out, strlen (out)) != 0)
	{
	  printf ("FAIL: strncmp (ASN1_VERSION, %s, strlen (%s))\n", out,
		  out);
	  exit_code = EXIT_FAILURE;
	}
    }

  if (asn1_check_version ("4711.42.23"))
    {
      printf ("FAIL: asn1_check_version(4711.42.23)\n");
      exit_code = EXIT_FAILURE;
    }

  if (asn1_check_version ("UNKNOWN"))
    {
      printf ("FAIL: asn1_check_version (UNKNOWN)\n");
      exit_code = EXIT_FAILURE;
    }

  return exit_code;
}