File: test-pem.c

package info (click to toggle)
ostree 2025.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,996 kB
  • sloc: ansic: 70,831; sh: 15,652; xml: 5,007; yacc: 1,236; javascript: 531; makefile: 243; python: 155
file content (124 lines) | stat: -rw-r--r-- 4,378 bytes parent folder | download | duplicates (2)
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
123
124
/*
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library. If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gio/gio.h>

#include "ostree-blob-reader-private.h"

static const guint8 pubkey_ed25519[]
    = { 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00, 0x36, 0x09, 0x06,
        0x69, 0xf3, 0x52, 0xb1, 0xe3, 0x7e, 0xd4, 0xb5, 0xe3, 0x4c, 0x52, 0x6b, 0x7d, 0xdb, 0xba,
        0x37, 0x6a, 0xac, 0xe6, 0xb9, 0x5f, 0xf5, 0xdd, 0xf1, 0x95, 0xa5, 0x5c, 0x96, 0x09 };

static const gchar pem_pubkey_ed25519[]
    = "-----BEGIN PUBLIC KEY-----\n"
      "MCowBQYDK2VwAyEANgkGafNSseN+1LXjTFJrfdu6N2qs5rlf9d3xlaVclgk=\n"
      "-----END PUBLIC KEY-----\n";

static const gchar pem_pubkey_ed25519_whitespace[]
    = "-----BEGIN PUBLIC KEY-----\n"
      " \n"
      "MCowBQYDK2VwAyEANgkGafNSseN+1LXjTFJrfdu6N2qs5rlf9d3xlaVclgk=\n"
      "-----END PUBLIC KEY-----\n";

static const gchar pem_pubkey_empty[] = "";

static const gchar pem_pubkey_ed25519_no_trailer[]
    = "-----BEGIN PUBLIC KEY-----\n"
      "MCowBQYDK2VwAyEANgkGafNSseN+1LXjTFJrfdu6N2qs5rlf9d3xlaVclgk=\n";

static const gchar pem_pubkey_ed25519_label_mismatch[]
    = "-----BEGIN PUBLIC KEY X-----\n"
      "MCowBQYDK2VwAyEANgkGafNSseN+1LXjTFJrfdu6N2qs5rlf9d3xlaVclgk=\n"
      "-----END PUBLIC KEY Y-----\n";

static void
test_ostree_read_pem_block_valid (void)
{
  static const struct
  {
    const gchar *pem_data;
    gsize pem_size;
    const gchar *label;
    const guint8 *data;
    gsize size;
  } tests[] = {
    { pem_pubkey_ed25519, sizeof (pem_pubkey_ed25519), "PUBLIC KEY", pubkey_ed25519,
      sizeof (pubkey_ed25519) },
    { pem_pubkey_ed25519_whitespace, sizeof (pem_pubkey_ed25519_whitespace), "PUBLIC KEY",
      pubkey_ed25519, sizeof (pubkey_ed25519) },
    { pem_pubkey_empty, sizeof (pem_pubkey_empty), NULL, NULL, 0 },
  };

  for (gsize i = 0; i < G_N_ELEMENTS (tests); i++)
    {
      g_autoptr (GInputStream) stream
          = g_memory_input_stream_new_from_data (tests[i].pem_data, tests[i].pem_size, NULL);
      g_autoptr (GDataInputStream) data_stream = g_data_input_stream_new (stream);

      g_autofree char *label = NULL;
      g_autoptr (GBytes) bytes = NULL;
      g_autoptr (GError) error = NULL;
      bytes = _ostree_read_pem_block (data_stream, &label, NULL, &error);
      g_assert_no_error (error);
      g_assert_cmpstr (label, ==, tests[i].label);
      if (tests[i].data)
        {
          g_autoptr (GBytes) expected_bytes = g_bytes_new_static (tests[i].data, tests[i].size);
          g_assert (g_bytes_equal (bytes, expected_bytes));
        }
    }
}

static void
test_ostree_read_pem_block_invalid (void)
{
  static const struct
  {
    const gchar *pem_data;
    gsize pem_size;
  } tests[] = {
    { pem_pubkey_ed25519_no_trailer, sizeof (pem_pubkey_ed25519_no_trailer) },
    { pem_pubkey_ed25519_label_mismatch, sizeof (pem_pubkey_ed25519_label_mismatch) },
  };

  for (gsize i = 0; i < G_N_ELEMENTS (tests); i++)
    {
      g_autoptr (GInputStream) stream
          = g_memory_input_stream_new_from_data (tests[i].pem_data, tests[i].pem_size, NULL);
      g_autoptr (GDataInputStream) data_stream = g_data_input_stream_new (stream);

      g_autofree char *label = NULL;
      g_autoptr (GBytes) bytes = NULL;
      g_autoptr (GError) error = NULL;
      bytes = _ostree_read_pem_block (data_stream, &label, NULL, &error);
      g_assert_null (bytes);
      g_assert_null (label);
      g_assert_nonnull (error);
    }
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);
  g_test_add_func ("/ostree_read_pem_block/valid", test_ostree_read_pem_block_valid);
  g_test_add_func ("/ostree_read_pem_block/invalid", test_ostree_read_pem_block_invalid);
  return g_test_run ();
}