aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTao Bao2017-11-04 02:08:08 -0500
committerTao Bao2017-11-07 14:50:02 -0600
commit6798315327690fdbe93add15159be5b925779bfe (patch)
tree521c5e1d88928db38f3a7aaa8e3dea76e307e78d /tests
parent73dca3e983d5ec2eaf01572eac57dbdb55367f22 (diff)
downloadplatform-bootable-recovery-6798315327690fdbe93add15159be5b925779bfe.tar.gz
platform-bootable-recovery-6798315327690fdbe93add15159be5b925779bfe.tar.xz
platform-bootable-recovery-6798315327690fdbe93add15159be5b925779bfe.zip
otautil: Remove the aborts in RangeSet::Parse().
We used to CHECK and abort on parsing errors. While it works fine for the updater use case (because recovery starts updater in a forked process and collects the process exit code), it's difficult for other clients to use RangeSet as a library (e.g. update_verifier). This CL switches the aborts to returning empty RangeSet instead. Callers need to check the parsing results explicitly. The CL also separates RangeSet::PushBack() into a function, and moves SortedRangeSet::Clear() into RangeSet. Test: recovery_unit_test Test: Sideload an OTA package with the new updater on angler. Test: Sideload an OTA package with injected range string errors. The updater aborts from the explicit checks. Change-Id: If2b7f6f41dc93af917a21c7877a83e98dc3fd016
Diffstat (limited to 'tests')
-rw-r--r--tests/unit/rangeset_test.cpp78
1 files changed, 64 insertions, 14 deletions
diff --git a/tests/unit/rangeset_test.cpp b/tests/unit/rangeset_test.cpp
index b3ed9921..5141bb67 100644
--- a/tests/unit/rangeset_test.cpp
+++ b/tests/unit/rangeset_test.cpp
@@ -17,12 +17,24 @@
17#include <signal.h> 17#include <signal.h>
18#include <sys/types.h> 18#include <sys/types.h>
19 19
20#include <limits>
20#include <vector> 21#include <vector>
21 22
22#include <gtest/gtest.h> 23#include <gtest/gtest.h>
23 24
24#include "otautil/rangeset.h" 25#include "otautil/rangeset.h"
25 26
27TEST(RangeSetTest, ctor) {
28 RangeSet rs(std::vector<Range>{ Range{ 8, 10 }, Range{ 1, 5 } });
29 ASSERT_TRUE(rs);
30
31 RangeSet rs2(std::vector<Range>{});
32 ASSERT_FALSE(rs2);
33
34 RangeSet rs3(std::vector<Range>{ Range{ 8, 10 }, Range{ 5, 1 } });
35 ASSERT_FALSE(rs3);
36}
37
26TEST(RangeSetTest, Parse_smoke) { 38TEST(RangeSetTest, Parse_smoke) {
27 RangeSet rs = RangeSet::Parse("2,1,10"); 39 RangeSet rs = RangeSet::Parse("2,1,10");
28 ASSERT_EQ(static_cast<size_t>(1), rs.size()); 40 ASSERT_EQ(static_cast<size_t>(1), rs.size());
@@ -37,27 +49,64 @@ TEST(RangeSetTest, Parse_smoke) {
37 49
38 // Leading zeros are fine. But android::base::ParseUint() doesn't like trailing zeros like "10 ". 50 // Leading zeros are fine. But android::base::ParseUint() doesn't like trailing zeros like "10 ".
39 ASSERT_EQ(rs, RangeSet::Parse(" 2, 1, 10")); 51 ASSERT_EQ(rs, RangeSet::Parse(" 2, 1, 10"));
40 ASSERT_EXIT(RangeSet::Parse("2,1,10 "), ::testing::KilledBySignal(SIGABRT), ""); 52 ASSERT_FALSE(RangeSet::Parse("2,1,10 "));
41} 53}
42 54
43TEST(RangeSetTest, Parse_InvalidCases) { 55TEST(RangeSetTest, Parse_InvalidCases) {
44 // Insufficient number of tokens. 56 // Insufficient number of tokens.
45 ASSERT_EXIT(RangeSet::Parse(""), ::testing::KilledBySignal(SIGABRT), ""); 57 ASSERT_FALSE(RangeSet::Parse(""));
46 ASSERT_EXIT(RangeSet::Parse("2,1"), ::testing::KilledBySignal(SIGABRT), ""); 58 ASSERT_FALSE(RangeSet::Parse("2,1"));
47 59
48 // The first token (i.e. the number of following tokens) is invalid. 60 // The first token (i.e. the number of following tokens) is invalid.
49 ASSERT_EXIT(RangeSet::Parse("a,1,1"), ::testing::KilledBySignal(SIGABRT), ""); 61 ASSERT_FALSE(RangeSet::Parse("a,1,1"));
50 ASSERT_EXIT(RangeSet::Parse("3,1,1"), ::testing::KilledBySignal(SIGABRT), ""); 62 ASSERT_FALSE(RangeSet::Parse("3,1,1"));
51 ASSERT_EXIT(RangeSet::Parse("-3,1,1"), ::testing::KilledBySignal(SIGABRT), ""); 63 ASSERT_FALSE(RangeSet::Parse("-3,1,1"));
52 ASSERT_EXIT(RangeSet::Parse("2,1,2,3"), ::testing::KilledBySignal(SIGABRT), ""); 64 ASSERT_FALSE(RangeSet::Parse("2,1,2,3"));
53 65
54 // Invalid tokens. 66 // Invalid tokens.
55 ASSERT_EXIT(RangeSet::Parse("2,1,10a"), ::testing::KilledBySignal(SIGABRT), ""); 67 ASSERT_FALSE(RangeSet::Parse("2,1,10a"));
56 ASSERT_EXIT(RangeSet::Parse("2,,10"), ::testing::KilledBySignal(SIGABRT), ""); 68 ASSERT_FALSE(RangeSet::Parse("2,,10"));
57 69
58 // Empty or negative range. 70 // Empty or negative range.
59 ASSERT_EXIT(RangeSet::Parse("2,2,2"), ::testing::KilledBySignal(SIGABRT), ""); 71 ASSERT_FALSE(RangeSet::Parse("2,2,2"));
60 ASSERT_EXIT(RangeSet::Parse("2,2,1"), ::testing::KilledBySignal(SIGABRT), ""); 72 ASSERT_FALSE(RangeSet::Parse("2,2,1"));
73}
74
75TEST(RangeSetTest, Clear) {
76 RangeSet rs = RangeSet::Parse("2,1,6");
77 ASSERT_TRUE(rs);
78 rs.Clear();
79 ASSERT_FALSE(rs);
80
81 // No-op to clear an empty RangeSet.
82 rs.Clear();
83 ASSERT_FALSE(rs);
84}
85
86TEST(RangeSetTest, PushBack) {
87 RangeSet rs;
88 ASSERT_FALSE(rs);
89
90 ASSERT_TRUE(rs.PushBack({ 3, 5 }));
91 ASSERT_EQ(RangeSet::Parse("2,3,5"), rs);
92
93 ASSERT_TRUE(rs.PushBack({ 5, 15 }));
94 ASSERT_EQ(RangeSet::Parse("4,3,5,5,15"), rs);
95 ASSERT_EQ(static_cast<size_t>(2), rs.size());
96 ASSERT_EQ(static_cast<size_t>(12), rs.blocks());
97}
98
99TEST(RangeSetTest, PushBack_InvalidInput) {
100 RangeSet rs;
101 ASSERT_FALSE(rs);
102 ASSERT_FALSE(rs.PushBack({ 5, 3 }));
103 ASSERT_FALSE(rs);
104 ASSERT_FALSE(rs.PushBack({ 15, 15 }));
105 ASSERT_FALSE(rs);
106
107 ASSERT_TRUE(rs.PushBack({ 5, 15 }));
108 ASSERT_FALSE(rs.PushBack({ 5, std::numeric_limits<size_t>::max() - 2 }));
109 ASSERT_EQ(RangeSet::Parse("2,5,15"), rs);
61} 110}
62 111
63TEST(RangeSetTest, Overlaps) { 112TEST(RangeSetTest, Overlaps) {
@@ -90,7 +139,7 @@ TEST(RangeSetTest, equality) {
90 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,7")); 139 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,7"));
91 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,2,7")); 140 ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,2,7"));
92 141
93 // The orders of Range's matter. "4,1,5,8,10" != "4,8,10,1,5". 142 // The orders of Range's matter, e.g. "4,1,5,8,10" != "4,8,10,1,5".
94 ASSERT_NE(RangeSet::Parse("4,1,5,8,10"), RangeSet::Parse("4,8,10,1,5")); 143 ASSERT_NE(RangeSet::Parse("4,1,5,8,10"), RangeSet::Parse("4,8,10,1,5"));
95} 144}
96 145
@@ -111,13 +160,14 @@ TEST(RangeSetTest, iterators) {
111 ASSERT_EQ((std::vector<Range>{ Range{ 8, 10 }, Range{ 1, 5 } }), ranges); 160 ASSERT_EQ((std::vector<Range>{ Range{ 8, 10 }, Range{ 1, 5 } }), ranges);
112} 161}
113 162
114TEST(RangeSetTest, tostring) { 163TEST(RangeSetTest, ToString) {
164 ASSERT_EQ("", RangeSet::Parse("").ToString());
115 ASSERT_EQ("2,1,6", RangeSet::Parse("2,1,6").ToString()); 165 ASSERT_EQ("2,1,6", RangeSet::Parse("2,1,6").ToString());
116 ASSERT_EQ("4,1,5,8,10", RangeSet::Parse("4,1,5,8,10").ToString()); 166 ASSERT_EQ("4,1,5,8,10", RangeSet::Parse("4,1,5,8,10").ToString());
117 ASSERT_EQ("6,1,3,4,6,15,22", RangeSet::Parse("6,1,3,4,6,15,22").ToString()); 167 ASSERT_EQ("6,1,3,4,6,15,22", RangeSet::Parse("6,1,3,4,6,15,22").ToString());
118} 168}
119 169
120TEST(SortedRangeSetTest, insertion) { 170TEST(SortedRangeSetTest, Insert) {
121 SortedRangeSet rs({ { 2, 3 }, { 4, 6 }, { 8, 14 } }); 171 SortedRangeSet rs({ { 2, 3 }, { 4, 6 }, { 8, 14 } });
122 rs.Insert({ 1, 2 }); 172 rs.Insert({ 1, 2 });
123 ASSERT_EQ(SortedRangeSet({ { 1, 3 }, { 4, 6 }, { 8, 14 } }), rs); 173 ASSERT_EQ(SortedRangeSet({ { 1, 3 }, { 4, 6 }, { 8, 14 } }), rs);