1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <memory>
17 #include "common/common_test.h"
18 #include "include/api/serialization.h"
19
20 namespace mindspore {
21 class TestCxxApiSerialization : public UT::Common {
22 public:
23 TestCxxApiSerialization() = default;
24 };
25
TEST_F(TestCxxApiSerialization,test_load_no_encrpty_mindir_SUCCESS)26 TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_SUCCESS) {
27 Graph graph;
28 ASSERT_TRUE(Serialization::Load("./data/mindir/add_no_encrpty.mindir", ModelType::kMindIR, &graph) == kSuccess);
29 }
30
TEST_F(TestCxxApiSerialization,test_load_output_args_nullptr_FAILED)31 TEST_F(TestCxxApiSerialization, test_load_output_args_nullptr_FAILED) {
32 auto status = Serialization::Load("./data/mindir/add_no_encrpty.mindir", ModelType::kMindIR, nullptr);
33 ASSERT_TRUE(status != kSuccess);
34 auto err_mst = status.GetErrDescription();
35 ASSERT_TRUE(err_mst.find("null") != std::string::npos);
36 }
37
TEST_F(TestCxxApiSerialization,test_load_file_not_exist_FAILED)38 TEST_F(TestCxxApiSerialization, test_load_file_not_exist_FAILED) {
39 Graph graph;
40 auto status = Serialization::Load("./data/mindir/file_not_exist.mindir", ModelType::kMindIR, &graph);
41 ASSERT_TRUE(status != kSuccess);
42 auto err_mst = status.GetErrDescription();
43 ASSERT_TRUE(err_mst.find("exist") != std::string::npos);
44 }
45
TEST_F(TestCxxApiSerialization,test_load_encrpty_mindir_SUCCESS)46 TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_SUCCESS) {
47 Graph graph;
48 std::string key_str = "0123456789ABCDEF";
49 Key key;
50 memcpy(key.key, key_str.data(), key_str.size());
51 key.len = key_str.size();
52 ASSERT_TRUE(Serialization::Load("./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir", ModelType::kMindIR, &graph,
53 key, kDecModeAesGcm) == kSuccess);
54 }
55
TEST_F(TestCxxApiSerialization,test_load_encrpty_mindir_without_key_FAILED)56 TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_without_key_FAILED) {
57 Graph graph;
58 auto status =
59 Serialization::Load("./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir", ModelType::kMindIR, &graph);
60 ASSERT_TRUE(status != kSuccess);
61 auto err_mst = status.GetErrDescription();
62 ASSERT_TRUE(err_mst.find("be encrypted") != std::string::npos);
63 }
64
TEST_F(TestCxxApiSerialization,test_load_encrpty_mindir_with_wrong_key_FAILED)65 TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_with_wrong_key_FAILED) {
66 Graph graph;
67 std::string key_str = "WRONG_KEY";
68 Key key;
69 memcpy(key.key, key_str.data(), key_str.size());
70 key.len = key_str.size();
71 auto status = Serialization::Load("./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir", ModelType::kMindIR, &graph,
72 key, kDecModeAesGcm);
73 ASSERT_TRUE(status != kSuccess);
74 }
75
TEST_F(TestCxxApiSerialization,test_load_no_encrpty_mindir_with_wrong_key_FAILED)76 TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_with_wrong_key_FAILED) {
77 Graph graph;
78 std::string key_str = "WRONG_KEY";
79 Key key;
80 memcpy(key.key, key_str.data(), key_str.size());
81 key.len = key_str.size();
82 auto status = Serialization::Load("./data/mindir/add_no_encrpty.mindir", ModelType::kMindIR, &graph,
83 key, kDecModeAesGcm);
84 ASSERT_TRUE(status != kSuccess);
85 }
86
TEST_F(TestCxxApiSerialization,test_load_no_encrpty_mindir_x1_SUCCESS)87 TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_x1_SUCCESS) {
88 std::vector<Graph> graphs;
89 ASSERT_TRUE(Serialization::Load(std::vector<std::string>(1, "./data/mindir/add_no_encrpty.mindir"),
90 ModelType::kMindIR, &graphs) == kSuccess);
91 }
92
TEST_F(TestCxxApiSerialization,test_load_no_encrpty_mindir_x2_SUCCESS)93 TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_x2_SUCCESS) {
94 std::vector<Graph> graphs;
95 ASSERT_TRUE(Serialization::Load(std::vector<std::string>(2, "./data/mindir/add_no_encrpty.mindir"),
96 ModelType::kMindIR, &graphs) == kSuccess);
97 }
98
TEST_F(TestCxxApiSerialization,test_load_file_not_exist_x2_FAILED)99 TEST_F(TestCxxApiSerialization, test_load_file_not_exist_x2_FAILED) {
100 std::vector<Graph> graphs;
101 auto status = Serialization::Load(std::vector<std::string>(2, "./data/mindir/file_not_exist.mindir"),
102 ModelType::kMindIR, &graphs);
103 ASSERT_TRUE(status != kSuccess);
104 auto err_mst = status.GetErrDescription();
105 ASSERT_TRUE(err_mst.find("exist") != std::string::npos);
106 }
107
TEST_F(TestCxxApiSerialization,test_load_encrpty_mindir_without_key_x2_FAILED)108 TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_without_key_x2_FAILED) {
109 std::vector<Graph> graphs;
110 auto status = Serialization::Load(
111 std::vector<std::string>(2, "./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir"), ModelType::kMindIR, &graphs);
112 ASSERT_TRUE(status != kSuccess);
113 auto err_mst = status.GetErrDescription();
114 ASSERT_TRUE(err_mst.find("be encrypted") != std::string::npos);
115 }
116 } // namespace mindspore
117