blob: 2a0c5e7d8f51ace01781b3434bb5bd560e0e7116 [file] [log] [blame]
Lutz Justen90281402019-07-05 15:14:37 +02001// Copyright 2019 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 "kerberos/krb5_interface_impl.h"
6
7#include <string>
8
9#include <gtest/gtest.h>
10
11namespace kerberos {
12
13namespace {
14
15constexpr char kValidConfig[] = "";
16constexpr char kBadKrb5conf[] = "\n\n[libdefaults";
Lutz Justen70496c12019-07-24 11:11:55 +020017constexpr char kBadBoolKrb5conf[] =
18 "[libdefaults]\nignore_acceptor_hostname=bad_bool";
Lutz Justen90281402019-07-05 15:14:37 +020019
20} // namespace
21
22class Krb5InterfaceImplTest : public ::testing::Test {
23 public:
24 Krb5InterfaceImplTest() {}
Qijiang Fan6bc59e12020-11-11 02:51:06 +090025 Krb5InterfaceImplTest(const Krb5InterfaceImplTest&) = delete;
26 Krb5InterfaceImplTest& operator=(const Krb5InterfaceImplTest&) = delete;
27
Lutz Justen90281402019-07-05 15:14:37 +020028 ~Krb5InterfaceImplTest() override = default;
29
30 protected:
31 Krb5InterfaceImpl krb5_;
Lutz Justen90281402019-07-05 15:14:37 +020032};
33
34// Can't test terribly much here since the actual krb5 functionality involves
35// network operations. The validation can be tested, though.
36
37// Tests config validation with a valid config.
38TEST_F(Krb5InterfaceImplTest, ValidateConfigSuccess) {
39 ConfigErrorInfo error_info;
40 ErrorType error = krb5_.ValidateConfig(kValidConfig, &error_info);
41 EXPECT_EQ(ERROR_NONE, error);
42 EXPECT_TRUE(error_info.has_code());
43 EXPECT_EQ(CONFIG_ERROR_NONE, error_info.code());
44 EXPECT_FALSE(error_info.has_line_index());
45}
46
47// Tests config validation with a bad config.
48TEST_F(Krb5InterfaceImplTest, ValidateConfigFailure) {
49 ConfigErrorInfo error_info;
50 ErrorType error = krb5_.ValidateConfig(kBadKrb5conf, &error_info);
51 EXPECT_EQ(ERROR_BAD_CONFIG, error);
52 EXPECT_EQ(CONFIG_ERROR_SECTION_SYNTAX, error_info.code());
53 EXPECT_TRUE(error_info.has_line_index());
54 EXPECT_EQ(2, error_info.line_index());
55}
56
57// Tests the krb5-part of config validation.
58TEST_F(Krb5InterfaceImplTest, ValidateConfigViaKrb5Failure) {
Lutz Justen70496c12019-07-24 11:11:55 +020059 // |kBadBoolKrb5conf| contains a bool variable that's not true or false. The
Felipe Andradea0cbde72020-04-01 15:40:10 +020060 // MIT parser doesn't accept this, but ConfigParser does since it doesn't
61 // check values. Thus, the config validation returns a generic KRB5 error
62 // without line index.
Lutz Justen90281402019-07-05 15:14:37 +020063 ConfigErrorInfo error_info;
Lutz Justen70496c12019-07-24 11:11:55 +020064 ErrorType error = krb5_.ValidateConfig(kBadBoolKrb5conf, &error_info);
Lutz Justen90281402019-07-05 15:14:37 +020065 EXPECT_EQ(ERROR_BAD_CONFIG, error);
66 EXPECT_EQ(CONFIG_ERROR_KRB5_FAILED_TO_PARSE, error_info.code());
67 EXPECT_FALSE(error_info.has_line_index());
68}
69
70} // namespace kerberos