| 1 | /* |
| 2 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <WebCore/ParsedContentRange.h> |
| 29 | #include <wtf/text/WTFString.h> |
| 30 | |
| 31 | using namespace WebCore; |
| 32 | |
| 33 | namespace TestWebKitAPI { |
| 34 | |
| 35 | TEST(WebCore, ParsedContentRangeFromString) |
| 36 | { |
| 37 | // Basic parsing |
| 38 | ASSERT_TRUE(ParsedContentRange("bytes 0-1/2" ).isValid()); |
| 39 | ASSERT_TRUE(ParsedContentRange("bytes 0-1/*" ).isValid()); |
| 40 | ASSERT_EQ(0, ParsedContentRange("bytes 0-1/2" ).firstBytePosition()); |
| 41 | ASSERT_EQ(1, ParsedContentRange("bytes 0-1/2" ).lastBytePosition()); |
| 42 | ASSERT_EQ(2, ParsedContentRange("bytes 0-1/2" ).instanceLength()); |
| 43 | ASSERT_EQ(ParsedContentRange::UnknownLength, ParsedContentRange("bytes 0-1/*" ).instanceLength()); |
| 44 | |
| 45 | // Whitespace errors |
| 46 | ASSERT_FALSE(ParsedContentRange("bytes 0-1/*" ).isValid()); |
| 47 | ASSERT_FALSE(ParsedContentRange("bytes 0 -1/*" ).isValid()); |
| 48 | ASSERT_FALSE(ParsedContentRange("bytes 0- 1/*" ).isValid()); |
| 49 | ASSERT_FALSE(ParsedContentRange("bytes 0-1 /*" ).isValid()); |
| 50 | ASSERT_FALSE(ParsedContentRange("bytes 0-1/ *" ).isValid()); |
| 51 | ASSERT_FALSE(ParsedContentRange("bytes 0-1/* " ).isValid()); |
| 52 | ASSERT_FALSE(ParsedContentRange("bytes 0-1/ 2" ).isValid()); |
| 53 | ASSERT_FALSE(ParsedContentRange("bytes 0-1/2 " ).isValid()); |
| 54 | |
| 55 | // Non-digit errors |
| 56 | ASSERT_FALSE(ParsedContentRange("bytes abcd-1/2" ).isValid()); |
| 57 | ASSERT_FALSE(ParsedContentRange("bytes 0-abcd/2" ).isValid()); |
| 58 | ASSERT_FALSE(ParsedContentRange("bytes 0-1/abcd" ).isValid()); |
| 59 | |
| 60 | // Range requirement errors |
| 61 | ASSERT_FALSE(ParsedContentRange("bytes 1-0/2" ).isValid()); |
| 62 | ASSERT_FALSE(ParsedContentRange("bytes 0-2/1" ).isValid()); |
| 63 | ASSERT_FALSE(ParsedContentRange("bytes 2/0-1" ).isValid()); |
| 64 | ASSERT_FALSE(ParsedContentRange("abcd 0/1-2" ).isValid()); |
| 65 | |
| 66 | // Negative value errors |
| 67 | ASSERT_FALSE(ParsedContentRange("bytes -0-1/*" ).isValid()); |
| 68 | ASSERT_FALSE(ParsedContentRange("bytes -1/*" ).isValid()); |
| 69 | ASSERT_FALSE(ParsedContentRange("bytes 0--0/2" ).isValid()); |
| 70 | ASSERT_FALSE(ParsedContentRange("bytes 0-1/-2" ).isValid()); |
| 71 | |
| 72 | // Edge cases |
| 73 | ASSERT_TRUE(ParsedContentRange("bytes 9223372036854775805-9223372036854775806/9223372036854775807" ).isValid()); |
| 74 | ASSERT_FALSE(ParsedContentRange("bytes 9223372036854775808-9223372036854775809/9223372036854775810" ).isValid()); |
| 75 | } |
| 76 | |
| 77 | TEST(WebCore, ParsedContentRangeFromValues) |
| 78 | { |
| 79 | ASSERT_TRUE(ParsedContentRange(0, 1, 2).isValid()); |
| 80 | ASSERT_TRUE(ParsedContentRange(0, 1, ParsedContentRange::UnknownLength).isValid()); |
| 81 | ASSERT_FALSE(ParsedContentRange().isValid()); |
| 82 | ASSERT_FALSE(ParsedContentRange(1, 0, 2).isValid()); |
| 83 | ASSERT_FALSE(ParsedContentRange(0, 2, 1).isValid()); |
| 84 | ASSERT_FALSE(ParsedContentRange(0, 0, 0).isValid()); |
| 85 | ASSERT_FALSE(ParsedContentRange(-1, 1, 2).isValid()); |
| 86 | ASSERT_FALSE(ParsedContentRange(0, -1, 2).isValid()); |
| 87 | ASSERT_FALSE(ParsedContentRange(0, 1, -2).isValid()); |
| 88 | ASSERT_FALSE(ParsedContentRange(-2, -1, 2).isValid()); |
| 89 | } |
| 90 | |
| 91 | TEST(WebCore, ParsedContentRangeToString) |
| 92 | { |
| 93 | ASSERT_STREQ("bytes 0-1/2" , ParsedContentRange(0, 1, 2).headerValue().utf8().data()); |
| 94 | ASSERT_STREQ("bytes 0-1/*" , ParsedContentRange(0, 1, ParsedContentRange::UnknownLength).headerValue().utf8().data()); |
| 95 | ASSERT_STREQ("" , ParsedContentRange().headerValue().utf8().data()); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |