tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

result_sink.cc (3728B)


      1 /*
      2 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "modules/audio_coding/neteq/test/result_sink.h"
     12 
     13 #include <cstddef>
     14 #include <cstdint>
     15 #include <cstdio>
     16 #include <string>
     17 
     18 #include "absl/strings/string_view.h"
     19 #include "api/neteq/neteq.h"
     20 #include "rtc_base/message_digest.h"
     21 #include "rtc_base/string_encode.h"
     22 #include "test/gtest.h"
     23 
     24 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
     25 
     26 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
     27 #include "external/webrtc/webrtc/modules/audio_coding/neteq/neteq_unittest.pb.h"
     28 #else
     29 #include "modules/audio_coding/neteq/neteq_unittest.pb.h"
     30 #endif
     31 
     32 #endif
     33 
     34 namespace webrtc {
     35 
     36 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
     37 void Convert(const NetEqNetworkStatistics& stats_raw,
     38             neteq_unittest::NetEqNetworkStatistics* stats) {
     39  stats->set_current_buffer_size_ms(stats_raw.current_buffer_size_ms);
     40  stats->set_preferred_buffer_size_ms(stats_raw.preferred_buffer_size_ms);
     41  stats->set_jitter_peaks_found(stats_raw.jitter_peaks_found);
     42  stats->set_expand_rate(stats_raw.expand_rate);
     43  stats->set_speech_expand_rate(stats_raw.speech_expand_rate);
     44  stats->set_preemptive_rate(stats_raw.preemptive_rate);
     45  stats->set_accelerate_rate(stats_raw.accelerate_rate);
     46  stats->set_secondary_decoded_rate(stats_raw.secondary_decoded_rate);
     47  stats->set_secondary_discarded_rate(stats_raw.secondary_discarded_rate);
     48  stats->set_mean_waiting_time_ms(stats_raw.mean_waiting_time_ms);
     49  stats->set_median_waiting_time_ms(stats_raw.median_waiting_time_ms);
     50  stats->set_min_waiting_time_ms(stats_raw.min_waiting_time_ms);
     51  stats->set_max_waiting_time_ms(stats_raw.max_waiting_time_ms);
     52 }
     53 
     54 void AddMessage(FILE* file, MessageDigest* digest, absl::string_view message) {
     55  int32_t size = message.length();
     56  if (file)
     57    ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
     58  digest->Update(&size, sizeof(size));
     59 
     60  if (file)
     61    ASSERT_EQ(static_cast<size_t>(size),
     62              fwrite(message.data(), sizeof(char), size, file));
     63  digest->Update(message.data(), sizeof(char) * size);
     64 }
     65 
     66 #endif  // WEBRTC_NETEQ_UNITTEST_BITEXACT
     67 
     68 ResultSink::ResultSink(absl::string_view output_file)
     69    : output_fp_(nullptr), digest_(MessageDigestFactory::Create(DIGEST_SHA_1)) {
     70  if (!output_file.empty()) {
     71    output_fp_ = fopen(std::string(output_file).c_str(), "wb");
     72    EXPECT_TRUE(output_fp_ != nullptr);
     73  }
     74 }
     75 
     76 ResultSink::~ResultSink() {
     77  if (output_fp_)
     78    fclose(output_fp_);
     79 }
     80 
     81 void ResultSink::AddResult(const NetEqNetworkStatistics& stats_raw) {
     82 #ifdef WEBRTC_NETEQ_UNITTEST_BITEXACT
     83  neteq_unittest::NetEqNetworkStatistics stats;
     84  Convert(stats_raw, &stats);
     85 
     86  std::string stats_string;
     87  ASSERT_TRUE(stats.SerializeToString(&stats_string));
     88  AddMessage(output_fp_, digest_.get(), stats_string);
     89 #else
     90  FAIL() << "Writing to reference file requires Proto Buffer.";
     91 #endif  // WEBRTC_NETEQ_UNITTEST_BITEXACT
     92 }
     93 
     94 void ResultSink::VerifyChecksum(absl::string_view checksum) {
     95  std::string buffer;
     96  buffer.resize(digest_->Size());
     97  digest_->Finish(buffer.data(), buffer.size());
     98  const std::string result = hex_encode(buffer);
     99  if (checksum.size() == result.size()) {
    100    EXPECT_EQ(checksum, result);
    101  } else {
    102    // Check result is one the '|'-separated checksums.
    103    EXPECT_NE(checksum.find(result), absl::string_view::npos)
    104        << result << " should be one of these:\n"
    105        << checksum;
    106  }
    107 }
    108 
    109 }  // namespace webrtc