<commit_before>// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


// This file contains unit tests for P4ConfigVerifier.

#include "stratum/hal/lib/p4/p4_config_verifier.h"

#include <memory>
#include <string>
#include <vector>

#include "gflags/gflags.h"
#include "stratum/glue/status/status_test_util.h"
#include "stratum/hal/lib/p4/p4_info_manager.h"
#include "stratum/lib/utils.h"
#include "stratum/public/lib/error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/memory/memory.h"
#include "github.com/p4lang/PI/p4/v1/p4runtime.pb.h"
#include "stratum/glue/gtl/map_util.h"

// P4ConfigVerifier flags to override for some tests.
DECLARE_string(match_field_error_level);
DECLARE_string(action_field_error_level);

using ::testing::HasSubstr;

namespace stratum {
namespace hal {

// This class is the P4ConfigVerifier test fixture.
class P4ConfigVerifierTest : public testing::Test {
 protected:
  void SetUpP4ConfigFromFiles() {
    const std::string kTestP4InfoFile =
        "stratum/hal/lib/p4/testdata/test_p4_info.pb.txt";
    ASSERT_OK(ReadProtoFromTextFile(kTestP4InfoFile, &test_p4_info_));
    const std::string kTestP4PipelineConfigFile =
        "stratum/hal/lib/p4/testdata/"
        "test_p4_pipeline_config.pb.txt";
    ASSERT_OK(ReadProtoFromTextFile(
        kTestP4PipelineConfigFile, &test_p4_pipeline_config_));

    // P4ConfigVerifier assumes P4InfoManager pre-validation of P4Info.
    p4_info_manager_ = absl::make_unique<P4InfoManager>(test_p4_info_);
    ASSERT_OK(p4_info_manager_->InitializeAndVerify());
  }

  // Verifies that the first table in test_p4_info_ has a valid table
  // descriptor.
  bool FirstTableHasDescriptor() {
    bool descriptor_ok = false;
    CHECK_LT(0, test_p4_info_.tables_size()) << "Test P4Info has no tables";
    const ::p4::config::v1::Table& first_p4_table = test_p4_info_.tables(0);<commit_msg>Adding reference to gflags namespace in p4_config_verifier_test.cc<commit_after>// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


// This file contains unit tests for P4ConfigVerifier.

#include "stratum/hal/lib/p4/p4_config_verifier.h"

#include <memory>
#include <string>
#include <vector>

#include "gflags/gflags.h"
#include "stratum/glue/status/status_test_util.h"
#include "stratum/hal/lib/p4/p4_info_manager.h"
#include "stratum/lib/utils.h"
#include "stratum/public/lib/error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/memory/memory.h"
#include "github.com/p4lang/PI/p4/v1/p4runtime.pb.h"
#include "stratum/glue/gtl/map_util.h"

// P4ConfigVerifier flags to override for some tests.
DECLARE_string(match_field_error_level);
DECLARE_string(action_field_error_level);

using ::testing::HasSubstr;
using ::gflags::FlagSaver;

namespace stratum {
namespace hal {

// This class is the P4ConfigVerifier test fixture.
class P4ConfigVerifierTest : public testing::Test {
 protected:
  void SetUpP4ConfigFromFiles() {
    const std::string kTestP4InfoFile =
        "stratum/hal/lib/p4/testdata/test_p4_info.pb.txt";
    ASSERT_OK(ReadProtoFromTextFile(kTestP4InfoFile, &test_p4_info_));
    const std::string kTestP4PipelineConfigFile =
        "stratum/hal/lib/p4/testdata/"
        "test_p4_pipeline_config.pb.txt";
    ASSERT_OK(ReadProtoFromTextFile(
        kTestP4PipelineConfigFile, &test_p4_pipeline_config_));

    // P4ConfigVerifier assumes P4InfoManager pre-validation of P4Info.
    p4_info_manager_ = absl::make_unique<P4InfoManager>(test_p4_info_);
    ASSERT_OK(p4_info_manager_->InitializeAndVerify());
  }

  // Verifies that the first table in test_p4_info_ has a valid table
  // descriptor.
  bool FirstTableHasDescriptor() {
    bool descriptor_ok = false;
    CHECK_LT(0, test_p4_info_.tables_size()) << "Test P4Info has no tables";
    const ::p4::config::v1::Table& first_p4_table = test_p4_info_.tables(0);
{'fraction_non_alphanumeric': 0.10862489829129374, 'fraction_numerical': 0.019324654190398697, 'mean_word_length': 4.5, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5554716', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1631, 'n_words': 403}