File: instrument_metadata_validator_test.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (83 lines) | stat: -rw-r--r-- 3,232 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stddef.h>
#include <string>
#include <vector>

#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/metrics/instrument_metadata_validator.h"

static std::string CreateVeryLargeString(size_t multiple)
{
  std::string result   = "START";
  std::string repeater = "0123456789";
  for (size_t i = 0; i < multiple; i++)
  {
    result += repeater;
  }
  return result;
}

TEST(InstrumentMetadataValidator, TestName)
{
  opentelemetry::sdk::metrics::InstrumentMetaDataValidator validator;
  std::vector<std::string> invalid_names = {
      "",      // empty string
      "1sdf",  // string starting with number
      "\x31\x32\x33\xe2\x82\xac\x41\x41\x41\xe2\x82\xac\x42\x42\x42",  // unicode characters
      "/\\sdsd",                        // string starting with special character
      "***sSSs",                        // string starting with special character
      "a\\broken\\path",                // contains backward slash
      CreateVeryLargeString(25) + "X",  // total 256 characters
      CreateVeryLargeString(26),        // string much bigger than 255 character
  };
  for (auto const &str : invalid_names)
  {
    EXPECT_FALSE(validator.ValidateName(str));
  }

  std::vector<std::string> valid_names = {
      "T",                                      // single char string
      "s123",                                   // starting with char, followed by numbers
      "dsdsdsd_-.",                             // string , and valid nonalphanumeric
      "d1234_-sDSDs.sdsd344",                   // combination of all valid characters
      "a/path/to/some/metric",                  // contains forward slash
      CreateVeryLargeString(5) + "ABCERTYG",    // total 63 characters
      CreateVeryLargeString(5) + "ABCERTYGJ",   // total 64 characters
      CreateVeryLargeString(24) + "ABCDEFGHI",  // total 254 characters
      CreateVeryLargeString(25),                // total 255 characters
  };
  for (auto const &str : valid_names)
  {
    EXPECT_TRUE(validator.ValidateName(str));
  }
}

TEST(InstrumentMetadataValidator, TestUnit)
{
  opentelemetry::sdk::metrics::InstrumentMetaDataValidator validator;
  std::vector<std::string> invalid_units = {
      CreateVeryLargeString(5) + "ABCERTYGJ",  // total 64 charactes
      CreateVeryLargeString(7),                // string bigger than 63 chars
      "\x31\x32\x33\xe2\x82\xac\x41\x41\x41\xe2\x82\xac\x42\x42\x42",  // unicode string
  };
  for (auto const &str : invalid_units)
  {
    EXPECT_FALSE(validator.ValidateUnit(str));
  }

  std::vector<std::string> valid_units = {
      "T",                                    // single char
      "s123",                                 // starting with char, followed by numbers
      "dsdsdsd_-.",                           // string , and valid nonalphanumeric
      "d1234_-sdsds.sdsd344",                 // combination of all valid characters
      CreateVeryLargeString(5) + "ABCERTYG",  // total 63 charactes
      "ASDDSDF",                              // uppercase
  };
  for (auto const &str : valid_units)
  {
    EXPECT_TRUE(validator.ValidateName(str));
  }
}