File: op_xz_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,497 bytes parent folder | download | duplicates (3)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/update_client/op_xz.h"

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/types/expected.h"
#include "components/update_client/test_utils.h"
#include "components/update_client/unzip/in_process_unzipper.h"
#include "components/update_client/unzipper.h"
#include "components/update_client/update_client_errors.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace update_client {

class XzOperationTest : public testing::Test {
 private:
  // `env_` must be constructed before sequence_checker_.
  base::test::TaskEnvironment env_;

 protected:
  void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }

  base::FilePath TempPath(base::FilePath basename) {
    return temp_dir_.GetPath().Append(basename);
  }

  base::FilePath CopyToTemp(const std::string& file_name) {
    base::FilePath dest = TempPath(base::FilePath().AppendUTF8(file_name));
    EXPECT_TRUE(base::CopyFile(GetTestFilePath(file_name.c_str()), dest));
    return dest;
  }

  base::RepeatingCallback<void(base::Value::Dict)> MakePingCallback() {
    return base::BindLambdaForTesting(
        [&](base::Value::Dict ping) { pings_.push_back(std::move(ping)); });
  }

  SEQUENCE_CHECKER(sequence_checker_);
  base::RunLoop loop_;
  std::vector<base::Value::Dict> pings_;

 private:
  base::ScopedTempDir temp_dir_;
};

TEST_F(XzOperationTest, Success) {
  base::FilePath in_file = CopyToTemp("file1.xz");
  XzOperation(base::MakeRefCounted<InProcessUnzipperFactory>(
                  InProcessUnzipperFactory::SymlinkOption::DONT_PRESERVE)
                  ->Create(),
              MakePingCallback(), in_file,
              base::BindLambdaForTesting(
                  [&](base::expected<base::FilePath, CategorizedError> result) {
                    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
                    ASSERT_TRUE(result.has_value());
                    EXPECT_TRUE(base::ContentsEqual(GetTestFilePath("file1"),
                                                    result.value()));
                  })
                  .Then(loop_.QuitClosure()));
  loop_.Run();
  EXPECT_FALSE(base::PathExists(in_file));
  ASSERT_EQ(pings_.size(), 1u);
  EXPECT_EQ(pings_[0].FindInt("eventtype"), 60);
  EXPECT_EQ(pings_[0].FindInt("eventresult"), 1);
}

TEST_F(XzOperationTest, BadPatch) {
  base::FilePath in_file = CopyToTemp("file1");
  XzOperation(base::MakeRefCounted<InProcessUnzipperFactory>(
                  InProcessUnzipperFactory::SymlinkOption::DONT_PRESERVE)
                  ->Create(),
              MakePingCallback(), in_file,
              base::BindLambdaForTesting(
                  [&](base::expected<base::FilePath, CategorizedError> result) {
                    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
                    ASSERT_FALSE(result.has_value());
                  })
                  .Then(loop_.QuitClosure()));
  loop_.Run();
  EXPECT_FALSE(base::PathExists(in_file));
  ASSERT_EQ(pings_.size(), 1u);
  EXPECT_EQ(pings_[0].FindInt("eventtype"), 60);
  EXPECT_EQ(pings_[0].FindInt("eventresult"), 0);
}

}  // namespace update_client