You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

serialization_test.cc 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. namespace mindspore {
  20. class TestCxxApiSerialization : public UT::Common {
  21. public:
  22. TestCxxApiSerialization() = default;
  23. };
  24. TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_SUCCESS) {
  25. Graph graph;
  26. ASSERT_TRUE(Serialization::Load("./data/mindir/add_no_encrpty.mindir", ModelType::kMindIR, &graph) == kSuccess);
  27. }
  28. TEST_F(TestCxxApiSerialization, test_load_output_args_nullptr_FAILED) {
  29. auto status = Serialization::Load("./data/mindir/add_no_encrpty.mindir", ModelType::kMindIR, nullptr);
  30. ASSERT_TRUE(status != kSuccess);
  31. auto err_mst = status.GetErrDescription();
  32. ASSERT_TRUE(err_mst.find("null") != std::string::npos);
  33. }
  34. TEST_F(TestCxxApiSerialization, test_load_file_not_exist_FAILED) {
  35. Graph graph;
  36. auto status = Serialization::Load("./data/mindir/file_not_exist.mindir", ModelType::kMindIR, &graph);
  37. ASSERT_TRUE(status != kSuccess);
  38. auto err_mst = status.GetErrDescription();
  39. ASSERT_TRUE(err_mst.find("exist") != std::string::npos);
  40. }
  41. TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_SUCCESS) {
  42. Graph graph;
  43. std::string key_str = "0123456789ABCDEF";
  44. Key key;
  45. memcpy(key.key, key_str.data(), key_str.size());
  46. key.len = key_str.size();
  47. ASSERT_TRUE(Serialization::Load("./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir", ModelType::kMindIR, &graph,
  48. key, kDecModeAesGcm) == kSuccess);
  49. }
  50. TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_without_key_FAILED) {
  51. Graph graph;
  52. auto status =
  53. Serialization::Load("./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir", ModelType::kMindIR, &graph);
  54. ASSERT_TRUE(status != kSuccess);
  55. auto err_mst = status.GetErrDescription();
  56. ASSERT_TRUE(err_mst.find("be encrypted") != std::string::npos);
  57. }
  58. TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_with_wrong_key_FAILED) {
  59. Graph graph;
  60. std::string key_str = "WRONG_KEY";
  61. Key key;
  62. memcpy(key.key, key_str.data(), key_str.size());
  63. key.len = key_str.size();
  64. auto status = Serialization::Load("./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir", ModelType::kMindIR, &graph,
  65. key, kDecModeAesGcm);
  66. ASSERT_TRUE(status != kSuccess);
  67. }
  68. TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_with_wrong_key_FAILED) {
  69. Graph graph;
  70. std::string key_str = "WRONG_KEY";
  71. Key key;
  72. memcpy(key.key, key_str.data(), key_str.size());
  73. key.len = key_str.size();
  74. auto status = Serialization::Load("./data/mindir/add_no_encrpty.mindir", ModelType::kMindIR, &graph,
  75. key, kDecModeAesGcm);
  76. ASSERT_TRUE(status != kSuccess);
  77. }
  78. TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_x1_SUCCESS) {
  79. std::vector<Graph> graphs;
  80. ASSERT_TRUE(Serialization::Load(std::vector<std::string>(1, "./data/mindir/add_no_encrpty.mindir"),
  81. ModelType::kMindIR, &graphs) == kSuccess);
  82. }
  83. TEST_F(TestCxxApiSerialization, test_load_no_encrpty_mindir_x2_SUCCESS) {
  84. std::vector<Graph> graphs;
  85. ASSERT_TRUE(Serialization::Load(std::vector<std::string>(2, "./data/mindir/add_no_encrpty.mindir"),
  86. ModelType::kMindIR, &graphs) == kSuccess);
  87. }
  88. TEST_F(TestCxxApiSerialization, test_load_file_not_exist_x2_FAILED) {
  89. std::vector<Graph> graphs;
  90. auto status = Serialization::Load(std::vector<std::string>(2, "./data/mindir/file_not_exist.mindir"),
  91. ModelType::kMindIR, &graphs);
  92. ASSERT_TRUE(status != kSuccess);
  93. auto err_mst = status.GetErrDescription();
  94. ASSERT_TRUE(err_mst.find("exist") != std::string::npos);
  95. }
  96. TEST_F(TestCxxApiSerialization, test_load_encrpty_mindir_without_key_x2_FAILED) {
  97. std::vector<Graph> graphs;
  98. auto status = Serialization::Load(
  99. std::vector<std::string>(2, "./data/mindir/add_encrpty_key_0123456789ABCDEF.mindir"), ModelType::kMindIR, &graphs);
  100. ASSERT_TRUE(status != kSuccess);
  101. auto err_mst = status.GetErrDescription();
  102. ASSERT_TRUE(err_mst.find("be encrypted") != std::string::npos);
  103. }
  104. } // namespace mindspore