hdfspp_errors.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <hdfs/hdfs.h>
  19. #include <libhdfspp/hdfs_ext.h>
  20. #include <google/protobuf/io/coded_stream.h>
  21. #include <gmock/gmock.h>
  22. #include <string.h>
  23. #include <string>
  24. using ::testing::_;
  25. using ::testing::InvokeArgument;
  26. using ::testing::Return;
  27. /* Don't need a real minidfs cluster since this just passes invalid params. */
  28. TEST(HdfsppErrors, NullFileSystem) {
  29. char buf[4096];
  30. hdfsFS fs = nullptr;
  31. hdfsFile fd = reinterpret_cast<hdfsFile>(1);
  32. tSize res = hdfsRead(fs, fd, buf, 4096);
  33. ASSERT_EQ(res, -1);
  34. hdfsGetLastError(buf, 4096);
  35. ASSERT_EQ(std::string(buf), "Cannot perform FS operations with null FS handle.");
  36. }
  37. TEST(HdfsppErrors, NullFileHandle) {
  38. char buf[4096];
  39. hdfsFS fs = reinterpret_cast<hdfsFS>(1);
  40. hdfsFile fd = nullptr;
  41. tSize res = hdfsRead(fs, fd, buf, 4096);
  42. ASSERT_EQ(res, -1);
  43. hdfsGetLastError(buf, 4096);
  44. ASSERT_EQ(std::string(buf), "Cannot perform FS operations with null File handle.");
  45. }
  46. TEST(HdfsppErrors, ZeroLength) {
  47. char buf[1];
  48. buf[0] = 0;
  49. hdfsFS fs = reinterpret_cast<hdfsFS>(1);
  50. hdfsFile fd = nullptr;
  51. tSize res = hdfsRead(fs, fd, buf, 1);
  52. ASSERT_EQ(res, -1);
  53. hdfsGetLastError(buf, 0);
  54. ASSERT_EQ(std::string(buf), "");
  55. }
  56. TEST(HdfsppErrors, NegativeLength) {
  57. char buf[1];
  58. buf[0] = 0;
  59. hdfsFS fs = reinterpret_cast<hdfsFS>(1);
  60. hdfsFile fd = nullptr;
  61. tSize res = hdfsRead(fs, fd, buf, 1);
  62. ASSERT_EQ(res, -1);
  63. hdfsGetLastError(buf, -1);
  64. ASSERT_EQ(std::string(buf), "");
  65. }
  66. TEST(HdfsppErrors, MessageTruncation) {
  67. char buf[4096];
  68. hdfsFS fs = reinterpret_cast<hdfsFS>(1);
  69. hdfsFile fd = nullptr;
  70. tSize res = hdfsRead(fs, fd, buf, 4096);
  71. ASSERT_EQ(res, -1);
  72. hdfsGetLastError(buf, 10);
  73. ASSERT_EQ(std::string(buf), "Cannot pe");
  74. }
  75. int main(int argc, char *argv[]) {
  76. // The following line must be executed to initialize Google Mock
  77. // (and Google Test) before running the tests.
  78. ::testing::InitGoogleMock(&argc, argv);
  79. int exit_code = RUN_ALL_TESTS();
  80. google::protobuf::ShutdownProtobufLibrary();
  81. return exit_code;
  82. }