instrument_metadata_validator_test.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <gtest/gtest.h>
  4. #include <stddef.h>
  5. #include <string>
  6. #include <vector>
  7. #include "opentelemetry/sdk/metrics/instrument_metadata_validator.h"
  8. static std::string CreateVeryLargeString(size_t multiple)
  9. {
  10. std::string result = "START";
  11. std::string repeater = "0123456789";
  12. for (size_t i = 0; i < multiple; i++)
  13. {
  14. result += repeater;
  15. }
  16. return result;
  17. }
  18. TEST(InstrumentMetadataValidator, TestName)
  19. {
  20. opentelemetry::sdk::metrics::InstrumentMetaDataValidator validator;
  21. std::vector<std::string> invalid_names = {
  22. "", // empty string
  23. "1sdf", // string starting with number
  24. "\x31\x32\x33\xe2\x82\xac\x41\x41\x41\xe2\x82\xac\x42\x42\x42", // unicode characters
  25. "/\\sdsd", // string starting with special character
  26. "***sSSs", // string starting with special character
  27. "a\\broken\\path", // contains backward slash
  28. CreateVeryLargeString(25) + "X", // total 256 characters
  29. CreateVeryLargeString(26), // string much bigger than 255 character
  30. };
  31. for (auto const &str : invalid_names)
  32. {
  33. EXPECT_FALSE(validator.ValidateName(str));
  34. }
  35. std::vector<std::string> valid_names = {
  36. "T", // single char string
  37. "s123", // starting with char, followed by numbers
  38. "dsdsdsd_-.", // string , and valid nonalphanumeric
  39. "d1234_-sDSDs.sdsd344", // combination of all valid characters
  40. "a/path/to/some/metric", // contains forward slash
  41. CreateVeryLargeString(5) + "ABCERTYG", // total 63 characters
  42. CreateVeryLargeString(5) + "ABCERTYGJ", // total 64 characters
  43. CreateVeryLargeString(24) + "ABCDEFGHI", // total 254 characters
  44. CreateVeryLargeString(25), // total 255 characters
  45. };
  46. for (auto const &str : valid_names)
  47. {
  48. EXPECT_TRUE(validator.ValidateName(str));
  49. }
  50. }
  51. TEST(InstrumentMetadataValidator, TestUnit)
  52. {
  53. opentelemetry::sdk::metrics::InstrumentMetaDataValidator validator;
  54. std::vector<std::string> invalid_units = {
  55. CreateVeryLargeString(5) + "ABCERTYGJ", // total 64 charactes
  56. CreateVeryLargeString(7), // string bigger than 63 chars
  57. "\x31\x32\x33\xe2\x82\xac\x41\x41\x41\xe2\x82\xac\x42\x42\x42", // unicode string
  58. };
  59. for (auto const &str : invalid_units)
  60. {
  61. EXPECT_FALSE(validator.ValidateUnit(str));
  62. }
  63. std::vector<std::string> valid_units = {
  64. "T", // single char
  65. "s123", // starting with char, followed by numbers
  66. "dsdsdsd_-.", // string , and valid nonalphanumeric
  67. "d1234_-sdsds.sdsd344", // combination of all valid characters
  68. CreateVeryLargeString(5) + "ABCERTYG", // total 63 charactes
  69. "ASDDSDF", // uppercase
  70. };
  71. for (auto const &str : valid_units)
  72. {
  73. EXPECT_TRUE(validator.ValidateName(str));
  74. }
  75. }