1 // Copyright 2017 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "bsdiff/bsdiff_arguments.h"
6
7 #include <gtest/gtest.h>
8
9 namespace bsdiff {
10
TEST(BsdiffArgumentsTest,ParseCompressorTypeTest)11 TEST(BsdiffArgumentsTest, ParseCompressorTypeTest) {
12 std::set<CompressorType> types;
13 EXPECT_TRUE(BsdiffArguments::ParseCompressorTypes("Brotli", &types));
14 EXPECT_EQ(1U, types.size());
15 EXPECT_NE(types.end(), types.find(CompressorType::kBrotli));
16
17 types.clear();
18
19 EXPECT_TRUE(BsdiffArguments::ParseCompressorTypes("bz2", &types));
20 EXPECT_EQ(1U, types.size());
21 EXPECT_NE(types.end(), types.find(CompressorType::kBZ2));
22
23 types.clear();
24
25 EXPECT_FALSE(BsdiffArguments::ParseCompressorTypes("invalid", &types));
26 }
27
TEST(BsdiffArgumentsTest,ParseMultipleCompressorTypeTest)28 TEST(BsdiffArgumentsTest, ParseMultipleCompressorTypeTest) {
29 std::set<CompressorType> types;
30 EXPECT_TRUE(BsdiffArguments::ParseCompressorTypes("bz2:brotli:nocompression",
31 &types));
32 EXPECT_EQ(3U, types.size());
33 EXPECT_NE(types.end(), types.find(CompressorType::kBrotli));
34 EXPECT_NE(types.end(), types.find(CompressorType::kBZ2));
35 EXPECT_NE(types.end(), types.find(CompressorType::kNoCompression));
36
37 types.clear();
38
39 // No space in the type string.
40 EXPECT_FALSE(
41 BsdiffArguments::ParseCompressorTypes("bz2 : nocompression", &types));
42 }
43
TEST(BsdiffArgumentsTest,ParseBsdiffFormatTest)44 TEST(BsdiffArgumentsTest, ParseBsdiffFormatTest) {
45 BsdiffFormat format;
46 EXPECT_TRUE(BsdiffArguments::ParseBsdiffFormat("bsdf2", &format));
47 EXPECT_EQ(BsdiffFormat::kBsdf2, format);
48
49 EXPECT_TRUE(BsdiffArguments::ParseBsdiffFormat("Legacy", &format));
50 EXPECT_EQ(BsdiffFormat::kLegacy, format);
51
52 EXPECT_TRUE(BsdiffArguments::ParseBsdiffFormat("bsdiff40", &format));
53 EXPECT_EQ(BsdiffFormat::kLegacy, format);
54
55 EXPECT_TRUE(BsdiffArguments::ParseBsdiffFormat("endsley", &format));
56 EXPECT_EQ(BsdiffFormat::kEndsley, format);
57
58 EXPECT_FALSE(BsdiffArguments::ParseBsdiffFormat("Other", &format));
59 }
60
TEST(BsdiffArgumentsTest,ParseQualityTest)61 TEST(BsdiffArgumentsTest, ParseQualityTest) {
62 int quality;
63 EXPECT_TRUE(BsdiffArguments::ParseQuality("9", &quality, 0, 11));
64 EXPECT_EQ(9, quality);
65
66 // Check the out of range quality values.
67 EXPECT_FALSE(BsdiffArguments::ParseQuality("30", &quality, 0, 11));
68 EXPECT_FALSE(BsdiffArguments::ParseQuality("1234567890", &quality, 0, 1000));
69 EXPECT_FALSE(BsdiffArguments::ParseQuality("aabb", &quality, 0, 1000));
70 }
71
TEST(BsdiffArgumentsTest,ParseMinLengthTest)72 TEST(BsdiffArgumentsTest, ParseMinLengthTest) {
73 size_t len;
74 EXPECT_TRUE(BsdiffArguments::ParseMinLength("11", &len));
75 EXPECT_EQ(11U, len);
76
77 // Check the out of range quality values.
78 EXPECT_FALSE(BsdiffArguments::ParseMinLength("-1", &len));
79 EXPECT_FALSE(BsdiffArguments::ParseMinLength("aabb", &len));
80 }
81
TEST(BsdiffArgumentsTest,ArgumentsValidTest)82 TEST(BsdiffArgumentsTest, ArgumentsValidTest) {
83 // Default arguments using BsdiffFormat::kLegacy and CompressorType::kBZ2
84 // should be valid.
85 EXPECT_TRUE(BsdiffArguments().IsValid());
86
87 // brotli is not supported for BsdiffFormat::kLegacy.
88 EXPECT_FALSE(
89 BsdiffArguments(BsdiffFormat::kLegacy, {CompressorType::kBrotli}, -1)
90 .IsValid());
91
92 EXPECT_TRUE(
93 BsdiffArguments(BsdiffFormat::kBsdf2, {CompressorType::kBrotli}, 9)
94 .IsValid());
95
96 // Compression quality out of range for brotli.
97 EXPECT_FALSE(
98 BsdiffArguments(BsdiffFormat::kBsdf2, {CompressorType::kBrotli}, 20)
99 .IsValid());
100 }
101
TEST(BsdiffArgumentsTest,ParseArgumentsSmokeTest)102 TEST(BsdiffArgumentsTest, ParseArgumentsSmokeTest) {
103 std::vector<const char*> args = {"bsdiff", "--format=bsdf2",
104 "--type=brotli:bz2", "--brotli_quality=9",
105 "--minlen=12"};
106
107 BsdiffArguments arguments;
108 EXPECT_TRUE(
109 arguments.ParseCommandLine(args.size(), const_cast<char**>(args.data())));
110
111 EXPECT_EQ(BsdiffFormat::kBsdf2, arguments.format());
112
113 std::vector<CompressorType> types = {CompressorType::kBZ2,
114 CompressorType::kBrotli};
115 EXPECT_EQ(types, arguments.compressor_types());
116
117 EXPECT_EQ(9, arguments.brotli_quality());
118 EXPECT_EQ(12, arguments.min_length());
119 }
120
121 } // namespace bsdiff
122