Elliott Hughes | 26fda77 | 2016-04-06 11:56:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <resolv.h> |
| 30 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 31 | #include <sys/cdefs.h> |
| 32 | |
Elliott Hughes | 26fda77 | 2016-04-06 11:56:41 -0700 | [diff] [blame] | 33 | #include <gtest/gtest.h> |
| 34 | |
| 35 | TEST(resolv, b64_pton_28035006) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 36 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 26fda77 | 2016-04-06 11:56:41 -0700 | [diff] [blame] | 37 | // Test data from https://groups.google.com/forum/#!topic/mailing.openbsd.tech/w3ACIlklJkI. |
| 38 | const char* data = |
| 39 | "p1v3+nehH3N3n+/OokzXpsyGF2VVpxIxkjSn3Mv/Sq74OE1iFuVU+K4bQImuVj" |
| 40 | "S55RB2fpCpbB8Nye7tzrt6h9YPP3yyJfqORDETGmIB4lveZXA4KDxx50F9rYrO" |
| 41 | "dFbTLyWfNBb/8Q2TnD72eY/3Y5P9qwtJwyDL25Tleic8G3g="; |
| 42 | |
| 43 | // This buffer is exactly the right size, but old versions of the BSD code |
| 44 | // incorrectly required an extra byte. http://b/28035006. |
| 45 | uint8_t buf[128]; |
| 46 | ASSERT_EQ(128, b64_pton(data, buf, sizeof(buf))); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 47 | #else |
| 48 | GTEST_SKIP() << "musl doesn't have b64_pton"; |
| 49 | #endif |
Elliott Hughes | 26fda77 | 2016-04-06 11:56:41 -0700 | [diff] [blame] | 50 | } |
Elliott Hughes | 70f0209 | 2017-11-08 21:13:15 -0800 | [diff] [blame] | 51 | |
| 52 | TEST(resolv, b64_ntop) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 53 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 70f0209 | 2017-11-08 21:13:15 -0800 | [diff] [blame] | 54 | char buf[128]; |
| 55 | memset(buf, 'x', sizeof(buf)); |
| 56 | ASSERT_EQ(static_cast<int>(strlen("aGVsbG8=")), |
| 57 | b64_ntop(reinterpret_cast<u_char const*>("hello"), strlen("hello"), |
| 58 | buf, sizeof(buf))); |
| 59 | ASSERT_STREQ(buf, "aGVsbG8="); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 60 | #else |
| 61 | GTEST_SKIP() << "musl doesn't have b64_ntop"; |
| 62 | #endif |
Elliott Hughes | 70f0209 | 2017-11-08 21:13:15 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST(resolv, b64_pton) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 66 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 70f0209 | 2017-11-08 21:13:15 -0800 | [diff] [blame] | 67 | u_char buf[128]; |
| 68 | memset(buf, 'x', sizeof(buf)); |
| 69 | ASSERT_EQ(static_cast<int>(strlen("hello")), b64_pton("aGVsbG8=", buf, sizeof(buf))); |
| 70 | ASSERT_STREQ(reinterpret_cast<char*>(buf), "hello"); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 71 | #else |
| 72 | GTEST_SKIP() << "musl doesn't have b64_pton"; |
| 73 | #endif |
Elliott Hughes | 70f0209 | 2017-11-08 21:13:15 -0800 | [diff] [blame] | 74 | } |
Elliott Hughes | 3a5fd00 | 2018-07-17 13:02:52 -0700 | [diff] [blame] | 75 | |
| 76 | TEST(resolv, p_class) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 77 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 3a5fd00 | 2018-07-17 13:02:52 -0700 | [diff] [blame] | 78 | ASSERT_STREQ("IN", p_class(ns_c_in)); |
| 79 | ASSERT_STREQ("BADCLASS", p_class(-1)); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 80 | #else |
| 81 | GTEST_SKIP() << "musl doesn't have p_class"; |
| 82 | #endif |
Elliott Hughes | 3a5fd00 | 2018-07-17 13:02:52 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST(resolv, p_type) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 86 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 3a5fd00 | 2018-07-17 13:02:52 -0700 | [diff] [blame] | 87 | ASSERT_STREQ("AAAA", p_type(ns_t_aaaa)); |
| 88 | ASSERT_STREQ("BADTYPE", p_type(-1)); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 89 | #else |
| 90 | GTEST_SKIP() << "musl doesn't have p_type"; |
| 91 | #endif |
Elliott Hughes | 3a5fd00 | 2018-07-17 13:02:52 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | TEST(resolv, res_init) { |
| 95 | ASSERT_EQ(0, res_init()); |
| 96 | } |
| 97 | |
| 98 | TEST(resolv, res_randomid) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame^] | 99 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 3a5fd00 | 2018-07-17 13:02:52 -0700 | [diff] [blame] | 100 | res_randomid(); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 101 | #else |
| 102 | GTEST_SKIP() << "musl doesn't have res_randomid"; |
| 103 | #endif |
Elliott Hughes | 3a5fd00 | 2018-07-17 13:02:52 -0700 | [diff] [blame] | 104 | } |