1 /*
2  * Copyright (c) 2020 [Ribose Inc](https://www.ribose.com).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *
11  * 2.  Redistributions in binary form must reproduce the above copyright notice,
12  *     this list of conditions and the following disclaimer in the documentation
13  *     and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "../rnp_tests.h"
28 #include "../support.h"
29 #include <librepgp/stream-ctx.h>
30 #include "pgp-key.h"
31 #include "ffi-priv-types.h"
32 
TEST_F(rnp_tests,test_issue_1171_key_import_and_remove)33 TEST_F(rnp_tests, test_issue_1171_key_import_and_remove)
34 {
35     rnp_ffi_t ffi = NULL;
36     assert_rnp_success(rnp_ffi_create(&ffi, "GPG", "GPG"));
37     assert_true(import_pub_keys(ffi, "data/test_key_validity/alice-sub-pub.pgp"));
38 
39     rnp_key_handle_t key = NULL;
40     assert_rnp_success(
41       rnp_locate_key(ffi, "grip", "3E3D52A346F0AD47754611B078117C240ED237E9", &key));
42     assert_non_null(key);
43     assert_rnp_success(rnp_key_remove(key, RNP_KEY_REMOVE_PUBLIC));
44     assert_rnp_success(rnp_key_handle_destroy(key));
45 
46     assert_rnp_success(
47       rnp_locate_key(ffi, "grip", "3E3D52A346F0AD47754611B078117C240ED237E9", &key));
48     assert_null(key);
49 
50     assert_rnp_success(
51       rnp_locate_key(ffi, "grip", "128E494F41F107E119AA1EEF7850C375A804341C", &key));
52     assert_non_null(key);
53     uint32_t bits = 0;
54     assert_rnp_success(rnp_key_get_bits(key, &bits));
55     assert_int_equal(bits, 256);
56 
57     /* directly use rnp_tests_get_key_by_grip() which caused crash */
58     pgp_key_t *subkey = rnp_tests_get_key_by_grip(ffi->pubring, key->pub->grip());
59     assert_int_equal(subkey->material().bits(), 256);
60     assert_rnp_success(rnp_key_handle_destroy(key));
61 
62     assert_true(import_pub_keys(ffi, "data/test_key_validity/alice-sub-pub.pgp"));
63 
64     rnp_ffi_destroy(ffi);
65 }
66